- 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
- 71 lines of Go from the credited upstream file 1650F.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 CF1650F(_r io.Reader, _w io.Writer) {11 in := bufio.NewReader(_r)12 out := bufio.NewWriter(_w)13 defer out.Flush()14 15 var T, n, m, e, t, p int16o:17 for Fscan(in, &T); T > 0; T-- {18 Fscan(in, &n, &m)19 a := make([]int, n)20 for i := range a {21 Fscan(in, &a[i])22 }23 type pair struct{ t, p, i int }24 gs := make([][]pair, n)25 for i := 1; i <= m; i++ {26 Fscan(in, &e, &t, &p)27 gs[e-1] = append(gs[e-1], pair{t, p, i})28 }29 30 ans := []interface{}{}31 t := 032 for k, g := range gs {33 dp := [101]int{}34 for i := range dp {35 dp[i] = 1e9 + 136 }37 dp[0] = 038 from := make([][101]int, len(g))39 for i, p := range g {40 for j := 100; j > 0; j-- {41 k := j - p.p42 if k < 0 {43 k = 044 }45 if v := dp[k] + p.t; v < dp[j] {46 dp[j] = v47 from[i][j] = k48 } else {49 from[i][j] = j50 }51 }52 }53 t += dp[100]54 if t > a[k] {55 Fprintln(out, -1)56 continue o57 }58 for i, j := len(g)-1, 100; i >= 0; i-- {59 if from[i][j] != j {60 ans = append(ans, g[i].i)61 j = from[i][j]62 }63 }64 }65 Fprintln(out, len(ans))66 Fprintln(out, ans...)67 }68}69 7071