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: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
neg_A = len(list(filter( lambda x: x < 0, A)))
A = sorted(map(abs, A))

if neg_A%2 == 0:
  print(sum(A))
else:
  print(sum(A)-2*A[0])

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
count = 0
for i in range(n):
    if a[i]<0:
        count += 1
        a[i] = abs(a[i])
a = sorted(a)
if count%2 != 0:
    a[0] = a[0]*-1
ans = 0
for i in range(n):
    ans += a[i]
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
li = list(map(int,input().split()))
cnt = 0
for i in li:
    if i <= 0:cnt+=1
if cnt%2:
    d = sorted(li,key=lambda x:abs(x))[0]
    print(sum(list(map(lambda x:abs(x),li)))-abs(d)*2)
else:print(sum(list(map(lambda x:abs(x),li))))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
A.sort()
import numpy as np
Ac = A.copy()
Ac.sort(key = lambda x:np.abs(x))

import bisect
zero_idx = bisect.bisect(A,0)
if zero_idx == 0:
    An = []
    Ap = A[:]
elif zero_idx == N:
    Ap = []
    An = A[:]
else:
    An = A[:zero_idx]
    Ap = A[zero_idx:]
    
pnum = len(Ap)
nnum = len(An)
ans = 0
total = 0
for a in A:
    total += int(np.abs(a))

if N%2 == 0:
    if nnum % 2 == 0:
        ans = total
    else:
        ans = total - 2 * np.abs(Ac[0])
           
    
else:
    if nnum % 2 == 1:        
        ans = total - 2 * np.abs(Ac[0])
    else:           
        ans = total
    
    
print(ans)

Submission status: Accepted

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

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

if count % 2 == 0:
    print(sum_A)
else:
    print(sum_A - 2*min(A))

Submission status: Accepted

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

b=0
minus=0
low=abs(a[0])
for i in range(n):
    if a[i]>0:
        b += a[i]
    else:
        b -= a[i]
        minus += 1
    if abs(a[i])<low:
        low = abs(a[i])

print(b if minus%2==0 else b-2*low)

Submission status: Accepted

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

p = 0
m = float('inf')
ret = 0
for a in A :
    if a < 0 :
        p ^= 1
        m = min(m, -a)
        ret -= a
    else :
        m = min(m, a)
        ret += a
        
if p == 0 :
    print(ret)
else :
    print(ret - 2 * m)

Submission status: Accepted

Solution code:
import bisect
n = int(input())
A = [int(a) for a in input().split()]
A.sort()
A_abs = [abs(ab) for ab in A]
A_abs.sort()
sum_abs = sum(A_abs)

wh = bisect.bisect_left(A, 0)
dev2 = wh % 2
if dev2 == 0:
  print(sum_abs)
else:
  print(sum_abs - 2 * A_abs[0])

Submission status: Accepted

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

#print(s)

count = 0
odd = 0

min1 = abs(s[0])
#print(min1)
for i in range(N):
  min1 = min(min1, abs(s[i]))
  count += abs(s[i])
  if abs(s[i]) != s[i]:
    odd += 1

count2 = count - 2*min1

if odd%2 == 1:
  print(count2)
else:
  print(count)

Submission status: Accepted

Solution code:
import numpy as np

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

minus = 0

minim = 100000000

for i,v in enumerate(A):
    if v < 0 :
        minus += 1
        A[i] = -v
    minim = min(minim,A[i])

if minus % 2 == 0:
    print(sum(A))
else:
    print(sum(A) - 2 * minim)

Submission status: Accepted

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

sign = len([i for i in a if i < 0]) % 2
b = list(map(abs, a))
b.sort()

if sign == 0:
	print(sum(b))
else:
	print(sum(b) - b[0] * 2)

Submission status: Accepted

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

sum_abs_all = False
minus_count = 0

for i in A_list:
    if i == 0:
        sum_abs_all = True
        break
    if i < 0:
        minus_count += 1

