-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathC.py
59 lines (36 loc) · 1.17 KB
/
C.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import sys
from typing import List
def read_int() -> int:
return int(sys.stdin.readline())
def read_list_int(length: int) -> List[int]:
result = list(map(int, sys.stdin.readline().split()))
assert (
len(result) == length
), f"List must of of length of {length}, but got {result}"
return result
def read_str() -> str:
return sys.stdin.readline()
def can_sell(requests: List[int], max_sugar: int, total_sugar: int) -> bool:
left_sugar = total_sugar
for request in requests:
will_give = min(request, max_sugar)
if will_give <= left_sugar:
left_sugar -= will_give
else:
return False
return True
def max_sugar(requests: List[int], total_sugar: int) -> int:
left, right = 0, total_sugar + 1
while left < right:
mid = left + (right - left) // 2
if not can_sell(requests, mid, total_sugar):
right = mid
else:
left = mid + 1
return left - 1
def main() -> None:
customers, total_sugar = read_list_int(2)
requests = read_list_int(customers)
print(max_sugar(requests, total_sugar))
if __name__ == "__main__":
main()