Untitled

 avatar
unknown
plain_text
13 days ago
634 B
3
Indexable
import math
import functools

N = int(input())
a = list(map(int, input().split()))

mm = (1 << N) - 1

@functools.lru_cache(maxsize=1048578)
def get(mask):
    if mask == 0:
        return 0
    rnd = N - bin(mask).count('1')
    rnd = rnd // 2  + 1
    ans = 0
    elems = [i for i in range(N) if ((1 << i) & mask)]
    for i1 in range(len(elems)):
        for j1 in range(i1 + 1, len(elems)):
            i = elems[i1]
            j = elems[j1]
            curr = math.gcd(a[i],a[j]) * rnd
            new_mask = mask ^ ( 1<< i) ^ ( 1<< j)
            ans = max(ans, curr + get(new_mask))
    return ans

print(get(mm))
            
Editor is loading...
Leave a Comment