- 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
- 70 lines of Go from the credited upstream file 1341D.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 CF1341D(_r io.Reader, _w io.Writer) {11 in := bufio.NewReader(_r)12 ds := [10]string{"1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"}13 14 var n, k int15 var s []byte16 Fscan(in, &n, &k)17 a := make([][10]int, n)18 for i := range a {19 Fscan(in, &s)20 for j, d := range ds {21 for k, b := range s {22 if b > d[k] {23 a[i][j] = 1e924 break25 }26 a[i][j] += int(d[k] - b)27 }28 }29 }30 31 dp := make([][]int8, n)32 for i := range dp {33 dp[i] = make([]int8, k+1)34 for j := range dp[i] {35 dp[i][j] = -136 }37 }38 ans := make([]byte, n)39 var f func(int, int) int840 f = func(p, left int) (res int8) {41 if p >= n {42 if left > 0 {43 return44 }45 return 146 }47 v := &dp[p][left]48 if *v >= 0 {49 return *v50 }51 defer func() { *v = res }()52 for i := 9; i >= 0; i-- {53 if a[p][i] <= left {54 ans[p] = byte('0' + i)55 if f(p+1, left-a[p][i]) == 1 {56 return 157 }58 }59 }60 return61 }62 if f(0, k) == 0 {63 Fprint(_w, -1)64 } else {65 Fprint(_w, string(ans))66 }67}68 6970