Approach
Sorting and greedy selection
For ABC334 D — Reindeer and Sleigh, 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
- 44 lines of Python from the credited upstream file abc334_d.py.
- The implementation visibly relies on sequence storage, ordered lookup.
- 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.
12 3 4from bisect import bisect_right5from typing import List6 7 8def bisect_le(sorted_array: List[int], value: int):9 """Find the largest element <= x and its index, or None if it doesn't exist."""10 11 if sorted_array[0] <= value:12 index: int = bisect_right(sorted_array, value) - 113 14 return index, sorted_array[index]15 16 return None, None17 18 19def main():20 import sys21 from itertools import accumulate22 23 input = sys.stdin.readline24 25 n, q = map(int, input().split())26 inf = 10**1827 r = sorted(list(map(int, input().split()))) + [inf]28 summed_r = list(accumulate(r, initial=0))29 30 31 for _ in range(q):32 qi = int(input())33 34 i, value = bisect_le(summed_r, qi) 35 36 if i is None:37 i = 038 39 print(i)40 41 42if __name__ == "__main__":43 main()44