Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|

Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |

Language: The Fewest Coins
Description Farmer John has gone to town to buy some farm supplies. Being a very efficient man, he always pays for his goods in such a way that the smallest number of coins changes hands, i.e., the number of coins he uses to pay plus the number of coins he receives in change is minimized. Help him to determine what this minimum number is. FJ wants to buy V ≤ 120). Farmer John is carrying _{i}C_{1} coins of value V_{1}, C_{2} coins of value V_{2}, ...., and C coins of value _{N}V (0 ≤ _{N}C ≤ 10,000). The shopkeeper has an unlimited supply of all the coins, and always makes change in the most efficient manner (although Farmer John must be sure to pay in a way that makes it possible to make the correct change)._{i}Input Line 1: Two space-separated integers: N and T.
Line 2: N space-separated integers, respectively V_{1}, V_{2}, ..., V coins (_{N}V_{1}, ...V)
_{N}Line 3: N space-separated integers, respectively C_{1}, C_{2}, ..., C_{N}Output Line 1: A line containing a single integer, the minimum number of coins involved in a payment and change-making. If it is impossible for Farmer John to pay and receive exact change, output -1. Sample Input 3 70 5 25 50 5 2 1 Sample Output 3 Hint Farmer John pays 75 cents using a 50 cents and a 25 cents coin, and receives a 5 cents coin in change, for a total of 3 coins used in the transaction. Source |

[Submit] [Go Back] [Status] [Discuss]

All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di

Any problem, Please Contact Administrator