Problem:


Score : 400 points

Problem Statement

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.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

-10^9 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible value of B_1 + B_2 + ... + B_N.

Sample Input 1

3
-10 5 -4

Sample Output 1

19

If we perform the operation as follows:

Choose 1 as i, which changes the sequence to 10, -5, -4.

Choose 2 as i, which changes the sequence to 10, 5, 4.

we have B_1 = 10, B_2 = 5, B_3 = 4. The sum here, B_1 + B_2 + B_3 = 10 + 5 + 4 = 19, is the maximum possible result.

Sample Input 2

5
10 -4 -8 -11 3

Sample Output 2

30

Sample Input 3

11
-1000000000 1000000000 -1000000000 1000000000 -1000000000 0 1000000000 -1000000000 1000000000 -1000000000 1000000000

Sample Output 3

10000000000

The output may not fit into a 32-bit integer type.



Sample input:
3
-10 5 -4


Sample output:
19

Submission status: Wrong Answer

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

plus=0
zero=0
minus=0

for i in range(N):
    if A[i]>0:
        plus+=1
    elif A[i]<0:
        minus+=1
    else:
        zero+=1
ans=0
m=10*9+10
for i in range(N):
    ans+=abs(A[i])
    m=min(m,abs(A[i]))


if minus%2==0 or zero>0:
    pass
else:
    ans=ans-2*m

print(ans)

Submission status: Wrong Answer

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

souwa = 0
cnt = 0
keep = 1e9

for i in range(N):
    if A[i] < 0:
        cnt += 1
        A[i] *= (-1)

    souwa += A[i]
    keep = min(keep, A[i])

print(souwa - 2 * keep * (cnt % 2))

Submission status: Wrong Answer

Solution code:
import numpy as np

n=int(input())
A=np.array(list(map(int,input().split())))
A = np.abs(A)

if 0 in A:
  print(np.sum(A))
else:
  print(np.sum(A) - np.min(A))

Submission status: Wrong Answer

Solution code:
import sys


stdin = sys.stdin
def ns(): return stdin.readline().rstrip()
def ni(): return int(stdin.readline().rstrip())
def nm(): return map(int, stdin.readline().split())
def nl(): return list(map(int, stdin.readline().split()))


def main():
    n = ni()
    A = nl()
    for i in range(n - 1):
        if A[i] <= 0 and -A[i] >= A[i + 1]:
            A[i] *= -1
            A[i + 1] *= -1
    print(sum(A))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(int, input().split()))
cnt = 0
sum_a = 0
min_a = A[0]
for i in range(n):
    if A[i] <= 0:
        cnt += 1
        A[i] = A[i]*(-1)
    sum_a += A[i]
    if min_a > A[i]:
        min_a = A[i]
if cnt % 2 == 0:
    for a in A:
        ans = sum_a
else:
    ans = sum_a - 2*min_a
print(ans)

Submission status: Wrong Answer

Solution code:
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():






    n = I()
    a = LI()

    ans = 0
    mn = 0

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

    if (mn %2 == 0):
        ans = sum(a)
    else:
        ans = sum(a)-min(a)

    print(ans)

                    


main()

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[int(i) for i in input().split()]
x=0
b=[]
for i in a:
  if i<0: x=(x+1)%2
  b.append(abs(i))
b.sort()
print(sum(b)-b[0] if  x else sum(b))

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[int(i) for i in input().split()]
for i in range(3):
    for j in range(n-1):
        x=a[j]+a[j+1]
        if x<0:
            a[j]*=-1
            a[j+1]*=-1
print(sum(a))

Submission status: Wrong Answer

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

ans, cnt = 0, 0
for i, v in enumerate(a):
    ans += abs(v)
    if v == 0:
        cnt = 0
    elif v < 0:
        cnt += 1
if cnt % 2 == 1:
    ans -= 2*min(abs(a[-1]), abs(a[-2]))
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = sum(a<0 for a in A)
C = sum(a==0 for a in A)
D = sum(abs(a) for a in A)

if B%2==0 or 1<=C:
  print(D)
