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: Runtime Error

Solution code:
N=int(input())
A=list(map(int,input().split(" ")))
A_p = [ a for a in A if a > 0 ]
A_n = [ -1*a for a in A if a < 0 ]
A_0 = [ a for a in A if a == 0 ]

l_p = len(A_p)
l_n = len(A_n)
l_0 = len(A_0)

if l_p == 0:
    if l_n%2 == 0:
        print(sum(A_n.append(0)))
    if l_0 > 0:
        print(sum(A_n.append(0)))
    else:
        tmp_n=min(A_n)
        A_n.remove(tmp_n)
        print(sum(A_n.append(0))-tmp_n)
elif l_n == 0:
    print(sum(A_p.append(0)))

elif l_n%2 == 0:
    print(sum(A_p)+sum(A_n))
else:
    if l_0 > 0:
        print(sum(A_p)+sum(A_n))
    else:
        if min(A_n) > min(A_p):
            tmp_n=min(A_p)
            A_p.remove(tmp_n)
            print(sum(A_p.append(0))+sum(A_n)-tmp_n)    
        else:
            tmp_n=min(A_n)
            A_n.remove(tmp_n)
            print(sum(A_p)+sum(A_n.append(0))-tmp_n)

Submission status: Runtime Error

Solution code:
N = int(input())
a = input().split()
num=[]
tex=[]
count
for i in range(N):
	num.append(int(a[i]))
	if num[i] < 0 :
		count +=1
	elif num[i] == 0:
		count2 +=1
if count%2 ==0 or count2 > 0:
  res = sum(num)
else:tex=[i for i, x in enumerate(num) if x <0]
  res =sum(num)-min(tex[0],tex[-1])
print(res)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
a.sort()
max = sum(a)
A = [i for i in a if i < 0 ]
MIN = (min(p for p in a if p > 0))
A.append(MIN)
for i in range(0, len(A)-1):
    A[i] *= -1
    A[i+1] *= -1
    if max < sum(A):
        max  = sum(A)
        A[i] *= -1
        A[i + 1] *= -1
    A[i] *= -1
    A[i+1] *= -1
MAX = sum(p for p in a if p > 0 and p != MIN)
max += MAX
print(max)

Submission status: Runtime Error

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

ret = 0
for i in range(len(a)-1):
    if a[i] < 0:
        a[i] *= -1
        a[i+1] *= -1
    ret += a[i]
    print(a[i], ret)
ret += a[-1]

abs_list = list(map(abs, a))
test = sorted(enumerate(abs_list), key=lambda x:x[1])
print(max(solve(a), solve(a[::-1]), solve(a[::-1], test[0][0]), solve(a[::-1], test[1][0])))

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
count=0
countk=0
saidaiti=0
while True:
    saidaiti=saidaiti+abs(a[count])
    count=count+1
    if a[count]%2==1:
        countk=countk+1
    if count==n:
        break
if countk%2==1:
    saidaiti=saidaiti-2*a[-1]
print(saidaiti)

Submission status: Runtime Error

Solution code:
import numpy as np
n = int(input())
a = np.array(list(map(int, input().split())))

if 0 in a:
  b = list(map(abs, a))
  print(sum(b))
  
else:
  if (a[a<0].size % 2) == 0:
    b = list(map(abs, a))
    print(sum(b))
  else:
    b = list(map(abs, a))
    c = sorted(b).reverse()
    d = c.pop()
    print(sum(c))

Submission status: Runtime Error

Solution code:
n = int(input())
w = input().split(" ")

a = []
k = False
for i in range(n):
    a.append(int(w[i]))
    if a[i] < 0:
        a[i] *= -1
        k = not(k)
    if i == 1:
        min = a[i]
    if a[i] < min:
        min = a[i]

sum = sum(a)
if k:
    sum -= min
print(sum)

Submission status: Runtime Error

Solution code:
n = int(input())
a =[int(i) for i in input().split()]
aa = sum(abs(a))
print(aa)

Submission status: Runtime Error

Solution code:
#1行に1つと
#1行に複数行の読み込み方

n = int(input())
a = list(map(int, input().split()))
sum = 0
minus_count = 0
zero_flag = 0
min_ind = 0

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

if zero_flag ==1:
	for i in range(n):
		sum += abs(a[i])