if minus_count % 2 == 0:
    sum_abs_all = True

abs_A_list = [abs(i) for i in A_list]

ans = 0
for i in abs_A_list:
    ans += i

if not sum_abs_all:
    abs_A_list.sort()
    ans -= 2*abs_A_list[0]

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
a_abs = [abs(i) for i in a]
if len([x for x in a if x < 0])%2 == 1:
    print(sum(a_abs)-2*min(a_abs))
else:
    print(sum(a_abs))

Submission status: Accepted

Solution code:
N, *A = map(int, open(0).read().split())

dp = [[0] * 2 for _ in range(N + 1)]
dp[0][1] = -float('inf')

for i in range(N):
    dp[i + 1][0] = max(dp[i][0] + A[i], dp[i][1] - A[i])
    dp[i + 1][1] = max(dp[i][0] - A[i], dp[i][1] + A[i])
    
print(dp[N][0])

Submission status: Accepted

Solution code:
N, *A = map(int, open(0).read().split())

s = list(abs(x) for x in A)
cnt = sum(x < 0 for x in A)
if (cnt % 2 == 0) or (0 in A):
    print(sum(s))
else:
    print(sum(s) - 2 * min(s))

Submission status: Accepted

Solution code:
import sys
from collections import deque
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
MAX = 10**5+5
INF = 10**9

def resolve():
    n = int(input())
    a = [0]
    a.extend(list(map(int,input().split())))
    dp = [[0,0] for _ in range(n+1)]
    dp[0][1]=-INF
    for i in range(1,n+1):
        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])
    print(dp[n][0])

if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#ABC125 D

n = int(input())
a = list(map(int,input().split()))
cnt = 0
mi = float('inf')
tmp = 0
for i in range(n):
    if a[i] < 0:
        cnt += 1
    if mi > abs(a[i]):
        mi = abs(a[i])
        tmp = i
s = list(map(lambda x:abs(x),a))
if cnt % 2:
    print(sum(s)-2*s[tmp])
else:
    print(sum(s))

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
c=0
d=0
for i in range(n):
    if(a[i]==0):
        d=1
    if(a[i]<=0):
        c+=1
if(c%2==0):
    d=1
for i in range(n):
    a[i]=abs(a[i])
b=min(a)
if(d==1):
    print('{0}'.format(sum(a)))
else:
    print('{0}'.format(sum(a)-2*b))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
min_num = 10**9
cnt = 0
sum_num = 0
for idx in range(N):
  if A[idx] < 0:
    cnt += 1
  min_num = min(min_num, abs(A[idx]))
  sum_num += abs(A[idx])
if cnt%2 == 0:
  print(sum_num)
else:
  print(sum_num-min_num*2)

Submission status: Accepted

Solution code:
import bisect
n = int(input())
a = [int(_) for _ in input().split()]
a.sort()
i = bisect.bisect_left(a,0)
if i % 2 == 1:
    i -= 1
for j in range(i):
    a[j] *= -1
if i == n-1 or i == n:
    print(sum(a))
else:
    if a[i] + a[i+1] >= 0:
        print(sum(a))
    else:
        a[i] *= -1
        a[i+1] *= -1
        print(sum(a))

Submission status: Accepted

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

if 0 in a:
    print(sum(a_abs))
else:
    cnt = 0
    for i in a:
        if i < 0:
            cnt += 1
    if cnt%2 == 0:
        print(sum(a_abs))
    else:
        print(sum(a_abs)-min(a_abs)*2)

Submission status: Accepted

Solution code:
# n = int(input())
# a, b = map(int, input().split())
# int_list = list(map(int, input().split()))
# l = list(input().split())
n = int(input())
a_list = list(map(int, input().split()))

n_minus = 0
mn = 10**9
abssum = 0
for a in a_list:
  absa = abs(a)
  abssum += absa
  if a < 0:
    n_minus += 1
  if absa < mn:
    mn = absa
    
