#define _USE_MATH_DEFINES #include "bits/stdc++.h" using namespace std; #define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i)) #define rep(i,j) FOR(i,0,j) #define each(x,y) for(auto &(x):(y)) #define mp make_pair #define MT make_tuple #define all(x) (x).begin(),(x).end() #define debug(x) cout<<#x<<": "<<(x)<; using vi = vector; using vll = vector; template class DynamicRMQ { int n; Val init; vector dat; Cmp cmp; inline Val query(int a, int b, int k, int l, int r) { if (r <= a || b <= l) return init; if (a <= l && r <= b) return dat[k]; else { Val vl, vr; vl = query(a, b, k << 1, l, (l + r) >> 1); vr = query(a, b, (k << 1) | 1, (l + r) >> 1, r); return cmp(vl, vr) ? vl : vr; } } public: DynamicRMQ() {} DynamicRMQ(int n_, Val init_) :n(1), init(init_) { for (; n(n << 1, init); } void update(int k, Val a) { k += n; dat[k] = a; while (k > 1) { k >>= 1; dat[k] = cmp(dat[k << 1], dat[(k << 1) | 1]) ? dat[k << 1] : dat[(k << 1) | 1]; } } Val query(int a, int b) { return query(a, b, 1, 0, n); } }; typedef DynamicRMQ > RMinQ64; typedef DynamicRMQ > RMaxQ64; typedef DynamicRMQ > RMinQ32; typedef DynamicRMQ > RMaxQ32; template vector compress(vector &v) { vector a = v; sort(all(a)); a.erase(unique(all(a)), a.end()); each(b, v)b = (int)(lower_bound(all(a), b) - a.begin()); return a; } void solve() { int N; cin >> N; vi A(N), B(N), C(N); rep(i, N) { cin >> A[i] >> B[i] >> C[i]; } compress(B); map> M; rep(i, N) { M[A[i]].emplace_back(B[i], C[i]); } RMaxQ64 ma(N + 1, 0ll); each(p, M) { auto &V = p.second; sort(V.rbegin(), V.rend()); each(q, V) { ma.update(q.first + 1, max(ma.query(q.first+1,q.first+2),ma.query(0, q.first + 1) + q.second)); } } cout << ma.query(0, N + 1) << endl; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(15); solve(); return 0; }