Jesse loves cookies and wants the sweetness of some cookies to be greater than value $\boldsymbol{\mbox{k}}$. To do this, two cookies with the least sweetness are repeatedly mixed. This creates a special combined cookie with:

sweetness $=(1\times$ Least sweet cookie $\mbox{t}$  $2\times$ 2nd least sweet cookie).

This occurs until all the cookies have a sweetness  $\geq k$.  

Given the sweetness of a number of cookies, determine the minimum number of operations required. If it is not possible, return $-1$.

Example 

$k=9$ 

$A=[2,7,3,6,4,6]$   

The smallest values are $2,3$. 

Remove them then return $2+2\times3=8$ to the array.  Now $A=[8,7,6,4,6]$. 

Remove $4,6$ and return $4+6\times2=16$ to the array.  Now $A=[16,8,7,6]$. 

Remove $6,7$, return $6+2\times7=20$ and $A=[20,16,8,7]$. 

Finally, remove $8,7$ and return $7+2\times8=23$ to $\mbox{A}$.  Now $A=[23,20,16]$. 

All values are $\geq k=9$ so the process stops after $\begin{array}{c}4\end{array}$ iterations.  Return $\begin{array}{c}4\end{array}$.   

Function Description 

Complete the cookies function in the editor below.  

cookies has the following parameters:  

int k: the threshold value  
int A[n]: an array of sweetness values  

Returns  

int: the number of iterations required or $-1$  

Input Format

The first line has two space-separated integers, $n$ and $\boldsymbol{\mbox{k}}$, the size of $\boldsymbol{A\left[\right]}$ and  the minimum required sweetness respectively.   

The next line contains $n$ space-separated integers, $A[i]$.  

Constraints

$1\leq n\leq10^{6}$ 

$0\leq k\leq10^9$ 

$\textbf{0}\leq A[i]\leq10^6$  

Sample Input
STDIN               Function
-----               --------
6 7                 A[] size n = 6, k = 7
1 2 3 9 10 12       A = [1, 2, 3, 9, 10, 12]  

Sample Output
2

Explanation

Combine the first two cookies to create a cookie with sweetness $=1\times1+2\times2$ = $5$ 

After this operation, the cookies are $3,5,9,10,12$. 

Then, combine the cookies with sweetness $3$ and sweetness $5$, to create a cookie with resulting sweetness $=1\times3+2\times5$ = $13$ 

Now, the cookies are $9,10,12,13$. 

All the cookies have a sweetness $\geq7$. 
 
Thus, $2$ operations are required to increase the sweetness.

Source: hackerrank | Difficulty: EASY
Tags: Data structures


Solutions:
import sys
from collections import deque

def combine(a, b):
	return a + b * 2
(n, k) = [int(x) for x in input().split()]
queue = deque(sorted([int(x) for x in input().split()]))
combines = 0
n_queue = deque()
top_two = []
while queue:
	if queue and n_queue and (len(queue) == 1):
		while n_queue:
			queue.append(n_queue.popleft())
	if queue and n_queue and (n_queue[0] <= queue[0]):
		queue.appendleft(n_queue.popleft())
	if queue and len(top_two) == 0 and (k <= queue[0]):
		break
	cookie = queue.popleft()
	top_two.append(cookie)
	if len(top_two) == 2:
		new_cookie = combine(top_two[0], top_two[1])
		top_two = []
		n_queue.append(new_cookie)
		combines += 1
while n_queue:
	queue.append(n_queue.popleft())
if any((x < k for x in queue)):
	print('-1')
else:
	print(combines)


import heapq
[N, K] = list(map(int, input().split(' ')))
cookies = list(map(int, input().split(' ')))
operations = 0
if len(cookies) == 0:
	print(operations)
else:
	heapq.heapify(cookies)
	while cookies[0] < K:
		if len(cookies) <= 1:
			break
		operations += 1
		c1 = heapq.heappop(cookies)
		c2 = heapq.heappop(cookies)
		nc = c1 + 2 * c2
		heapq.heappush(cookies, nc)
if cookies[0] < K:
	print(-1)
else:
	print(operations)


from heapq import *
(n, m) = list(map(int, input().split()))
string = list(map(int, input().split()))
heapify(string)
i = 0
while string[0] < m:
	if len(string) > 1:
		a = heappop(string)
		b = heappop(string)
		new_cookie = a * 1 + b * 2
		heappush(string, new_cookie)
		i += 1
	else:
		i = -1
		break
print(i)


import heapq

def sweeten(n, k, arr):
	arr = sorted(arr)
	count = 0
	while arr[0] < k:
		if len(arr) <= 1 and arr[0] < k:
			return -1
		heapq.heappush(arr, heapq.heappop(arr) + heapq.heappop(arr) * 2)
		count += 1
	return count
(n, k) = [int(x) for x in input().split()]
arr = [int(x) for x in input().split()]
print(sweeten(n, k, arr))


import heapq
(N, K) = map(int, input().split())
A = list(map(int, input().split()))
heapq.heapify(A)
res = 0
while A[0] < K:
	if len(A) == 1:
		res = -1
		break
	c1 = heapq.heappop(A)
	c2 = heapq.heappop(A)
	heapq.heappush(A, c1 + 2 * c2)
	res += 1
print(res)


import heapq
(n, s) = [int(i) for i in input().split()]
cookies = [int(i) for i in input().split()]
heapq.heapify(cookies)
ops = 0
while cookies[0] < s:
	try:
		ops += 1
		cookie1 = heapq.heappop(cookies)
		cookie2 = heapq.heappop(cookies)
		newcookie = 1 * cookie1 + 2 * cookie2
		heapq.heappush(cookies, newcookie)
	except IndexError:
		ops = -1
		break
print(ops)


from heapq import *
(number, target) = [int(x) for x in input().split()]
cookies = [int(x) for x in input().split()]
operations = 0
heapify(cookies)
while cookies[0] < target:
	if len(cookies) == 1:
		print('-1')
		break
	cookie1 = heappop(cookies)
	cookie2 = heappop(cookies)
	newcookie = cookie1 + cookie2 * 2
	operations += 1
	heappush(cookies, newcookie)
else:
	print(operations)


import heapq
(num_cookies, min_sweet) = [int(val) for val in input().strip().split()]
cookies = [int(val) for val in input().strip().split()]
cookie_heap = []
for cookie in cookies:
	heapq.heappush(cookie_heap, cookie)
operation_cnt = 0
while cookie_heap[0] < min_sweet:
	if len(cookie_heap) == 1:
		operation_cnt = -1
		break
	c1 = heapq.heappop(cookie_heap)
	c2 = heapq.heappop(cookie_heap)
	heapq.heappush(cookie_heap, c1 + 2 * c2)
	operation_cnt += 1
print(operation_cnt)


import heapq
(N, K) = map(int, input().split())
cookies = list(map(int, input().split()))
heapq.heapify(cookies)

def step_count(cookies, k):
	current_count = 0
	while cookies:
		least_sweet = heapq.heappop(cookies)
		if least_sweet >= k:
			return current_count
		if not cookies:
			return -1
		second_least_sweet = heapq.heappop(cookies)
		new_cookie = least_sweet + second_least_sweet * 2
		heapq.heappush(cookies, new_cookie)
		current_count += 1
	return -1
