File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed
Expand file tree Collapse file tree 1 file changed +38
-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+ static BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
7+ static BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (System . out));
8+ static StringTokenizer st;
9+ static int N ,K ;
10+ static Set<Integer > coins;
11+ static int [] dp; // dp[i]: i원을 만드는데 필요한 동전의 최소 개수
12+ public static void main (String [] args ) throws IOException {
13+ st = new StringTokenizer (br. readLine());
14+ N = Integer . parseInt(st. nextToken());
15+ K = Integer . parseInt(st. nextToken());
16+ coins = new HashSet<> ();
17+ dp = new int [K + 1 ];
18+ for (int i = 0 ; i < N ; i++ ) {
19+ coins. add(Integer . parseInt(br. readLine()));
20+ }
21+
22+ Arrays . fill(dp,Integer . MAX_VALUE );
23+ dp[0 ] = 0 ;
24+
25+ for (int i : coins) {
26+ for (int j = i; j <= K ; j++ ) {
27+ if (dp[j- i] != Integer . MAX_VALUE ){
28+ dp[j] = Math . min(dp[j],dp[j- i]+ 1 );
29+ }
30+ }
31+ }
32+ int ans = dp[K ] == Integer . MAX_VALUE ? - 1 : dp[K ];
33+
34+ bw. write(ans + " " );
35+ bw. close();
36+ }
37+ }
38+ ```
You can’t perform that action at this time.
0 commit comments