elif minus_count % 2 == 0:
	for i in range(n):
		sum += abs(a[i])
else:
	for i in range(n):
	min_ind = a[a.index(min(a))]
		sum += abs(a[i])
		sum -= abs(a[min_ind])

print(sum)

Submission status: Runtime Error

Solution code:
import numpy as np

n = int(input())
l = np.array([int(x) for x in input().split()])
ans = 0
if 0 in l:
  for i in range(n):
    ans += abs(l[i])
  sys.exit()
m = abs(l[0])
for i in range(n):
  if l[i] < 0:
    p += 1
  if m < abs(l[i])
for i in range(n):
  ans += abs(l[i])
if p % 2 == 0:
  print(ans)
else:
  print(ans - m*2)

Submission status: Runtime Error

Solution code:
from sys import stdin
import numpy as np
N=int(stdin.readline().rstrip())
data=[int(x) for x in stdin.readline().rstrip().split()]
data=np.array(data)
ans=0
cnt=0
for i in data[data > 0]:
    ans+=i
for j in data[data < 0][0:-1]:
    ans+=abs(j)
if len(data[data<0]) %2 ==0:
    ans+=abs(data[data<0][-1])
print(ans)

Submission status: Runtime Error

Solution code:
N=int(input())
A=list(map(int,input().split(" ")))
A_p = [ a for a in A if a > 0 ]
A_n = [ -1*a for a in A if a < 0 ]
A_0 = [ a for a in A if a == 0 ]

l_p = len(A_p)
l_n = len(A_n)
l_0 = len(A_0)

if l_p == 0:
    if l_n%2 == 0:
        print(sum(A_n.append(0)))
    if l_0 > 0:
        print(sum(A_n.append(0)))
    else:
        tmp_n=min(A_n)
        A_n.remove(tmp_n)
        print(sum(A_n.append(0))-tmp_n)

elif l_n == 0:
    print(sum(A_p.append(0)))

    

if l_n%2 == 0:
    print(sum(A_p)+sum(A_n))
else:
    if l_0 > 0:
        print(sum(A_p)+sum(A_n))
    else:
        if min(A_n) > min(A_p):
            tmp_n=min(A_p)
            A_p.remove(tmp_n)
            print(sum(A_p.append(0))+sum(A_n)-tmp_n)    
        else:
            tmp_n=min(A_n)
            A_n.remove(tmp_n)
            print(sum(A_p)+sum(A_n.append(0))-tmp_n)

Submission status: Runtime Error

Solution code:
import sys
import numpy as np

N = int(input())

A = input().split()
A = np.array([int(x) for x in A])

if np.sum(A==0) != 0:
# Aに0が含まれる場合
    # 最後に0が出てくる場所
    z = np.max(np.where(A==0))
    B = A[z:N]

    # B = [0 -?]のときだけ例外
    if B.shape[0] == 2 and B[-1] < 0:
        sign = 1
        break

    elif np.sum(B < 0)%2 ==0:
        sign = 1
    else:
        sign = -1

# Aに0が含まれない場合
else:
    if np.sum(A < 0)%2 ==0:
        sign = 1
    else:
        sign = -1

print(np.abs(A[0:N-1]).sum() + sign*abs(A[-1]))

Submission status: Runtime Error

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

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

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

ans = sum(a_list)

min_list = []
for i in range(N):
    if a_list[i] < 0:
        min_list.append(a_list[i])

if len(min_list) % 2 == 0:
    abc = sum(min_list)
    abc *= -1
    print(ans + 2*abc)

if len(min_list) % 2 == 1 and len(min_list) !=1 :
    abc.sort()
    min_list = min_list[:-1]
    abc = sum(min_list)
    abc *= -1
    print(ans + 2*abc)
else:
    print(ans)

Submission status: Runtime Error

Solution code:
import copy

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

plus = [i for i in A if i >= 0]
minus = [i for i in A if i < 0]

M = len(minus)


if M % 2 == 0:
  ans = sum(A) + abs(sum(minus))*2
elif M % 2 == 1:
  minus2 = copy.copy(minus)
  minus2.pop(-1)
  tmp = sum(A) + abs(sum(minus2))*2
  tmp2 = sum(A) + abs(sum(minus))*2 - min(plus) * 2
  ans = max(tmp, tmp2)
