File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.* ;
3+ import java.util.* ;
4+
5+ public class Main {
6+
7+ static int N ;
8+ static int [][] S ;
9+ static boolean [] startTeam;
10+ static int minDiff = Integer . MAX_VALUE ;
11+
12+ public static void main (String [] args ) throws IOException {
13+ BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
14+ StringTokenizer st;
15+ N = Integer . parseInt(br. readLine());
16+ S = new int [N ][N ];
17+
18+ for (int i = 0 ; i < N ; i++ ) {
19+ st = new StringTokenizer (br. readLine());
20+ for (int j = 0 ; j < N ; j++ ) {
21+ S [i][j] = Integer . parseInt(st. nextToken());
22+ }
23+ }
24+
25+ startTeam = new boolean [N ];
26+
27+ divide(0 );
28+
29+ System . out. println(minDiff);
30+ }
31+
32+ static void divide (int idx ) {
33+ if (idx == N ) {
34+ calculate();
35+ return ;
36+ }
37+
38+ startTeam[idx] = true ;
39+ divide(idx + 1 );
40+
41+ startTeam[idx] = false ;
42+ divide(idx + 1 );
43+ }
44+
45+ static void calculate () {
46+ int startSum = 0 ;
47+ int linkSum = 0 ;
48+
49+ for (int i = 0 ; i < N ; i++ ) {
50+ for (int j = i + 1 ; j < N ; j++ ) {
51+ if (startTeam[i] && startTeam[j]) {
52+ startSum += S [i][j] + S [j][i];
53+ } else if (! startTeam[i] && ! startTeam[j]) {
54+ linkSum += S [i][j] + S [j][i];
55+ }
56+ }
57+ }
58+
59+ int diff = Math . abs(startSum - linkSum);
60+
61+ if (diff == 0 ) {
62+ System . out. println(0 );
63+ System . exit(0 );
64+ }
65+
66+ minDiff = Math . min(minDiff, diff);
67+ }
68+ }
69+ ```
You can’t perform that action at this time.
0 commit comments