Exact Change
InterviewTime limit1sMemory limit128 MB
Given a price and a multiset of up to 100 coin values, pick a subset summing to at least the price; minimize the sum first, then the number of coins.
- Level
Easy3 of 10
- Topics
- Dynamic programming, Array, Implementation
- Solved
- No attempts yet
Problem
- Seller: That will be fourteen dollars.
- Buyer: Here's a twenty.
- Seller: Sorry, I don't have any change.
- Buyer: OK, here's a ten and a five. Keep the change.
When travelling to remote locations it is often helpful to carry cash, in case you want to buy something from a seller who does not accept credit or debit cards. It also helps to carry a variety of denominations, in case the seller cannot make change. Even then you may not have the exact amount and will have to pay a little more than the price. The same situation can arise in cities too, for example at a vending machine that returns no change.
You want to minimize the amount you pay, but you must pay at least the price of the item. Among all ways to pay that minimum possible amount, you also want to use as few coins and bills as possible.
Input
The first line contains one integer: the number of test cases that follow.
Each test case begins with a line containing one integer, the price of the item in cents. The price does not exceed 10,000 cents (that is, $100). The next line contains one integer , the number of bills and coins you have, with at most 100. Each of the following lines contains one integer, the value in cents of one bill or coin.
The denominations may be any number of cents; they are not restricted to the coins and bills in ordinary use. However, no bill or coin has a value greater than 10,000 cents ($100). The total value of your bills and coins is always at least the price of the item.
Output
For each test case, output a single line with two integers: the total amount paid (in cents) and the total number of coins and bills used.