#include #include #include #include typedef int32_t i32; typedef int64_t i64; typedef struct cake { i32 a, b, c; } node; int cmp_node (const void *a, const void *b) { node *p = (node *)a; node *q = (node *)b; if (p->a != q->a) return p->a < q->a ? -1 : 1; return p->b > q->b ? -1 : 1; } int cmp_int (const void *a, const void *b) { i32 d = *(i32 *)a - *(i32 *)b; return d == 0 ? 0 : d < 0 ? -1 : 1; } i32 to_index (i32 *z, i32 n, i32 v) { i32 l = 0; i32 r = n; while (r - l > 1) { i32 m = (l + r) / 2; if (z[m] <= v) { l = m; } else { r = m; } } return l; } i64 max (i64 a, i64 b) { return a > b ? a : b; } void update (i64 *bit, i32 x, i64 v) { i32 n = bit[0]; for (i32 i = x; i <= n && bit[i] < v; i += i & -i) { bit[i] = v; } } i64 calc_max (i64 *bit, i32 x) { i64 res = 0; for (i32 i = x; i > 0; i -= i & -i) { res = max (res, bit[i]); } return res; } void run (void) { i32 n; scanf ("%" SCNi32, &n); node *p = (node *) calloc (n, sizeof (node)); i32 *z = (i32 *) calloc (n, sizeof (i32)); for (i32 i = 0; i < n; ++i) { i32 a, b, c; scanf ("%" SCNi32 "%" SCNi32 "%" SCNi32, &a, &b, &c); p[i] = (node) {a, b, c}; z[i] = b; } qsort (p, n, sizeof (node), cmp_node); qsort (z, n, sizeof (i32), cmp_int); i32 len = 1; for (i32 i = 1; i < n; ++i) { if (z[i] == z[len - 1]) continue; z[len++] = z[i]; } i64 *bit = (i64 *) calloc (len + 1, sizeof (i64)); bit[0] = len; for (i32 i = 0; i < n; ++i) { i32 k = to_index (z, len, p[i].b) + 1; i64 v = calc_max (bit, k - 1) + p[i].c; update (bit, k, v); } printf ("%" PRIi64 "\n", calc_max (bit, len)); } int main (void) { run(); return 0; }