#include #include #include #include using lint = long long; constexpr lint INF = 1LL << 60; struct Item { lint a, b, c; int id; }; void solve() { int n; std::cin >> n; std::vector ts(n); for (int i = 0; i < n; ++i) { auto& t = ts[i]; std::cin >> t.a >> t.b >> t.c; t.id = i; } std::sort(ts.begin(), ts.end(), [](auto lhs, auto rhs) { return lhs.a < rhs.a; }); std::vector ans(n, -1), acc(n, -1); std::function dfs = [&](int v) -> lint { if (ans[v] != -1) { if (v > 0 && acc[v - 1] != -1) { acc[v] = std::max(ans[v], acc[v - 1]); } return ans[v]; } int ok = -1, ng = n; while (ng - ok > 1) { int mid = (ok + ng) / 2; if (ts[mid].a <= ts[v].b - ts[v].c) { ok = mid; } else { ng = mid; } } if (ok == -1) { ans[v] = ts[v].b; return ans[v]; } lint res = 0; ans[v] = INF; for (int u = ok; u >= 0; --u) { if (u == v) continue; if (acc[u] != -1) { res = std::max(res, acc[u] + ts[v].b); break; } else { res = std::max(res, dfs(u) + ts[v].b); } } if (res >= INF) res = INF; ans[v] = res; if (v == 0) { acc[v] = ans[v]; } else if (acc[v - 1] != -1) { acc[v] = std::max(ans[v], acc[v - 1]); } return ans[v]; }; std::vector as(n); for (int v = 0; v < n; ++v) as[ts[v].id] = dfs(v); for (auto a : as) { if (a == INF) { std::cout << "BAN" << std::endl; } else { std::cout << a << std::endl; } } } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); solve(); return 0; }