#include"bits/stdc++.h" using namespace std; using ll = long long; using ld = long double; using pii = pair; using pll = pair; #define FOR(k,m,n) for(ll (k)=(m);(k)<(n);(k)++) #define REP(i,n) FOR((i),0,(n)) #define WAITING(str) int str;std::cin>>str; #define DEBUGING(str) cout<< #str << " " str< 0) { k = (k - 1) / 2; dat[k] = max(dat[k * 2 + 1], dat[k * 2 + 2]); } } ll query(ll a, ll b) { return query_impl(a, b, 0, 0, n); } private: ll n; vector dat; ll query_impl(ll a, ll b, ll k, ll l, ll r) { if (r <= a || b <= l)return 0; if (a <= l && r <= b)return dat[k]; else { ll vl = query_impl(a, b, k * 2 + 1, l, (l + r) / 2); ll vr = query_impl(a, b, k * 2 + 2, (l + r) / 2, r); return max(vl, vr); } } }; int main() { ll N; cin >> N; vector> abc(N); vector v, v2; REP(i, N) { ll a, b, c; cin >> a >> b >> c; abc[i] = { a, -b,c }; v.push_back(b); } v2.resize(N, 0); sort(abc.begin(), abc.end()); sort(v.begin(), v.end()); // zaatu for (auto& row : abc) { row[1] *= -1; ll b = row[1]; auto itr = upper_bound(v.begin(), v.end(), b); --itr; assert(*itr == b); ll dist = itr - v.begin(); row[1] = dist - v2[dist] + 1; v2[dist]++; } SegmentTree st(3e5); for (auto row : abc) { ll key = row[1]; ll value = row[2]; ll max = st.query(1, key); st.update(key, max + value); } cout << st.query(1, N + 1) << endl; return 0; }