if n_minus %2 == 0:
  print(abssum)
else:
  print(abssum - 2*mn)

Submission status: Accepted

Solution code:
import sys
from functools import reduce

def main():
    input = sys.stdin.readline
    N = int(input())
    As = list(map(int, input().split()))
    sign = reduce(lambda x, y: x * y, [int(a >= 0) * 2 - 1 for a in As])
    A_abs = [abs(a) for a in As]
    ans = sum(A_abs) if sign > 0 else sum(A_abs) - 2 * min(A_abs)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
c = 0
z = False
for aa in a:
    if aa < 0:
        c += 1
    elif aa == 0:
        z = True
if z or c % 2 ==0:
    print(sum(map(abs, a)))
else:
    ab = [abs(x) for x in a]
    ans = sum(ab) - min(ab)*2
    print(ans)

Submission status: Accepted

Solution code:
n = int(input())
an = [int(x) for x in input().split()]

dp_0 = an[0]
dp_1 = -an[0]

for i in range(1,n):
  dp_0, dp_1 = [max(dp_0 + an[i], dp_1 - an[i]), max(dp_0 - an[i], dp_1 + an[i])]

print(dp_0)

Submission status: Accepted

Solution code:
from numpy import zeros

inf = float('inf')

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

dp = zeros((n + 1, 2))
dp[0][1] = -inf
# dp[idx][マイナスを使う必要がある個数]

for i, aa in enumerate(a):
    if aa >= 0:
        dp[i + 1][0] = max(dp[i][0] + aa, dp[i][1] - aa)
        dp[i + 1][1] = max(dp[i][1] + aa, dp[i][0] - aa)
    else:
        dp[i + 1][0] = max(dp[i][1] - aa, dp[i][0] + aa)
        dp[i + 1][1] = max(dp[i][0] - aa, dp[i][1] + aa)

# print(dp)
print(int(dp[n][0]))

Submission status: Accepted

Solution code:
import numpy as np

inf = float('inf')

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

dp = np.zeros((n + 1, 2))
dp[0][1] = -inf
# dp[idx][マイナスを使う必要がある個数]

for i, aa in enumerate(a):
    if aa >= 0:
        dp[i + 1][0] = max(dp[i][0] + aa, dp[i][1] - aa)
        dp[i + 1][1] = max(dp[i][1] + aa, dp[i][0] - aa)
    else:
        dp[i + 1][0] = max(dp[i][1] - aa, dp[i][0] + aa)
        dp[i + 1][1] = max(dp[i][0] - aa, dp[i][1] + aa)

# print(dp)
print(int(dp[n][0]))

Submission status: Accepted

Solution code:
inf = float('inf')

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

dp = [[0] * 2 for _ in range(n + 1)]
dp[0][1] = -inf
# dp[idx][マイナスを使う必要がある個数]

for i, aa in enumerate(a):
    if aa >= 0:
        dp[i + 1][0] = max(dp[i][0] + aa, dp[i][1] - aa)
        dp[i + 1][1] = max(dp[i][1] + aa, dp[i][0] - aa)
    else:
        dp[i + 1][0] = max(dp[i][1] - aa, dp[i][0] + aa)
        dp[i + 1][1] = max(dp[i][0] - aa, dp[i][1] + aa)

# print(dp)
print(dp[n][0])

Submission status: Accepted

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

minus_count = len(tuple(filter(lambda x: x < 0, a)))
b = tuple(sorted(map(abs, a)))
s = sum(b)
if minus_count % 2 == 1:
    s -= b[0] * 2
print(s)

Submission status: Accepted

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

cnt = 0
s = 0
minabs = 10**11
for a in A:
    if a <= -1:
        cnt += 1
    s += abs(a)
    minabs = min(minabs, abs(a))

if cnt % 2 == 0:
    print(s)
else:
    print(s-minabs*2)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