print(ans)

Submission status: Runtime Error

Solution code:
import sys
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
import math
from functools import reduce


def input(): return sys.stdin.readline().strip()


def INT(): return int(input())


def MAP(): return map(int, input().split())


def LIST(): return list(map(int, input().split()))


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

n = INT()

a = LIST()

ct = 0

for i, x in enumerate(a):
    if x <= 0:
        ct += 1
        if x < 0:
            p = [i, x]
        else:
            zero = i
            ct = 0

"print(p)"

lis = [abs(x) for x in a]
"""print(ct, sum(lis))"""

if ct % 2 == 0:
    print(sum(lis))
else:
    if p[0] > zero:
        if p[0] == n-1:
            print(sum(lis) - 2 * min(abs(p[1]), abs(a[p[0] - 1])))
        else:
            print(sum(lis) - 2 * min(abs(p[1]), abs(a[p[0] + 1])))
    else:
        print(sum(lis))

Submission status: Runtime Error

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 i == N - 2 and A[i] == 0:
    A[i + 1] *= -1
    A[i + 2] *= -1
print(sum(A))

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
import math

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



if 0 in A:
    
   # print (A)
    print (sum(list(map(abs, A))))
    
    
    
else:
    
    for index in range(N-1):
        print(A)
        if A[index] <=0 and A[index+1]<=0:
            A[index]*=-1
            A[index+1]*=-1
        
            
        if A[index]<=0 and A[index+1]>=0:
            A[index]*=-1
            A[index+1]*=-1
            
        
   # print(A)
    print(sum(A))

Submission status: Runtime Error

Solution code:
N = int(input())
Alist = list(map(int, input().split()))
memo = [{} for i in range(N)]

def ddp(cur_N, ai):
    if ai in memo[cur_N]:
        return memo[cur_N][ai]
    if cur_N >=N-1:
        return ai
    aj = Alist[cur_N+1]
    ret = max((ai + ddp(cur_N+1, aj)), (-1*ai + ddp(cur_N+1, -1*aj)))
    memo[cur_N][ai] = ret
    return ret
print(ddp(0, Alist[0]))

Submission status: Runtime Error

Solution code:
n = input()
n = int(n)
a = input().split
b = list()
k = 0
for i in range(n):
  s = int(a[k])
  b.append(s)
  k += 1
l = 0
for i in range(10000):
  if b[l] < 0:
    if b[l+1]<0:
      x = b[l]*(-1)
      y = b[l+1]*(-1)
      b.replace(str(b[l]),str(x))
      b.replace(str(b[l+1]),str(y))
      l += 1
  l += 1
m = 0
c = list()
for i in range(n):
  r = int(b[m])
  c.append(r)
  m += 1
print(sum(c))

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
import math
import numpy as np
import matplotlib.pyplot as plt

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



if 0 in A:
    
   # print (A)
    print (sum(list(map(abs, A))))
    
    
    
else:
    
    for index in range(N-1):
        print(A)
        if A[index] <=0 and A[index+1]<=0:
            A[index]*=-1
            A[index+1]*=-1
        
            
        if A[index]<=0 and A[index+1]>=0:
            A[index]*=-1
            A[index+1]*=-1
            
        
   # print(A)
    print(sum(A))

Submission status: Runtime Error

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

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

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

ans = sum(a_list)

min_list = []
for i in range(N):
    if a_list[i] < 0:
        min_list.append(a_list[i])

if len(min_list) % 2 == 0:
    abc = sum(min_list)
    abc *= -1
    print(ans + 2*abc)

if len(min_list) % 2 == 1 and len(min_list) != 1:
    abc.sort()
    min_list = min_list[:-1]
    abc = sum(min_list)
    abc *= -1
    print(ans + 2*abc)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

sum1=0
index=0
while index < len(listA):
    sum1+=listA[index]
    index+=1

if len(listA)%2==0:
    leng = len(listA)-1
else:
    leng = len(listA)-2
