- Identify the ordered answer range or sorted search domain.
- Write a predicate whose truth changes only once.
- Move the appropriate boundary after each midpoint check and return the final feasible position.
Code notes
- 60 lines of Python from the credited upstream file abc184_f.py.
- The implementation visibly relies on sequence storage, ordered lookup.
- 1 loop block detected.
Complexity
Multiply the logarithmic number of midpoint checks by the cost of one predicate evaluation.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
12 3 4def generate_patterns(elements, bound):5 results = [0]6 size = len(elements)7 8 for bit in range(1, 1 << size):9 summed = 010 11 for i in range(size):12 if bit & (1 << i):13 summed += elements[i]14 15 if summed <= bound:16 results.append(summed)17 18 return results19 20 21def main():22 import sys23 24 input = sys.stdin.readline25 26 n, t = map(int, input().split())27 a = list(map(int, input().split()))28 29 mid = n 230 first = a[:mid]31 second = a[mid:]32 ans = 033 34 first_patterns = generate_patterns(first, t)35 second_patterns = sorted([0] + generate_patterns(second, t))36 37 left_index = 038 right_index = len(second_patterns)39 40 for first_pattern in first_patterns:41 remain = t - first_pattern42 left = left_index43 right = right_index44 45 while (right - left) > 1:46 mid = (left + right) 247 48 if second_patterns[mid] > remain:49 right = mid50 else:51 left = mid52 53 ans = max(ans, first_pattern + second_patterns[left])54 55 print(ans)56 57 58if __name__ == "__main__":59 main()60