취준/백준

14889 - 파이썬

놀만큼논사람 2022. 5. 2. 16:24

https://www.acmicpc.net/problem/14889

 

14889번: 스타트와 링크

예제 2의 경우에 (1, 3, 6), (2, 4, 5)로 팀을 나누면 되고, 예제 3의 경우에는 (1, 2, 4, 5), (3, 6, 7, 8)로 팀을 나누면 된다.

www.acmicpc.net


import sys

input = sys.stdin.readline

def update():
     global ret
     team1,team2 = 0,0
     for i in range(a):
          for j in range(a):
               if pick[i] and pick[j]:
                    team1 += b[i][j]
               elif not pick[i] and not pick[j]:
                    team2 += b[i][j]
     ret = min(ret,abs(team1-team2))

def solution(cur,pick_count):
     if pick_count ==a//2:
          update()
          return

     for i in range(cur,a):
          pick[i]=1
          solution(i+1,pick_count+1)
          pick[i]=0

a = int(input())
b=[list(map(int,input().split())) for _ in range(a)]
pick=[0] * a
ret=1e9  
solution(0,0)
print(ret)