#include using namespace std; using i64=int64_t; #define rep(i,x,y) for(i64 i=i64(x),i##_max_for_repmacro=i64(y); i ostream &operator<<(ostream &os, const vector &vec){ os << "["; for (const auto &v : vec) { os << v << ","; } os << "]"; return os; } template class segtree { public: int n, size_; vector dat; T id_; segtree() = default; segtree(int size, T id, T initial_value) { init(size, id, initial_value); } void init(int size, T id, T initial_value) { size_ = size; id_ = id; n = 1; while (n < size) n *= 2; dat.assign(2 * n - 1, id); for (int i = 0; i < size; ++i) update(i, initial_value); } int size() const { return size_; } void update(int k, T a) { k += n - 1; // leaf dat[k] = a; while (k > 0) { k = (k - 1) / 2; dat[k] = op(dat[k * 2 + 1], dat[k * 2 + 2]); } } T at(int index) { return dat[index + n - 1]; } void add(int k, T a) { update(k, at(k) + a); } T query(int a, int b) { return query(a, b, 0, 0, n); } T query(int a, int b, int k, int l, int r) { if (r <= a or b <= l) return id_; if (a <= l and r <= b) return dat[k]; int m = (l + r) / 2; return op(query(a, b, k * 2 + 1, l, m), query(a, b, k * 2 + 2, m, r)); } }; i64 mymax(i64 a,i64 b){ return max(a,b); } void solve(){ i64 N; cin >> N; vector> ABC(N); rep(i,0,N){ i64 A,B,C; cin >> A >> B >> C; ABC[i]=make_tuple(A,B,C); } sort(begin(ABC),end(ABC)); vector> bi(N); rep(i,0,N) bi[i]=make_pair(get<1>(ABC[i]),i); sort(begin(bi),end(bi)); vector rev1(N),rev2(N); { i64 j=0; rep(i, 0, N){ rev1[bi[i].second] = i; if(i and bi[i].first>bi[i-1].first) j=i; rev2[bi[i].second]=j; } } segtree seg(N,-inf64,-inf64); map>> mp; rep(i,0,N){ i64 a,b,c; tie(a,b,c)=ABC[i]; mp[a].push_back(make_tuple(b,c,i)); } for(const auto& p:mp){ vector> iv; for(const auto& t:p.second){ i64 b,c,i; tie(b,c,i)=t; i64 tmp=max(i64(0),seg.query(0,rev2[i])); iv.push_back(make_pair(rev1[i],tmp+c)); } for(auto& tmp:iv) seg.update(tmp.first,tmp.second); } cout << seg.query(0,N) << endl; } int main(){ std::cin.tie(0); std::ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(16); solve(); return 0; }