File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed
Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change 1+ ```java
2+ import java.io.*;
3+ import java.util.*;
4+
5+ public class Main {
6+ public static void main(String[] args) throws IOException {
7+ BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+ StringTokenizer st = new StringTokenizer(br.readLine());
9+
10+ int n = Integer.parseInt(st.nextToken());
11+ int k = Integer.parseInt(st.nextToken());
12+
13+ int[] coins = new int[n];
14+ for (int i = 0; i < n; i++) {
15+ coins[i] = Integer.parseInt(br.readLine().trim());
16+ }
17+
18+ int[] dp = new int[k + 1];
19+ Arrays.fill(dp, Integer.MAX_VALUE);
20+ dp[0] = 0;
21+
22+ for (int i = 1; i <= k; i++) {
23+ for (int coin : coins) {
24+ if (coin <= i && dp[i - coin] != Integer.MAX_VALUE) {
25+ dp[i] = Math.min(dp[i], dp[i - coin] + 1);
26+ }
27+ }
28+ }
29+
30+ System.out.println(dp[k] == Integer.MAX_VALUE ? -1 : dp[k]);
31+ }
32+ }
33+ ```
You can’t perform that action at this time.
0 commit comments