Skip to content

Commit c5ad165

Browse files
authored
[20250211] BOJ / 골드5 / 창영이와 커피 / 김수연
1 parent c4c3a1e commit c5ad165

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
class Solution
6+
{
7+
public static void main(String[] args) throws Exception{
8+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
9+
StringTokenizer st = new StringTokenizer(br.readLine());
10+
int N = Integer.parseInt(st.nextToken());
11+
int K = Integer.parseInt(st.nextToken());
12+
Integer[] coffee = new Integer[N];
13+
int[] dp = new int[K+1];
14+
Arrays.fill(dp, Integer.MAX_VALUE);
15+
dp[0] = 0;
16+
st = new StringTokenizer(br.readLine());
17+
for (int i = 0; i < N; i++) {
18+
coffee[i] = Integer.parseInt(st.nextToken());
19+
}
20+
21+
Arrays.sort(coffee, Collections.reverseOrder());
22+
23+
for (int i = 0; i < N; i++) {
24+
if (coffee[i] > K) continue;
25+
dp[coffee[i]] = 1;
26+
for (int j = K; j > coffee[i]; j--) {
27+
if (dp[j - coffee[i]] != Integer.MAX_VALUE) {
28+
dp[j] = Math.min(dp[j], dp[j - coffee[i]] + 1);
29+
}
30+
}
31+
}
32+
33+
if (dp[K] != Integer.MAX_VALUE) {
34+
System.out.println(dp[K]);
35+
} else {
36+
System.out.println(-1);
37+
}
38+
}
39+
}
40+
41+
```

0 commit comments

Comments
 (0)