Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class Main {
- public static void main(String[] args) throws Exception {
- Scanner sc = new Scanner(System.in);
- int n = sc.nextInt();
- int[][] s = new int[n][n];
- for (int a = 0; a < n; a++) {
- for (int b = 0; b < n; b++) {
- s[a][b] = sc.nextInt();
- }
- }
- int answer = 200000;
- for (int a = 0; a < 1 << (n); a++) {
- boolean[] bool = new boolean[n];
- int count = 0;
- for (int b = 0; b < n; b++) {
- if ((a & 1 << b) != 0) {
- bool[b] = true;
- count++;
- }
- }
- if (n - count == count) {
- int team0 = 0;
- int team1 = 0;
- for (int c = 0; c < n; c++) {
- for (int d = 0; d < n; d++) {
- if (bool[c] == bool[d]) {
- if (!bool[c]) {
- team0 += s[c][d];
- } else {
- team1 += s[c][d];
- }
- }
- }
- }
- answer = Math.min(answer, Math.abs(team1 - team0));
- }
- }
- System.out.println(answer);
- sc.close();
- }
- }
Add Comment
Please, Sign In to add comment