print(step_count(cookies, K))


from heapq import heappush, heappop
params = input()
params2 = params.split(' ')
n = int(params2[0])
k = int(params2[1])
heap = []
vals = input().split(' ')
for v in vals:
	heappush(heap, int(v))
moves = 0
while True:
	x = heappop(heap)
	if x >= k:
		break
	if len(heap) == 0:
		moves = -1
		break
	y = heappop(heap)
	heappush(heap, x + 2 * y)
	moves += 1
print(moves)


import sys
import heapq

def fast_sweetness(input):
	first_line = input.readline().strip().split(' ')
	(N, K) = (int(v) for v in first_line)
	second_line = input.readline().strip().split(' ')
	q = []
	for i in second_line:
		heapq.heappush(q, int(i))
	size = N
	while size:
		smallest = heapq.heappop(q)
		if smallest >= K:
			return N - size
		elif size == 1 and smallest < K:
			return -1
		size -= 1
		next_smallest = heapq.heappop(q)
		new = smallest + 2 * next_smallest
		heapq.heappush(q, new)
		continue
print(fast_sweetness(sys.stdin))


import heapq
(n, k) = map(int, input().split(' '))
cookies = []
for el in map(int, input().split(' ')):
	heapq.heappush(cookies, el)
iterations = 0
while len(cookies) != 1:
	cookie1 = heapq.heappop(cookies)
	if cookie1 >= k:
		break
	cookie2 = heapq.heappop(cookies)
	heapq.heappush(cookies, cookie1 + 2 * cookie2)
	iterations += 1
if len(cookies) == 1:
	cookie = heapq.heappop(cookies)
	if cookie >= k:
		print(iterations)
	else:
		print(-1)
else:
	print(iterations)


import heapq
from bisect import insort
(_, K) = [int(i) for i in input().split()]
less_sweets = []
has_more_sweets = False
sweets = (int(i) for i in input().split())
for s in sweets:
	if s < K:
		heapq.heappush(less_sweets, s)
	else:
		has_more_sweets = True
n = 0
while len(less_sweets) >= 2:
	a = heapq.heappop(less_sweets)
	b = heapq.heappop(less_sweets)
	x = a + 2 * b
	if x < K:
		heapq.heappush(less_sweets, x)
	else:
		has_more_sweets = True
	n += 1
if less_sweets and less_sweets[0] < K:
	if has_more_sweets:
		print(n + 1)
	else:
		print(-1)
else:
	print(n)


import heapq
push = heapq.heappush
pop = heapq.heappop
(N, K) = [int(x) for x in input().split()]
ar = [int(x) for x in input().split()]
heapq.heapify(ar)
count = 0
while ar[0] < K and len(ar) >= 2:
	count += 1
	push(ar, pop(ar) + 2 * pop(ar))
print(count if ar[0] >= K else -1)


import sys
from collections import deque

def combine(a, b):
	return a + b * 2
(n, k) = [int(x) for x in input().split()]
queue = deque(sorted([int(x) for x in input().split()]))
combines = 0
n_queue = deque()
top_two = []
while queue:
	if queue and n_queue and (len(queue) == 1):
		while n_queue:
			queue.append(n_queue.popleft())
	if queue and n_queue and (n_queue[0] <= queue[0]):
		queue.appendleft(n_queue.popleft())
	if queue and len(top_two) == 0 and (k <= queue[0]):
		break
	cookie = queue.popleft()
	top_two.append(cookie)
	if len(top_two) == 2:
		new_cookie = combine(top_two[0], top_two[1])
		top_two = []
		n_queue.append(new_cookie)
		combines += 1
while n_queue:
	queue.append(n_queue.popleft())
if any((x < k for x in queue)):
	print('-1')
else:
	print(combines)


from heapq import *
[n, k] = [int(x) for x in input().split()]
arr = sorted([int(x) for x in input().split() if int(x) < k])
niter = 0
if len(arr) == 0:
	print(0)
elif len(arr) == 1:
	print(1)
else:
	while len(arr) > 1 and arr[0] < k:
		niter += 1
		min1 = heappop(arr)
		min2 = heappop(arr)
		heappush(arr, min1 + min2 * 2)
	if arr[0] < k:
		print(-1)
	else:
		print(niter)


import heapq
(N, K) = list(map(int, input().strip().split()))
A = list(map(int, input().split()))
heapq.heapify(A)
count = 0
while len(A) >= 1:
	smallest = heapq.heappop(A)
	if smallest < K:
		if len(A) > 0:
			secondSmallest = heapq.heappop(A)
			heapq.heappush(A, smallest + 2 * secondSmallest)
			count += 1
		else:
			print(-1)
			break
	else:
		print(count)
		break
else:
	print(-1)


import heapq
(N, K) = input().split(' ')
N = int(N)
K = int(K)
arr = [int(val) for val in input().split(' ')]
heapq.heapify(arr)
operations = 0
while len(arr) > 0:
	temp = heapq.heappop(arr)
	if temp < K and len(arr) <= 0:
		operations = -1
		break
	if temp < K:
		temp2 = heapq.heappop(arr)
		heapq.heappush(arr, temp + 2 * temp2)
		operations += 1
print(operations)


import heapq
(N, K) = [int(x) for x in input().split()]
pq = [int(y) for y in input().split()]
heapq.heapify(pq)
count = 0
while N > 1:
	m1 = heapq.heappop(pq)
	if m1 >= K:
		heapq.heappush(pq, m1)
		break
	m2 = heapq.heappop(pq)
	m = m1 + m2 * 2
	count += 1
	heapq.heappush(pq, m)
	N -= 1
if N == 1 and heapq.heappop(pq) < K:
	print(-1)
else:
	print(count)


import heapq
(N, K) = [int(a) for a in input().split(' ')]
A = [int(a) for a in input().split(' ')]
heapq.heapify(A)
count = 0
while A[0] < K:
	if len(A) == 1:
		print(-1)
		break
	count += 1
	a = heapq.heappop(A)
	b = heapq.heappop(A)
	c = a + 2 * b
	heapq.heappush(A, c)
else:
	print(count)


import heapq
(NUM_COOKIES, MIN) = [int(i) for i in input().split()]
sweetness = [int(i) for i in input().split()]
heapq.heapify(sweetness)

def solution(sweetness):
	cnt = 0
	while sweetness[0] < MIN:
		if len(sweetness) == 1:
			return -1
		cur_sweet = heapq.heappop(sweetness) + 2 * heapq.heappop(sweetness)
		heapq.heappush(sweetness, cur_sweet)
		cnt += 1
	return cnt
print(solution(sweetness))


import sys
sys.setrecursionlimit(100000)
import heapq
[N, K] = input().split(' ')
list = input().split(' ')
N = int(N)
K = int(K)
list = [int(x) for x in list]
end = False
nbop = 0
heapq.heapify(list)
heap = list
while not end and heap != []:
	n1 = heapq.heappop(heap)
	try:
		n2 = heapq.heappop(heap)
		if n1 < K:
			heapq.heappush(heap, n1 + 2 * n2)
			nbop += 1
		else:
			end = True
			print(nbop)
	except:
		if n1 < K:
			end = True
			print(-1)
		else:
			end = True
			print(nbop)


import heapq

