#!/usr/bin/ python3.8 import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines N = int(readline()) m = map(int, read().split()) A, B, C = zip(*zip(m, m, m)) class MaxBIT(): def __init__(self, max_n): self.size = max_n + 1 self.data = [0] * self.size def get_max(self, i): s = 0 while i: t = self.data[i] if s < t: s = t i -= i & -i return s def update(self, i, x): while i < self.size: if self.data[i] >= x: return self.data[i] = x i += i & -i def compress(A): x_to_i = {x: i for i, x in enumerate(sorted(set(A)), 1)} return [x_to_i[x] for x in A] A = compress(A) B = compress(B) A_to_BC = [[] for _ in range(N + 10)] for a, b, c in zip(A, B, C): A_to_BC[a].append((b, c)) dp = MaxBIT(N) for BC in A_to_BC: update = [] for b, c in BC: update.append((b, dp.get_max(b - 1) + c)) for b, x in update: dp.update(b, x) print(dp.get_max(N))