Skip to content

Commit b5a7ca6

Browse files
authored
[20250904] BOJ / P5 / Two Machines / 한종욱
1 parent 5d74bf5 commit b5a7ca6

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 final int INF = 70000;
9+
private static int[][] task;
10+
private static int[][] dp;
11+
private static int N, maxTime;
12+
13+
public static void main(String[] args) throws IOException {
14+
init();
15+
DP();
16+
17+
int answer = INF;
18+
for (int j = 0; j <= maxTime; j++) {
19+
if (dp[N][j] != INF) {
20+
answer = Math.min(answer, Math.max(j, dp[N][j]));
21+
}
22+
}
23+
24+
bw.write(answer + "\n");
25+
bw.flush();
26+
bw.close();
27+
br.close();
28+
}
29+
30+
private static void init() throws IOException {
31+
N = Integer.parseInt(br.readLine());
32+
int a = 0;
33+
int b = 0;
34+
35+
task = new int[2][N];
36+
37+
for (int i = 0; i < N; i++) {
38+
StringTokenizer st = new StringTokenizer(br.readLine());
39+
task[0][i] = Integer.parseInt(st.nextToken());
40+
task[1][i] = Integer.parseInt(st.nextToken());
41+
42+
a += task[0][i];
43+
b += task[1][i];
44+
}
45+
46+
maxTime = Math.max(a, b);
47+
48+
dp = new int[N + 1][maxTime + 1];
49+
for (int i = 0; i <= N; i++) {
50+
Arrays.fill(dp[i], INF);
51+
}
52+
dp[0][0] = 0;
53+
}
54+
55+
private static void DP() {
56+
for (int i = 0; i < N; i++) {
57+
for (int j = 0; j <= maxTime; j++) {
58+
if (dp[i][j] == INF) continue;
59+
60+
if (j + task[0][i] <= maxTime) dp[i + 1][j + task[0][i]] = Math.min(dp[i + 1][j + task[0][i]], dp[i][j]);
61+
62+
dp[i + 1][j] = Math.min(dp[i + 1][j], dp[i][j] + task[1][i]);
63+
}
64+
}
65+
}
66+
}
67+
68+
```

0 commit comments

Comments
 (0)