def make_new_cookie(cookies):
	cookieA = heapq.heappop(cookies)
	cookieB = heapq.heappop(cookies)
	new_cookie = cookieA + 2 * cookieB
	heapq.heappush(cookies, new_cookie)

def mix_cookies(cookies, min_sweetness):
	mix_count = 0
	while cookies[0] < min_sweetness:
		if len(cookies) > 1:
			make_new_cookie(cookies)
			mix_count += 1
		else:
			return -1
	return mix_count
line_input = input().strip().split(' ')
num_cookies = int(line_input[0])
min_sweetness = int(line_input[1])
cookies = [int(i) for i in input().strip().split(' ')]
heapq.heapify(cookies)
print(mix_cookies(cookies, min_sweetness))


import heapq

def get_cookies_sweetness(sweet_ar, req_sweetness):
	heapq.heapify(sweet_ar)
	if sweet_ar[0] >= req_sweetness:
		return 0
	steps = 1
	while True:
		try:
			least_sweet = heapq.heappop(sweet_ar)
			second_least_sweet = heapq.heappop(sweet_ar)
		except IndexError:
			return -1
		new_sweetness = least_sweet + 2 * second_least_sweet
		heapq.heappush(sweet_ar, new_sweetness)
		if sweet_ar[0] >= req_sweetness:
			return steps
		steps += 1
(no_of_cookies, required_sweetness) = (int(s) for s in input().strip().split(' '))
sweetness_ar = [int(s) for s in input().strip().split(' ')]
print(get_cookies_sweetness(sweetness_ar, required_sweetness))


import heapq

def combine_cookies(c1, c2):
	return c1 + 2 * c2

def merge_cookies(sorted_cookies):
	min_cookie_1 = heapq.heappop(sorted_cookies)
	min_cookie_2 = heapq.heappop(sorted_cookies)
	heapq.heappush(sorted_cookies, combine_cookies(min_cookie_1, min_cookie_2))

def solve_case(k, a):
	no_of_operations = 0
	sorted_cookies = a
	heapq.heapify(sorted_cookies)
	while len(sorted_cookies) > 1:
		min_cookie = sorted_cookies[0]
		if min_cookie < k:
			merge_cookies(sorted_cookies)
			no_of_operations += 1
		else:
			break
	if sorted_cookies[0] < k:
		return -1
	return no_of_operations
(n, k) = [int(_) for _ in input().split(' ')]
a = [int(_) for _ in input().split(' ')]
print(solve_case(k, a))


import heapq as hq
(N, K) = map(int, input().strip().split())
Sweetnesses = list(map(int, input().strip().split()))
hq.heapify(Sweetnesses)
n = len(Sweetnesses)
k = 0
newsweet = lambda x, y: x + 2 * y
while Sweetnesses[0] < K and n > 1:
	s1 = hq.heappop(Sweetnesses)
	s2 = hq.heappop(Sweetnesses)
	hq.heappush(Sweetnesses, newsweet(s1, s2))
	n -= 1
	k += 1
if Sweetnesses[0] < K:
	print(-1)
else:
	print(k)


import heapq

def sweetnes(ar, K):
	if len(ar) == 0:
		return -1
	if len(ar) == 1:
		return 0 if ar[0] >= K else -1
	heapq.heapify(ar)
	steps = 0
	while True:
		a1 = heapq.heappop(ar)
		if a1 >= K:
			break
		if len(ar) == 0:
			return -1
		a2 = heapq.heappop(ar)
		steps += 1
		heapq.heappush(ar, a1 + 2 * a2)
	return steps
(n, k) = input().strip().split()
(n, k) = [int(n), int(k)]
ar = [int(i) for i in input().strip().split()]
print(sweetnes(ar, k))


import heapq
import sys
[N, K] = map(int, sys.stdin.readline().split(' '))
A = list(map(int, sys.stdin.readline().split(' ')))
heapq.heapify(A)
combinations = 0
while len(A) > 0:
	x = heapq.heappop(A)
	if x >= K:
		break
	if len(A) < 1:
		combinations = -1
		break
	y = heapq.heappop(A)
	xy = 1 * x + 2 * y
	heapq.heappush(A, xy)
	combinations += 1
print(combinations)


import heapq
heap = []
(n, k) = [int(x) for x in input().strip().split()]
numbers = [int(x) for x in input().strip().split()]
for x in numbers:
	heapq.heappush(heap, x)
operations = 0
found = True
while heap[0] < k:
	if len(heap) <= 1:
		found = False
		print(-1)
		break
	operations += 1
	(min1, min2) = (heapq.heappop(heap), heapq.heappop(heap))
	cookie = min1 + 2 * min2
	heapq.heappush(heap, cookie)
if found:
	print(operations)


from math import floor
import bisect

def read_ints():
	return map(int, input().split(' '))

def go(nums, k):
	q = []
	i = 0
	j = 0
	sm = 0

	def pick_next(i, j):
		e = None
		if i < len(nums):
			if j < len(q) and q[j] < nums[i]:
				j += 1
				e = q[j - 1]
			else:
				i += 1
				e = nums[i - 1]
		elif j < len(q):
			j += 1
			e = q[j - 1]
		return (e, i, j)
	while i < len(nums) or j < len(q):
		(a, i, j) = pick_next(i, j)
		(b, i, j) = pick_next(i, j)
		if (a == None or a >= k) and (b == None or b >= k):
			break
		elif a == None or b == None:
			return -1
		s = a + b * 2
		q.append(s)
		sm += 1
	return sm
(n, k) = read_ints()
nums = sorted(read_ints())
print(go(nums, k))


import heapq

def make(cookies, K):
	if len(cookies) == 0:
		return -1
	count = 0
	while len(cookies) >= 2:
		a = heapq.heappop(cookies)
		if a >= K:
			return count
		b = heapq.heappop(cookies)
		new_cookie = a + 2 * b
		heapq.heappush(cookies, new_cookie)
		count += 1
	if heapq.heappop(cookies) >= K:
		return count
	else:
		return -1
(N, K) = [int(item) for item in input().strip().split()]
cookies = [int(item) for item in input().strip().split()]
heapq.heapify(cookies)
print(make(cookies, K))


import heapq
nums = [int(i) for i in input().split()]
k = nums[1]
cookies = [int(i) for i in input().split()]
cookie_heap = []
for cookie in cookies:
	heapq.heappush(cookie_heap, cookie)
step_count = 0
cookie_count = len(cookies)
while cookie_heap[0] < k and cookie_count > 1:
	step_count += 1
	new_cookie = heapq.heappop(cookie_heap) + 2 * heapq.heappop(cookie_heap)
	heapq.heappush(cookie_heap, new_cookie)
	cookie_count -= 1
print(-1) if cookie_count == 1 and cookie_heap[0] < k else print(step_count)


import heapq
(n, k) = [int(x) for x in input().split()]
heap = [int(x) for x in input().split()]
heapq.heapify(heap)
operations = 0
try:
	while heap[0] < k:
		smallest = heapq.heappop(heap)
		second = heapq.heappop(heap)
		new = smallest + 2 * second
		heapq.heappush(heap, new)
		operations += 1
except:
	operations = -1
print(operations)


