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):
N = int(input())
A = list(map(int,input().split()))

debug = True

while True:
    changed = False
    i = 1
    while i < N - 1:
        if (A[i] > 0) and (A[i - 1] < 0) and (A[i + 1] < 0):
            A[i] = A[i] * -1
            A[i - 1] = A[i - 1] * -1
            changed = True
            if debug: print("change 0:" + str(A))
            i = i + 3
        else:
            i = i + 1
    for i in range(N - 1):
        if (A[i] < 0) and (A[i + 1] < 0):
            A[i] = A[i] * -1
            A[i + 1] = A[i + 1] * -1
            changed = True
            if debug: print("change 1:" + str(A))
    for i in range(N - 1):
        if (A[i] < 0):
            val = A[i] * -1
            if (i > 0):
                if A[i + 1] < A[i - 1]:
                    if val > A[i + 1]:
                        A[i] = A[i] * -1
                        A[i + 1] = A[i + 1] * -1
                        changed = True
                        if debug: print("change 2:" + str(A))
                else:
                    if val > A[i - 1]:
                        A[i] = A[i] * -1
                        A[i - 1] = A[i - 1] * -1
                        changed = True
                        if debug: print("change 3:" + str(A))
            else:
                if val > A[i + 1]:
                    A[i] = A[i] * -1
                    A[i + 1] = A[i + 1] * -1
                    changed = True
                    if debug: print("change 4:" + str(A))
    if (not changed):
        break


print(sum(A))



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

debug = True

while True:
    changed = False
    i = 1
    while i < N - 1:
        if (A[i] > 0) and (A[i - 1] < 0) and (A[i + 1] < 0):
            A[i] = A[i] * -1
            A[i - 1] = A[i - 1] * -1
            changed = True
            if debug: print("change 0:" + str(A))
            i = i + 3
        else:
            i = i + 1
    for i in range(N - 1):
        if (A[i] < 0) and (A[i + 1] < 0):
            A[i] = A[i] * -1
            A[i + 1] = A[i + 1] * -1
            changed = True
            if debug: print("change 1:" + str(A))
    for i in range(N - 1):
        if (A[i] < 0):
            val = A[i] * -1
            if (i > 0):
                if A[i + 1] < A[i - 1]:
                    if val > A[i + 1]:
                        A[i] = A[i] * -1
                        A[i + 1] = A[i + 1] * -1
                        changed = True
                        if debug: print("change 2:" + str(A))
                else:
                    if val > A[i - 1]:
                        A[i] = A[i] * -1
                        A[i - 1] = A[i - 1] * -1
                        changed = True
                        if debug: print("change 3:" + str(A))
            else:
                if val > A[i + 1]:
                    A[i] = A[i] * -1
                    A[i + 1] = A[i + 1] * -1
                    changed = True
                    if debug: print("change 4:" + str(A))
    if (not changed):
        break


print(sum(A))



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

minus_cnt = 0
abs_sum = 0
min_abs_val = 1000000000
for i in range(N):
    val = A[i]
    abs_val = abs(val)
    abs_sum += abs_val
    if abs_val < min_abs_val:
        min_abs_val = abs_val
    if val < 0:
        minus_cnt += 1

B_max_sum = abs_sum
if minus_cnt % 2 != 0:
    B_max_sum -= (min_abs_val * 2)

print(B_max_sum)



Verdicts: Runtime Error

Runtime Error

Accepted