X=[[0,0]]
Y=[[0,0]]
for i in A:
    if i<0:
        k=1-X[-1][1]
    else:
        k=X[-1][1]
    X.append([X[-1][0]+abs(i),k])
for i in A[::-1]:
    if i<0:
        k=1-Y[-1][1]
    else:
        k=Y[-1][1]
    Y.append([Y[-1][0]+abs(i),k])
Y.reverse()

ans=-10**20
for i in range(N):
    t=(-1)**(X[i][1]+Y[i+1][1])
    a=X[i][0]+Y[i+1][0]+t*A[i]
    ans=max(a,ans)
print(ans)

Submission status: Accepted

Solution code:
input()
A=list(map(int,input().strip().split(" ")))
pos=[]
neg=[]
for x in A:
    if x>=0:
        pos+=[x]
    else:
        neg+=[x]

pos=sorted(pos)
neg=sorted(neg)

if len(neg)%2==0:
    print(sum(pos)-sum(neg))
else:
    ans1=sum(pos)-sum(neg)+2*neg[-1]
    ans2=sum(pos)-sum(neg)+2*neg[-1]
    if len(pos)>0:
        ans2=sum(pos)-sum(neg)-2*pos[0]
    print(max(ans1,ans2))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = [abs(ai) for ai in a]
b.sort()
negative_count = len([ai for ai in a if ai < 0])
if negative_count % 2 == 1:
    b[0] *= (-1)

print(sum(b))

Submission status: Accepted

Solution code:
def larger(x, y):
	if x > y:
		return x
	return y

N = int(input())
A = list(map(int, input().split()))
dpp = [A[0]]
dpn = [-A[0]]
for i in range(1, N-1):
	nextdpp = larger(dpp[-1]+A[i], dpn[-1]-A[i])
	nextdpn = larger(dpp[-1]-A[i], dpn[-1]+A[i])
	dpp.append(nextdpp)
	dpn.append(nextdpn)
print(larger(dpp[-1]+A[N-1], dpn[-1]-A[N-1]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from collections import deque  # 双方向キュー
from collections import defaultdict  # 初期化済み辞書
from heapq import heapify, heappush, heappop, heappushpop  # プライオリティキュー
from bisect import bisect_left, bisect_right  # 二分探索
#import numpy as np  # 1.8.2
#import scipy  # 0.13.3
#sys.setrecursionlimit(int(1e+6))
s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in ss]
ss2nnn = lambda ss: [s2nn(s) for s in ss]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)]
ii2sss = lambda n: [list(sys.stdin.readline().rstrip()) for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))
MOD = int(1e+9) + 7

def main():
    N = i2n()
    A = i2nn()
    A.sort()
    n = 0
    for i in range(1, N, 2):
        t = A[i-1] + A[i]
        if t < 0:
            t = -t
        n += t
    if N % 2 == 1:
        n += A[-1]
    print(n)

main()

Submission status: Accepted

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

tmp = sum(np.abs(a))
if np.count_nonzero(a < 0)%2 == 0:
    print(tmp)
else:
    ind = np.argmin(np.abs(a))
    print(tmp-abs(a[ind])*2)

Submission status: Accepted

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

    n_neg = 0
    near_zero = 10 ** 10
    for a in A:
        if a < 0:
            n_neg += 1
        near_zero = min(near_zero, abs(a))

    s = sum([abs(a) for a in A])
    if n_neg % 2 == 0:
        print(s)
    else:
        print(s - near_zero * 2)


main()

Submission status: Accepted

Solution code:
import math

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

  count = 0
  sum = 0
  min_number = 10000000000
  for i in a:
    count += 1 if i<0 else 0
    sum += abs(i)
    min_number = min(min_number, abs(i))
    
  if count % 2 == 0:
    print(sum)
  else:
    print(sum - min_number*2)
  
main()

Submission status: Accepted

