File tree Expand file tree Collapse file tree 1 file changed +62
-0
lines changed
Expand file tree Collapse file tree 1 file changed +62
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.* ;
3+
4+ public class BJ_4384_ 공평하게_팀_나누기 {
5+
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+
9+ private static int N , sum;
10+ private static int [] weight;
11+ private static boolean [][] dp;
12+
13+ public static void main (String [] args ) throws IOException {
14+ init();
15+ sol();
16+ }
17+
18+ private static void init () throws IOException {
19+ N = Integer . parseInt(br. readLine());
20+ sum = 0 ;
21+
22+ weight = new int [N ];
23+ for (int i = 0 ; i < N ; i++ ) {
24+ weight[i] = Integer . parseInt(br. readLine());
25+ sum += weight[i];
26+ }
27+
28+ dp = new boolean [N / 2 + 1 ][sum + 1 ];
29+ dp[0 ][0 ] = true ;
30+ }
31+
32+ private static void sol () throws IOException {
33+ // 각 사람에 대해
34+ for (int k = 0 ; k < N ; k++ ) {
35+ for (int i = N / 2 ; i > 0 ; i-- ) {
36+ for (int j = sum; j >= weight[k]; j-- ) {
37+ if (dp[i - 1 ][j - weight[k]]) {
38+ dp[i][j] = true ;
39+ }
40+ }
41+ }
42+ }
43+
44+ int minDiff = Integer . MAX_VALUE ;
45+ int ans = 0 ;
46+ for (int j = 0 ; j <= sum; j++ ) {
47+ if (dp[N / 2 ][j]) {
48+ int diff = Math . abs(sum - j - j);
49+ if (minDiff > diff) {
50+ minDiff = diff;
51+ ans = j;
52+ }
53+ }
54+ }
55+ bw. write(Math . min(ans, sum - ans) + " " + Math . max(ans, sum - ans));
56+ bw. flush();
57+ bw. close();
58+ br. close();
59+ }
60+
61+ }
62+ ```
You can’t perform that action at this time.
0 commit comments