import heapq
(n, k) = map(int, input().split())
cookies = [int(cookie) for cookie in input().split()]
heapq.heapify(cookies)
operations = 0
while cookies[0] < k:
	if len(cookies) < 2:
		operations = -1
		break
	operations += 1
	least = heapq.heappop(cookies)
	second = heapq.heappop(cookies)
	sweetness = 1 * least + 2 * second
	heapq.heappush(cookies, sweetness)
print(operations)


import heapq
(n, k) = [int(x) for x in input().split(' ')]
cookies = [int(x) for x in input().split(' ')]
heapq.heapify(cookies)
ops = 0
while True:
	min_cookie_one = heapq.heappop(cookies)
	if min_cookie_one >= k:
		print(ops)
		break
	if len(cookies) == 0:
		print(-1)
		break
	min_cookie_two = heapq.heappop(cookies)
	heapq.heappush(cookies, min_cookie_one + 2 * min_cookie_two)
	ops = ops + 1


(n, k) = [int(x) for x in input().strip().split()]
cookies = sorted([int(x) for x in input().strip().split()])
q = list()
iterations = 0
not_possible = False
cookie_i = 0
q_i = 0
while (cookies[cookie_i] < k if cookie_i < len(cookies) else False) or (q[q_i] < k if q_i < len(q) else False):
	if cookie_i < len(cookies) and q_i < len(q):
		if cookies[cookie_i] < q[q_i]:
			if len(cookies) - cookie_i > 1 and cookies[cookie_i + 1] < q[q_i]:
				cookie1 = cookies[cookie_i]
				cookie_i += 1
				cookie2 = cookies[cookie_i]
				cookie_i += 1
			else:
				cookie1 = cookies[cookie_i]
				cookie_i += 1
				cookie2 = q[q_i]
				q_i += 1
		elif len(q) - q_i > 1 and q[q_i + 1] < cookies[cookie_i]:
			cookie1 = q[q_i]
			q_i += 1
			cookie2 = q[q_i]
			q_i += 1
		else:
			cookie1 = q[q_i]
			q_i += 1
			cookie2 = cookies[cookie_i]
			cookie_i += 1
	elif cookie_i < len(cookies):
		if len(cookies) - cookie_i > 1:
			cookie1 = cookies[cookie_i]
			cookie_i += 1
			cookie2 = cookies[cookie_i]
			cookie_i += 1
		else:
			not_possible = True
			break
	elif q_i < len(q):
		if len(q) - q_i > 1:
			cookie1 = q[q_i]
			q_i += 1
			cookie2 = q[q_i]
			q_i += 1
		else:
			not_possible = True
			break
	else:
		not_possible = True
		break
	cookie = cookie1 + 2 * cookie2
	q.append(cookie)
	iterations += 1
if not_possible:
	print(-1)
else:
	print(iterations)


import heapq
(n, k) = [int(x) for x in input().strip().split(' ')]
arr = [int(x) for x in input().strip().split(' ')]
heapq.heapify(arr)

def sweet(arr, k):
	num_steps = 0
	while len(arr) > 1:
		if arr[0] >= k:
			return num_steps
		first = heapq.heappop(arr)
		second = heapq.heappop(arr)
		new = first + second * 2
		heapq.heappush(arr, new)
		num_steps += 1
	if arr[0] >= k:
		return num_steps
	return -1
print(sweet(arr, k))


import heapq
(N, K) = [int(x) for x in input().split()]
cookies = [int(x) for x in input().split()]
heap = []
for cookie in cookies:
	heapq.heappush(heap, cookie)
nops = 0
try:
	while heap[0] < K:
		a = heapq.heappop(heap)
		b = heapq.heappop(heap)
		c = a + 2 * b
		heapq.heappush(heap, c)
		nops += 1
except:
	print(-1)
else:
	print(nops)


import heapq

def num_operations(cookies, k):
	num_ops = 0
	heapq.heapify(cookies)
	while cookies[0] < k:
		if len(cookies) == 2 and cookies[0] + 2 * cookies[1] < k:
			return -1
		c1 = heapq.heappop(cookies)
		c2 = heapq.heappop(cookies)
		heapq.heappush(cookies, c1 + 2 * c2)
		num_ops += 1
	return num_ops
(_, k) = [int(el) for el in input().split()]
c = [int(el) for el in input().split()]
print(num_operations(c, k))


from heapq import heapify, heappush, heappop

def get_number_mixes(cookies, minimum_value):
	cookies = list(cookies)
	heapify(cookies)
	count = 0
	while cookies[0] < minimum_value:
		if len(cookies) < 2:
			return -1
		heappush(cookies, heappop(cookies) + 2 * heappop(cookies))
		count += 1
	return count
(N, K) = map(int, input().split())
A = map(int, input().split())
print(get_number_mixes(A, K))


import heapq
(_, minSweetness) = map(int, input().strip().split(' '))
cookies = list(map(int, input().strip().split(' ')))
heapq.heapify(cookies)
operations = 0
while len(cookies) >= 2 and cookies[0] < minSweetness:
	first = heapq.heappop(cookies)
	second = heapq.heappop(cookies)
	heapq.heappush(cookies, first + 2 * second)
	operations += 1
if cookies[0] < minSweetness:
	print(-1)
else:
	print(operations)


from heapq import *
(n, k) = map(int, input().split())
h = list(map(int, input().split()))
h.sort()
ans = 0
while h[0] < k:
	if n < 2:
		ans = -1
		break
	n -= 1
	x = heappop(h)
	y = heappop(h)
	heappush(h, x + 2 * y)
	ans += 1
print(ans)


import heapq
(n, k) = [int(x) for x in input().strip().split(' ')]
cookies = [int(x) for x in input().strip().split(' ')]
heapq.heapify(cookies)
steps = 0
a = heapq.heappop(cookies)
while a < k:
	if len(cookies) == 0:
		print(-1)
		break
	b = heapq.heappop(cookies)
	new_cookie = 1 * a + 2 * b
	heapq.heappush(cookies, new_cookie)
	steps += 1
	a = heapq.heappop(cookies)
else:
	print(steps)


from heapq import heappush, heappop

def const_heap(data):
	heap = []
	for entry in data:
		heappush(heap, int(entry))
	return heap

def count_operations(data, k):
	data = const_heap(data)
	count = 0
	while True:
		try:
			low1 = heappop(data)
			if low1 >= k:
				return count
			low2 = heappop(data)
		except IndexError:
			return -1
		new_sweet = low1 + 2 * low2
		count += 1
		heappush(data, new_sweet)
(num_cookies, k) = input().split(' ')
cookies = input().split(' ')
count = count_operations(cookies, int(k))
print(count)


import heapq
(n, k) = list(map(int, input().split()))
s = list(map(int, input().split()))
m = 0
heapq.heapify(s)
t = 0
while len(s) > 1 and s[0] < k:
	m = heapq.heappop(s) + 2 * heapq.heappop(s)
	heapq.heappush(s, m)
	t += 1
print(t) if len(s) > 1 or m >= k else print(-1)


from heapq import *
lst = input().split(' ')
(n, k) = (int(lst[0]), int(lst[1]))
yin = [int(x) for x in input().split(' ')]
y = []
heapn = 0
for i in range(n):
	if yin[i] < k:
		heappush(y, yin[i])
		heapn += 1