Solution code:
n = int(input())
num_list = [ int(v) for v in input().split() ]

sign = len([ v for v in num_list if v < 0 ])
mini = min([ abs(v) for v in num_list ])
total = sum([ abs(v) for v in num_list ])

if sign % 2 == 0:
    ans = total
else:
    ans = total - (2*mini)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

A.sort()
k = 0
while k<=n-1 and A[k]<0:
    k += 1
if k%2==0:
    print(-sum(A[:k])+sum(A[k:]))
elif k%2==1 and k==n:
    print(-sum(A[:k-1]) + sum(A[k-1:]))
else:
    print(max(-sum(A[:k-1])+sum(A[k-1:]) , -sum(A[:k+1])+sum(A[k+1:])))

Submission status: Accepted

Solution code:
import bisect
total = 0
N = int(input())
A = list(map(int, input().split()))
hu = [i for i in A if i < 0]
sei = [i for i in A if i >= 0]
hu.sort()
sei.sort()
if len(hu) % 2 == 0:
    print(sum(sei)+sum(hu)*-1)
else:
    if sei:
        print(sum(sei)+sum(hu)*-1 - min(-1*hu[-1], sei[0])*2)
    else:
        print(sum(sei)+sum(hu)*-1 + 1*hu[-1]*2)

Submission status: Accepted

Solution code:
N = int(input())
line = [int(a) for a in input().split()]
if 0 in line:
  line = [abs(a) for a in line]
  print(sum(line))
else:
  b = [i for i in line if i<0]
  if len(b)%2==0:
    line = [abs(a) for a in line]
    ans = sum(line)
    print(ans)
  else:
    line = [abs(a) for a in line]
    line.sort()
    ans = sum(line)-2*line[0]
    print(ans)

Submission status: Accepted

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

negative_number_count = list(map(lambda x: x < 0, A)).count(True)
B_tmp = sorted(map(abs, A))

if negative_number_count % 2 == 0:
    print(sum(B_tmp))
else:
    print(sum(B_tmp[1:]) - B_tmp[0])

Submission status: Accepted

Solution code:
from datetime import datetime as dt
N = int(input())
Ns = list(map(int,input().split(" ")))

l1 = len(Ns)
c = 0
for i in Ns:
    if i <= 0:
        c += 1

Ns = list(map(abs,Ns))
if c % 2 == 0:
    diff = 0
else:
    diff = min(Ns)
ans = sum(Ns) -2*diff
print(ans)

Submission status: Accepted

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

Ap=[]
Am=[]
for a in A:
    if a >= 0:
        Ap.append(a)
    else:
        Am.append(a)

Ap=sorted(Ap)
Am=sorted(Am,reverse=True)


if len(Ap)==0:
    if len(Am)%2==0:
        ans=-sum(Am)
    else:
        ans=-sum(Am)+2*Am[0]
        
elif len(Am)%2==0:
    Ams=-sum(Am)
    Aps=sum(Ap)
    ans=Ams+Aps
else:
    Ams=-sum(Am)+Am[0]
    Aps=sum(Ap)-Ap[0]
    tmp=Ap[0]+Am[0]
    if tmp<0:
        tmp=-tmp
    ans=Ams+Aps+tmp
        
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
from collections import defaultdict
import math
import re

def main():
    #A, B, T = map(int, input().split())
    N = int(input())
    A_list = [int(a) for a in input().split()]

    min_val = 10 ** 10
    neg_num = 0
    total = 0
    for a in A_list:
        abs_a = a if a > 0 else -a
        if abs_a < min_val:
            min_val = abs_a
        if a < 0:
            neg_num += 1
        total += abs_a

    if neg_num % 2 != 0:
        total -= 2 * min_val
    print(total)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
mini = 1000000001
zero = 0
wa = 0
for n in range(N):
	a = abs(A[n])
	mini = min(mini, a)
	wa += a
	if A[n] < 0:
		zero += 1
