#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } constexpr long long MAX = 5100000; constexpr long long INF = 1LL << 60; constexpr int inf = 1 << 28; //constexpr long long mod = 1000000007LL; constexpr long long mod = 998244353LL; using namespace std; typedef unsigned long long ull; typedef long long ll; struct SegmentTree { public: int n; vector node; vector> g; SegmentTree(vector v) { int sz = v.size(); n = 1; while (sz > n) n *= 2; } void add_edge(int a, int b, int nd, int k = 0, int l = 0, int r = -1) { if (r < 0) r = n; if (r <= a || b <= l) return; if (a <= l && r <= b) { g[nd + n - 1].emplace_back(k); return; } add_edge(a, b, nd, 2 * k + 1, l, (l + r) / 2); add_edge(a, b, nd, 2 * k + 2, (l + r) / 2, r); } }; struct technique { ll a, b, c; int id; technique(ll _a, ll _b, ll _c, int _id) :a(_a), b(_b), c(_c), id(_id) {} bool operator <(technique &t) { if (a == t.a) { if (b == t.b) return c < t.c; else return b < t.b; } else return a < t.a; } }; vector dp; vector used; vector vt; vector score; int n; ll dfs(int cur, SegmentTree& seg) { if (dp[cur] != -1) return dp[cur]; if (used[cur]) return dp[cur] = INF; ll res = 0; used[cur] = true; for (auto next : seg.g[cur]) { ll tmp = dfs(next, seg); if (tmp == INF) res = INF; else chmax(res, tmp); } if (res == INF) return dp[cur] = INF; return dp[cur] = res + score[cur]; } int main() { /* cin.tie(nullptr); ios::sync_with_stdio(false); */ scanf("%d", &n); vector vt; vt.reserve(n); for (int i = 0; i < n; i++) { ll a, b, c; scanf("%lld %lld %lld", &a, &b, &c); vt.emplace_back(a, b, c, i); } SegmentTree seg(vector(n,0)); n = seg.n; used = vector(n * 2, false); seg.g.resize(n * 2); dp = vector(n * 2, -1); score.resize(n * 2); for (int i = 0; i < n - 1; i++) { seg.g[i].emplace_back(i * 2 + 1); seg.g[i].emplace_back(i * 2 + 2); } sort(vt.begin(), vt.end()); for (int i = 0; i < vt.size(); i++) { score[i + n - 1] = vt[i].b; } for (int i = 0; i < vt.size(); i++) { ll d = vt[i].b - vt[i].c; int left = -1; int right = vt.size(); while (right - left > 1) { int mid = (left + right) >> 1; if (mid == vt.size()) left = mid; else if (d < vt[mid].a) right = mid; else left = mid; } //cout << i << " " << right << endl; if (right == 0) continue; if(right < i) seg.add_edge(0, right, i); else { seg.add_edge(0, i, i); if(i + 1!= right) seg.add_edge(i + 1, right, i); } } vector ans(vt.size()); for (int i = 0; i < vt.size(); i++) { ans[vt[i].id] = dfs(i + n - 1, seg); } for (int i = 0; i < ans.size(); i++) { if (ans[i] == INF) puts("BAN"); else printf("%lld\n", ans[i]); } return 0; }