op = 0
sweet = not y
while not sweet:
	if y[0] >= k:
		sweet = True
		break
	else:
		if len(y) == 1:
			break
		(a, b) = (heappop(y), heappop(y))
		heappush(y, a + 2 * b)
		op += 1
if sweet:
	print(op)
elif heapn < n:
	print(op + 1)
else:
	print(-1)


import queue
import heapq

def print_hqueue(hq):
	while not hq.empty():
		print(hq.get())

def get_ways_tastiest_cookies(cookies, K):
	hq = []
	ways = 0
	for item in cookies:
		heapq.heappush(hq, int(item))
	while hq:
		min1 = heapq.heappop(hq)
		if min1 >= K:
			return ways
		if hq:
			min2 = heapq.heappop(hq)
			smash = 1 * min1 + 2 * min2
			heapq.heappush(hq, smash)
			ways = ways + 1
		else:
			return -1
	return ways
nk = input().strip().split(' ')
N = int(nk[0])
K = int(nk[1])
cookies = input().strip().split(' ')
print('' + str(get_ways_tastiest_cookies(cookies, K)))


import heapq

def get_cookie_ops(cookies, k):
	ops = 0
	while True:
		if len(cookies) == 1 and cookies[0] < k:
			return -1
		if cookies[0] >= k:
			return ops
		c1 = heapq.heappop(cookies)
		c2 = heapq.heappop(cookies)
		heapq.heappush(cookies, c1 + 2 * c2)
		ops += 1
(n, k) = map(int, input().strip().split())
cookies = [int(cookie) for cookie in input().strip().split()]
heapq.heapify(cookies)
print(str(get_cookie_ops(cookies, k)))


from heapq import heapify, heappop, heappush

def main():
	(n, k) = list(map(int, input().split()))
	heap = list(map(int, input().split()))
	heapify(heap)
	count = soln(heap, k)
	print(count)

def soln(heap, k):
	count = 0
	while heap[0] < k and len(heap) > 1:
		first_min = heappop(heap)
		second_min = heappop(heap)
		sweetness = first_min + 2 * second_min
		count += 1
		heappush(heap, sweetness)
	if heap[0] < k:
		return -1
	else:
		return count
main()


import heapq
min_heap = []
(no_cookies, sweetness) = list(map(int, str(input()).split()))
cookies = list(map(int, str(input()).split()))

def new_sweetness(a, b):
	return 1 * a + 2 * b
count = 0
[heapq.heappush(min_heap, n) for n in cookies]
if no_cookies % 2 == 0:
	go_to = no_cookies
else:
	go_to = no_cookies - 1
for i in range(go_to):
	min_a = heapq.heappop(min_heap)
	if min_a >= sweetness:
		break
	elif len(min_heap) < 1:
		break
	else:
		count += 1
		min_b = heapq.heappop(min_heap)
		heapq.heappush(min_heap, new_sweetness(min_a, min_b))
if min_a >= sweetness:
	print(count)
else:
	print('-1')


import heapq
(N, K) = map(int, input().strip().split())
cookies = list(map(int, input().strip().split()))
heapq.heapify(cookies)

def combine(c1, c2):
	return c1 + c2 * 2
count = 0
while True:
	c1 = False
	c2 = False
	try:
		c1 = heapq.heappop(cookies)
		c2 = heapq.heappop(cookies)
	except IndexError:
		if c1 and (not c2) and (c1 >= K):
			print(count)
		else:
			print(-1)
		break
	if c1 >= K:
		print(count)
		break
	c3 = combine(c1, c2)
	heapq.heappush(cookies, c3)
	count += 1


import heapq

def combine_cookies(least, second_least):
	return least + 2 * second_least
info1 = input()
(n, k) = [int(x) for x in info1.split(' ')]
info2 = input()
a = [int(x) for x in info2.split(' ')]
heapq.heapify(a)
i = 0
while a[0] < k:
	if len(a) == 1:
		i = -1
		break
	least = heapq.heappop(a)
	combined = combine_cookies(least, a[0])
	heapq.heapreplace(a, combined)
	i += 1
print(i)


import heapq
(n, k) = map(int, input().split())
cookies = list(map(int, input().split()))
heapq.heapify(cookies)
cnt = 0
flag = False
while True:
	first = heapq.heappop(cookies)
	if first >= k:
		flag = True
		break
	if not cookies:
		break
	second = heapq.heappop(cookies)
	heapq.heappush(cookies, first + 2 * second)
	cnt += 1
print(cnt if flag else -1)


import sys
import heapq
(n, k) = map(int, input().split())
ns = list(map(int, input().split()))
cnt = 0
heapq.heapify(ns)
while len(ns) >= 2:
	a = heapq.heappop(ns)
	if a >= k:
		print(cnt)
		sys.exit(0)
	b = heapq.heappop(ns)
	c = a + 2 * b
	cnt += 1
	heapq.heappush(ns, c)
if min(ns) >= k:
	print(cnt)
else:
	print(-1)


import heapq
(n, k) = map(int, input().split())
cookies = list(map(int, input().split()))
heapq.heapify(cookies)
while cookies[0] < k:
	if len(cookies) < 2:
		print(-1)
		break
	smallest = heapq.heappop(cookies)
	second = heapq.heappop(cookies)
	smooshed = smallest + 2 * second
	heapq.heappush(cookies, smooshed)
else:
	print(n - len(cookies))


import heapq
import sys
K = int(sys.stdin.readline().split(' ')[1])
heap = [int(x) for x in sys.stdin.readline().split(' ')]
heapq.heapify(heap)
minSweetness = -1
numOps = 0
while heap[0] < K and len(heap) >= 2:
	first = heapq.heappop(heap)
	if first >= K:
		heapq.heapify(heap)
		break
	second = heapq.heappop(heap)
	heapq.heappush(heap, first + 2 * second)
	numOps += 1
if heap[0] >= K:
	print(numOps)
else:
	print(-1)


(n, k) = [int(x) for x in input().split()]
arr = [int(x) for x in input().split()]
op = 0
import heapq
heapq.heapify(arr)
while arr[0] < k:
	(small1, small2) = (heapq.heappop(arr), heapq.heappop(arr))
	temp = small1 + small2 * 2
	heapq.heappush(arr, temp)
	op += 1
	if len(arr) == 1:
		break
if arr[0] < k:
	op = -1
print(op)


from heapq import heappush, heappop
(n, k) = input().split(' ')
(n, k) = (int(n), int(k))
numbers = [int(t) for t in input().split(' ')]
a = []
for number in numbers:
	heappush(a, number)
if n == 1:
	print(-1 if a[0] < k else 0)
else:
	steps = 0
	successful = False
	while True:
		t = heappop(a)
		if t >= k:
			successful = True
			break
		elif len(a) < 1:
			break
		t2 = heappop(a)
		new_cookie = 2 * t2 + t
		steps += 1
		heappush(a, new_cookie)
	if successful:
		print(steps)
	else:
		print(-1)


from heapq import heapify, heappush as push, heappop as pop
(n, q) = (int(i) for i in input().split())
h = [int(i) for i in input().split()]
heapify(h)
i = 0
while True:
	if len(h) < 2:
		if h[0] < q:
			print(-1)
			break
		else:
			print(i)
			break
	(a, b) = (pop(h), pop(h))
	if a < q or b < q:
		push(h, a + 2 * b)
		i += 1
	else:
		print(i)
		break