while leng > 1:
    index=0
    max1 = 0
    startI=0
    endI=0
    while index < len(listA)-leng+1:
        if sum(listA[index:index+leng+1]) <= 0 and max1 < sum(listA[index:index+leng+1])*-2:
            max1 = -2*(sum(listA[index:index+leng+1]))
            startI = index
            endI = index+leng
        index+=1
    if max1!=0:
        sum1+=max1
        index2=startI
        while index2 < endI+1:
            listA[index2]*=-1
            index2+=1
    leng-=1

print(sum1)

Submission status: Runtime Error

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

for i in range(N-2):
    if a_list[i] <0 and a_list[i+1] < 0:
       a_list[i] *= -1
       a_list[i+1] *= -1

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

ans = sum(a_list)

min_list = []
for i in range(N):
    if a_list[i] < 0:
        min_list.append(a_list[i])

if len(min_list) % 2 == 0:
    abc = sum(min_list)
    abc *= -1
    print(ans + 2*abc)

if len(min_list) % 2 == 1 and len(min_list) !=1 :
    abc.sort()
    min_list = min_list[:-1]
    abc = sum(min_list)
    abc *= -1
    print(ans + 2*abc)

Submission status: Runtime Error

Solution code:
n = int(input())

A = [int(x) for x in input().split()]

plus = [x for x in A if x >0]

minus = [x for x in A if x <=0]

minus.sort()
if len(A)==1:
    print(A[0])
elif len(minus) % 2 == 0:
    print(sum(plus)-sum(minus))

else:
    plus.sort()
    r = min(plus[0],-1*minus[-1])
    print(sum(plus)-sum(minus)-2*r)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
counter = 0
m = []
def abab(l):
    for i in range(len(l)):
        m.append(abs(l[i]))
    return m

for i in range(N):
    if A[i] < 0:
        counter += 1
      
if counter % 2 == 1:
    m = abab(A)
    idx = A.index(min(m))
    m[idx] *= -1
    print(sum(m))
else:
    print(sum(abab(A)))

Submission status: Runtime Error

Solution code:
n = int(input())
w = input().split(" ")

a = []
k = False
for i in range(n):
    a.append(int(w[i]))
    if a[i] < 0:
    	a[i] *= -1
        k= not(k)
    if i == 1:
        min = a[i]
    if a[i] < min:
        min = a[i]
2
sum = sum(a)
if k:
    sum -= min
print(sum)

Submission status: Runtime Error

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

for i in range(N-2):
    if a_list[i] <0 and a_list[i+1] < 0:
       a_list[i] *= -1
       a_list[i+1] *= -1

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

ans = sum(a_list)

min_list = []
for i in range(N-1):
    if a_list[i] < 0:
        min_list.append(a_list[i])

if len(min_list) % 2 == 0:
    abc = sum(min_list)
    abc *= -1
    print(ans + 2*abc)

if len(min_list) % 2 == 1:
    abc.sort()
    min_list = min_list[:-1]
    abc = sum(min_list)
    abc *= -1
    print(ans + 2*abc)

Submission status: Runtime Error

Solution code:
def gcd_core(a, b):
    if b == 0:
        return a
    else:
        return gcd_core(b, a % b)


def gcd(arr):
    g = gcd_core(arr[0], arr[1])
    for i in range(2, len(arr)):
        g = gcd_core(g, arr[i])
    return g








def gcd_core(a, b):
    if b == 0:
        return a
    else:
        return gcd_core(b, a % b)


def gcd(arr):
    g = gcd_core(arr[0], arr[1])
    for i in range(2, len(arr)):
        g = gcd_core(g, arr[i])
    return g

n = int(input())

a = list(map(int,input().split()))
b = a[1:-1]
m = max(b)
base = 0

for i in range(n):
    base += abs(a[i])

kouho1 = base
if a[0] > 0:
    kouho1 -=a[0]*2
kouho2 = base

if a[-1] > 0:
    kouho2 -= a[-1]*2

kouho3 = base
if m < 0:
    kouho3 += m*2


l = [kouho1,kouho2,kouho3]
print(max(l))

Submission status: Runtime Error

Solution code:
n = int(input())

A = [int(x) for x in input().split()]

plus = [x for x in A if x >0]

minus = [x for x in A if x <=0]

minus.sort()
if len(A)==0:
    print(A[0])
elif len(minus) % 2 == 0:
    print(sum(plus)-sum(minus))

