Problem: There are N integers, A_1, A_2, ..., A_N, arranged in a row in this order.  You can perform the following operation on this integer sequence any number of times:  **Operation** : Choose an integer i satisfying 1 \leq i \leq N-1. Multiply both A_i and A_{i+1} by -1.  Let B_1, B_2, ..., B_N be the integer sequence after your operations.  Find the maximum possible value of B_1 + B_2 + ... + B_N.

Submission attempts (in order):
def gcd(a, b):
    while b > 0:
        a, b = b, a % b
    return a


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

li = []
for i in range(n - 1):
    li.append(gcd(a[i], a[i + 1]))

    if (n - 2) != i and n != 2:
        li.append(gcd(a[i], a[i + 2]))

if(n == 2):
    print(max(a[0], a[1]))
elif (n == 3):
    li.sort()
    print(li[2])

elif n == 4:
    mae1 = li[0]
    mae2 = li[1]
    mae3 = li[2]
    mae4 = li[3]
    usiro1 = li[-1]
    usiro2 = li[-2]
    usiro3 = li[-3]
    usiro4 = li[-4]
    li.sort()

    if (li[0] == mae1 and li[1] == mae2) or (li[0] == usiro1 and li[1] == usiro2):
        print(li[2])
    else:
        print(li[3])


else:
    mae1 = li[0]
    mae2 = li[1]
    mae3 = li[2]
    mae4 = li[3]
    usiro1 = li[-1]
    usiro2 = li[-2]
    usiro3 = li[-3]
    usiro4 = li[-4]

    li.sort()

    if (li[0] == mae1 and li[1] == mae3 and li[2] == mae4) or (li[0] == usiro1 and li[1] == usiro3 and li[2] == usiro4):
        print(li[3])
    elif (li[0] == mae2 and li[0] == mae1) or (li[0] == usiro2 and li[0] == usiro1):
        print(li[2])
    else:
        print(li[4])


def gcd(a, b):
    while b > 0:
        a, b = b, a % b
    return a


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

li = []
for i in range(n - 1):
    li.append(gcd(a[i], a[i + 1]))

    if (n - 2) != i and n != 2:
        li.append(gcd(a[i], a[i + 2]))

if(n == 2):
    print(max(a[0], a[1]))
elif (n == 3):
    li.sort()
    print(li[2])

elif n == 4:
    mae1 = li[0]
    mae2 = li[1]
    mae3 = li[2]
    mae4 = li[3]
    usiro1 = li[-1]
    usiro2 = li[-2]
    usiro3 = li[-3]
    usiro4 = li[-4]
    li.sort()

    if (li[0] == mae1 and li[1] == mae2) or (li[0] == usiro1 and li[1] == usiro2):
        print(li[2])
    else:
        print(li[3])


else:
    mae1 = li[0]
    mae2 = li[1]
    mae3 = li[2]
    mae4 = li[3]
    usiro1 = li[-1]
    usiro2 = li[-2]
    usiro3 = li[-3]
    usiro4 = li[-4]

    li.sort()

    if (li[0] == mae1 and li[1] == mae3 and li[2] == mae4) or (li[0] == usiro1 and li[1] == usiro3 and li[2] == usiro4):
        print(li[3])
    elif (li[0] == mae2 and li[0] == mae1) or (li[0] == usiro2 and li[0] == usiro1):
        print(li[2])
    else:
        print(li[4])


def gcd(a, b):
    while b > 0:
        a, b = b, a % b
    return a


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

li = []
for i in range(n - 1):
    li.append(gcd(a[i], a[i + 1]))

    if (n - 2) != i and n != 2:
        li.append(gcd(a[i], a[i + 2]))

if(n == 2):
    print(max(a[0], a[1]))
elif (n == 3):
    li.sort()
    print(li[2])
elif n == 4:
    mae1 = li[0]
    mae2 = li[1]
    mae3 = li[2]
    mae4 = li[3]
    usiro1 = li[-1]
    usiro2 = li[-2]
    usiro3 = li[-3]
    usiro4 = li[-4]
    li.sort()

    if (li[0] == mae1 and li[1] == mae2) or (li[0] == usiro1 and li[1] == usiro2):
        print(li[2])
    else:
        print(li[3])
else:
    mae1 = li[0]
    mae2 = li[1]
    mae3 = li[2]
    mae4 = li[3]
    usiro1 = li[-1]
    usiro2 = li[-2]
    usiro3 = li[-3]
    usiro4 = li[-4]

    li.sort()

    if (li[0] == mae1 and li[1] == mae3 and li[2] == mae4) or (li[0] == usiro1 and li[1] == usiro3 and li[2] == usiro4):
        print(li[3])
    elif (li[0] == mae2 and li[0] == mae1) or (li[0] == usiro2 and li[0] == usiro1):
        print(li[2])
    else:
        print(li[4])


n = int(input())
a = list(map(int, input().split()))  
ans = 0

a.sort()
num = 0



for i in range(n):
    if a[i] < 0:
        num += 1


a = list(map(abs, a))
if num % 2 == 0:
    print(sum(a))
else:
    a.sort()
    print(sum(a) - a[0]*2)


Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Accepted