- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 60 lines of Go from the credited upstream file 721C.go.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected.
Complexity
Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.
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 CF721C(_r io.Reader, _w io.Writer) {11 in := bufio.NewReader(_r)12 out := bufio.NewWriter(_w)13 defer out.Flush()14 15 var n, m int16 var maxT int3217 Fscan(in, &n, &m, &maxT)18 es := make([][3]int32, m)19 for i := range es {20 Fscan(in, &es[i][0], &es[i][1], &es[i][2])21 }22 23 const mx = 500124 f := make([][mx]int32, n)25 for i := range f {26 for j := 1; j <= n; j++ {27 f[i][j] = maxT + 128 }29 }30 f[0][1] = 031 from := make([][mx]int16, n+1)32 33 34 ans := 035 for i := 1; i < n; i++ {36 for _, e := range es {37 v, w, t := e[0], e[1], e[2]38 sumT := f[i-1][v] + t39 if sumT < f[i][w] {40 f[i][w] = sumT41 from[i][w] = int16(v)42 }43 }44 if f[i][n] <= maxT {45 ans = i46 }47 }48 49 Fprintln(out, ans+1)50 path := make([]any, ans+1)51 v := int16(n)52 for i := ans; i >= 0; i-- {53 path[i] = v54 v = from[i][v]55 }56 Fprint(out, path...)57}58 5960