All problems

Subset Sum Problem

hardPythonDynamic Programming

Whitfield Accounts receives a lump payment and a list of unpaid invoices. Before chasing the customer, the clerk checks whether the payment settles some group of those invoices exactly — no change, nothing part-paid. Invoices in that group need not be next to each other on the list, and each can be settled only once.

Task: Print whether some group of the invoice amounts adds up to exactly the payment.

Input

Two lines. The first holds the payment, an integer from 0 to 10000. The second holds between 1 and 100 positive integers separated by single spaces — the unpaid invoice amounts. Amounts may repeat.

Output

One line holding exactly Yes or No, with that capitalisation. Yes means some group of the amounts adds up to the payment exactly. The group may be any selection from the list, whether or not those invoices sit next to each other, and each invoice may appear in it at most once. A payment of 0 is Yes, settled by the empty group.

Example:

Input:
9
3 34 4 12 5 2

Output:
Yes

(4 + 5 = 9)

Sign in to solve this problem

Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.

Sign in

Discussion

Sign in to join the discussion — reading is open to everyone.

Loading comments…