Approach
Stack-based processing
For Codeforces 767E — Change-free, the implementation keeps unresolved items in last-in, first-out order, often to match boundaries, parse structure, or maintain monotonic candidates.
- Define what every stack entry represents.
- Pop entries once the current item resolves or invalidates them.
- Push the current item with only the information later steps need.
Code notes
- 55 lines of Go from the credited upstream file 767E.go.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected.
Complexity
If each item is pushed and popped at most once, the stack work is linear.
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 1011func cf767E(in io.Reader, _w io.Writer) {12 out := bufio.NewWriter(_w)13 defer out.Flush()14 var n, coin, w, sum int15 Fscan(in, &n, &coin)16 c := make([]int, n)17 for i := range c {18 Fscan(in, &c[i])19 }20 21 ans := make([]pair67, n)22 h := hp67{}23 for i, c := range c {24 Fscan(in, &w)25 r := c % 10026 ans[i] = pair67{c / 100, r}27 if r == 0 {28 continue29 }30 coin -= r31 heap.Push(&h, pair67{w * (100 - r), i})32 if coin < 0 {33 p := heap.Pop(&h).(pair67)34 sum += p.v35 ans[p.i].v++36 ans[p.i].i = 037 coin += 10038 }39 }40 41 Fprintln(out, sum)42 for _, p := range ans {43 Fprintln(out, p.v, p.i)44 }45}46 4748type pair67 struct{ v, i int }49type hp67 []pair6750func (h hp67) Len() int { return len(h) }51func (h hp67) Less(i, j int) bool { return h[i].v < h[j].v }52func (h hp67) Swap(i, j int) { h[i], h[j] = h[j], h[i] }53func (h *hp67) Push(v any) { *h = append(*h, v.(pair67)) }54func (h *hp67) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v }55