You have a laser with P amount of energy in it. You are playing a game where you have to use the laser to destroy some crystals, each with some health of their own. Initially, you have 0 points.
Each crystal has an associated health. The health of the ith crystal is given by health[i]. You can perform one of the two actions:
- 
At the cost of health[i] energy, you can destroy the ith crystal and gain 1 point. You can only perform this action if your laser has atleast health[i] energy in it.
- 
At the cost of 1 point, you can destroy the ith crystal and refuel the laser's energy by an amount equal to health[i]. This action can only be performed if you have atleast one point.
Note: Each crystal can only be destroyed once.
Determine the maximum number of points you can obtain by destroying any number of crystals and performing either action as you wish.

-----Input:-----
- First line will contain n, number of crystals. 
- Second line will contain space separated integers, health of each crystal.
- Third line will contain an integer P, initial energy of the laser.

-----Output:-----
Print the largest number of points we can have after destroying any number of crystals.

-----Constraints-----
- health.length <= 1000
- 0 <= health[i] < 10000
- 0 <= P < 10000

-----Subtasks-----
- 40 points : 1 <= health.length <= 100
- 60 points : health.length > 100

-----Sample Input 1:-----
1
200
100

-----Sample Output 1:-----
0

-----Explanation:-----
The laser initially has only 100 energy. Since the only crystal requires 200 energy to destroy, we cannot perform any action here, and the game ends. The number of points is zero.

-----Sample Input 2:-----
2
100 200
150

-----Sample Output 2:-----
1

-----Explanation:-----
The laser has 150 energy. We can consume 100 energy and destroy the first crystal and gain 1 point. The remaining energy is 50, which is not enough to destroy the other crystal. We end the game here as we have the maximum possible points attainable.

Difficulty: UNKNOWN_DIFFICULTY

Starter code:

Solutions:
def game(n, l, p):
	if len(l) == 0:
		return 0
	l.sort()
	if len(l) >= 1 and p < l[0]:
		return 0
	l.sort()
	c = 0
	ma = set()
	ma.add(0)
	while len(l):
		if p >= l[0]:
			p -= l[0]
			c += 1
			ma.add(c)
			l = l[1:]
		elif c > 0:
			c -= 1
			ma.add(c)
			p += l[-1]
			l = l[:-1]
		else:
			return max(ma)
	return max(ma)
n = int(input())
l = list(map(int, input().split()))
p = int(input())
print(game(n, l, p))


n = int(input())
health = list(map(int, input().split()))
initial = int(input())
if n > 1:
	health.sort()
points = 0
mxpts = 0
(i, j) = (0, n - 1)
while i <= j and i < n and (j >= 0):
	if health[i] <= initial:
		points += 1
		mxpts = max(points, mxpts)
		initial -= health[i]
		i += 1
	elif points > 0:
		initial += health[j]
		j -= 1
		points -= 1
	else:
		break
print(mxpts)


n = int(input())
arr = list(map(int, input().split()))
h = int(input())
arr.sort()
ans = 0
i = 0
if n == 1 and h >= arr[0]:
	print(1)
	return
while len(arr) != 0:
	if arr[i] > h and len(arr) > 1:
		h += arr[-1]
		ans -= 1
		del arr[-1]
	elif len(arr) == 1:
		break
	h -= arr[i]
	del arr[i]
	ans += 1
print(max(ans, 0))


from sys import *
input = stdin.readline
n = int(input())
l = list(map(int, input().split()))
l.sort()
p = int(input())
c = 0
i = 0
while i < n:
	if l[i] <= p:
		c = c + 1
		p = p - l[i]
		i = i + 1
	elif c > 0:
		if p + l[n - 1] >= l[i]:
			p = p + l[n - 1] - l[i]
			n = n - 1
			i = i + 1
	else:
		break
print(c)


n = int(input())
health = sorted(map(int, input().split()))
laser = int(input())
(left, right) = (0, len(health) - 1)
ans = 0
while left <= right:
	if health[left] <= laser:
		laser -= health[left]
		ans += 1
		left += 1
	else:
		if ans == 0 or left >= right - 1:
			break
		laser += health[right]
		ans -= 1
		right -= 1
print(ans)


n = int(input())
n -= 1
l = list(map(int, input().split()))
x = int(input())
c = 0
m = 0
z = 0
i = 0
l.sort()
while i < n:
	if l[i] <= x:
		c += 1
		x = x - l[i]
		i += 1
	elif c >= 1:
		c -= 1
		x = x + l[n]
		n -= 1
	if m < c:
		m = c
print(m)


crystel = int(input())
cry_val = list(map(int, input().split()))
laser = int(input())
(point, iterr) = (0, 0)
count = 1
point_mx = [0]
cry_val.sort()
while count <= crystel:
	if cry_val[iterr] <= laser:
		point += 1
		point_mx.append(point)
		laser -= cry_val[iterr]
		iterr += 1
		count += 1
	else:
		laser += cry_val[-1]
		cry_val.remove(cry_val[-1])
		point -= 1
		count += 1
print(max(point_mx))


try:
	n = int(input())
	li = list(map(int, input().split()))
	l = int(input())
	p = 0
	m = 0
	k = len(li) - 1
	li.sort()
	i = 0
	while i <= k:
		if l >= li[i]:
			l -= li[i]
			p += 1
			i += 1
			if m < p:
				m = p
		elif p > 0:
			l += li[k]
			p -= 1
			li.pop(k)
			k -= 1
		else:
			break
	print(m)
except:
	pass


n = int(input())
arr = list(map(int, input().split()))
arr.sort()
p = int(input())
pt = 0
maxx = [0]
i = 0
ct = 1
while ct <= n:
	if arr[i] <= p:
		pt += 1
		maxx.append(pt)
		p -= arr[i]
		i += 1
		ct += 1
	else:
		p += arr[-1]
		arr.remove(arr[-1])
		pt -= 1
		ct += 1
print(max(maxx))


n = int(input())
a = list(map(int, input().split()))
h = int(input())
a.sort()
p = 0
mp = 0
l = n - 1
flag = True
c = 0
for x in a:
	while x > h and c < l:
		if p > 0:
			p = p - 1
			h = h + a[l]
			del a[l]
			l = l - 1
		else:
			flag = False
			break
	if x <= h and flag == True:
		c = c + 1
		h = h - x
		p = p + 1
		if p > mp:
			mp = p
	else:
		break
print(mp)


crystals = int(input())
li = list(map(int, input().split()))
a = li.sort()
points = 0
health = int(input())
pointer = -1
for i in range(len(li)):
	if health >= li[i]:
		health = health - li[i]
		points += 1
		li[i] = 0
	elif points >= 1 and li[pointer] != 0:
		points -= 1
		health += li[pointer]
		pointer -= 1
		if health >= li[i]:
			health = health - li[i]
			points += 1
			li[i] = 0
	else:
		break
print(points)


n = int(input().strip())
l = [int(x) for x in input().strip().split()]
p = int(input().strip())
l.sort()
left = 0
right = n - 1
maxo = 0
cur = 0
while left <= right:
	if p >= l[left]:
		p -= l[left]
		left += 1
		cur += 1
		maxo = max(maxo, cur)
	elif cur > 0:
		p += l[right]
		right -= 1
		cur -= 1
	else:
		break
print(maxo)