## https://yukicoder.me/problems/no/2957 import heapq from collections import deque def main(): N = int(input()) cxy = [] for _ in range(N): c, x, y = map(int, input().split()) cxy.append((c, x, y)) c_map = {} queue = [] for i in range(N): c, x, y = cxy[i] d = x - y heapq.heappush(queue, (d, i)) if c not in c_map: c_map[c] = [] c_map[c].append(i) used = [False] * N answer = 0 for _, _,y in cxy: answer += y q = deque() for c in range(N): if c in c_map: for j in c_map[c]: _, x, y = cxy[j] if not used[j]: used[j] = True q.append(x - y) while len(queue) > 0 and used[queue[0][1]]: heapq.heappop(queue) if len(queue) > 0: if queue[0][0] <= 0: _, j = heapq.heappop(queue) used[j] = True elif len(q) > 0: d = q.popleft() answer += d else: _, j = heapq.heappop(queue) used[j] = True else: d = q.popleft() answer += d print(answer) if __name__ == "__main__": main()