Approach
Breadth-first search
For Codeforces 2026E — Best Subsequence, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.
- Model each valid configuration as a state and each legal move as an edge.
- Seed the queue with the starting state and mark it immediately.
- Expand each state once, recording distance or reachability for unseen neighbours.
Code notes
- 87 lines of Go from the credited upstream file 2026E.go.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected, together with recursive traversal.
Complexity
Verify that each state and transition is processed only a bounded number of times; that determines the traversal cost.
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 "math"7 "math/bits"8)9 1011func cf2026E(in io.Reader, out io.Writer) {12 var T, n int13 var s uint14 for Fscan(in, &T); T > 0; T-- {15 Fscan(in, &n)16 const mx = 6017 st := n + mx18 end := st + 119 type nb struct{ to, rid, cap int }20 g := make([][]nb, end+1)21 addEdge := func(from, to, cap int) {22 g[from] = append(g[from], nb{to, len(g[to]), cap})23 g[to] = append(g[to], nb{from, len(g[from]) - 1, 0})24 }25 for i := range n {26 for Fscan(in, &s); s > 0; s &= s - 1 {27 j := bits.TrailingZeros(s)28 addEdge(i, n+j, 1)29 }30 addEdge(st, i, 1)31 }32 for j := range mx {33 addEdge(n+j, end, 1)34 }35 36 d := make([]int, len(g))37 bfs := func() bool {38 clear(d)39 d[st] = 140 q := []int{st}41 for len(q) > 0 {42 v := q[0]43 q = q[1:]44 for _, e := range g[v] {45 if w := e.to; e.cap > 0 && d[w] == 0 {46 d[w] = d[v] + 147 q = append(q, w)48 }49 }50 }51 return d[end] > 052 }53 iter := make([]int, len(g))54 var dfs func(int, int) int55 dfs = func(v, totalFlow int) (curFlow int) {56 if v == end {57 return totalFlow58 }59 for ; iter[v] < len(g[v]); iter[v]++ {60 e := &g[v][iter[v]]61 if w := e.to; e.cap > 0 && d[w] > d[v] {62 f := dfs(w, min(totalFlow-curFlow, e.cap))63 if f == 0 {64 continue65 }66 e.cap -= f67 g[w][e.rid].cap += f68 curFlow += f69 if curFlow == totalFlow {70 break71 }72 }73 }74 return75 }76 maxFlow := 077 for bfs() {78 clear(iter)79 maxFlow += dfs(st, math.MaxInt)80 }81 82 Fprintln(out, n-maxFlow)83 }84}85 8687