Skip to content

Commit 5ffd31d

Browse files
authored
Merge pull request #1802 from AlgorithmWithGod/Ukj0ng
[20260118] BOJ / G4 / 호텔 / 한종욱
2 parents 585e9e4 + a7b10c2 commit 5ffd31d

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

Ukj0ng/202601/18 BOJ G4 호텔.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
```
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
8+
private static int[] dp;
9+
private static int[][] cites;
10+
private static int N, C;
11+
12+
public static void main(String[] args) throws IOException {
13+
init();
14+
15+
bw.write(dp[N] + "\n");
16+
bw.flush();
17+
bw.close();
18+
br.close();
19+
}
20+
21+
private static void init() throws IOException {
22+
StringTokenizer st = new StringTokenizer(br.readLine());
23+
N = Integer.parseInt(st.nextToken());
24+
C = Integer.parseInt(st.nextToken());
25+
26+
cites = new int[C][2];
27+
dp = new int[N+1];
28+
29+
for (int i = 0; i < C; i++) {
30+
st = new StringTokenizer(br.readLine());
31+
cites[i][0] = Integer.parseInt(st.nextToken());
32+
cites[i][1] = Integer.parseInt(st.nextToken());
33+
}
34+
35+
Arrays.fill(dp, Integer.MAX_VALUE);
36+
dp[0] = 0;
37+
38+
for (int i = 0; i < C; i++) {
39+
for (int j = 1; j < N; j++) {
40+
if (j - cites[i][1] >= 0 && dp[j - cites[i][1]] != Integer.MAX_VALUE) {
41+
dp[j] = Math.min(dp[j], dp[j - cites[i][1]] + cites[i][0]);
42+
}
43+
}
44+
45+
for (int j = N - cites[i][1]; j <= N; j++) {
46+
if (j >= 0 && dp[j] != Integer.MAX_VALUE) dp[N] = Math.min(dp[N], dp[j] + cites[i][0]);
47+
}
48+
}
49+
}
50+
}
51+
```

0 commit comments

Comments
 (0)