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