#P17187. [ICPC 2017 Hong Kong R] Optimal Coin Change

[ICPC 2017 Hong Kong R] Optimal Coin Change

Problem Description

In a 1010-dollar shop, everything is 1010 dollar or less. In order to serve customers more effectively at the cashier, change needs to be provided in a minimum number of coins.

In this problem, you are going to provide a given value of the change in different coins. Write a program to calculate the number of coins needed for each type of coin.

The input includes a value vv, a size of the coinage set nn, and a face value of each coin, f1,f2,,fnf_1, f_2, \dots, f_n. The output is a list of numbers, namely, c1,,cnc_1, \dots, c_n, indicating the number of coins needed for each type of coin. There may be many ways for the change. The value vv is an integer satisfying 0<v20000 < v \le 2000, representing the change required in cents. The face value of a coin is less than or equal to 1000010000. The output of your program should take the combination with the least number of coins needed.

For example, the Hong Kong coinage issued by the Hong Kong Monetary Authority consists of 1010 cents, 2020 cents, 5050 cents, 11 dollar, 22 dollars, 55 dollars and 1010 dollars would be represented in the input by n=7n = 7, f1=10f_1 = 10, f2=20f_2 = 20, f3=50f_3 = 50, f4=100f_4 = 100, f5=200f_5 = 200, f6=500f_6 = 500, f7=1000f_7 = 1000.

Input Format

The test data may contain many test cases, please process it to the end of the file. Each test case contains integers v,n,f1,,fnv, n, f_1, \dots, f_n in a line. It is guaranteed that n10n \le 10 and f1<f2<<fnf_1 < f_2 < \dots < f_n.

Output Format

The output be nn numbers in a line, separated by space. If there is no possible change, your output should be a single 1-1. If there are more than one possible solutions, your program should output the one that uses more coins of a lower face value.

2000 7 10 20 50 100 200 500 1000
250 4 10 20 125 150
35 4 10 20 125 150
48 4 1 8 16 20
40 4 1 10 13 37
43 5 1 2 21 40 80
0 0 0 0 0 0 2
0 0 2 0
-1
0 1 0 2  
3 0 0 1
1 1 0 1 0