- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 61 lines of Go from the credited upstream file 75D.go.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected.
Complexity
Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1package main2 3import (4 "bufio"5 . "fmt"6 "io"7)8 910func CF75D(_r io.Reader, out io.Writer) {11 in := bufio.NewReader(_r)12 const inf int64 = 1e913 min := func(a, b int64) int64 {14 if a < b {15 return a16 }17 return b18 }19 max := func(a, b int64) int64 {20 if a > b {21 return a22 }23 return b24 }25 26 var n, m, k, id int27 var v int6428 Fscan(in, &n, &m)29 type res struct{ s, minS, maxS, maxSubS int64 }30 rs := make([]res, n)31 for i := range rs {32 r := res{0, inf, -inf, -inf}33 dp := -inf34 for Fscan(in, &k); k > 0; k-- {35 Fscan(in, &v)36 r.s += v37 r.minS = min(r.minS, r.s)38 r.maxS = max(r.maxS, r.s)39 dp = max(dp, 0) + v40 r.maxSubS = max(r.maxSubS, dp)41 }42 rs[i] = r43 }44 45 Fscan(in, &id)46 r := rs[id-1]47 ans := r.maxSubS48 minS := min(0, r.minS)49 base := r.s50 for m--; m > 0; m-- {51 Fscan(in, &id)52 r := rs[id-1]53 ans = max(ans, max(r.maxSubS, r.maxS+base-minS))54 minS = min(minS, base+r.minS)55 base += r.s56 }57 Fprint(out, ans)58}59 6061