Approach
Sorting and greedy selection
For Codeforces 960B — Minimize the error, 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 960B.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 "sort"8)9 1011func CF960B(_r io.Reader, out io.Writer) {12 in := bufio.NewReader(_r)13 abs := func(x int) int {14 if x < 0 {15 return -x16 }17 return x18 }19 20 var n, k, v int21 Fscan(in, &n, &k, &v)22 k += v23 a := make([]int, n, n+1)24 for i := range a {25 Fscan(in, &a[i])26 }27 for i := range a {28 Fscan(in, &v)29 a[i] = abs(a[i] - v)30 }31 sort.Sort(sort.Reverse(sort.IntSlice(a)))32 a = append(a, 0)33 34 s := 035 for i := 0; a[i] > 0; {36 v := a[i]37 for ; a[i] == v; i++ {38 s += v39 }40 if s-k >= a[i]*i {41 s -= k42 h, ex := int64(s/i), s%i43 ans := int64(ex)*(h+1)*(h+1) + int64(i-ex)*h*h44 for _, v := range a[i:] {45 ans += int64(v) * int64(v)46 }47 Fprint(out, ans)48 return49 }50 }51 Fprint(out, (k-s)%2)52}53 5455