-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
34 lines (27 loc) · 771 Bytes
/
Main.java
File metadata and controls
34 lines (27 loc) · 771 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int N = sc.nextInt();
int K = sc.nextInt();
int [] dp = new int [K+1];
Arrays.fill(dp,10001);
int [] coinList = new int[N];
for (int i=0; i<N;i++){
int coin = sc.nextInt();
coinList[i]=coin;
}
dp[0]=0;
for (int i=0; i< coinList.length; i++){
for(int j=coinList[i]; j<K+1; j++){
dp[j]=Math.min(dp[j],dp[j-coinList[i]]+1);
}
}
if (dp[K]==10001){
System.out.println(-1);
}else {
System.out.println(dp[K]);
}
}
}