#include using namespace std; using lint = long long; const lint inf = 1LL << 60; const lint mod = 1000000007; struct StronglyConnectedComponents { vector> const &edges_in; vector> edges, rev, dag; vector comp, order, used; vector> cs; // list of nodes in each component StronglyConnectedComponents(vector> const &g) : edges_in(g), edges(g.size()), rev(g.size()), comp(g.size(), -1), used(g.size()), cs(g.size()) { for (int i = 0; i < (int)edges_in.size(); ++i) { for (auto &v : edges_in[i]) { edges[i].push_back(v); rev[v].push_back(i); } } build(); } int operator[](int k) { return comp[k]; } void dfs(int c) { if (used[c]) return; used[c] = true; for (auto &v : edges[c]) dfs(v); order.push_back(c); } void rdfs(int c, int cnt) { if (comp[c] != -1) return; comp[c] = cnt; cs[cnt].emplace_back(c); for (auto &v : rev[c]) rdfs(v, cnt); } void build() { for (int i = 0; i < (int)edges.size(); ++i) dfs(i); reverse(order.begin(), order.end()); int ptr = 0; for (auto &i : order) { if (comp[i] == -1) rdfs(i, ptr++); } dag.resize(ptr); for (int i = 0; i < (int)edges_in.size(); ++i) { int x = comp[i]; for (auto &v : edges_in[i]) { int y = comp[v]; if (x == y) continue; dag[x].push_back(y); } } } }; template bool chmax(T &a, const T &b) { return (a < b) ? (a = b, 1) : 0; } template bool chmin(T &a, const T &b) { return (b < a) ? (a = b, 1) : 0; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); lint n; cin >> n; map idx; vector a(n), b(n), c(n); for (int i = 0; i < n; ++i) { cin >> a[i] >> b[i] >> c[i]; idx[a[i]] = 0; idx[b[i] - c[i]] = 0; } int id = 0; for (auto &p : idx) { p.second = id++; } int k = idx.size(); vector> edges(n + k); for (int i = 0; i < k - 1; ++i) { edges[i].push_back(i + 1); } for (int i = k; i < k + n; ++i) { edges[i].push_back(idx[a[i - k]]); edges[idx[b[i - k] - c[i - k]]].push_back(i); } StronglyConnectedComponents scc(edges); vector cnt(n + k, 0); for (int i = k; i < n + k; ++i) { cnt[scc[i]]++; } lint m = scc.dag.size(); vector dp(m, 0); for (int i = 0; i < m; ++i) { if (cnt[i] > 1) { dp[i] = inf; } for (auto &v : scc.cs[i]) { if (v >= k && dp[i] < inf) dp[i] += b[v - k]; } for (auto &v : scc.dag[i]) { chmax(dp[v], dp[i]); } } for (int i = k; i < k + n; ++i) { if (dp[scc[i]] >= inf) cout << "BAN" << endl; else cout << dp[scc[i]] << endl; } return 0; }