This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
def can_pay_all_salaries(N, M, salaries, banknotes):
# Сортируем зарплаты и купюры в порядке убывания
salaries.sort(reverse=True)
banknotes.sort(reverse=True)
used = [False] * M
def backtrack(idx):
if idx == N:
return True
current_salary = salaries[idx]
def dfs(start, total):
if total == current_salary:
if backtrack(idx + 1):
return True
return False
if total > current_salary:
return False
prev = -1
for i in range(start, M):
if not used[i] and total + banknotes[i] <= current_salary:
if banknotes[i] == prev:
continue
used[i] = True
if dfs(i + 1, total + banknotes[i]):
return True
used[i] = False
prev = banknotes[i]
return False
return dfs(0, 0)
return backtrack(0)
def main():
import sys
import sys
input = sys.stdin.read
data = input().split()
ptr = 0
N = int(data[ptr])
M = int(data[ptr +1])
ptr +=2
salaries = []
for _ in range(N):
salaries.append(int(data[ptr]))
ptr +=1
banknotes = []
for _ in range(M):
banknotes.append(int(data[ptr]))
ptr +=1
total_salary = sum(salaries)
total_banknotes = sum(banknotes)
if total_salary > total_banknotes:
print("NO")
return
if can_pay_all_salaries(N, M, salaries, banknotes):
print("YES")
else:
print("NO")
if __name__ == "__main__":
main()
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |