#P3092. [USACO13NOV] No Change G
[USACO13NOV] No Change G
题目描述
Farmer John is at the market to purchase supplies for his farm. He has in his pocket coins (), each with value in the range . 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.
输入格式
Line : Two integers, and .
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.
输出格式
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
提示
FJ has coins of values , , and . He must make purchases in sequence of value , , , , , and .
FJ spends his 10-unit coin on the first two purchases, then the 15-unit coin on the remaining purchases. This leaves him with the 12-unit coin.