#!/usr/bin/ python3.8 import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines import numpy as np from scipy.sparse import csr_matrix from scipy.sparse.csgraph import connected_components N = int(readline()) ABC = np.array(read().split(), np.int64) A = ABC[::3] B = ABC[1::3] C = ABC[2::3] sort_key = (A << 32) + (B - C) ind = np.concatenate([np.argsort(sort_key), [N]]) nums = np.concatenate([[-10**10], A]) nums.sort() fr = [] to = [] cost = [] fr.append(ind[0:-2]) to.append(ind[1:-1]) fr.append([N]) to.append([ind[0]]) cost.append(np.zeros(N, np.int64)) fr.append(ind[np.searchsorted(nums, B - C, side='right') - 2]) to.append(np.arange(N)) cost.append(B) fr = np.concatenate(fr) to = np.concatenate(to) cost = np.concatenate(cost) G = csr_matrix((np.ones_like(fr), (fr, to)), (N + 1, N + 1)) n_comp, comp = connected_components(G, directed=True, connection='strong') comp = comp.tolist() INF = 10 ** 18 fr = fr.tolist() to = to.tolist() cost = cost.tolist() dp = [0] * (n_comp) G = [[] for _ in range(n_comp)] self_loop = [0] * n_comp in_deg = [0] * n_comp for v, w, c in zip(fr, to, cost): cv = comp[v] cw = comp[w] if cv == cw: if c == 0: continue if self_loop[cv]: self_loop[cv] = INF else: self_loop[cv] = c else: G[cv].append((cw, c)) in_deg[cw] += 1 deg_0 = [i for i, x in enumerate(in_deg) if not x] dp = [0] * (n_comp) while deg_0: v = deg_0.pop() dp[v] += self_loop[v] for w, c in G[v]: x = dp[v] + c if dp[w] < x: dp[w] = x in_deg[w] -= 1 if not in_deg[w]: deg_0.append(w) answer = [None] * N for v, w, c in zip(fr, to, cost): cv = comp[v] cw = comp[w] if c == 0: continue if cv == cw: x = dp[cv] else: x = dp[cv] + c answer[w] = str(x) if x < INF else 'BAN' print('\n'.join(map(str, answer)))