A Weighty Problem
Time limit1sMemory limit128 MB
Choose which coins to hand over for a purchase so that the total weight of unspent coins plus the store's greedy change is minimized.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Greedy, Math, Implementation
- Solved
- No attempts yet
Problem
You like to pay with coins, but you have a problem: you carry so much loose change that your pocket has grown too heavy. You have a plan to lighten it, and you need a program to carry it out.
Your next purchase costs cents. You will hand over some of your coins whose total value is at least ; if you overpay, the store gives you change. Choose which coins to spend so that the total weight of the coins left in your pocket afterward — the coins you did not spend, plus the coins the store hands back as change — is as small as possible.
When the store owes you cents it makes change greedily: it repeatedly gives you one coin of the largest denomination whose value is at most , subtracts that value from , and continues until reaches . The store has an unlimited supply of every denomination.
There are denominations. Denomination has an integer value (in cents) and a weight (in grams). Exactly one denomination has value , and no two denominations share a value.
You own coins; coin is of denomination .
Constraints: , , , , (given to two decimal places), and .
Input
The first line contains three integers , , and : the cost of the purchase in cents, the number of coin denominations, and the number of coins you own.
Each of the next lines contains an integer and a real number (given to exactly two decimal places): the value in cents and the weight in grams of denomination .
Each of the next lines contains one integer : the 1-based denomination of the -th coin you own.
Output
If you can afford the purchase, print the minimum achievable total weight in grams, rounded to two decimal places (the answer is always an exact multiple of ). Otherwise, print too poor.
Hint
Suppose you own seven 5-cent coins and are buying something for 3 cents, with denominations of 1, 5, 10, and 20 cents weighing 1, 2, 1, and 9 grams respectively.
One optimal choice is to spend three 5-cent coins: the store then owes you 12 cents and returns one 10-cent coin and two 1-cent coins. Another is to spend four 5-cent coins: the store owes you 17 cents and returns one 10-cent coin, one 5-cent coin, and two 1-cent coins. Both leave grams in your pocket.