package main import ( "bufio" "fmt" "os" "sort" "strconv" ) func configure(scanner *bufio.Scanner) { scanner.Split(bufio.ScanWords) scanner.Buffer(make([]byte, 1000005), 1000005) } func getNextString(scanner *bufio.Scanner) string { scanned := scanner.Scan() if !scanned { panic("scan failed") } return scanner.Text() } func getNextInt(scanner *bufio.Scanner) int { i, _ := strconv.Atoi(getNextString(scanner)) return i } func getNextInt64(scanner *bufio.Scanner) int64 { i, _ := strconv.ParseInt(getNextString(scanner), 10, 64) return i } func getNextFloat64(scanner *bufio.Scanner) float64 { i, _ := strconv.ParseFloat(getNextString(scanner), 64) return i } func main() { fp := os.Stdin wfp := os.Stdout cnt := 1 if os.Getenv("I") == "IronMan" { fp, _ = os.Open(os.Getenv("END_GAME")) cnt = 100 } scanner := bufio.NewScanner(fp) configure(scanner) writer := bufio.NewWriter(wfp) defer func() { r := recover() if r != nil { fmt.Fprintln(writer, r) } writer.Flush() }() for i := 0; i < cnt; i++ { if i > 0 { fmt.Fprintln(writer, "-----------------------------------") } solve(scanner, writer) } } func solve(scanner *bufio.Scanner, writer *bufio.Writer) { n := getNextInt(scanner) bb := make([]int64, n) cakes := make([]cake, n) counter := map[int64]int{} for i := 0; i < n; i++ { cakes[i].a = getNextInt64(scanner) cakes[i].b = getNextInt64(scanner) cakes[i].c = getNextInt64(scanner) bb[i] = cakes[i].b counter[cakes[i].a]++ } rm := rank(bb) sort.SliceStable(cakes, func(i, j int) bool { return cakes[i].a > cakes[j].a }) dec := newDecomposition(len(rm)) for i := 0; i < n; i++ { r := rm[cakes[i].b] mx := cint(0) a := cakes[i].a c := cakes[i].c i += counter[a] - 1 for counter[a] > 0 { counter[a]-- if r+1 < len(rm) { mx.maxAs(dec.max(r+1, len(rm))) } } dec.maximize(r, int64(mx)+c) } fmt.Fprintln(writer, dec.max(0, len(rm))) } func rank(aa []int64) map[int64]int { m := map[int64]int{} for _, a := range aa { m[a] = 1 } rr := make([]int64, 0) for a := range m { rr = append(rr, a) } sort.SliceStable(rr, func(i, j int) bool { return rr[i] < rr[j] }) res := map[int64]int{} for i := 0; i < len(rr); i++ { res[rr[i]] = i } return res } type cake struct { a, b, c int64 } type decomposition struct { chunk int bucket [][]int64 } func newDecomposition(n int) decomposition { bucket := make([][]int64, 1) bucket[0] = make([]int64, n) chunk := 8 for i := 0; n > 1; i++ { n = (n-1)/chunk + 1 bucket = append(bucket, make([]int64, n)) } return decomposition{ chunk: chunk, bucket: bucket, } } func (dec *decomposition) upleft(l int) int { return (l + dec.chunk - 1) / dec.chunk } func (dec *decomposition) upright(r int) int { return r / dec.chunk } func (dec *decomposition) maximize(index int, value int64) { dec.bucket[0][index] = value h := len(dec.bucket) for i := 0; i < h-1; i++ { p := index / dec.chunk r := (p + 1) * dec.chunk if r >= len(dec.bucket[i]) { r = len(dec.bucket[i]) } dec.bucket[i+1][p] = value for j := p * dec.chunk; j < r; j++ { if dec.bucket[i+1][p] < dec.bucket[i][j] { dec.bucket[i+1][p] = dec.bucket[i][j] } } index /= dec.chunk } } func (dec *decomposition) max(l, r int) int64 { return dec.maxh(l, r, 0) } func (dec *decomposition) maxh(l, r, h int) int64 { m := dec.bucket[h][l] ll := dec.upleft(l) rr := dec.upright(r) for i := l; i < r && i < ll*dec.chunk; i++ { if m < dec.bucket[h][i] { m = dec.bucket[h][i] } } if ll < rr { mm := dec.maxh(ll, rr, h+1) if m < mm { m = mm } } for i := rr * dec.chunk; i < r && ll <= rr; i++ { if m < dec.bucket[h][i] { m = dec.bucket[h][i] } } return m } type cint int64 func (ct *cint) cintize(x interface{}) cint { if y, isCint := x.(cint); isCint { return y } if y, isInt64 := x.(int64); isInt64 { return cint(y) } return cint(x.(int)) } func (ct *cint) minAs(x interface{}) *cint { *ct = ct.min(x) return ct } func (ct *cint) maxAs(x interface{}) *cint { *ct = ct.max(x) return ct } func (ct cint) min(x interface{}) cint { y := ct.cintize(x) if ct > y { return y } return ct } func (ct cint) max(x interface{}) cint { y := ct.cintize(x) if ct < y { return y } return ct } func (ct cint) add(x interface{}) cint { return ct + ct.cintize(x) } func (ct cint) sub(x interface{}) cint { return ct - ct.cintize(x) } func (ct cint) mul(x interface{}) cint { return ct * ct.cintize(x) } func (ct cint) div(x interface{}) cint { return ct / ct.cintize(x) }