#include using namespace std; using ll = long long; template using Pa = pair; template using vec = vector; template using vvec = vector>; template class SegmentTree{ private: int sz; vector seg; const F op;//演算 const Monoid e;//単位元 public: SegmentTree(int n,const F op,const Monoid &e):op(op),e(e){ sz = 1; while(sz<=n) sz <<= 1; seg.assign(2*sz,e); } void set(int k, const Monoid &x){ seg[k+sz] = x; } void build(){ for(int i=sz-1;i>0;i--){ seg[i] = op(seg[2*i],seg[2*i+1]); } } void update(int k,const Monoid &x){ k += sz; seg[k] = x; while(k>>=1){ seg[k] = op(seg[2*k],seg[2*k+1]); } } Monoid query(int l,int r){ Monoid L = e,R = e; for(l+=sz,r+=sz;l>=1,r>>=1){ if(l&1) L = op(L,seg[l++]); if(r&1) R = op(seg[--r],R); } return op(L,R); } Monoid operator[](const int &k)const{ return seg[k+sz]; } }; constexpr ll inf = 1e18; int main(){ cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; vec A(N),B(N),C(N); map cnt; for(int i=0;i> A[i] >> B[i] >> C[i]; cnt[A[i]]++; } vec id(N); iota(id.begin(),id.end(),0); sort(id.begin(),id.end(),[&](int i,int j){ if(A[i]!=A[j]) return A[i] dp(N,-1); auto mymax = [](ll a,ll b){return max(a,b);}; SegmentTree seg(N,mymax,0); for(int i=0;i,vec>,greater>> Q; for(int i=0;i=ne) break; Q.pop(); int r = partition_point(id.begin(),id.end(),[&](int x){return p.first>=A[x];})-id.begin(); seg.update(p.second,0); dp[id[p.second]] = min(inf,B[id[p.second]]+seg.query(0,r)); seg.update(p.second,dp[id[p.second]]); } } for(auto& x:dp){ if(x==inf) cout << "BAN\n"; else cout << x << "\n"; } }