else:
  print(D-2*min(A,key=abs))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = sum(a<0 for a in A)
C = sum(a==0 for a in A)
D = sum([abs(a) for a in A])

if 2<=C:
  print(D)
else:
  if B%2==0:
    print(D)
  else:
    print(D-2*min(A,key=abs))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = sum(a<0 for a in A)
C = sum(a==0 for a in A)
D = sum([abs(a) for a in A])

if 2<=C:
  print(D)
else:
  if B%2==0:
    print(C)
  else:
    print(C-2*min(A,key=abs))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = sum([a<0 for a in A])
C = sum([abs(a) for a in A])

if B%2==0:
  print(C)
else:
  print(C-2*min(A,key=abs))

Submission status: Wrong Answer

Solution code:
import sys
from sys import exit
from collections import deque
from bisect import bisect_left, bisect_right, insort_left, insort_right #func(リスト,値)
from heapq import heapify, heappop, heappush
from itertools import product, permutations, combinations, combinations_with_replacement
from functools import reduce
from math import gcd, sin, cos, tan, asin, acos, atan, degrees, radians

sys.setrecursionlimit(10**6)
INF = 10**20
eps = 1.0e-20
MOD = 10**9+7

def lcm(x,y):
    return x*y//gcd(x,y)
def lgcd(l):
    return reduce(gcd,l)
def llcm(l):
    return reduce(lcm,l)
def powmod(n,i,mod):
    return pow(n,mod-1+i,mod) if i<0 else pow(n,i,mod)
def div2(x):
    return x.bit_length()
def div10(x):
    return len(str(x))-(x==0)
def perm(n,mod=None):
    ans = 1
    for i in range(1,n+1):
        ans *= i
        if mod!=None:
            ans %= mod
    return ans
def intput():
    return int(input())
def mint():
    return map(int,input().split())
def lint():
    return list(map(int,input().split()))
def ilint():
    return int(input()), list(map(int,input().split()))
def judge(x, l=['Yes', 'No']):
    print(l[0] if x else l[1])
def lprint(l, sep='\n'):
    for x in l:
        print(x, end=sep)
def ston(c, c0='a'):
    return ord(c)-ord(c0)
def ntos(x, c0='a'):
    return chr(x+ord(c0))
class counter(dict):
    def __init__(self, *args):
        super().__init__(args)
    def add(self,x,d=1):
        self.setdefault(x,0)
        self[x] += d
    def list(self):
        l = []
        for k in self:
            l.extend([k]*self[k])
        return l