else:
    plus.sort()
    if len(plus) == 0:
    r = min(plus[0],-1*minus[-1])
    print(sum(plus)-sum(minus)-2*r)

Submission status: Runtime Error

Solution code:
import math
from functools import reduce

def gcd_list(numbers):
    return reduce(math.gcd, numbers)

N = int(input())
v_list = [int(i) for i in input().split()]

ans_list = []
for i in range(N):
    if N != 2:
        ans_list.append(gcd_list((v_list[:i] + v_list[i+1:])))
    else:
        ans_list = v_list

print(max(ans_list))

Submission status: Runtime Error

Solution code:
n = int(input())
w = input().split(" ")

a = []
k = False
for i in range(n):
    a.append(int(w[i]))
    if a[i] < 0:
       a[i] *= -1
       k = not(k)
    if i == 1:
        min = a[i]
    if a[i] < min:
        min = a[i]
2
sum = sum(a)
if k:
    sum -= min
print(sum)

Submission status: Runtime Error

Solution code:
N=int(input())
A=list(map(int,input().split(" ")))
A_p = [ a for a in A if a > 0 ]
A_n = [ -1*a for a in A if a < 0 ]
A_0 = [ a for a in A if a == 0 ]

l_n = len(A_n)
l_0 = len(A_0)


if l_n%2 == 0:
    print(sum(A_p)+sum(A_n))
else:
    if l_0 > 0:
        print(sum(A_p)+sum(A_n))
    else:
        if min(A_n) > min(A_p):
            tmp_n=min(A_p)
            A_p.remove(tmp_n)
            print(sum(A_p)+sum(A_n)-tmp_n)    
        else:
            tmp_n=min(A_n)
            A_n.remove(tmp_n)
            print(sum(A_p)+sum(A_n)-tmp_n)

Submission status: Runtime Error

Solution code:
def change(l, start):
  if start >= len(l) - 1:
    return sum(l)
  
  for i in range(start, len(l) - 1, 1):
    if l[i] < 0 and abs(l[i]) > l[i+1]:
      l[i] = -l[i]
      l[i+1] = -l[i+1]
      nextStart = i + 1
      break
      
  return change(l, nextStart)

n = int(input())
a = [int(i) for i in input().split(" ")]

print(change(a, 0))

Submission status: Runtime Error

Solution code:
n = int(input())
w = input().split(" ")

a = []
k = False
for i in range(n):
    a.append(int(w[i]))
    if a[i] < 0:
       a[i] *= -1
       k = not(k)
    if i == 1:
        min = a[i]
    if a[i] < min:
        min = a[i]
2
sum = sum(a)
if k:
    sum -= min
print(sum)

Submission status: Runtime Error

Solution code:
N = int(input())
Alist = list(map(int, input().split()))
memo = [{} for i in range(N)]

def ddp(cur_N, ai):
    if ai in memo[cur_N]:
        return memo[cur_N][ai]
    if cur_N >=N-1:
        return ai
    nx = Alist[cur_N+1]
    ret = max(ai + ddp(cur_N+1, nx), -1*ai + ddp(cur_N+1, -1*nx))
    memo[cur_N][ai] = ret
    return ret
print(ddp(0, Alist[0]))

Submission status: Runtime Error

Solution code:
n = int(input())

A = [int(x) for x in input().split()]

plus = [x for x in A if x >0]

minus = [x for x in A if x <=0]

minus.sort()
if len(minus) % 2 == 0:
    print(sum(plus)-sum(minus))

else:
    plus.sort()
    if len(plus) == 0:
        plus == [0]
    r = min(plus[0],-1*minus[-1])
    print(sum(plus)-sum(minus)-2*r)

Submission status: Runtime Error

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

####################
m=[0 for i in range(N)]
m[N-1] = abs(A[N-1])
m[N-2] = (abs(A[N-1]+A[N-2]))

m1 = A[N-1]+A[N-2]+A[N-3]
m2 = A[N-1]-A[N-2]-A[N-3]
m3 = -A[N-1]-A[N-2]+A[N-3]
m4 = -A[N-1]+A[N-2]-A[N-3]

m[N-3] = max(m1,m2,m3,m4)

for i in range(N-4,-1,-1):
    m[i]=max(m[i+3]+A[i]+abs(A[i+1]+A[i+2]),m[i+2]+abs(A[i]+A[i+1]))

