Paying the Dinner Bill
Time limit2sMemory limit256 MB
Given each person's bill share and coin/bill counts across ten denominations, decide whether pooling all cash can pay the total exactly.
- Level
Medium5 of 10
- Topics
- Greedy, Math, Implementation
- Solved
- No attempts yet
Problem
You and your friends have just finished a pleasant dinner, and now everyone has to pay their share.
The restaurant does not accept cards, so the bill must be paid in cash, and it gives no change. Therefore each person must pay their share exactly.
The people who shared the dinner may freely exchange coins and bills with one another to gather the amounts they need. Because money can be exchanged freely like this, what really matters is whether, by pooling everyone's cash, the group can pay the total of all shares exactly, without needing a single unit of change.
Given the cash each person starts with, decide whether the group can pay the exact total of everyone's shares.
Input
The input consists of several test cases.
The first line of each test case contains the number of people . Each of the next lines describes one person in the following order:
x c1 c5 c10 c25 c100 c500 c1000 c2000 c5000 c10000
Here is the amount that person must pay, and is the number of coins or bills of denomination that person holds. The ten denominations are .
The last line of the input contains a single , marking the end of the input.
Every person always holds at least as much cash as the amount they must pay, and the total cash held by each person fits in a signed 32-bit integer. Also, .
Output
For each test case, print Case k: (where is the case number starting from 1), a single space, and then YES if everyone can pay exactly, or NO otherwise.