import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class boj15661 {
static int N;
static int[][] stat;
static int minimum = Integer.MAX_VALUE;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
stat = new int[N][N];
for(int j = 0; j < N; j++){
st = new StringTokenizer(br.readLine());
for(int i = 0; i < N; i++){
stat[i][j] = Integer.parseInt(st.nextToken());
}
}
DFS(0, new boolean[N]);
System.out.println(minimum);
}
static void DFS(int D, boolean[] visited) {
if (D == N) {
int S = 0, L = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (visited[i] && visited[j]) {
S += stat[i][j];
} else if (!visited[i] && !visited[j]) {
L += stat[i][j];
}
}
}
minimum = Math.min(minimum, Math.abs(S - L));
} else {
// D 선수를 포함하는 경우
visited[D] = true;
DFS(D + 1, visited);
// D 선수를 포함하지 않는 경우
visited[D] = false;
DFS(D + 1, visited);
}
}
}
Leave a comment