import strutils, sequtils, algorithm, tables type SegmentTree = object n: int dat: seq[int64] proc init(sz: int): SegmentTree = var n = 1 while n0: k = (k-1) div 2 this.dat[k] = max(this.dat[k*2+1], this.dat[k*2+2]) proc rmax(this: SegmentTree, ql, qr, i, il, ir: int): int64 = if qr<=il or ir<=ql: result = 0 elif ql<=il and ir<=qr: result = this.dat[i] else: let m = (il+ir) div 2 result = max( this.rmax(ql, qr, i*2+1, il, m), this.rmax(ql, qr, i*2+2, m, ir)) proc main() = let n = stdin.readLine.parseInt var abcs = (0..int: result = cmp(y[0], x[0]) if result==0: result = cmp(y[1], x[1]) let m = map.len var tree = init(m) ans = 0'i64 for i, abc in abcs: let s = tree.rmax(0, map[abc[1]], 0, 0, tree.n) + abc[2] ans = max(ans, s) tree.update(abc[1], s) echo ans main()