import heapq
(n, k) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
heapq.heapify(a)
count = 0
while a[0] < k and len(a) > 1:
	combo = heapq.heappop(a) + 2 * heapq.heappop(a)
	if len(a) == 0:
		a = [combo]
		count += 1
		break
	heapq.heappush(a, combo)
	count += 1
if a[0] < k:
	print(-1)
else:
	print(count)


from heapq import *
(N, K) = map(int, input().split())
C = []
for c in map(int, input().split()):
	heappush(C, c)
n = 0
while len(C) > 1 and C[0] < K:
	n += 1
	a = heappop(C)
	b = heappop(C)
	heappush(C, a + b * 2)
if C[0] < K:
	n = -1
print(n)


class Node(object):

	def __init__(self):
		self.val = -1
		self.next = None

class queue(object):

	def __init__(self):
		super().__init__()
		self.first = None
		self.last = None
		self.no_of_items = 0

	def isEmpty(self):
		return self.first == None

	def push(self, val):
		n = Node()
		n.val = val
		if self.no_of_items == 0:
			self.first = n
			self.last = n
		else:
			self.last.next = n
			self.last = n
		self.no_of_items += 1

	def pop(self):
		if self.first == None:
			return None
		val = self.first.val
		self.first = self.first.next
		self.no_of_items -= 1
		return val

	def peek(self):
		return self.first.val

def mixCount(cookies, K):
	cookies = sorted(cookies)
	if len(cookies) == 0:
		return -1
	if len(cookies) == 1:
		if cookies[0] < K:
			return -1
		else:
			return 0
	if cookies[0] >= K:
		return 0
	q1 = queue()
	q_new = queue()
	for i in range(len(cookies)):
		q1.push(cookies[i])
	count = 0
	while True:
		if q1.isEmpty():
			if q_new.peek() >= K:
				return count
			else:
				a = q_new.pop()
				if q_new.isEmpty():
					return -1
				else:
					b = q_new.pop()
					q_new.push(a + 2 * b)
					count += 1
		elif q_new.isEmpty():
			a = q1.pop()
			b = q1.pop()
			if a >= K and b >= K:
				return count
			q_new.push(a + 2 * b)
			count += 1
		else:
			a = q1.pop()
			if not q1.isEmpty():
				if q1.peek() < q_new.peek():
					b = q1.pop()
				else:
					b = q_new.pop()
			else:
				b = q_new.pop()
			if a >= K and b >= K:
				return count
			else:
				q_new.push(a + 2 * b)
				count += 1
firstLine = input().strip().split()
N = int(firstLine[0])
K = int(firstLine[1])
cookies = [int(e) for e in input().strip().split()]
print(mixCount(cookies, K))


import heapq

class PriorityQueue:

	def __init__(self):
		self.queue = []
		self.index = 0

	def push(self, item, priority):
		heapq.heappush(self.queue, (-priority, self.index, item))
		self.index += 1

	def pop(self):
		return heapq.heappop(self.queue)[-1]
line = input().split(' ')
n = int(line[0])
k = int(line[1])
cookies = list(map(int, input().split(' ')))
pq = PriorityQueue()
for cookie in cookies:
	pq.push(cookie, -cookie)
counter = 0
while len(pq.queue) > 1:
	cookie1 = pq.pop()
	cookie2 = pq.pop()
	if cookie1 > k:
		break
	else:
		counter += 1
		pq.push(cookie1 + cookie2 * 2, -(cookie1 + cookie2 * 2))
if len(pq.queue) == 1 and pq.pop() < k:
	print(-1)
else:
	print(counter)


import heapq

class MinHeap(object):

	def __init__(self):
		self._h = []

	def __len__(self):
		return len(self._h)

	def push(self, o):
		heapq.heappush(self._h, o)

	def pop(self):
		return heapq.heappop(self._h)

	def peek(self):
		if self._h:
			return self._h[0]
		return None

	def empty(self):
		return len(self._h) == 0

def main():
	(n, k) = map(int, input().strip().split())
	a = [int(x) for x in input().strip().split()]
	h = MinHeap()
	for ai in a:
		h.push(ai)
	num_ops = 0
	while h.peek() < k and len(h) >= 2:
		num_ops += 1
		a = h.pop()
		b = h.pop()
		h.push(a + 2 * b)
	if h.peek() < k:
		print(-1)
	else:
		print(num_ops)
main()


from heapq import heapify, heappush, heappop
(N, K) = map(int, input().split())
A = list(map(int, input().split()))
heapify(A)
count = 0
while len(A) > 1 and A[0] < K:
	a = heappop(A)
	b = heappop(A)
	heappush(A, a + 2 * b)
	count += 1
if A[0] < K:
	print(-1)
else:
	print(count)


import heapq
(n, k) = [int(c) for c in input().split()]
heap = []
for item in [int(c) for c in input().split()]:
	heapq.heappush(heap, item)
steps = 0
while heap:
	if heap[0] >= k:
		print(steps)
		break
	elif len(heap) > 1:
		m1 = heapq.heappop(heap)
		m2 = heapq.heappop(heap)
		heapq.heappush(heap, m1 + m2 + m2)
		steps += 1
	else:
		heap.pop()
else:
	print(-1)


import sys
import heapq
nk = [int(arr_temp) for arr_temp in input().strip().split(' ')]
heap = []
arr = [int(arr_temp) for arr_temp in input().strip().split(' ')]
n = nk[0]
k = nk[1]
for sweetness in arr:
	heapq.heappush(heap, sweetness)
count = 0
while heap[0] < k:
	try:
		least_sweet = heapq.heappop(heap)
		next_least_sweet = heapq.heappop(heap)
		new_sweet = least_sweet * 1 + next_least_sweet * 2
		heapq.heappush(heap, new_sweet)
		count += 1
	except:
		count = -1
		break
print(count)


import heapq
(N, K) = map(int, input().split())
H = []
for a in map(int, input().split()):
	heapq.heappush(H, a)
j = 0
while H[0] < K:
	if len(H) <= 1:
		j = -1
		break
	else:
		heapq.heappush(H, heapq.heappop(H) + 2 * heapq.heappop(H))
		j += 1
print(j)


from heapq import heappop, heappush, heapify

def solve(heap, K):
	if heap[0] >= K:
		return 0
	counter = 0
	while len(heap) > 0:
		if len(heap) == 1 and heap[0] >= K:
			return counter
		elif len(heap) == 1 and heap[0] < K:
			return -1
		else:
			least = heappop(heap)
			secondleast = heappop(heap)
			mixed = least + 2 * secondleast
			heappush(heap, mixed)
			counter += 1
			if heap[0] >= K:
				return counter
(N, K) = map(int, input().split())
sweetness = list(map(int, input().split()))
heap = []
for sweet in sweetness:
	heappush(heap, sweet)
answer = solve(heap, K)
print(answer)


import heapq
(n, k) = map(int, input().split())
cookies = [int(x) for x in input().split()]
heapq.heapify(cookies)
ops = 0
while len(cookies) > 1:
	c1 = heapq.heappop(cookies)
	if c1 >= k:
		break
	c2 = heapq.heappop(cookies)
	heapq.heappush(cookies, c1 + 2 * c2)
	ops += 1
