- 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
- 79 lines of Go from the credited upstream file 933A.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 9101112131415 1617 1819func CF933A(_r io.Reader, out io.Writer) {20 max := func(a, b int) int {21 if a > b {22 return a23 }24 return b25 }26 in := bufio.NewReader(_r)27 var n int28 Fscan(in, &n)29 a := make([]int, n)30 sum := make([][2]int, n+1)31 for i := range a {32 Fscan(in, &a[i])33 sum[i+1][0] = sum[i][0]34 sum[i+1][1] = sum[i][1]35 sum[i+1][a[i]-1]++36 }37 ans := 038 for i := range a {39 maxL := 040 for _, s := range sum[:i+1] {41 maxL = max(maxL, s[0]+sum[i][1]-s[1])42 }43 maxR := 044 for _, s := range sum[i:] {45 maxR = max(maxR, s[0]-sum[i][0]+sum[n][1]-s[1])46 }47 ans = max(ans, maxL+maxR)48 }49 Fprint(out, ans)50}51 52func CF933A2(_r io.Reader, out io.Writer) {53 max := func(a, b int) int {54 if a > b {55 return a56 }57 return b58 }59 in := bufio.NewReader(_r)60 var n, v int61 Fscan(in, &n)62 dp := [5]int{}63 for ; n > 0; n-- {64 if Fscan(in, &v); v == 1 {65 dp[1]++66 dp[2] = max(dp[1], dp[2])67 dp[3] = max(dp[2], dp[3]+1)68 dp[4] = max(dp[3], dp[4])69 } else {70 dp[2] = max(dp[1], dp[2]+1)71 dp[3] = max(dp[2], dp[3])72 dp[4] = max(dp[3], dp[4]+1)73 }74 }75 Fprint(out, dp[4])76}77 7879