Approach
Sorting and greedy selection
For Codeforces 2165C — Binary Wine, the implementation first exposes a useful order, then scans that order while making locally justified choices.
- Choose the key that reveals the greedy or grouping structure.
- Sort the relevant records by that key.
- Scan in order, maintaining the invariant that makes each local choice safe.
Code notes
- 55 lines of Go from the credited upstream file 2165C.go.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected.
Complexity
Sorting is typically the dominant term unless the subsequent scan uses a more expensive nested operation.
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 "math/bits"8 "slices"9 "sort"10)11 1213func cf2165C(in io.Reader, _w io.Writer) {14 out := bufio.NewWriter(_w)15 defer out.Flush()16 var T, n, q, c int17 for Fscan(in, &T); T > 0; T-- {18 Fscan(in, &n, &q)19 a := make([]int, n)20 for i := range a {21 Fscan(in, &a[i])22 }23 slices.Sort(a)24 a = a[max(n-30, 0):]25 26 for range q {27 Fscan(in, &c)28 b := slices.Clone(a)29 ans := 030 for len(b) > 0 && b[len(b)-1] < c {31 v := b[len(b)-1]32 b = b[:len(b)-1]33 m := bits.Len(uint(c))34 hb := 1 << (m - 1)35 if bits.Len(uint(v)) == m {36 c ^= hb37 v ^= hb38 j := sort.SearchInts(b, v)39 b = slices.Insert(b, j, v)40 } else {41 if len(b) == 0 {42 ans += c - v43 break44 }45 ans += hb - v46 c ^= hb47 }48 }49 Fprintln(out, ans)50 }51 }52}53 5455