Skip to content

Commit 09acda4

Browse files
authored
[20251205] BOJ / G5 / 개업 / 이인희
1 parent 2fea2ce commit 09acda4

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
int N = Integer.parseInt(st.nextToken());
10+
int M = Integer.parseInt(st.nextToken());
11+
st = new StringTokenizer(br.readLine());
12+
int[] wok = new int[M];
13+
for(int i = 0; i < M; i++){
14+
wok[i] = Integer.parseInt(st.nextToken());
15+
}
16+
17+
ArrayList<Integer> cook = new ArrayList<>();
18+
for(int i = 0; i < M; i++){
19+
cook.add(wok[i]);
20+
}
21+
for(int i = 0; i < M; i++){
22+
for(int j = i + 1; j < M; j++){
23+
cook.add(wok[i] + wok[j]);
24+
}
25+
}
26+
27+
int INF = 100_000_000;
28+
int[] dp = new int[N + 1];
29+
Arrays.fill(dp, INF);
30+
dp[0] = 0;
31+
for(int x = 0; x <= N; x++){
32+
if(dp[x] == INF)
33+
continue;
34+
for(int c : cook){
35+
int nx = x + c;
36+
if(nx <= N){
37+
dp[nx] = Math.min(dp[nx], dp[x] + 1);
38+
}
39+
}
40+
}
41+
int answer = dp[N];
42+
System.out.println((answer >= INF)? -1 : answer);
43+
44+
}
45+
}
46+
47+
```

0 commit comments

Comments
 (0)