if zero % 2 == 1:
	print(wa - mini * 2)
else:
	print(wa)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
A = np.array(list(map(int,input().split())))
abs_A = [abs(i) for i in A]
ans = 0
if (0 in A) or (np.count_nonzero(A<0) % 2 == 0):
    ans = sum(abs_A)
else:
    ans = sum(abs_A) - 2*min(abs_A)
print(ans)

Submission status: Accepted

Solution code:
import numpy
N = map(int, input().split())
A_list = list(map(int, input().split()))
set = numpy.abs(numpy.array(A_list))
A_abs_min = min(set)
A_abs_sum = sum(set)
n = sum(x<0 for x in A_list)%2  # 真偽値リストを作って True の数を合計する方法
if n == 1:
    ans = A_abs_sum-2*A_abs_min
else:
    ans =  A_abs_sum
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
def solve(n, a):
    b = np.abs(a)
    num = 0
    for i in range(n):
        if a[i] < 0:
            num += 1
    if num % 2 == 0:
        return np.sum(b)
    else:
        return np.sum(b) - 2*np.min(b)

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

Submission status: Accepted

Solution code:
n = int(input())
s = [int(i) for i in input().split()]
count = 0
for i in range(len(s)):
    if s[i] <0:
        count += 1

s = list(map(abs, s))
if count%2 == 0:
    print(sum(s))
else:
    print(sum(s)-min(s)*2)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
Alist = np.array(list(map(int, input().split())))
Blist = list(map(abs, Alist))
nom = sum(Alist<0)
if nom%2 == 0 :
    ans = sum(Blist)
else :
    ans = sum(Blist) - 2*min(Blist)
print(ans)

Submission status: Accepted

Solution code:
# できるだけ正数が多いほうが良い。隣り合う負数は正にできる
# あと-+++++は+++++-のように-を移動できるので隣り合わせられる
# -が奇数なら0以上の最小の数に-を割り当ててあとは合計
# 偶数ならすべての-を+にできる

from functools import reduce

n = int(input())
minn = 10**9
arr = list(map(int, input().split()))
minus = 0
ans = reduce(lambda x, y: abs(x) + abs(y), arr)
for i in range(n):
    if arr[i] < 0:
        minus += 1
    if minn > abs(arr[i]):
        minn = abs(arr[i])
if minus % 2 == 0:
    print(ans)
else:
    print(ans-minn*2)

Submission status: Accepted

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

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

minus_cnt_is_even = True
has_zero = False
min_a = 1000000000
sum_a = 0
for item in a:
    if item < 0:
        item = -item
        minus_cnt_is_even = not minus_cnt_is_even
    elif item == 0:
        has_zero = True
    min_a = min(min_a, item)
    sum_a += item
if has_zero or minus_cnt_is_even:
    print(sum_a)
else:
    print(sum_a - min_a*2)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
absA = list(map(abs, A))
sumA = sum(absA)
if sum(i < 0 for i in A) % 2 == 1:
    print(sumA - 2 * min(absA))
else:
    print(sumA)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
ans = sum([abs(item) for item in A])
def flip(a):
    res = [a[0]]
    for i in range(len(a) - 2):
        if a[i] < 0:
            a[i+1] = - a[i+1]
        res.append(a[i+1])
    return(res)
r = flip(A[:])
l = flip(A[::-1])[::-1]
costs = []
for ri, li in  zip(r, l):
    if ri * li >= 0:
        costs = [0]
        break
    else:
        costs.append(min(abs(ri), abs(li)))
print(ans - 2 * min(costs))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
ri = lambda: int(input())
#rl = lambda: [int(x) if x.isdecimal() else x for x in input().split()]
#rl = lambda: list(input().split()))
rl = lambda: map(int,input().split())
rr = lambda N: [list(l) for l in zip(*[rl() for _ in range(N)])]
YN = lambda b: print('YES') if b else print('NO')
yn = lambda b: print('Yes') if b else print('No')
OE = lambda x: print('Odd') if x%2 else print('Even')
INF = 10**18
MOD = 10**9+7

