All questions
HardDynamic Programming · Bounded Knapsack
Minimum Coins with Limited Inventory
Written by a community member · · 1 attempt · 100% pass
0
You must assemble a scientific sample weighing exactly T grams using N unique metallic coin types. Each coin type i has a weight d_i and a maximum available quantity cnt_i. Unlike standard coin change problems with unlimited supply, you must stay within these inventory limits. Find the minimum total number of coins required to reach the target weight T. If it is impossible, return -1.
Input format:
- The first line contains an integer N, the number of denominations.
- The second line contains an integer T, the target weight.
- The next N lines each contain two integers d_i and cnt_i.
Output format: return the minimum number of coins needed, or -1 if impossible.
Constraints:
- 1 <= N <= 10^5
- 1 <= T <= 10^5
- 1 <= d_i, cnt_i <= 10^5
Examples:
- If N=3, T=11, coins = [[1,5], [5,2], [7,1]], one optimal way is 5+5+1, so the answer is 3.
- If N=2, T=7, coins = [[2,2], [4,1]], it is impossible to make 7 exactly, so the answer is -1.
The coins argument lists each coin type as [d_i, cnt_i].