class comb():
    def __init__(self, n, mod=None):
        self.l = [1]
        self.n = n
        self.mod = mod
    def get(self,k):
        l,n,mod = self.l, self.n, self.mod
        k = n-k if k>n//2 else k
        while len(l)<=k:
            i = len(l)
            l.append(l[i-1]*(n+1-i)//i if mod==None else (l[i-1]*(n+1-i)*powmod(i,-1,mod))%mod)
        return l[k]
def pf(x):
    C = counter()
    p = 2
    while x>1:
        k = 0
        while x%p==0:
            x //= p
            k += 1
        if k>0:
            C.add(p,k)
        p = p+2-(p==2) if p*p<x else x
    return C

N,A=ilint()
B=[abs(a) for a in A]
print(sum(B)-2*min(B))

Submission status: Wrong Answer

Solution code:
import sys
from sys import exit
from collections import deque
from bisect import bisect_left, bisect_right, insort_left, insort_right #func(リスト,値)
from heapq import heapify, heappop, heappush
from itertools import product, permutations, combinations, combinations_with_replacement
from functools import reduce
from math import gcd, sin, cos, tan, asin, acos, atan, degrees, radians

sys.setrecursionlimit(10**6)
INF = 10**20
eps = 1.0e-20
MOD = 10**9+7

def lcm(x,y):
    return x*y//gcd(x,y)
def lgcd(l):
    return reduce(gcd,l)
def llcm(l):
    return reduce(lcm,l)
def powmod(n,i,mod):
    return pow(n,mod-1+i,mod) if i<0 else pow(n,i,mod)
def div2(x):
    return x.bit_length()
def div10(x):
    return len(str(x))-(x==0)
def perm(n,mod=None):
    ans = 1
    for i in range(1,n+1):
        ans *= i
        if mod!=None:
            ans %= mod
    return ans
def intput():
    return int(input())
def mint():
    return map(int,input().split())
def lint():
    return list(map(int,input().split()))
def ilint():
    return int(input()), list(map(int,input().split()))
def judge(x, l=['Yes', 'No']):
    print(l[0] if x else l[1])
def lprint(l, sep='\n'):
    for x in l:
        print(x, end=sep)
def ston(c, c0='a'):
    return ord(c)-ord(c0)
def ntos(x, c0='a'):
    return chr(x+ord(c0))
class counter(dict):
    def __init__(self, *args):
        super().__init__(args)
    def add(self,x,d=1):
        self.setdefault(x,0)
        self[x] += d
    def list(self):
        l = []
        for k in self:
            l.extend([k]*self[k])
        return l
class comb():
    def __init__(self, n, mod=None):
        self.l = [1]
        self.n = n
        self.mod = mod
    def get(self,k):
        l,n,mod = self.l, self.n, self.mod
        k = n-k if k>n//2 else k
        while len(l)<=k:
            i = len(l)
            l.append(l[i-1]*(n+1-i)//i if mod==None else (l[i-1]*(n+1-i)*powmod(i,-1,mod))%mod)
        return l[k]
def pf(x):
    C = counter()
    p = 2
    while x>1:
        k = 0
        while x%p==0:
            x //= p
            k += 1
        if k>0:
            C.add(p,k)
        p = p+2-(p==2) if p*p<x else x
    return C

N,A=ilint()
print(sum(A)-2*min([abs(a) for a in A]))

Submission status: Wrong Answer

Solution code:
def main():
  n=int(input())
  a=list(map(int,input().split()))
  dp=[0]*2
  for i in range(n):
    dp2=[max(dp[0]+a[i],dp[1]-a[i]),max(dp[0]-a[i],dp[1]+a[i])]
    dp=dp2
  print(max(dp))
main()

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
for i in range(n-1):
    if a[i] < 0:
        a[i] *= -1
        a[i+1] *= -1
    elif a[i] == 0:
        if a[i+1] < 0:
            a[i+1] *= -1
if (a[-1] + a[-2]) < 0:
    a[-1] *= -1
    a[-2] *= -1
print(sum(a))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
for i in range(n-1):
    if a[i] < 0:
        a[i] *= -1
        a[i+1] *= -1
    elif a[i] == 0:
        if a[i+1] < 0:
            a[i+1] *= -1
print(sum(a))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
num = 0
mi = 10**9
for i in range(n):
    num += abs(a[i])
    if abs(mi) >= abs(a[i]):
        mi = a[i]
cnt = 0
for i in range(n):
    if a[i] < 0:
        cnt += 1
if cnt%2 == 1:
    print(num-2*mi)
else:
    print(num)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
cnt = 0
if n == 2 and sum(a) >= 0:
    print(abs(sum(a)))
elif n == 2:
    print(max(abs(a[0]),abs(a[1])))
else:
    l3 = abs(a[-3])
    l2 = abs(a[-2])
    l1 = abs(a[-1])
    last = l3 + l2 + l1
    for i in range(n-3):
        if a[i] < 0:
            a[i] *= -1
            a[i+1] *= -1
        cnt += a[i]
    # +,+,+ or +,-,- or -,-,+ or -,+,-
    #print(cnt,a[-3],a[-2],a[-1])
    if (a[-3] >= 0 and a[-2] >= 0 and a[-1] >= 0) or (a[-3] >= 0 and a[-2] <= 0 and a[-1] <= 0) or (a[-3] <= 0 and a[-2] <= 0 and a[-1] >= 0) or (a[-3] <= 0 and a[-2] >= 0 and a[-1] <= 0):
        print(cnt+last)
    elif (a[-3] <= 0 and a[-2] >= 0 and a[-1] >= 0): # -,+,+
        print(cnt + last - 2*min(l1,l2,l3))
    else:
        print(cnt + last - 2*min(l1,l2))

Submission status: Wrong Answer

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

while True:
    changed = False
    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
            #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
                        #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
                        #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
                    #print("change 4:" + str(A))
    if (not changed):
        break


print(sum(A))

Submission status: Wrong Answer

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

a.sort()
minus = n
for i in range(n):
  if a[i] >= 0:
    s = i
    break

a = list(map(abs, a))
a.sort()

if minus % 2 == 0:
  print(sum(a))
else:
  print(sum(a) - 2 * a[0])

Submission status: Wrong Answer

Solution code:
N = int(input())
*A, = map(int, input().split())

cnt_neg = sum(a < 0 for a in A)
cnt_zero = A.count(0)

if cnt_zero or cnt_neg % 2 == 0:
    print(sum(abs(a) for a in A))
else:
    loss = float('inf')
    for i, a in enumerate(A):
        if a > 0:
            continue
        loss = min(loss, -2*a)
        if i > 0 and A[i-1] > 0:
            loss = min(loss, 2*A[i-1])
        if i < N-1 and A[i+1] > 0:
            loss = min(loss, 2*A[i+1])
    print(sum(abs(a) for a in A)-loss)

Submission status: Wrong Answer

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

a.sort()
s=sum(a)
ans=[]
N=n//2

for i in range(1,N+1):
  x=i*2-1
  y=i*2
  s+=a[x-1]*-2
  s+=a[y-1]*-2
  ans.append(s)
  
print(max(ans))

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().split()))
B = []
for a in A:
    B.append(abs(a))