if len(cookies) >= 1 and heapq.heappop(cookies) >= k:
	print(ops)
else:
	print(-1)


from heapq import *
(n, k) = [int(x) for x in input().split()]
heap = [int(x) for x in input().split()]
heapify(heap)
try:
	operations = 0
	while heap[0] < k:
		operations += 1
		a = heappop(heap)
		b = heappop(heap)
		heappush(heap, a + 2 * b)
	print(operations)
except IndexError:
	print(-1)


import heapq
inp1 = list(map(int, input().split()))
inp2 = map(int, input().split())
(n, k) = (inp1[0], inp1[1])
count = 0
pos = True
a = []
for c in inp2:
	heapq.heappush(a, c)

def supersweet():
	if a[0] >= k:
		return True
	else:
		return False
while supersweet() != True:
	try:
		s1 = heapq.heappop(a)
		s2 = heapq.heappop(a)
		heapq.heappush(a, s1 + s2 * 2)
		count += 1
	except IndexError:
		pos = False
		break
if pos:
	print(count)
else:
	print(-1)


import heapq
x = input().split(' ')
t = int(x[1])
lis = list(map(lambda a: int(a), input().split(' ')))
heapq.heapify(lis)
minc = 0
while lis[0] < t:
	result = heapq.heappop(lis)
	result1 = heapq.heappop(lis)
	heapq.heappush(lis, result + 2 * result1)
	minc += 1
	if len(lis) == 1 and lis[0] < t:
		minc = -1
		break
print(minc)


import heapq
(n, k) = [int(s) for s in input().strip().split()]
a = [int(s) for s in input().strip().split()]
heapq.heapify(a)
tries = 0
while True:
	if a == [k]:
		print(tries)
		break
	if len(a) < 2:
		print(-1)
		break
	if a[0] >= k:
		print(tries)
		break
	tries += 1
	c1 = heapq.heappop(a)
	c2 = heapq.heappop(a)
	c3 = c1 + 2 * c2
	heapq.heappush(a, c3)


import heapq
vals = [int(val) for val in input().split(' ')]
(n, k) = (vals[0], vals[1])
cookies = [int(v) for v in input().split(' ')]
heapq.heapify(cookies)
op = 0
while len(cookies) >= 2:
	first = heapq.heappop(cookies)
	second = heapq.heappop(cookies)
	newVal = 1 * first + 2 * second
	if first >= k:
		break
	if first < 0 or second < 0:
		op = -1
		break
	if newVal < first or newVal < second:
		op = -1
		break
	heapq.heappush(cookies, newVal)
	op += 1
if len(cookies) > 0 and heapq.heappop(cookies) < k:
	op = -1
print(op)


import heapq
(n, k) = [int(x) for x in input().split()]
heap = [int(x) for x in input().split()]
heapq.heapify(heap)
while len(heap) >= 2 and heap[0] < k:
	l = heapq.heappop(heap)
	r = heapq.heappop(heap)
	heapq.heappush(heap, l + 2 * r)
print(n - len(heap) if heap[0] >= k else -1)


import heapq
(n, k) = input().strip().split(' ')
(n, k) = (int(n), int(k))
h = [int(x) for x in input().strip().split(' ')]
heap = []
for x in h:
	heapq.heappush(heap, x)
step = 0
while len(heap) > 1 and heap[0] < k:
	a = heapq.heappop(heap)
	b = heapq.heappop(heap)
	c = a + 2 * b
	step += 1
	heapq.heappush(heap, c)
print(step if heap[0] >= k else -1)


import heapq
(n, k) = [int(val) for val in input().strip().split()]
array = [int(val) for val in input().strip().split()]

def make_sweet_cookies(heap, k):
	heapq.heapify(heap)
	mixes = 0
	while heap:
		if heap[0] >= k:
			return mixes
		cookie1 = heapq.heappop(heap)
		if not heap:
			return -1
		cookie2 = heapq.heappop(heap)
		heapq.heappush(heap, 1 * cookie1 + 2 * cookie2)
		mixes += 1
	return -1
print(make_sweet_cookies(array[:], k))


import heapq
(n, k) = [int(x) for x in input().split(' ')]
cookies = [int(x) for x in input().split(' ')]

def do_it(n, k, cookies):
	heap = cookies
	heapq.heapify(heap)
	if n < 2:
		return -1
	count = 0
	while heap[0] < k and len(heap) >= 2:
		a = heapq.heappop(heap)
		b = heapq.heappop(heap)
		heapq.heappush(heap, a + 2 * b)
		count += 1
	if len(heap) == 1 and heap[0] < k:
		return -1
	return count
print(do_it(n, k, cookies))


import sys
from heapq import heappush, heappop
cookies = []
args = input().split(' ')
K = int(args[1])
temp = input().split(' ')
for i in range(0, len(temp)):
	heappush(cookies, int(temp[i]))
numberOfLoops = 0
while cookies[0] < K:
	if len(cookies) == 1:
		print('-1')
		sys.exit()
	ls1 = heappop(cookies)
	ls2 = heappop(cookies)
	newCookie = ls1 + 2 * ls2
	heappush(cookies, newCookie)
	numberOfLoops += 1
print(str(numberOfLoops))


from heapq import heapify, heappush, heappop
(n, k) = input().split()
k = int(k)
h = [int(i) for i in input().split()]
heapify(h)
count = 0
while True:
	if h[0] >= k:
		print(count)
		break
	try:
		a = heappop(h)
		b = heappop(h)
	except IndexError:
		print(-1)
		break
	heappush(h, a + 2 * b)
	count += 1


import heapq
(_, K) = map(int, input().strip().split())
cookies = [int(n) for n in input().strip().split()]
heapq.heapify(cookies)
operations = 0
least_sweet = heapq.heappop(cookies)
while cookies and least_sweet < K:
	second_least_sweet = heapq.heappop(cookies)
	combined = least_sweet + 2 * second_least_sweet
	heapq.heappush(cookies, combined)
	least_sweet = heapq.heappop(cookies)
	operations += 1
if least_sweet < K:
	print('-1')
else:
	print(operations)


import heapq
iter = 0
cmd_line = [int(arr_temp) for arr_temp in input().strip().split(' ')]
n = cmd_line[0]
k = cmd_line[1]
h = [int(arr_temp) for arr_temp in input().strip().split(' ')]
heapq.heapify(h)
while len(h) > 1 and h[0] < k:
	cookie1 = heapq.heappop(h)
	cookie2 = heapq.heappop(h)
	heapq.heappush(h, cookie1 + 2 * cookie2)
	iter += 1
if h[0] >= k:
	print(iter)
else:
	print(-1)


import heapq
(num, min_sweet) = [int(x) for x in input().split()]
c = [int(x) for x in input().split()]
heapq.heapify(c)
count = 0
while c:
	c1 = heapq.heappop(c)
	if c1 < min_sweet:
		if c:
			c2 = heapq.heappop(c)
			heapq.heappush(c, c1 + 2 * c2)
			count += 1
		else:
			print(-1)
			break
	else:
		print(count)
		break


