import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } alias Move = Tuple!(long, "a", long, "b", long, "c", int, "id"); enum INF = 10L^^18; void main() { try { for (; ; ) { const N = readInt(); auto P = new Move[N]; foreach (i; 0 .. N) { P[i].a = readLong(); P[i].b = readLong(); P[i].c = readLong(); P[i].id = i; } P.sort; auto ks = new int[N]; foreach (i; 0 .. N) { ks[i] = P.upperBound(Move(P[i].b - P[i].c, INF, INF, -1)); } debug { writeln("ks = ", ks); } int segN; for (segN = 1; segN < N; segN <<= 1) {} auto dp = new long[segN << 1]; auto mem = new bool[segN << 1]; dp[] = -INF; alias Entry = Tuple!(long, "val", int, "id"); auto ls = new Entry[][](N + 1, 2); ls[0][] = Entry(-INF, -1); foreach (i; 0 .. N) { ls[i + 1][] = ls[i][]; auto e = Entry(P[i].b - P[i].c, i); foreach (j; 0 .. 2) { if (ls[i + 1][j] < e) { swap(ls[i + 1][j], e); } } } foreach (i; 0 .. N) { bool cyc; foreach (j; 0 .. 2) { if (ls[ks[i]][j].id != i && ls[ks[i]][j].val >= P[i].a) { cyc = true; } } if (cyc) { for (int u = segN + i; u > 0; u >>= 1) { debug { writeln("INF ", i, "; ", u); } dp[u] = INF; mem[u] = true; } } } long calc(int u) { if (!mem[u]) { mem[u] = true; if (u < segN) { chmax(dp[u], calc(u << 1)); chmax(dp[u], calc(u << 1 | 1)); } else { const i = u - segN; if (0 <= i && i < N) { dp[u] = 0; void rangeAE(int v, int w) { for (v += segN, w += segN; v < w; v >>= 1, w >>= 1) { if (v & 1) chmax(dp[u], calc(v++)); if (w & 1) chmax(dp[u], calc(--w)); } } if (i < ks[i]) { rangeAE(0, i); rangeAE(i + 1, ks[i]); } else { rangeAE(0, ks[i]); } dp[u] += P[i].b; } } } return dp[u]; } auto ans = new long[N]; foreach (i; 0 .. N) { ans[P[i].id] = calc(segN + i); } foreach (i; 0 .. N) { if (ans[i] >= INF) { writeln("BAN"); } else { writeln(ans[i]); } } } } catch (EOFException e) { } }