#K69057. Coin Change Problem
Coin Change Problem
Coin Change Problem
You are given a list of coin denominations and an integer amount. Your task is to determine the fewest number of coins required to make up the given amount. If it is not possible to form that amount with any combination of the provided coins, output -1.
The problem can be formulated as follows: Given a set of coins with denominations \(c_1, c_2, \ldots, c_n\) and an amount \(A\), find the minimum number of coins \(k\) such that \[ \sum_{i=1}^{k} c_{j_i} = A, \] where each \(c_{j_i}\) is one of the coin denominations. If no such combination exists, output -1.
Example: For coins [1, 2, 5] and amount 11, one possible solution is 5 + 5 + 1, so the answer is 3.
inputFormat
The input is read from standard input and consists of two lines:
- The first line contains two space-separated integers:
n
(the number of coin denominations) andamount
(the target amount). - The second line contains
n
space-separated integers representing the coin denominations.
For example:
3 11 1 2 5
outputFormat
Output a single integer to standard output: the minimum number of coins required to form the given amount, or -1 if it is impossible.
## sample3 11
1 2 5
3
</p>