Approach
Sorting and greedy selection
For Codeforces 729C — Road to Cinema, 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 729C.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 . "fmt"5 "io"6 "slices"7 "sort"8)9 1011func cf729C(in io.Reader, out io.Writer) {12 var n, k, s, t int13 Fscan(in, &n, &k, &s, &t)14 a := make([]struct{ price, cap int }, n)15 for i := range a {16 Fscan(in, &a[i].price, &a[i].cap)17 }18 sort.Slice(a, func(i, j int) bool { return a[i].cap < a[j].cap })19 p := make([]int, k, k+2)20 for i := range p {21 Fscan(in, &p[i])22 }23 p = append(p, 0, s)24 slices.Sort(p)25 gap := make([]int, k+1)26 for i := range gap {27 gap[i] = p[i+1] - p[i]28 }29 slices.Sort(gap)30 mx := gap[k]31 32 time := s * 233 i := 034 for j, c := range a {35 if c.cap < mx {36 continue37 }38 for ; i <= k && gap[i]*2 <= c.cap; i++ {39 time -= gap[i]40 s -= gap[i]41 }42 if time-c.cap*(k+1-i)+s <= t {43 ans := int(2e9)44 for _, c := range a[j:] {45 ans = min(ans, c.price)46 }47 Fprint(out, ans)48 return49 }50 }51 Fprint(out, -1)52}53 5455