Approach
Direct simulation
For Codeforces 2021D — Boss, Thirsty, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.
- 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
- 60 lines of Go from the credited upstream file 2021D.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 . "fmt"5 "io"6)7 89func cf2021D(in io.Reader, out io.Writer) {10 const inf int = 1e1811 var t int12 for Fscan(in, &t); t > 0; t-- {13 var n, m int14 Fscan(in, &n, &m)15 f1 := make([]int, m+2)16 f2 := make([]int, m+2)17 f3 := make([]int, m+2)18 f4 := make([]int, m+2)19 a := make([]int, m+2)20 ans := -inf21 fk := 022 for n > 0 {23 n--24 for i := 1; i <= m; i++ {25 Fscan(in, &a[i])26 a[i] += a[i-1]27 f3[i] = -inf28 f4[i] = -inf29 }30 f := 031 g := -inf32 33 for i := 2; i <= m; i++ {34 f = min(f, a[i-2])35 f4[i] = max(f4[i-1], f2[i-1]-f, f1[i]-f)36 j := m + 1 - i37 g = max(g, a[j+1])38 f3[j] = max(f3[j+1], f1[j+1]+g, f2[j]+g)39 }40 41 for i := 1; i <= m; i++ {42 f1[i] = f3[i] - a[i-1]43 f2[i] = f4[i] + a[i]44 if fk == 0 {45 f1[i] = max(f1[i], a[i]-a[i-1])46 f2[i] = max(f2[i], a[i]-a[i-1])47 }48 if n == 0 {49 ans = max(ans, f1[i], f2[i])50 }51 }52 fk = 153 }54 55 Fprintln(out, ans)56 }57}58 5960