- Define precisely what one DP state represents.
- Establish the base cases before transitions are evaluated.
- Process states in dependency order and combine only already-known values.
Code notes
- 81 lines of Go from the credited upstream file 1077F2.go.
- The implementation visibly relies on sequence storage, cached states.
- No explicit loop blocks detected.
Complexity
Multiply the number of reachable states by the work performed for each transition, then include the stored state table in memory usage.
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 910type mqData77 struct {11 v int6412 del int13}14type mq77 struct {15 data []mqData7716 size int17}18 19func (q *mq77) push(v int64) {20 q.size++21 d := mqData77{v, 1}22 for len(q.data) > 0 && q.data[len(q.data)-1].v <= v {23 d.del += q.data[len(q.data)-1].del24 q.data = q.data[:len(q.data)-1]25 }26 q.data = append(q.data, d)27}28func (q *mq77) pop() {29 q.size--30 if q.data[0].del > 1 {31 q.data[0].del--32 } else {33 q.data = q.data[1:]34 }35}36func (q mq77) top() (v int64) { return q.data[0].v }37 38func CF1077F2(_r io.Reader, out io.Writer) {39 in := bufio.NewReader(_r)40 var n, lim, choose int41 Fscan(in, &n, &lim, &choose)42 a := make([]int64, n)43 for i := range a {44 Fscan(in, &a[i])45 }46 47 dp := make([][]int64, choose)48 for i := range dp {49 dp[i] = make([]int64, n)50 }51 copy(dp[0], a[:lim])52 for i := 1; i < choose; i++ {53 q := mq77{}54 q.push(dp[i-1][i-1])55 up := (i + 1) * lim56 if up > n {57 up = n58 }59 for j := i; j < up; j++ {60 if q.size > lim {61 q.pop()62 }63 dp[i][j] = q.top() + a[j]64 q.push(dp[i-1][j])65 }66 }67 68 ans := int64(0)69 for _, v := range dp[choose-1][n-lim:] {70 if v > ans {71 ans = v72 }73 }74 if ans == 0 {75 ans = -176 }77 Fprint(out, ans)78}79 8081