Approach
Depth-first search
For Codeforces 2026F — Bermart Ice Cream, 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
- 144 lines of Go from the credited upstream file 2026F.go.
- The implementation visibly relies on sequence storage, work queue.
- 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 "bufio"5 . "fmt"6 "io"7)8 910var mem26 [90002][2001]int32 11var cur26 = 112 13type tuple26 struct {14 w, v, fi int15}16 17type stack26 []tuple2618 19func (st stack26) res(w int) int32 {20 return mem26[st[len(st)-1].fi][w]21}22 23func (st *stack26) push(w, v int) {24 cur26++25 f := mem26[cur26][:]26 copy(f, mem26[(*st)[len(*st)-1].fi][:])27 for i := len(f) - 1; i >= w; i-- {28 f[i] = max(f[i], f[i-w]+int32(v))29 }30 *st = append(*st, tuple26{w, v, cur26})31}32 33func (st *stack26) pop() (w, v int) {34 n := len(*st) - 135 w, v = (*st)[n].w, (*st)[n].v36 *st = (*st)[:n]37 return38}39 40func (st stack26) empty() bool {41 return len(st) == 142}43 44type deque26 struct{ l, r stack26 }45 46func (q *deque26) rebalance() {47 if q.r.empty() {48 q.l, q.r = q.r, q.l49 defer func() { q.l, q.r = q.r, q.l }()50 }51 m := len(q.r) / 252 for i := m; i > 0; i-- {53 q.l.push(q.r[i].w, q.r[i].v)54 }55 t := q.r[m+1:]56 q.r = q.r[:1]57 for _, p := range t {58 q.r.push(p.w, p.v)59 }60}61 62func (q deque26) res(w int) (mx int32) {63 for i := range w + 1 {64 mx = max(mx, q.l.res(i)+q.r.res(w-i))65 }66 return67}68 69func (q *deque26) pushFront(w, v int) {70 q.l.push(w, v)71}72 73func (q *deque26) pushBack(w, v int) {74 q.r.push(w, v)75}76 77func (q *deque26) popFront() (w, v int) {78 if q.l.empty() {79 q.rebalance()80 }81 return q.l.pop()82}83 84func (q *deque26) popBack() {85 if q.r.empty() {86 q.rebalance()87 }88 q.r.pop()89}90 91func cf2026F(in io.Reader, _w io.Writer) {92 out := bufio.NewWriter(_w)93 defer out.Flush()94 var q, nodeId, op, x, p, t int95 Fscanln(in, &q)96 type edge struct{ to, op, p, t, i int }97 g := make([][]edge, q+1)98 pos := make([]int, q+1)99 store := 1100 101 for i := range q {102 Fscanln(in, &op, &x, &p, &t)103 nodeId++104 v := pos[x]105 g[v] = append(g[v], edge{nodeId, op, p, t, i})106 if op == 1 {107 store++108 pos[store] = nodeId109 } else {110 pos[x] = nodeId111 }112 }113 114 ans := make([]int32, q)115 dq := deque26{stack26{{}}, stack26{{fi: 1}}}116 var dfs func(int)117 dfs = func(v int) {118 for _, e := range g[v] {119 if e.op == 2 {120 dq.pushBack(e.p, e.t)121 } else if e.op == 3 {122 e.p, e.t = dq.popFront()123 } else if e.op == 4 {124 ans[e.i] = dq.res(e.p) + 1125 }126 dfs(e.to)127 if e.op == 2 {128 dq.popBack()129 } else if e.op == 3 {130 dq.pushFront(e.p, e.t)131 }132 }133 }134 dfs(0)135 136 for _, v := range ans {137 if v > 0 {138 Fprintln(out, v-1)139 }140 }141}142 143144