#include"bits/stdc++.h" using namespace std; #define REP(k,m,n) for(int (k)=(m);(k)<(n);(k)++) #define rep(i,n) REP((i),0,(n)) using ll = long long; template class SegmentTree { private: using F = function; // モノイド型 int n; // 横幅 F f; // モノイド T e; // モノイド単位元 vector data; public: // init忘れに注意 SegmentTree() {} SegmentTree(F f, T e) :f(f), e(e) {} void init(int n_) { n = 1; while (n < n_)n <<= 1; data.assign(n << 1, e); } void build(const vector& v) { int n_ = v.size(); init(n_); rep(i, n_)data[n + i] = v[i]; for (int i = n - 1; i >= 0; i--) { data[i] = f(data[(i << 1) | 0], data[(i << 1) | 1]); } } void set_val(int idx, T val) { idx += n; data[idx] = val; while (idx >>= 1) { data[idx] = f(data[(idx << 1) | 0], data[(idx << 1) | 1]); } } T query(int a, int b) { // [a,b) T vl = e, vr = e; for (int l = a + n, r = b + n; l < r; l >>= 1, r >>= 1) { if (l & 1)vl = f(vl, data[l++]); // unknown if (r & 1)vr = f(data[--r], vr); // unknown } return f(vl, vr); } }; // if sort with A, this problem can reduce to LIS // but for A_i==A_i+1's case, // unite A's value and batch process int main() { // input int N; cin >> N; vector> abc(N, vector(3)); rep(i, N)rep(j, 3)cin >> abc[i][j]; // preprocess map>> cakes; int cnt = 0; set st; map trans; for (const auto& row : abc)st.insert(row[1]); for (const auto& num : st)trans[num] = cnt++; for (const auto& row : abc) { cakes[row[0]].push_back({ trans[row[1]], row[2] }); } // segment tree init auto f = [](ll l, ll r) {return max(l, r); }; SegmentTree seg(f, 0); seg.init(cnt); // query for (auto& itr : cakes) { vector> batch; for (auto& cake : itr.second) { ll b, c; tie(b, c) = cake; ll val = seg.query(0, b); ll bef = seg.query(b, b + 1); batch.push_back({ b, val + c }); } for (auto& p : batch) { ll b, val; tie(b, val) = p; ll now = seg.query(b, b + 1); ll next = max(now, val); seg.set_val(b, next); } } cout << seg.query(0, cnt) << endl; return 0; }