题目描述
kotori拿到了一些正整数。她决定从每个正整数取出一个素因子。但是,kotori有强迫症,她不允许两个不同的正整数取出相同的素因子。
她想知道,最终所有取出的数的和的最小值是多少?
n = int(input())
nums = list(map(int, input().split()))
ans = float('inf')
def dfs(i, tol, s):
global ans
if i == n:
ans = min(ans, tol)
return
for x in range(2, nums[i] + 1):
if nums[i] % x == 0 and is_prime(x) and x not in s:
s.add(x)
dfs(i + 1, tol + x, s)
s.remove(x)
def is_prime(x):
if x <= 1:
return False
for i in range(2, int(x ** (0.5)) + 1):
if x % i == 0:
return False
return True
dfs(0, 0, set())
print(ans if ans != float('inf') else -1)