bzoj#P3312. [USACO2013 Nov] No Change
[USACO2013 Nov] No Change
Description
Farmer John is at the market to purchase supplies for his farm. He has in his pocket coins. FJ would like to make a sequence of purchases, where the ith purchase costs units of money. As he makes this sequence of purchases, he can periodically stop and pay, with a single coin, for all the purchases made since his last payment (of course, the single coin he uses must be large enough to pay for all of these). Unfortunately, the vendors at the market are completely out of change, so whenever FJ uses a coin that is larger than the amount of money he owes, he sadly receives no changes in return!
Please compute the maximum amount of money FJ can end up with after making his purchases in sequence. Output if it is impossible for FJ to make all of his purchases.
Input
Line : Two integers, .
Lines : Each line contains the amount of money of one of FJ's coins.
Lines : These lines contain the costs of FJ's intended purchases.
Output
Line : The maximum amount of money FJ can end up with, or if FJ cannot complete all of his purchases.
3 6
12
15
10
6
3
3
2
3
7
12
Sample Explain 1
FJ has coins of values . He must make purchases in sequence of value .
FJ spends his -unit coin on the first two purchases, then the -unit coin on the remaining purchases. This leaves him with the -unit coin.
Data scale and Agreement
For data, , , the value of each coin in the range , .
Source
Gold