#include #define rep(i,a,b) for(int i=a;i=b;i--) #define fore(i,a) for(auto &i:a) #define all(x) (x).begin(),(x).end() #pragma GCC optimize ("-O3") using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); } typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60; templatebool chmax(T &a, const T &b) { if (abool chmin(T &a, const T &b) { if (b struct SegTree { //[l,r) V comp(V& l, V& r) { return max(l, r); }; vector val; SegTree() { val = vector(NV * 2, def); } V get(int x, int y, int l = 0, int r = NV, int k = 1) { if (r <= x || y <= l)return def; if (x <= l && r <= y)return val[k]; auto a = get(x, y, l, (l + r) / 2, k * 2); auto b = get(x, y, (l + r) / 2, r, k * 2 + 1); return comp(a, b); } void update(int i, V v) { i += NV; val[i] = v; while (i>1) i >>= 1, val[i] = comp(val[i * 2], val[i * 2 + 1]); } void add(int i, V v) { update(i, val[i + NV] + v); } V operator[](int x) { return get(x, x + 1); } }; /*---------------------------------------------------------------------------------------------------             ∧_∧       ∧_∧  (´<_` )  Welcome to My Coding Space!      ( ´_ゝ`) /  ⌒i     /   \    | |     /   / ̄ ̄ ̄ ̄/  |   __(__ニつ/  _/ .| .|____      \/____/ (u ⊃ ---------------------------------------------------------------------------------------------------*/ int N, A[201010], B[201010], C[201010]; SegTree st; //--------------------------------------------------------------------------------------------------- void _main() { cin >> N; rep(i, 0, N) cin >> A[i] >> B[i] >> C[i]; vector v; rep(i, 0, N) v.push_back(B[i]); sort(all(v)); v.erase(unique(all(v)), v.end()); rep(i, 0, N) B[i] = lower_bound(all(v), B[i]) - v.begin(); map> buf; rep(i, 0, N) buf[A[i]].push_back(i); fore(p, buf) { auto& v = p.second; vector> nxt; fore(i, v) { ll c = st.get(0, B[i]) + C[i]; nxt.push_back({ B[i], c }); } fore(p, nxt) st.update(p.first, max(p.second, st[p.first])); } ll ans = st.get(0, N); cout << ans << endl; }