#include using namespace std; template< typename T > class segment_tree { const vector coord; const map mp; const int sz; const int Sz; vector seg; const function o; const T id; public: segment_tree ( const vector coord, const function o, const T id ) : coord([&](){ vector v = coord; int p = 1; for (; p < (int)v.size(); p <<= 1) {} v.resize(p, (int)((1LL << 31) - 1)); return v; }()), mp([&](){ map m; int i = 0; for (auto const& e : coord) { m[e] = i++; } return m; }()), sz(coord.size()), Sz(sz << 1), seg(Sz, id), o(o), id(id) { } void update (int k, const T& x) { auto it = mp.find(k); assert(it != mp.end()); k = it->second; k += sz; seg[k] = x; while(k >>= 1) { seg[k] = o(seg[2 * k], seg[2 * k + 1]); } } void add (int k, const T& x) { update(k, x + seg[k + sz]); } T query (int l, int r) { l = lower_bound(coord.begin(), coord.end(), l) - coord.begin(); r = lower_bound(coord.begin(), coord.end(), r) - coord.begin(); T L = id, R = id; for(l += sz, r += sz; l < r; l >>= 1, r >>= 1) { if(l & 1) L = o(L, seg[l++]); if(r & 1) R = o(seg[--r], R); } return o(L, R); } T at (int k) const { auto it = mp.find(k); assert(it != mp.end()); return seg[it->second + sz]; } }; int main() { int n; cin >> n; vector coord(n, 0); vector> abc(n); for (int i = 0; i < n; i++) { int a, b; long long c; cin >> a >> b >> c; coord[i] = b; abc[i] = make_tuple(a, b, c); } coord.push_back(0); sort(coord.begin(), coord.end()); sort(abc.begin(), abc.end(), []( decltype(abc)::value_type p, decltype(abc)::value_type q ) { int a, b, x, y; long long c, z; tie(a, b, c) = p; tie(x, y, z) = q; if (a < x) return true; if (a > x) return false; if (b > y) return true; return false; }); constexpr long long inf = 1LL << 60; segment_tree sgt( coord, [](long long a, long long b){return max(a, b);}, -inf ); sgt.update(0, 0); for (auto const& p : abc) { int b; long long c; tie(ignore, b, c) = p; long long tmp = sgt.query(0, b) + c; if (sgt.at(b) < tmp) sgt.update(b, tmp); } cout << sgt.query(0, 1 << 30) << endl; return 0; }