- 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
- 68 lines of Go from the credited upstream file 1542D.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 910func CF1542D(_r io.Reader, out io.Writer) {11 in := bufio.NewReader(_r)12 const mod = 99824435313 max := func(a, b int) int {14 if a > b {15 return a16 }17 return b18 }19 20 var n int21 var op string22 Fscan(in, &n)23 a := make([]int64, n+1)24 for i := 1; i <= n; i++ {25 if Fscan(in, &op); op == "+" {26 Fscan(in, &a[i])27 }28 }29 30 ans := int64(0)31 for p := 1; p <= n; p++ {32 v := a[p]33 if v == 0 {34 continue35 }36 dp := make([][]int, n+1)37 for i := range dp {38 dp[i] = make([]int, n+2)39 }40 dp[0][0] = 141 for i := 1; i <= n; i++ {42 w := a[i]43 for j := 0; j <= n; j++ {44 if w == 0 {45 if j > 0 || i <= p {46 dp[i][max(j-1, 0)] = (dp[i][max(j-1, 0)] + dp[i-1][j]) % mod47 }48 } else if w < v || w == v && i < p {49 dp[i][j+1] = (dp[i][j+1] + dp[i-1][j]) % mod50 } else {51 dp[i][j] = (dp[i][j] + dp[i-1][j]) % mod52 }53 if i != p {54 dp[i][j] = (dp[i][j] + dp[i-1][j]) % mod55 }56 }57 }58 s := int64(0)59 for _, fv := range dp[n] {60 s += int64(fv)61 }62 ans = (ans + s%mod*v) % mod63 }64 Fprint(out, ans)65}66 6768