####################
M = [0 for i in range(N)]
M[0] = abs(A[0])
M[1] = (abs(A[0]+A[1]))

m1 = A[0]+A[1]+A[2]
m2 = A[0]-A[1]-A[2]
m3 = -A[0]-A[1]+A[2]
m4 = -A[0]+A[1]-A[2]

M[2] = max(m1,m2,m3,m4)

for i in range(3,N):
    M[i]=max(M[i-3]+A[i]+abs(A[i-1]+A[i-2]),M[i-2]+abs(A[i]+A[i-1]))

ans = max(M[0],m[0])


print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())

A = [int(x) for x in input().split()]

plus = [x for x in A if x >0]

minus = [x for x in A if x <=0]

minus.sort()
if len(minus) % 2 == 0:
    print(sum(plus)-sum(minus))

else:
    plus.sort()
    if plus == []:
        plus == [0]
    r = min(plus[0],-1*minus[-1])
    print(sum(plus)-sum(minus)-2*r)

Submission status: Runtime Error

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

####################
m=[0 for i in range(N)]
m[N-1] = abs(A[N-1])
m[N-2] = (abs(A[N-1]+A[N-2]))

m1 = A[N-1]+A[N-2]+A[N-3]
m2 = A[N-1]-A[N-2]-A[N-3]
m3 = -A[N-1]-A[N-2]+A[N-3]
m4 = -A[N-1]+A[N-2]-A[N-3]

m[N-3] = max(m1,m2,m3,m4)

for i in range(N-4,-1,-1):
    m[i]=max(m[i+3]+A[i]+abs(A[i+1]+A[i+2]),m[i+2]+abs(A[i]+A[i+1]))

####################
M = [0 for i in range(N)]
M[0] = abs(A[0])
M[1] = (abs(A[0]+A[1]))

m1 = A[0]+A[1]+A[2]
m2 = A[0]-A[1]-A[3]
m3 = -A[0]-A[1]+A[3]
m4 = -A[0]+A[1]-A[2]

M[2] = max(m1,m2,m3,m4)

for i in range(3,N,):
    M[i]=max(m[i-3]+A[i]+abs(A[i-1]+A[i-2]),m[i-2]+abs(A[i]+A[i-1]))

ans = max(M[0],m[0])


print(ans)

Submission status: Runtime Error

Solution code:
import sys
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
import math
from functools import reduce


def input(): return sys.stdin.readline().strip()


def INT(): return int(input())


def MAP(): return map(int, input().split())


def LIST(): return list(map(int, input().split()))


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

n = INT()

a = LIST()

ct = 0

for i, x in enumerate(a):
    if x <= 0:
        p = [i, x]
        ct += 1

"print(p)"

lis = [abs(x) for x in a]
"""print(ct, sum(lis))"""

if ct % 2 == 0:
    print(sum(lis))
else:
    print(sum(lis)-2*max(p[1], a[p[0]+1]))

Submission status: Runtime Error

Solution code:
n = int(input())
w = input().split(' ')

a = []
k = False
for i in range(n):
    a.append(int(w[i]))
    if a[i] < 0:
       a[i] *= -1
       k = not(k)
    if i == 1:
        min = a[i]
    if a[i] < min:
        min = a[i]

sum = sum(a)
if k:
    sum -= min
print(sum)

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
import copy

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

ansA = 0
for i in range(N-2):
  if A[i] < 0:
    A[i+1] *= -1
  ansA += abs(A[i])
if A[-3] == 0:
  ansA += abs(A[-1])+abs(A[-2])
else:
  ansA += abs(A[-1]+A[-2])
ansB = 0
for i in reversed(range(2, N)):
  if X[i] < 0:
    X[i-1] *= -1
  ansB += abs(X[i])
if X[2] == 0:
  ansB += abs(X[0])+abs(X[1])
else:
  ansB += abs(X[0]+X[1])
print(max(ansA, ansB))

Submission status: Runtime Error

Solution code:
import math

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



ans = 0
for index in range(N)
    for index in range(N-1):
        
        if A[index] <=0 and A[index+1]<=0:
            A[index]*=-1
            A[index+1]*=-1
            
        if A[index]<=0 and A[index+1]>=0:
            A[index]*=-1
            A[index+1]*=-1
            
    