place = B.index(min(B))
answer2 = 0
for n in range(N):
    if n == place:
        answer2 -= abs(A[n])
    else:
        answer2 += abs(A[n])
if 0 in A:
    answer=0
    for a in A:
        answer+=abs(a)
    print(answer)
else:
    for n in range(N - 1):
        if abs(A[n]) > abs(A[n + 1]) and A[n] < 0:
            A[n] = A[n] * (-1)
            A[n + 1] = A[n + 1] * (-1)
        elif A[n] < 0 and A[n + 1] < 0:
            A[n] = A[n] * (-1)
            A[n + 1] = A[n + 1] * (-1)
    answer1=sum(A)
    print(max(answer1,answer2))

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().split()))
if 0 in A:
    answer=0
    for a in A:
        answer+=abs(a)
    print(answer)
else:
    for n in range(N - 1):
        if abs(A[n]) > abs(A[n + 1]) and A[n] < 0:
            A[n] = A[n] * (-1)
            A[n + 1] = A[n + 1] * (-1)
        elif A[n] < 0 and A[n + 1] < 0:
            A[n] = A[n] * (-1)
            A[n + 1] = A[n + 1] * (-1)
    print((sum(A)))

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
if sum([i<0 for i in a])%2:
  print(sum([abs(i) for i in a])-min([abs(i) for i in a]))
else:
  print(sum([abs(i) for i in a]))

Submission status: Wrong Answer

Solution code:
n = int(input())
lis = list(map(int, input().split()))

for i in range(n-1):
    if lis[i] > 0 and lis[i+1] > 0:
        continue
    elif lis[i] < 0 and lis[i+1] < 0:
        lis[i] = lis[i] * -1
        lis[i+1] = lis[i+1] * -1
    elif lis[i] > 0 and lis[i+1] < 0:
        if abs(lis[i+1])> lis[i]:
            lis[i] = lis[i] * -1
            lis[i+1] = lis[i+1] * -1
    elif lis[i] < 0 and lis[i+1] > 0:
        if abs(lis[i]) > lis[i+1]:
            lis[i] = lis[i] * -1
            lis[i+1] = lis[i+1] * -1
    elif lis[i] == 0 and lis[i+1] < 0:
        lis[i+1] = lis[i+1] * -1
    elif lis[i] < 0 and lis[i+1] == 0:
        lis[i] = lis[i] * -1