import heapq
(n, k) = list(map(int, input().split()))
nums = list(map(int, input().split()))
res = 0
heapq.heapify(nums)
while len(nums) > 1 and nums[0] < k:
	(a, b) = (heapq.heappop(nums), heapq.heappop(nums))
	heapq.heappush(nums, a + 2 * b)
	res += 1
print(res if nums and nums[0] >= k else -1)


import heapq
(n, k) = [int(i) for i in input().split()]
hp = [int(i) for i in input().split()]
heapq.heapify(hp)
counter = 0
while len(hp) >= 2 and hp[0] < k:
	a = heapq.heappop(hp)
	b = heapq.heappop(hp)
	heapq.heappush(hp, a + 2 * b)
	counter += 1
print(-1 if hp[0] < k else counter)


import heapq
import sys
h = []
(number_of_cookies, required_sweetness) = input().split()
number_of_cookies = int(number_of_cookies)
if number_of_cookies == 0:
	print('-1')
	sys.exit()
cookies = [int(i) for i in input().split()]
heapq.heapify(cookies)
steps = 0
while cookies[0] < int(required_sweetness):
	if len(cookies) == 1:
		print('-1')
		sys.exit()
	min_one = heapq.heappop(cookies)
	min_two = heapq.heappop(cookies)
	heapq.heappush(cookies, min_one + 2 * min_two)
	steps += 1
print(steps)


from heapq import *
(N, K) = list(map(int, input().split()))
arr = list(map(int, input().split()))
arr.sort()
i = 0
while len(arr) >= 2 and arr[0] < K:
	a = heappop(arr)
	b = heappop(arr)
	heappush(arr, a + 2 * b)
	i += 1
if arr[0] >= K:
	print(i)
else:
	print(-1)


[n, K] = [int(i) for i in input().strip().split(' ')]
cookies = [int(i) for i in input().strip().split(' ')]

def mixSteps(cookies, K):
	if len(cookies) == 0:
		return -1
	heap = []
	import heapq
	for cookie in cookies:
		heapq.heappush(heap, cookie)
	step = 0
	while heap[0] < K:
		if len(heap) < 2:
			return -1
		c1 = heapq.heappop(heap)
		c2 = heapq.heappop(heap)
		heapq.heappush(heap, c1 + 2 * c2)
		step += 1
	return step
print(mixSteps(cookies, K))


import heapq
(n, k) = [int(data) for data in input().strip().split()]
sweetness = [int(data) for data in input().strip().split()]
heapq.heapify(sweetness)
count = 0
try:
	while sweetness[0] < k:
		least_sweet = heapq.heappop(sweetness)
		No2_least_sweet = heapq.heappop(sweetness)
		heapq.heappush(sweetness, least_sweet + 2 * No2_least_sweet)
		count += 1
	print(count)
except:
	print(-1)


from heapq import heappush as push, heappop as pop, heapify
(n, k) = [int(i) for i in input().strip().split()]
cookies = [int(s) for s in input().strip().split()]
heapify(cookies)
num_comb = 0
while len(cookies) > 1 and cookies[0] < k:
	c1 = pop(cookies)
	c2 = pop(cookies)
	c3 = c1 + 2 * c2
	num_comb += 1
	push(cookies, c3)
if cookies[0] >= k:
	print(num_comb)
else:
	print(-1)


import heapq
(n, k) = map(int, input().split())
cookies = []
for cookie in map(int, input().split()):
	heapq.heappush(cookies, cookie)
num_mixes = 0
while any((cookie < k for cookie in cookies)) and len(cookies) > 1:
	new_cookie = heapq.heappop(cookies) + 2 * heapq.heappop(cookies)
	heapq.heappush(cookies, new_cookie)
	num_mixes += 1
if len(cookies) <= 1 and sum(cookies) < k:
	print(-1)
else:
	print(num_mixes)


import heapq
(numCookies, sweetness) = map(int, input().split())
cookies = [int(temp) for temp in input().strip().split(' ')]
heapq.heapify(cookies)
count = 0
while cookies[0] < sweetness:
	if len(cookies) < 2:
		print(-1)
		exit()
	else:
		cookie1 = heapq.heappop(cookies)
		cookie2 = heapq.heappop(cookies)
		heapq.heappush(cookies, cookie1 + 2 * cookie2)
		count += 1
print(count)


from heapq import *
(n, m) = list(map(int, input().split()))
string = list(map(int, input().split()))
heapify(string)
i = 0
while string[0] < m:
	if len(string) > 1:
		a = heappop(string)
		b = heappop(string)
		new_cookie = a * 1 + b * 2
		heappush(string, new_cookie)
		i += 1
	else:
		i = -1
		break
print(i)


from heapq import heappush, heappop
li = input().split(' ')
(n, k) = (li[0], int(li[1]))
heap = []
for x in input().split(' '):
	heappush(heap, int(x))
count = 0
while heap:
	h1 = heappop(heap)
	if h1 >= k:
		break
	else:
		try:
			h2 = heappop(heap)
			heappush(heap, h1 + 2 * h2)
			count += 1
		except:
			count = -1
			break
print(count)


import sys
import array
import heapq

def solve(data, k):
	heap = []
	res = 0
	for e in data:
		heap.append(e)
	heapq.heapify(heap)
	item1 = heapq.heappop(heap)
	while item1 < k:
		if len(heap) < 1:
			print(-1)
			return
		heapq.heappush(heap, 2 * heapq.heappop(heap) + item1)
		item1 = heapq.heappop(heap)
		res += 1
	print(res)
(n, k) = input().split(' ')
n = int(n)
k = int(k)
data = input().split(' ')
for i in range(len(data)):
	data[i] = int(data[i])
solve(data, k)


from heapq import *
(n, k) = map(int, input().split(' '))
a = list(map(int, input().split(' ')))

class minHeap:

	def init(self, l):
		self.h = [-1] + sorted(l)

	def swap(self, i, j):
		temp = self.h[i]
		self.h[i] = self.h[j]
		self.h[j] = temp

	def add(self, x):
		self.h.append(x)
		i = len(self.h) - 1
		while self.h[i] > self.h[i // 2]:
			self.swap(i, i // 2)
			i = i // 2

	def pop(self):
		return self.h.pop()

	def getMin(self):
		return self.h[1]
ans = 0
heapify(a)
while a[0] < k:
	if len(a) == 1:
		ans = -1
		break
	new = heappop(a) + 2 * heappop(a)
	heappush(a, new)
	ans += 1
print(ans)


import heapq
(N, K) = [int(x) for x in input().strip().split(' ')]
A = [int(x) for x in input().strip().split(' ')]
minheap = []
for item in A:
	heapq.heappush(minheap, item)
operations = 0
if minheap[0] >= K:
	print(0)
else:
	while len(minheap) > 1:
		operations += 1
		sw1 = heapq.heappop(minheap)
		sw2 = heapq.heappop(minheap)
		heapq.heappush(minheap, sw1 + sw2 * 2)
		if minheap[0] >= K:
			print(operations)
			break
	if minheap[0] < K:
		print(-1)


import heapq
(n, k) = map(int, input().split())
cookies = list(map(int, input().split()))
heapq.heapify(cookies)
rv = 0
while cookies[0] < k and n - rv > 1:
	heapq.heapreplace(cookies, heapq.heappop(cookies) + 2 * cookies[0])
	rv += 1
print(rv if cookies[0] >= k else -1)