#print(A)
print(sum(A))

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.read


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

abslsta=[abs(v) for v in lsta]
min_index=abslsta.index(min(abslsta))

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

if lsta[n-1]<0:
    if min_index==n-1:
        print(sum(lsta))

    else:
        print(sum(lsta)-lsta[n-1]*2-lsta[min_index]*2)

else:
    print(sum(lsta))

Submission status: Runtime Error

Solution code:
import itertools
import math

n = int(input())
a = [int(s) for s in input().split(' ')]

def ngcd(nums):
    d = 0
    for num in nums:
        d = math.gcd(d, num)
    return d

max = 1

for comb in itertools.combinations(a, n-1):
    ret = ngcd(comb)
    if ret > max:
        max = ret

print(max)

Submission status: Runtime Error

Solution code:
import copy

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

plus = [i for i in A if i >= 0]
minus = [i for i in A if i < 0]

M = len(minus)


if M % 2 == 0:
  ans = sum(A) + abs(sum(minus))*2
else:
  minus2 = copy.copy(minus)
  minus2.pop(-1)
  tmp = sum(A) + abs(sum(minus2))*2
  tmp2 = sum(A) + abs(sum(minus))*2 - min(plus) * 2
  ans = max(tmp, tmp2)
print(ans)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.read

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

abslsta=[abs(v) for v in lsta]
min_index=abslsta.index(min(abslsta))

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


if lsta[n-1]<0:
    if min_index==n-1:
        print(sum(lsta))

    else:
        print(sum(lsta)-lsta[n-1]*2-lsta[min_index]*2)

else:
    print(sum(lsta))

Submission status: Runtime Error

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

for i in range(n-1):
    if a[i] <= 0 and a[i+1] <= 0:
        a[i] = -a[i]
        a[i+1] = -a[i+1]
        
for i in range(n-2):
    if a[i] < 0 and a[i+1] >= 0 and a[i+2] <= 0:
        a[i] = -a[i]
        a[i+2] = -a[i+2]
        
for i in range(n-1):
    if a[i] < 0:
        if a[i]+a[i+1] < -(a[i]+a[i+1]):
            a[i] = -a[i]
            a[i+1] = -a[i+1]

if a[n-1]+a[n] < -(a[n-1]+a[n]):
    a[n-1] = -a[n-1]
    a[n] = -a[n]

print(sum(a))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n=int(input())
a=[int(x) for x in input().split()]
a.sort()
b=[x for x in a if x>0]
c=[x for x in a if x<=0]
if len(c)%2==0:
    s=sum(b)
    s-=sum(c)
    print(s)
else:
    c.append(b.pop(0))
    s=sum(b)
    s-=sum(c[:-2])
    if c[-1] > c[-2]*-1:
        s+=c[-1]
        s+=c[-2]
        print(s)
    else:
        s-=c[-1]
        s-=c[-2]
        print(s)

Submission status: Runtime Error

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

flag = 1
if len(A) >= 3:
    B = sum(map(abs, A[:-2]))
    cnt = 0
    for ai in A[:-2]:
        if ai <= 0:
            cnt += 1

    flag = 1 if cnt %2 == 0 else -1
#print(B)
C = A[-2:]
C[0] *= flag
if -C[0] > -C[1]:
    #print("case A", -C[0], -C[1])
    B += -C[0] - C[1]
else:
    B += C[0] + C[1]
    #print("case B", C[0], C[1])
#print(A[:-2], A[-2:])
print(B)

Submission status: Runtime Error

Solution code:
import numpy as np

N = int(input())
A = [int(i) for i in input().split()]

ab = len([n for n in A if n <0]) % 2

B = np.sort(np.abs(A))
if ab == 0:
  print(sum(A))
else:
  print(sum(A[1 :] - A[0]))

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split(" ")))
abs_list = list(map(abs, A))
abs_list.sort()
pos = list(filter(lambda x: x > 0, A))
neg = list(filter(lambda x: x < 0, A))
pos.sort()
neg.sort()

# A has 0
if abs_list[0] == 0:
	print(sum(abs_list))
elif len(neg) % 2 == 0:
	print(sum(abs_list))