print(sum(lis))

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
dp=[[0,0]for _ in range(n)]
dp[0]=[a[0],-a[0]]
for i in range(1,n):
  dp[i][0]=max(dp[i-1][0]+a[i],dp[i-1][1]-a[i])
  dp[i][1]=max(dp[i-1][0]-a[i],dp[i-1][1]+a[i])
for i in dp:print(i)
print(dp[n-1][0])

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
dp=[[0,0]for _ in range(n)]
dp[0]=[a[0],-a[0]]
dp[1]=[a[0]+a[1],-a[0]-a[1]]
for i in range(2,n):
  dp[i][0]=max(dp[i-1])+a[i]
  dp[i][1]=max(max(dp[i-2])+a[i-1]-a[i],max(dp[i-2])-a[i-1]-a[i])
print(max(dp[n-1]))

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
dp=[[0,0]for _ in range(n)]
dp[0]=[a[0],-a[0]]
dp[1]=[a[0]+a[1],-a[0]-a[1]]
for i in range(2,n):
  dp[i][0]=max(dp[i-1])+a[i]
  dp[i][1]=max(max(dp[i-2])-a[i-1]-a[i],dp[i-2][1]+a[i-1]-a[i])
print(max(dp[n-1]))

Submission status: Wrong Answer

Solution code:
import sys
import math
import itertools
import collections
from collections import deque
from collections import defaultdict

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()

NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()

def main():
    N = NI()
    A = NLI()
    
    cnt = 0
    minimum_abs = 10**9
    ans = 0
    flag = 0
    
    for n in range(N):
        ans += abs(A[n])
        minimum_abs = abs(A[n])
        if A[n] < 0:
            cnt += 1
        elif A[n] == 0:
          flag = 1

                
    if cnt % 2 == 0 or flag == 1:
        print(ans)
    else:
        print(ans - minimum_abs * 2)
            


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
import math
import itertools
import collections
from collections import deque
from collections import defaultdict

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()

NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()

def main():
    N = NI()
    A = NLI()
    
    cnt = 0
    minimum_abs = 10**9
    ans = 0
    
    for n in range(N):
        ans += abs(A[n])
        minimum_abs = abs(A[n])
        if A[n] < 0:
            cnt += 1

                
    if cnt % 2 == 0:
        print(ans)
    else:
        print(ans - minimum_abs * 2)
            


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

cnt=0
ans=0

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

print(a)



if cnt%2==0:
    ans=sum(a)

else:
    ans=sum(a)-2*min(a)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from functools import reduce
from bisect import bisect_left, insort_left
from heapq import heapify, heappush, heappop

INPUT = lambda: sys.stdin.readline().rstrip()
INT = lambda: int(INPUT())
MAP = lambda: map(int, INPUT().split())
S_MAP = lambda: map(str, INPUT().split())
LIST = lambda: list(map(int, INPUT().split()))
S_LIST = lambda: list(map(str, INPUT().split()))

sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    N = INT()
    A = LIST()

    neg = 0
    for i in range(N):
        m = INF
        if A[i] < 0:
            neg += 1
            A[i] = - A[i]
        m = min(m, A[i])

    if 0 in A:
        print(sum(A))
    else:
        print(sum(A) - (2 * m)*(neg % 2))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left,bisect,bisect_right
import sys

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

a.sort()
ii=bisect(a,0)

if ii==n:
    print(-sum(a))
    sys.exit()

if ii%2==0:
    asum=-sum(a[0:ii])+sum(a[ii:])
else:
    if -a[ii-1]<a[ii]:
        d=a[ii-1]+a[ii]
    else:
        d=-a[ii-1]-a[ii]
    asum=-sum(a[0:ii-1])+d+sum(a[ii+1:])

print(asum)

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque, Counter, OrderedDict
from bisect import bisect_left, bisect_right
from functools import reduce, lru_cache
from heapq import heappush, heappop, heapify

import itertools
import math, fractions
import sys, copy

