#include #include struct P { int a, b, c; }; int n; struct P a[200000]; long long bit[200001]; long long max(long long x, long long y) { return x > y ? x : y; } int cmp(const void *x_, const void *y_) { struct P x = *(struct P *)x_; struct P y = *(struct P *)y_; if (x.a != y.a) return x.a - y.a; return y.b - x.b; } int cmpb(const void *x_, const void *y_) { struct P x = *(struct P *)x_; struct P y = *(struct P *)y_; return x.b - y.b; } void update(int k, long long v) { if (bit[k] < v) { for (int i = k + 1; i <= 200000; i += i & -i) { bit[i] = max(bit[i], v); } } } long long query(int k) { long long res = 0; for (int i = k; i > 0; i -= i & -i) { res = max(res, bit[i]); } return res; } int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d %d %d", &a[i].a, &a[i].b, &a[i].c); } qsort(a, n, sizeof(struct P), cmpb); { int k = -1; int p = -1; for (int i = 0; i < n; i++) { k += p != a[i].b; p = a[i].b; a[i].b = k; } } qsort(a, n, sizeof(struct P), cmp); for (int i = 0; i < n; i++) { update(a[i].b, query(a[i].b) + a[i].c); } printf("%lld\n", query(200000)); }