- 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
- 65 lines of Go from the credited upstream file 1776J.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 . "fmt"5 "io"6)7 89func cf1776J(in io.Reader, out io.Writer) {10 var n, m, k int11 Fscan(in, &n, &m, &k)12 c := make([]int, n)13 for i := range c {14 Fscan(in, &c[i])15 }16 dis := make([][]int, n*2)17 for i := range dis {18 dis[i] = make([]int, n*2)19 for j := range dis[i] {20 dis[i][j] = 1e921 }22 dis[i][i] = 023 }24 for i := 0; i < n; i++ {25 dis[i][i+n] = k26 dis[i+n][i] = k27 }28 29 for range m {30 var x, y int31 Fscan(in, &x, &y)32 x--33 y--34 if c[x] != c[y] {35 dis[x][y+n] = 136 dis[y+n][x] = 137 dis[x+n][y] = 138 dis[y][x+n] = 139 } else {40 dis[x][y] = 141 dis[y][x] = 142 dis[x+n][y+n] = 143 dis[y+n][x+n] = 144 }45 }46 47 for k := range dis {48 for i := range dis {49 for j := range dis {50 dis[i][j] = min(dis[i][j], dis[i][k]+dis[k][j])51 }52 }53 }54 55 ans := 056 for i := 0; i < n; i++ {57 for j := 0; j < n; j++ {58 ans = max(ans, (dis[i][j]+dis[i][j+n]+k)>>1)59 }60 }61 Fprint(out, ans)62}63 6465