def L(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline().rstrip())
def SL(): return list(sys.stdin.readline().rstrip())
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI1(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return [list(x) for x in sys.stdin.readline().split()]
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def LIR1(n): return [LI1() for _ in range(n)]
def SR(n): return [SL() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
def LR(n): return [L() for _ in range(n)]

def perm(n, r): return math.factorial(n) // math.factorial(r)
def comb(n, r): return math.factorial(n) // (math.factorial(r) * math.factorial(n-r))

def make_list(n, *args, default=0): return [make_table(*args, default=default) for _ in range(n)] if len(args) > 0 else [default for _ in range(n)]

alphabets = "abcdefghijklmnopqrstuvwxyz"
ALPHABETS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

sys.setrecursionlimit(1000000)
dire = [[1, 0], [0, 1], [-1, 0], [0, -1]]
dire8 = [[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]]
MOD = 1000000007
INF = float("inf")

def main():
    N = I()
    A = LI()
    cnt = sum([ai for ai in A if ai < 0])
    B = [abs(ai) for ai in A]
    if cnt % 2 == 0:
        print(sum(B))
    else:
        print(sum(B) - min(B) * 2)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N = int( input() )
A = list ( int(x) for x in input().split() )

sign = []
for i in range( N ):
    if A[i] > 0:
        sign.append( 1 )
    elif A[i] == 0:
        sign.append( 0 )
    else:
        sign.append( -1 )


for i in range( N-2 ):
    if sign[i] == -1:
        sign[i] *= -1
        sign[i + 1] *= -1


ans = 0
for i in range( N - 2 ):
    ans += abs( A[i] )

if sign[ N - 2 ] * sign[ N - 1 ] == -1:
    ans += max (abs( A[ N - 2 ] ) , abs( A[ N - 1 ] ))
    ans -= min (abs( A[ N - 2 ] ) , abs( A[ N - 1 ] ))
else:
    ans += abs( A[ N - 2 ] )
    ans += abs( A[ N - 1 ] )

print( ans )

Submission status: Wrong Answer

Solution code:
N = int( input() )
A = list ( int(x) for x in input().split() )

sign = []
for i in range( N ):
    if A[i] > 0:
        sign.append( 1 )
    elif A[i] == 0:
        sign.append( 0 )
    else:
        sign.append( -1 )


for i in range( N-2 ):
    if sign[i] == -1:
        sign[i] *= -1
        sign[i + 1] *= -1


ans = 0
for i in range( N - 2 ):
    ans += abs( A[i] )

if sign[ N - 2 ] * sign[ N - 1 ] == -1:
    ans += max (abs( A[ N - 2 ] ) , abs( A[ N - 1 ] ))
    ans -= min (abs( A[ N - 2 ] ) , abs( A[ N - 1 ] ))

else:
    ans += abs( A[ N - 2 ] )
    ans += abs( A[ N - 1 ] )

print( ans )

Submission status: Wrong Answer

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

s = 0

b = [abs(x) for x in a]

if n % 2 == 0:
    s = sum(b)
else:
    s = sum(b) - 2 * min(b)

print(s)

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    A = list(map(int, input().split()))
    B = list(map(lambda x: abs(x), A))
    print(sum(B) - 2 * min(B))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

if 0 in A:
    S=0
    for a in A:
        S+=abs(a)
    print(S)
else:
    X=0
    S=0
    for a in A:
        X+=(a<0)
        S+=abs(a)

    if X%2==0:
        print(S)
    else:
        b=min(A,key=lambda x:abs(x))
        print(S-2*b)

Submission status: Wrong Answer

Solution code:

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

y = [abs(v) for v in X]
if N % 2 == 0:
    print(sum(y))
else:
    print(sum(y) - 2 * min(y))

Submission status: Wrong Answer

Solution code:

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

y = [abs(v) for v in X]
print(sum(y) - 2 * min(y))

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    a = list(map(int, input().split()))
    s, cnt = [0]*n, 0
    for i in range(n):
        s[i] = abs(a[i])
        if i < 0:
            cnt += 1
    if cnt%2:
        print(sum(s) - 2*min(s))
    else:
        print(sum(s))

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    a = list(map(int, input().split()))
    s = [abs(i) for i in a]
    print(sum(s) - 2*min(s))

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
print(sum(list(map(lambda x:abs(x), a))))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
#import bisect
#import numpy as np
#from copy import deepcopy
#from collections import deque

INF = 1 << 50



def run():
    N, *A = map(int, read().split())

    SUM = sum([abs(a) for a in A])
    if A.count(0):
        print(SUM)
        return

    do = [0]
    for a in A[:-1]:
        if not do[-1]:
            if a > 0:
                do.append(0)
            else:
                do.append(1)
        else:
            if a > 0:
                do.append(1)
            else:
                do.append(0)

    if A[-1] > 0:
        if do:
            base_ans = SUM - abs(A[-1]) * 2
        else:
            print(SUM)
            return
    else:
        if do:
            print(SUM)
            return
        else:
            base_ans = SUM - abs(A[-1]) * 2
    ans = base_ans
    for i in range(N-1):
        ans = max(ans, base_ans - abs(A[i]) * 2 + abs(A[-1]) * 2)

    print(ans)





if __name__ == "__main__":
    run()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
#import bisect
#import numpy as np
#from copy import deepcopy
#from collections import deque

INF = 1 << 50



def run():
    N, *A = map(int, read().split())

    SUM = sum([abs(a) for a in A])
    if A.count(0):
        print(SUM)
        return

    do = [0]
    for a in A:
        if not do[-1]:
            if a > 0:
                do.append(0)
                continue
            else:
                do.append(1)
        else:
            if a > 0:
                do.append(1)
            else:
                do.append(0)

    if A[-1] > 0:
        if do:
            base_ans = SUM - abs(A[-1]) * 2
        else:
            print(SUM)
            return
    else:
        if do:
            print(SUM)
            return
        else:
            base_ans = SUM - abs(A[-1]) * 2
    ans = base_ans
    for i in range(N-1):
        ans = max(ans, base_ans - abs(A[i]) * 2 + abs(A[-1]) * 2)

    print(ans)





if __name__ == "__main__":
    run()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
#import bisect
#import numpy as np
#from copy import deepcopy
#from collections import deque

INF = 1 << 50



def run():
    N, *A = map(int, read().split())

    SUM = sum([abs(a) for a in A])
    if A.count(0):
        print(SUM)
        return

    do = [0]
    for a in A:
        if not do[-1]:
            if a > 0:
                do.append(0)
                continue
            else:
                do.append(1)
        else:
            if a > 0:
                do.append(1)
            else:
                do.append(0)

    if A[-1] > 0:
        if do:
            base_ans = SUM - A[-1] * 2
        else:
            print(SUM)
            return
    else:
        if do:
            print(SUM)
            return
        else:
            base_ans = SUM - A[-1] * 2
    ans = base_ans
    for i in range(N-1):
        ans = max(ans, base_ans - abs(A[i]) * 2 - abs(A[i+1]) * 2 + abs(A[-1]))
        
    print(ans)





if __name__ == "__main__":
    run()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
#import bisect
#import numpy as np
#from copy import deepcopy
#from collections import deque

INF = 1 << 50



def run():
    N, *A = map(int, read().split())

    SUM = sum([abs(a) for a in A])
    if A.count(0):
        print(SUM)
        return

    do = [0]
    for a in A:
        if not do[-1]:
            if a > 0:
                do.append(0)
                continue
            else:
                do.append(1)
        else:
            if a > 0:
                do.append(1)
            else:
                do.append(0)

    if A[-1] > 0:
        if do:
            ans = SUM - A[-1] * 2
        else:
            print(SUM)
            return
    else:
        if do:
            print(SUM)
            return
        else:
            ans = SUM - A[-1] * 2

    for i in range(N-1):
        ans = max(ans, ans - abs(A[i]) * 2 - abs(A[i+1]) * 2 + abs(A[-1]))
    print(ans)





if __name__ == "__main__":
    run()