#include #define ALL(v) std::begin(v), std::end(v) #define ALLR(v) std::rbegin(v), std::rend(v) using ll = std::int64_t; using ull = std::uint64_t; using pii = std::pair; using tii = std::tuple; using pll = std::pair; using tll = std::tuple; template using vec = std::vector; template using vvec = vec>; template const T& var_min(const T &t) { return t; } template const T& var_max(const T &t) { return t; } template const T& var_min(const T &t, const Tail&... tail) { return std::min(t, var_min(tail...)); } template const T& var_max(const T &t, const Tail&... tail) { return std::max(t, var_max(tail...)); } template void chmin(T &t, const Tail&... tail) { t = var_min(t, tail...); } template void chmax(T &t, const Tail&... tail) { t = var_max(t, tail...); } template const T& clamp(const T &t, const T &low, const T &high) { return std::max(low, std::min(high, t)); } template void chclamp(T &t, const T &low, const T &high) { return t = clamp(t, low, high); } template T make_v(T init) { return init; } template auto make_v(T init, std::size_t s, Tail... tail) { auto v = std::move(make_v(init, tail...)); return vec(s, v); } template struct multi_dem_array { using type = std::array::type, Head>; }; template struct multi_dem_array { using type = std::array; }; template using mdarray = typename multi_dem_array::type; namespace init__ { struct InitIO { InitIO() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << std::fixed << std::setprecision(30); } } init_io; } namespace segtree { template struct SegmentTree { using Merge = std::function; vec nodes; Merge merge_f; T id_ele; SegmentTree(const vec &init_v, Merge merge_f, T id_ele) : nodes(ceil_pow2(init_v.size()) * 2 - 1, id_ele), merge_f(merge_f), id_ele(id_ele) { ssize_t s = size(); for (ssize_t i = 0; i < init_v.size(); i++) nodes[i + s - 1] = init_v[i]; for (ssize_t i = s - 2; 0 <= i; i--) nodes[i] = get_merged(i); } size_t size() const { return (nodes.size() + 1) / 2; } const T& operator [](ll idx) const { return nodes[idx + size() - 1]; } T get_query(ll ql, ll qr, ll nl, ll nr, ll nidx) const { if (nr <= ql || qr <= nl) return id_ele; if (ql <= nl && nr <= qr) return nodes[nidx]; ll mid = (nl + nr) / 2; ll lidx, ridx; std::tie(lidx, ridx) = get_children_idx(nidx); auto lv = get_query(ql, qr, nl, mid, lidx); auto rv = get_query(ql, qr, mid, nr, ridx); return merge_f(lv, rv); } T get_query(ll ql, ll qr) const { return get_query(ql, qr, 0, size(), 0); } void update_query(ll idx, T val) { idx += size() - 1; nodes[idx] = val; while (idx) { ll pidx = (idx - 1) / 2; nodes[pidx] = get_merged(pidx); idx = pidx; } } // FIXME : test T lower_bound(ll idx, std::function check, T sum) const { if (size() - 1 <= idx) return idx - (size() - 1); ll lidx, ridx; std::tie(lidx, ridx) = get_children_idx(idx); auto lv = merge_f(sum, nodes[lidx]); if (check(lv)) return lower_bound(lidx, check, sum); else return lower_bound(ridx, check, lv); } T lower_bound(std::function check) const { return lower_bound(0, check, id_ele); } private: ssize_t ceil_pow2(ssize_t s) { ssize_t ret = 1; while (ret <= s) ret *= 2; return ret; } pll get_children_idx(ll idx) const { return pll(2 * idx + 1, 2 * idx + 2); } T get_merged(ll idx) const { ll a, b; std::tie(a, b) = get_children_idx(idx); return merge_f(nodes[a], nodes[b]); } }; } namespace graph { using Node = ll; using Weight = ll; using Edge = std::pair; template struct Graph : public vvec { using vvec::vvec; void add_edge(Node f, Node t, Weight w = 1) { (*this)[f].emplace_back(t, w); if (!Directed) (*this)[t].emplace_back(f, w); } Graph build_inv() const { Graph ret(this->size()); for (Node i = 0; i < this->size(); i++) { for (const Edge &e : (*this)[i]) { Node j; Weight w; std::tie(j, w) = e; if (!Directed && j < i) continue; ret.add_edge(j, i, w); } } return ret; } }; } const std::size_t SIZE = 1e5 + 10; mdarray dp; bool limited[SIZE]; ll maxv[SIZE]; struct Compress { vec a; Compress(vec a_) : a(std::move(a_)) { std::sort(ALL(a)); auto ite = std::unique(ALL(a)); a.erase(ite, a.end()); } ll get_id(ll e) { return std::distance(a.begin(), std::lower_bound(ALL(a), e)); } }; const ll inf = 5e15; int main() { ll n; std::cin >> n; vec abc(n); vec av(n); for (ll i = 0; i < n; i++) { ll a, b, c; std::cin >> a >> b >> c; abc[i] = tll(a, b, c); av[i] = a; } Compress cmp(av); pll err(-inf, -inf); vec init_v(av.size()); vvec bc_lis(av.size()); for (ll i = 0; i < n; i++) { ll a, b, c; std::tie(a, b, c) = abc[i]; ll idx = cmp.get_id(a); bc_lis[idx].emplace_back(b - c, i); } for (auto &&v : bc_lis) { v.emplace_back(-inf, -inf); std::sort(ALLR(v)); } for (ll i = 0; i < init_v.size(); i++) init_v[i] = bc_lis[i][0]; auto merge = [](pll p, pll q) { ll a, b, c, d; std::tie(a, b) = p; std::tie(c, d) = q; if (a > c) return p; else return q; }; segtree::SegmentTree seg(init_v, merge, err); vec nxts(n, -1); for (ll i = 0; i < n; i++) { ll a, b, c; std::tie(a, b, c) = abc[i]; ll idx = cmp.get_id(a); bool changed = false; if (bc_lis[idx][0].second == i) { changed = true; seg.update_query(idx, bc_lis[idx][1]); } auto ite = std::upper_bound(ALL(cmp.a), b - c); auto r = std::distance(cmp.a.begin(), ite); if (0 < r) { auto qr = seg.get_query(0, r); nxts[i] = qr.second; } if (changed) seg.update_query(idx, bc_lis[idx][0]); } for (ll i = 0; i < n; i++) { ll b; std::tie(std::ignore, b, std::ignore) = abc[i]; if (nxts[i] == -1) { dp[0][i] = pll(-1, b); limited[i] = true; maxv[i] = b; } else { dp[0][i] = pll(nxts[i], b); } } for (ll i = 0; i < 32 - 1; i++) { for (ll j = 0; j < n; j++) { ll cur, sum; std::tie(cur, sum) = dp[i][j]; if (cur == -1) { maxv[j] = sum; limited[j] = true; dp[i + 1][j] = dp[i][j]; continue; } ll nxt, addv; std::tie(nxt, addv) = dp[i][cur]; sum += addv; dp[i + 1][j] = pll(nxt, sum); } } for (ll i = 0; i < n; i++) { if (!limited[i]) std::cout << "BAN"; else std::cout << maxv[i]; std::cout << '\n'; } return 0; }