-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjesse-and-cookies.py
More file actions
31 lines (26 loc) · 851 Bytes
/
jesse-and-cookies.py
File metadata and controls
31 lines (26 loc) · 851 Bytes
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
#!/bin/python3
import heapq
def cookies(k, A):
# Write your code here
count = 0
# Create a min-heap
heapq.heapify(A)
l = len(A)
# Execute only if there are at least two elements
while l > 1:
# If top is at least the required minimum, no further updates are necessary
if A[0] >= k:
break
# Get the minimum two elements (and double the second one)
top1 = heapq.heappop(A)
top2 = 2 * heapq.heappop(A)
# Push the sum back to the heap
heapq.heappush(A, top1 + top2)
count += 1
l -= 1
# If the heap top element is at least the required minimum, then return top, else fail
return count if A[0] >= k else -1
n, k = map(int, input().strip().split(' '))
A = list(map(int, input().rstrip().split()))
result = cookies(k, A)
print(result)