import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; alias Cake = Tuple!(int, "a", int, "b", int, "c"); void main() { int n; scan(n); auto cs = new Cake[](n); foreach (i ; 0 .. n) { int a, b, c; scan(a, b, c); cs[i] = Cake(a, b, c); } int[int] zt; cs.sort!"a.b < b.b"(); int rank = 1; zt[cs[0].b] = rank; foreach (i ; 1 .. n) { if (cs[i].b == cs[i-1].b) { continue; } rank++; zt[cs[i].b] = rank; } auto bit = FenwickTree!(long)(n + 10); cs.multiSort!("a.a < b.a", "a.b > b.b")(); auto cur = new long[](n + 10); foreach (i ; 0 .. n) { auto res = bit.pmax(zt[cs[i].b]); res += cs[i].c; if (res > cur[zt[cs[i].b]]) { bit.update(zt[cs[i].b], res); cur[zt[cs[i].b]] = res; } } auto ans = bit.pmax(n + 10); writeln(ans); } struct FenwickTree(T) { private { int _size; T[] _data; } this(int N) { _size = N; _data = new T[](_size + 1); } void update(int i, T x) { i++; while (i <= _size) { _data[i] = max(_data[i], x); i += i & (-i); } } T pmax(int r) { T res = 0; while (r > 0) { res = max(res, _data[r]); r -= r & (-r); } return res; } } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }