Auto-Coin-o-Matic
Time limit2sMemory limit2048 MB
Coins are removed over time, and for each query you must find the fewest coins that make amount v, or -1 if no combination works.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Implementation
- Solved
- No attempts yet
Problem
It is finally here! The day you unveil your new invention, the Auto-Coin-o-Matic, has arrived. People insert their card, type in the amount they want, and get exact change back with the fewest number of coins. You watch with glee and anxiety.
Was it actually the fewest number of coins? That is how the machine was programmed, but what if there is a bug? It is okay, you are watching. You decide to randomly pick some transactions and check whether the machine gave out the fewest coins possible. But the machine is running out of certain types of coins. Will it still work correctly?
Input
The input starts with two integers and (, ).
The next line contains integers, (), the denominations of coins available initially. All denominations are unique.
The next lines each contain a character () and an integer (), where is the type of event and is its value.
- If is
Q, this is a query. Output the minimum number of coins needed to give out exactly . If it is not possible to make exactly with the available denominations, output . - If is
X, the machine is out of coins of denomination . All later queries cannot use that denomination. EachXevent refers to a denomination that the machine currently has in stock.
Output
Output lines, where is the number of query events. On the th line, output the fewest number of coins needed for the th query, or if it is impossible.