from itertools import permutations
def solution(k, dungeons):
dun = list(permutations(dungeons))
answer = 0 ; aa = []
for d in dun :
a = 0
piro = k
for i in d:
if piro < i[0]:
break
elif piro >= i[0]:
a += 1
piro -= i[1]
if answer < a:
answer = a
aa.append(a)
return answer