import heapq def solve(schedule, n): res = 0 pq = [] for i in range(n): for val in schedule[i]: heapq.heappush(pq, -val) # using negative values to simulate max-heap if pq: val = -heapq.heappop(pq) res += val return res def main(): n = int(input()) x_great = [[] for _ in range(n + 1)] y_great = [[] for _ in range(n + 1)] ans = 0 for _ in range(n): c, x, y = map(int, input().split()) guarantee = min(x, y) ans += guarantee x -= guarantee y -= guarantee if x > 0: x_great[c].append(x) if y > 0: y_great[n - c].append(y) ans += solve(x_great, n) ans += solve(y_great, n) print(ans) if __name__ == "__main__": main()