Approach
Sorting and greedy selection
For ABC321 D — Set Menu, 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
- 76 lines of Python from the credited upstream file abc321_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_left, bisect_right5from typing import List6 7 8def bisect_lt(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_left(sorted_array, value) - 113 14 return index, sorted_array[index]15 16 return None, None17 18 19def bisect_le(sorted_array: List[int], value: int):20 """Find the largest element <= x and its index, or None if it doesn't exist."""21 22 if sorted_array[0] <= value:23 index: int = bisect_right(sorted_array, value) - 124 25 return index, sorted_array[index]26 27 return None, None28 29 30def bisect_gt(sorted_array: List[int], value: int):31 """Find the smallest element > x and its index, or None if it doesn't exist."""32 33 if sorted_array[-1] > value:34 index: int = bisect_right(sorted_array, value)35 36 return index, sorted_array[index]37 38 return None, None39 40 41def bisect_ge(sorted_array: List[int], value: int):42 """Find the smallest element >= x and its index, or None if it doesn't exist."""43 44 if sorted_array[-1] >= value:45 index: int = bisect_left(sorted_array, value)46 47 return index, sorted_array[index]48 49 return None, None50 51 52def main():53 import sys54 from itertools import accumulate55 56 input = sys.stdin.readline57 58 n, m, p = map(int, input().split())59 a = sorted(list(map(int, input().split())))60 b = sorted(list(map(int, input().split())))61 acc_b = list(accumulate([0] + b + [0]))62 inf = 10**1863 c = [-inf] + b + [inf]64 ans = 065 66 for ai in a:67 i, value = bisect_le(c, p - ai) 68 ans += acc_b[i] + ai * i69 ans += p * max(m - i, 0)70 71 print(ans)72 73 74if __name__ == "__main__":75 main()76