elif len(pos) > 0 and abs(pos[0]) < abs(neg[-1]):
	print(sum(abs_list) - 2*pos[0])
else:
	print(sum(abs_list[1:]-abs_list[0]))

Submission status: Runtime Error

Solution code:
N = int(input())
A = [int(i) for i in input().split()]

A.sort()
su = 0
m = 0
for i in range(N):
    if A[i] < 0:
       su -= A[i]
       m += 1
    else:
       su += A[i]

if m % 2 == 1:
    print(su - 2 * A[m])
else:
    print(su)

Submission status: Runtime Error

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

for i in range(N-1):
	if As[i] <= 0 and As[i+1] <= 0:
		As[i] = -As[i]
		As[i+1] = -As[i+1]

m = sum(As)

for i in range(N-1):
	nl = As[0:i] + [-1*As[i]] + [-1*As[i+1]] + As[i+2:N]
	nls = sum(nl)
	if(m <= nls):
		m = nls
		As = nl

for i in list(range(N))[::-1][0:N-1]:
	nl = As[0:N-i+1] + [-1*As[N-i+1]] + [-1*As[N-i]] + As[N-i:i]
	nls = sum(nl)
	if(m <= nls):
		m = nls
		As = nl

print(m)

Submission status: Runtime Error

Solution code:
N = int(input().rstrip())
A = map(int, input().rstrip().split())
_A = A
for i in range(len(A)):
  if i == len(A) - 2:
    break
  if A[i] >= 0 and A[i+1] >= 0:
    pass
  else:
    s = _A[i] + _A[i+1]
    if s < -s:
      _A[i] = -1 * A[i]
      _A[i+1] = -1 * A[i+1]
print(sum(_A))

Submission status: Runtime Error

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

count = 0
for i in range(N):
    if A[i] < 0:
        count += 1
        
Aabs = [abs(i) for i in A]
print(sum(Aabs))

if count % 2 == 0:
    print(sum(Aabs))
else:
    mini = min(Aabs)
    print(Asum - 2*mini)

Submission status: Runtime Error

Solution code:
n = int(input())
li = list(map(int,input().split()))
ans = 0

for i in range(n - 1):
    if (((li[i] * -1) + (li[i] * -1)) >= (li[i] + li[i + 1])):
        li[i] *= -1
        li[i + 1] *= -1
    elif ((li[i] * -1) + li[i + 1] + (li[i + 2] * -1) >= (li[i] + li[i + 1] + li[i + 2])):
        li[i] *= -1
        li[i + 2] *= -1
        i += 1

for i in range(n):
    ans += li[i]
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())

A = [int(x) for x in input().split()]

plus = [x for x in A if x >0]

minus = [x for x in A if x <=0]

minus.sort()
if len(minus) % 2 == 0:
    print(sum(plus)-sum(minus))

else:
    plus.sort()
    r = min(plus[0],-1*minus[-1])
    print(sum(plus)-sum(minus)-2*r)

Submission status: Runtime Error

Solution code:
n = int(input())

A = [int(x) for x in input().split()]

plus = [x for x in A if x >0]

minus = [x for x in A if x <=0]

minus.sort()
plus.sort()
if len(minus) % 2 == 0:
    print(sum(plus)-sum(minus))

else:
    r = min(plus[0],-1*minus[-1])
    print(sum(plus)-sum(minus)-2*r)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
c=[0]*n
e=1
ok=0
for i in range(n):
    if a[i]<0:
        c[i]=-1
        a[i]=-a[i]
    else:
        c[i]=1
for i in range(n-1):
    if c[i]==c[i+1]:
        e+=1
    else:
        if e%2==0:
            ok=1
        e=1
        sum=0
for i in range(n):
    sum+=a[i]
m=min(a)
if ok!=1:
    sum=sum-2*m
print(sum)

Submission status: Runtime Error

Solution code:
N = int(input().rstrip())
A = map(int, input().rstrip().split())
_A = list()
for i in range(len(A)):
  if i == len(A) - 2:
    break
  if A[i] >= 0 and A[i+1] >= 0:
    pass
  else:
    s = A[i] + A[i+1]
    if s < -s:
      A[i] = -A[i]
      A[i+1] = -A[i+1]
print(sum(A))