/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 15ms 3.113 MiB

Code

from itertools import permutations
from math import gcd

def max_gcd_sum(arr):
    n = len(arr)
    max_sum = 0
    for perm in permutations(arr):
        odd_gcd = perm[0]
        even_gcd = perm[1]
        for i in range(2, n):
            if i % 2 == 0:
                even_gcd = gcd(even_gcd, perm[i])
            else:
                odd_gcd = gcd(odd_gcd, perm[i])
        max_sum = max(max_sum, odd_gcd + even_gcd)
    return max_sum

def main():
    t = int(input())
    for _ in range(t):
        n = int(input())
        arr = list(map(int, input().split()))
        print(max_gcd_sum(arr))

if __name__ == "__main__":
    main()

Information

Submit By
Type
Pretest
Problem
P1076 Even Odd GCD (Easy Version)
Language
Python 3 (Python 3.12.3)
Submit At
2024-08-16 16:25:35
Judged At
2024-11-11 03:14:49
Judged By
Score
10
Total Time
15ms
Peak Memory
3.113 MiB