Skip to content

Commit 76f5a80

Browse files
authored
Merge pull request #767 from AlgorithmWithGod/Ukj0ng
[20250829] BOJ / G1 / 공평하게 팀 나누기 / 한종욱
2 parents 9597c65 + 08cb4eb commit 76f5a80

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
```
2+
import java.io.*;
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
public class Main {
7+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
9+
private static Set<Integer>[] dp;
10+
private static int[] weight;
11+
private static int N, sum, max;
12+
13+
public static void main(String[] args) throws IOException {
14+
init();
15+
DP();
16+
17+
int answer = 0;
18+
19+
for (int val : dp[dp.length-1]) {
20+
answer = Math.max(val, answer);
21+
}
22+
23+
if (N%2 == 1) {
24+
for (int val : dp[dp.length-2]) {
25+
answer = Math.max(val, answer);
26+
}
27+
}
28+
29+
bw.write(answer + " " + (sum - answer) + "\n");
30+
bw.flush();
31+
bw.close();
32+
br.close();
33+
}
34+
35+
private static void init() throws IOException {
36+
N = Integer.parseInt(br.readLine());
37+
sum = 0;
38+
max = 0;
39+
40+
int length = N/2;
41+
if (N%2 == 1) length++;
42+
weight = new int[N];
43+
dp = new Set[length + 1];
44+
for (int i = 0; i < dp.length; i++) {
45+
dp[i] = new HashSet<>();
46+
}
47+
48+
dp[0].add(0);
49+
50+
for (int i = 0; i < N; i++) {
51+
weight[i] = Integer.parseInt(br.readLine());
52+
sum += weight[i];
53+
}
54+
55+
max = sum / 2;
56+
}
57+
58+
private static void DP() {
59+
for (int i = 0; i < N; i++) {
60+
for (int j = dp.length-1; j > 0; j--) {
61+
if (!dp[j-1].isEmpty()) {
62+
for (int val : dp[j-1]) {
63+
int result = val + weight[i];
64+
if (result <= max) {
65+
dp[j].add(result);
66+
}
67+
}
68+
}
69+
}
70+
}
71+
}
72+
}
73+
```

0 commit comments

Comments
 (0)