N=ri()
A=list(rl())
cnt = [0]*3
ans = 0
sei = []
for a in A:
    sei.append(abs(a))
    ans += abs(a)
    if a > 0:
        cnt[0] += 1
    elif a == 0:
        cnt[1] += 1
    elif a < 0:
        cnt[2] += 1

if cnt[1] > 0 or cnt[2]%2 == 0:
    None
else:
    ans -= 2 * min(sei)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = map(int, input().split())
minus_count = 0
total = 0
minimum = 999999999999999999

for a in A:
	total += abs(a)
	if abs(a) < minimum:
		minimum = abs(a)
	if a < 0:
		minus_count += 1
			
if minus_count % 2 == 0:
	print(total)
else:
	print(total - 2 * minimum)

Submission status: Accepted

Solution code:
n = int(input())

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

# 負の数が偶数の時、全てのを非負に変換可能
# 奇数の時、一つを除いて非負に変換可能、絶対値が最小の値を選択する

cnt_minus = sum([1 if v < 0 else 0 for v in a])

list_abs = list(map(abs, a))
sum_abs = sum(list_abs)
min_abs = min(list_abs)

if cnt_minus % 2 == 0:
    ans = sum_abs
else:
    ans = sum_abs - 2*min_abs

print(ans)

Submission status: Accepted

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

neg = 0
for a in A:
    if a<0:
        neg+=1
        
A = [abs(a) for a in A]        
A.sort()

if neg%2==1:
    A[0] = -1*A[0]

print(sum(A))

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
L = [0 for i in range(n)]

for i in range(n):
    if A[i] < 0:
        A[i] = abs(A[i])
        L[i] = L[i] + 1 

if sum(L)%2 != 0:
    B = sum(A)-min(A)*2
else:
    B = sum(A)

print(B)

Submission status: Accepted

Solution code:
from sys import stdin

N = int(stdin.readline().rstrip())
A = [int(x) for x in stdin.readline().rstrip().split()]
if 0 in A:
    print(sum([abs(x) for x in A]))
    exit()
minus = [x for x in A if x < 0]
if len(minus) % 2 == 0:
    print(sum([abs(x) for x in A]))
else:
    absolute = sorted([abs(x) for x in A])
    print(sum(absolute) - 2 * absolute[0])

Submission status: Accepted

Solution code:
N = int(input())
A = input().split()
A = [int(x) for x in A]
b = []
answer = 0
count = 0
for i in range(N):
  if A[i] < 0:
    count = count + 1
  b.append(int(abs(A[i])))
  answer = answer + b[i] 
b.sort()
b.reverse()
if count % 2 == 1:
  answer = 0
  for j in range(N-1):
    answer = answer + b[j]
  answer = answer - b[j+1]
print(answer)

Submission status: Accepted

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

count = 0
for a in A:
	count += (a < 0)

a = sorted(map(abs, A))

if not count % 2:
	print(sum(a))
else:
	print(sum(a[1:]) - a[0])

Submission status: Accepted

Solution code:
def larger(x, y):
	if x > y: return x
	return y

N = int(input())
A = list(map(int, input().split()))
dp1 = [A[0], -A[1]-A[0]] #change
dp2 = [A[0], A[1]+A[0]] #notchange
for i in range(2, N):
	newdp1 = larger(dp1[i-1]+2*A[i-1], dp2[i-1]-2*A[i-1]) - A[i]
	newdp2 = larger(dp1[i-1], dp2[i-1]) + A[i]
	dp1.append(newdp1)
	dp2.append(newdp2)
print(larger(dp1[N-1], dp2[N-1]))

Submission status: Accepted

