#include "bits/stdc++.h" using namespace std; #pragma warning(disable:4996) #define Seg_Max_N (1<<18) using Value = long long int; const Value ini =0; struct segtree { int N; vectordat; segtree() {} segtree(int n) :dat(2 * Seg_Max_N) { N = 1; while (N < n) N *= 2; for (int i = 0; i < 2 * N - 1; i++) { dat[i] = ini; } } Value connect(const Value&l, const Value&r) { return max(l,r); } // update k th element void update(int k, Value a) { k += N - 1; dat[k] = a; while (k > 0) { k = (k - 1) / 2; const Value al(dat[k * 2 + 1]); const Value ar(dat[k * 2 + 2]); dat[k] = connect(al, ar); } } // min [a, b) Value query(int a, int b) { return query(a, b, 0, 0, N); } Value query(int a, int b, int k, int l, int r) { if (r <= a or b <= l) return ini; if (a <= l and r <= b) return dat[k]; const int m = (l + r) / 2; const Value al(query(a, b, k * 2 + 1, l, m)); const Value ar(query(a, b, k * 2 + 2, m, r)); return connect(al, ar); } }; struct cake { int a; int b; long long int c; }; bool operator<(const cake&l, const cake&r) { return l.a struct Compress { mapmp; maprevmp; Compress(vectorvs) { setmp(vs); } Compress() :mp(), revmp() { } void setmp(vectorvs) { sort(vs.begin(), vs.end()); vs.erase(unique(vs.begin(), vs.end()), vs.end()); for (int i = 0; i < static_cast(vs.size()); ++i) { mp[vs[i]] = i; revmp[i] = vs[i]; } } }; int main() { int N;cin>>N; map>mp; vectorbs{ 0 }; for (int i = 0; i < N; ++i) { int a,b; long long int c; scanf("%d %d %lld",&a,&b,&c); bs.push_back(b); mp[a].push_back(cake{ a,b,c }); } Compress cb(bs); segtree seg(N+1); seg.update(0,0); for (auto m : mp) { vectornums; for (auto c : m.second) { long long int num = seg.query(0, cb.mp[c.b]); nums.push_back(num); } int id = 0; for (auto c : m.second) { seg.update(cb.mp[c.b], max(seg.query(cb.mp[c.b], cb.mp[c.b] + 1), nums[id] + c.c)); id++; } } long long int answer=seg.query(0,N+1); cout<