Submission #768345

#TimeUsernameProblemLanguageResultExecution timeMemory
768345iamjiamingliuBank (IZhO14_bank)Pypy 3
100 / 100
356 ms36944 KiB
from sys import exit

people_num, note_num = [int(i) for i in input().split()]
people = [int(i) for i in input().split()]
banknotes = [int(i) for i in input().split()]

leftover = [-1 for _ in range(1 << note_num)]
people_covered = [-1 for _ in range(1 << note_num)]
leftover[0] = 0
people_covered[0] = 0
for s in range(1, 1 << note_num):
	for last in range(note_num):
		if (s & (1 << last)) == 0:
			continue
		prev = s & ~(1 << last)
		if people_covered[prev] == -1:
			continue

		new_amt = leftover[prev] + banknotes[last]
		# the salary of the current person we're going to try to pay
		curr_target = people[people_covered[prev]]
		# if it's still not enough, just increment the leftover pile
		if new_amt < curr_target:
			people_covered[s] = people_covered[prev]
			leftover[s] = new_amt
			"""
			or it's exactly right, in which case reset the leftover count
			and increment the covered amount
			"""
		elif new_amt == curr_target:
			people_covered[s] = people_covered[prev] + 1
			leftover[s] = 0

	# we've covered all the people!
	if people_covered[s] == people_num:
		print("YES")
		exit()
print("NO")
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...