728x90
반응형

브루트포스 알고리즘 문제이다. combinations를 사용해서 모든 조합을 다 확인하는 방식으로 최솟값을 찾았다.

from itertools import combinations

N = int(input())
arr = [list(map(int, input().split(" "))) for _ in range(N)]
coms = [combinations(arr, i) for i in range(1, N+1)]
ans = 1000000000
for com in coms:
    for t in com:
        S, B = 1, 0
        for s, b in t:
            S *= s
            B += b
        ans = min(ans, abs(S-B))
print(ans)
728x90
반응형

+ Recent posts