## 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 q = deque() answer = 0 max_answer = 0 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) 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 answer += cxy[j][2] elif len(q) > 0: d = q.popleft() answer += d else: _, j = heapq.heappop(queue) used[j] = True answer += cxy[j][2] else: d = q.popleft() answer += d max_answer = max(max_answer, answer) print(max_answer) if __name__ == "__main__": main()