import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; class Main { void run() { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); PrintWriter pw = new PrintWriter(System.out); int[][] v = new int[N][3]; for (int i = 0; i < N; ++i) { v[i][0] = sc.nextInt(); v[i][1] = sc.nextInt(); v[i][2] = sc.nextInt(); --v[i][0]; --v[i][1]; } compress(v); Arrays.sort(v, new Comparator() { @Override public int compare(int[] o1, int[] o2) { if (o1[0] != o2[0]) { return Integer.compare(o1[0], o2[0]); } else { return -Integer.compare(o1[1], o2[1]); } } }); SegmentTree seg = new SegmentTree(N + 1); for (int i = 0; i < N; ++i) { long val = seg.query(0, v[i][1]); seg.update(v[i][1], val + v[i][2]); } pw.println(seg.query(0, N + 1)); pw.close(); } void compress(int[][] a) { int n = a.length; int[][] tmp = new int[n][4]; for (int i = 0; i < n; ++i) { for (int j = 0; j < a[i].length; ++j) { tmp[i][j] = a[i][j]; } tmp[i][3] = i; } Arrays.sort(tmp, new Comparator() { @Override public int compare(int[] o1, int[] o2) { return Integer.compare(o1[0], o2[0]); } }); int p = 0; for (int i = 0; i < n; ++i) { if (i + 1 < n && tmp[i][0] != tmp[i + 1][0]) { tmp[i][0] = p++; } else { tmp[i][0] = p; } } Arrays.sort(tmp, new Comparator() { @Override public int compare(int[] o1, int[] o2) { return Integer.compare(o1[1], o2[1]); } }); p = 0; for (int i = 0; i < n; ++i) { if (i + 1 < n && tmp[i][1] != tmp[i + 1][1]) { tmp[i][1] = p++; } else { tmp[i][1] = p; } } for (int i = 0; i < n; ++i) { for (int j = 0; j < 3; ++j) { a[tmp[i][3]][j] = tmp[i][j]; } } } class SegmentTree { int n; long[] v; public SegmentTree(int n_) { n = 1; while (n < n_) { n *= 2; } v = new long[2 * n - 1]; } void update(int k, long val) { k += n - 1; v[k] = Math.max(v[k], val); while (k > 0) { k = (k - 1) / 2; v[k] = Math.max(v[2 * k + 1], v[2 * k + 2]); } } long query(int a, int b) { return query(0, n, a, b, 0); } long query(int l, int r, int a, int b, int k) { if (r <= a || b <= l) return 0; else if (a <= l && r <= b) return v[k]; else { return Math.max(query(l, (l + r) / 2, a, b, 2 * k + 1), query((l + r) / 2, r, a, b, 2 * k + 2)); } } } void tr(Object... objects) { System.out.println(Arrays.deepToString(objects)); } public static void main(String[] args) throws FileNotFoundException { new Main().run(); } }