- 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
- 50 lines of Go from the credited upstream file 1799F.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 "slices"7)8 910func cf1799F(in io.Reader, out io.Writer) {11 var T, n, b, div, sub int12 for Fscan(in, &T); T > 0; T-- {13 Fscan(in, &n, &b, &div, &sub)14 a := make([]int, n)15 for i := range a {16 Fscan(in, &a[i])17 }18 slices.SortFunc(a, func(a, b int) int { return b - a })19 20 s1 := make([]int, n+1)21 s2 := make([]int, n+1)22 s3 := make([]int, n+1)23 for i, v := range a {24 s1[i+1] = s1[i] + (v+1)/225 s2[i+1] = s2[i] + max(v-b, 0)26 s3[i+1] = s3[i] + v27 }28 29 ans := int(1e18)30 s := 031 for i := range min(div, sub) + 1 {32 leftDiv := div - i33 leftSub := sub - i34 if n-i >= leftDiv+leftSub {35 for j := i; j <= i+leftDiv; j++ {36 k := j + leftSub37 r := k + leftDiv - (j - i)38 ans = min(ans, s+s1[j]-s1[i]+s2[k]-s2[j]+s1[r]-s1[k]+s3[n]-s3[r])39 }40 }41 if i < n {42 s += max((a[i]+1)/2-b, 0)43 }44 }45 Fprintln(out, ans)46 }47}48 4950