Approach
Depth-first search
For Codeforces 633F — The Chocolate Spree, the implementation follows one branch at a time, making it suitable for components, trees, backtracking, or dependency exploration.
- Define the state carried into one recursive or stack frame.
- Mark or choose the current state before exploring children.
- Combine child results or undo the choice when the branch finishes.
Code notes
- 60 lines of Go from the credited upstream file 633F.go.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected, together with recursive traversal.
Complexity
Count unique states for graph traversal; for backtracking, count the branching factor and maximum depth.
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 8func cf633F(in io.Reader, out io.Writer) {9 var n, ans int10 Fscan(in, &n)11 a := make([]int, n)12 for i := range a {13 Fscan(in, &a[i])14 }15 g := make([][]int, n)16 for i := 1; i < n; i++ {17 var v, w int18 Fscan(in, &v, &w)19 v--20 w--21 g[v] = append(g[v], w)22 g[w] = append(g[w], v)23 }24 25 var dfs func(int, int) (int, int, int)26 dfs = func(v, fa int) (int, int, int) {27 val := a[v]28 maxChain := val 29 maxPathV := val 30 maxPathW := 0 31 maxChainPath := val 32 for _, w := range g[v] {33 if w == fa {34 continue35 }36 chainW, pathW, chainPathW := dfs(w, v)37 38 39 40 41 ans = max(ans, max(maxPathV, maxPathW)+pathW, maxChain+chainPathW, maxChainPath+chainW)42 43 44 45 46 47 maxChainPath = max(maxChainPath, chainPathW+val, maxPathW+chainW+val, maxChain+pathW)48 49 maxPathV = max(maxPathV, maxChain+chainW)50 maxPathW = max(maxPathW, pathW)51 maxChain = max(maxChain, chainW+val)52 }53 return maxChain, max(maxPathV, maxPathW), maxChainPath54 }55 dfs(0, -1)56 Fprint(out, ans)57}58 5960