/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 21ms 3.066 MiB
#2 Time Exceeded ≥1006ms ≥3.129 MiB
#3 Time Exceeded ≥1006ms ≥3.02 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
Submission
Problem
P1076 Even Odd GCD (Easy Version)
Contest
Bangladesh 2.0
Language
Python 3 (Python 3.12.3)
Submit At
2024-08-16 16:25:38
Judged At
2024-11-11 03:14:49
Judged By
Score
1
Total Time
≥1006ms
Peak Memory
≥3.129 MiB