Solution code:
n=int(input())
al=list(map(int,input().split()))
ans=0
z=True
x=0
m=10**10
for a in al:
    ans+=abs(a)
    z=True if a==0 else False
    x+=1 if a<0 else 0
    m=min(m,abs(a))
if z or x%2==0:
    print(ans)
else:
    print(ans-m*2)

Submission status: Accepted

Solution code:
N=int(input())
list1=list(map(int,input().split()))
dp=[[0,-2**32]]+[[0,0] for i in range(N)]
for i in range(0,N):
    dp[i+1][0]=max(dp[i][0]+list1[i],dp[i][1]-list1[i])
    dp[i+1][1]=max(dp[i][0]-list1[i],dp[i][1]+list1[i])
print(dp[N][0])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
mina = 10**9
minus = 0
sumA = 0
for a in A:
    if a < 0:
        minus += 1
        a = -a
    sumA += a
    mina = min(a, mina)
if minus % 2 == 1:
    sumA -= 2 * mina
print(sumA)

Submission status: Accepted

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

Aodd=[]
Aeven=[]
sumodd=0
sumeven=0
for i in range(N):
    if i%2==0:
        Aeven.append(A[i])
        if A[i]<0:
            sumeven+=1
    else:
        Aodd.append(A[i])
        if A[i]<0:
            sumodd+=1
if sumeven%2==sumodd%2:
    ans=sum(map(abs,A))
else:
    ans=sum(map(abs,A))-min(map(abs,A))*2
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
A = map(int, input().split())
count = 0
B = []
for i in A:
    if(i<0):
        count += 1
    B.append(abs(i))
if(count % 2 == 0):
    print(sum(B))
else:
    print(sum(B) - 2*min(B))

Submission status: Accepted

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

count = 0
for num in nums:
    if num < 0:
        count = count + 1

mode = count % 2 == 1
sumVal = 0
if mode == False:
    for num in nums:
        sumVal = sumVal + abs(num)
else:
    minVal = None
    for num in nums:
        sumVal = sumVal + abs(num)
        if minVal == None or minVal > abs(num):
            minVal = abs(num)
    sumVal = sumVal - (minVal * 2)
print(sumVal)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
mina = abs(A[0])
minus = 0
sumA = 0
for a in A:
    if a < 0:
        minus += 1
        a = -a
    sumA += a
    mina = min(a, mina)
if minus % 2 == 1:
    sumA -= 2 * mina
print(sumA)

Submission status: Accepted

Solution code:
from math import fabs

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

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

goukei = sum(fabs(a) for a in A)

if fu_count % 2 == 1:
    goukei -= 2 * min(fabs(a) for a in A)
print(int(goukei))

Submission status: Accepted

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

if (List.count(0)!=0)|((sum(x<0 for x in List)%2==0)):
    print(sum([abs(List[i]) for i in range(N)]))

else:
    nList = [abs(List[i]) for i in range(N)]
    print(sum(nList)-2*min(nList))

Submission status: Accepted

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

negative_count = 0
for a in A:
    if a < 0:
        negative_count += 1

A_positive = [abs(a) for a in A]

if negative_count % 2 == 0:
    # すべてプラスにできる
    print(sum(A_positive))
else:
    # 1つマイナス
    print(sum(A_positive) - min(A_positive) * 2)

Submission status: Accepted

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

B = [abs(A[i]) for i in range(N)]
A.sort()
B.sort()
count=0
for i in range(N):
    if A[i]<0:
        count+=1

if count%2==0:
    print(sum(B))
else:
    print(sum(B[1:])-B[0])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
a = list(map(int, input().split()))

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

ans = sum(a)
if count % 2 == 0:
    print(ans)
else:
    print(ans - min(a) * 2)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
a = list(map(int, input().split()))

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

if count % 2 == 0:
    print(ans)
else:
    print(ans - min(a) * 2)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
a = list(map(int, input().split()))

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

ans = sum(a)
if count % 2 == 0:
    print(ans)
else:
    print(ans - min(a) * 2)