- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 43 lines of Go from the credited upstream file 1428E.go.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected.
Complexity
Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.
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 "container/heap"6 . "fmt"7 "io"8)9 1011type pair28 struct{ x, c int }12type hp28 []pair2813 14func f28(x, c int) int64 { l := int64(x / c); return int64(c-x%c)*l*l + int64(x%c)*(l+1)*(l+1) }15func dec28(p pair28) int64 { return f28(p.x, p.c) - f28(p.x, p.c+1) }16func (h hp28) Len() int { return len(h) }17func (h hp28) Less(i, j int) bool { return dec28(h[i]) > dec28(h[j]) }18func (h hp28) Swap(i, j int) { h[i], h[j] = h[j], h[i] }19func (hp28) Push(interface{}) {}20func (hp28) Pop() (_ interface{}) { return }21 22func CF1428E(_r io.Reader, out io.Writer) {23 in := bufio.NewReader(_r)24 var n, m, v int25 var ans int6426 Fscan(in, &n, &m)27 h := make(hp28, n)28 for i := range h {29 Fscan(in, &v)30 ans += int64(v) * int64(v)31 h[i] = pair28{v, 1}32 }33 heap.Init(&h)34 for i := n; i < m; i++ {35 ans -= dec28(h[0])36 h[0].c++37 heap.Fix(&h, 0)38 }39 Fprint(out, ans)40}41 4243