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:
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())
    A = list(map(int, input().split()))

    cnt = 0
    total = 0
    mi = f_inf
    for a in A:
        total += abs(a)
        if a < 0:
            cnt += 1
        if mi > abs(a):
            mi = abs(a)

    if cnt % 2 == 0:
        print(total)
    else:
        print(total - mi * 2)


def resolve2():
    n = int(input())
    A = list(map(int, input().split()))

    dp = [[0 for _ in range(2)] for _ in range(n + 1)]
    dp[0][1] = -f_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])


if __name__ == '__main__':
    resolve2()

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())
    A = list(map(int, input().split()))

    cnt = 0
    total = 0
    mi = f_inf
    for a in A:
        total += abs(a)
        if a < 0:
            cnt += 1
        if mi > abs(a):
            mi = abs(a)

    if cnt % 2 == 0:
        print(total)
    else:
        print(total - mi * 2)


if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
absA = [abs(a) for a in A]

if sum(True if a < 0 else False for a in A) % 2 == 0:
  ans = sum(absA)
else:
  ans = sum(absA) - 2 * min(absA)
  
print(ans)

Submission status: Accepted

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

minus = 0
x = abs(A[0])
cnt = 0
for a in A:
    minus += (a < 0)
    a = abs(a)
    x = min(x, a)
    cnt += a

if x == 0:
    ans = cnt
elif minus % 2 == 0:
    ans = cnt
else:
    ans = cnt - 2 * x
print(ans)

Submission status: Accepted

Solution code:
def minus(n):
  if n>=0:
    return 0
  else:
    return 1
N=int(input())
L=list(map(int,input().split()))
c=sum(list(map(minus,L)))
if c%2==0:
  t=sum(list(map(abs,L)))
  print(t)
else:
  L=list(map(abs,L))
  print(sum(L)-min(L)*2)

Submission status: Accepted

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

min_idx = 0
min_val = float('inf')

for i, a in enumerate(A):
    if abs(a) < min_val:
        min_idx = i
        min_val = abs(a)


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


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


print(sum(A))

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline
N = int(input())
A = list(map(int, input().split()))
m = 0
zero = 0
ans = 0
odd = 10**9
for i in A:
    if i < 0:
        m += 1
    if i == 0:
        zero = 1
    ans += abs(i)
    if odd > abs(i):
        odd = abs(i)
if m %2 == 0 or zero:
    print(ans)
else:
    print(ans-odd*2)

Submission status: Accepted

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

B = A.copy()

K = 0

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

Tashita = sum(B)

if K%2 != 0:
	Tashita-=2*min(B)

print(Tashita)

Submission status: Accepted

Solution code:
import collections

n = int(raw_input())
elems = map(int, raw_input().split(' '))
a = min(map(abs, elems))
s = sum(map(abs, elems))
count = 0
for c in elems:
	if c < 0: count +=1		
a = a if count % 2 else 0

print s - 2*a
"""
+++++----+++

obs 1
if the connected component has || even cardinality % 2==0


obs 2
if the connected component has odd cardinality I can reduce it to 1 
and move the - sign around 

obs 3
as I can move things around, I can merge ccs 


a = 0 if even number of negative signs else min([|v| for v in nums])
return sum([abs(v) for v in nums]) - a

"""

Submission status: Accepted

Solution code:
import sys
import time
#from collections import deque, Counter, defaultdict
#from fractions import gcd
#import bisect
#import heapq
import math
start=time.time()
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
inf = 10**18
MOD = 1000000007
ri = lambda : int(input())
rs = lambda : input().strip()
rl = lambda : list(map(int, input().split()))
n = rl()
a=rl()
cnt=0
for i in a:
    if i<0:
        cnt+=1
if cnt%2==0:
    print(sum(map(abs,a)))
else:
    print(sum(map(abs,a))-2*min(map(abs, a)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

N = int(input())
A = list(map(int, sys.stdin.readline().rsplit()))

cnt = 0
S = 0
minA = 10 ** 9
for a in A:
    if a < 0:
        cnt += 1
    minA = min(minA, abs(a))
    S += abs(a)

if cnt % 2 == 0:
    res = S
else:
    res = S - 2 * minA

print(res)

Submission status: Accepted

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

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

    abs_sum = sum(map(abs,A))
    if n_negative % 2 == 0:
        print(abs_sum)
    else:
        print(abs_sum - 2 * min(map(abs,A)))

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
n= int(input())
a = list(map(int,input().split()))
mc = 0
mi = 999999999
hasz=False
for i in range(n):
    if a[i]==0:hasz=True
    else:
        if a[i]<0:
            mc+=1
            a[i]*=-1
        mi = min(mi,a[i])
if hasz or mc%2==0:print(sum(a))
else:print(sum(a)-mi*2)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
"""
マイナスの数偶数→いける
マイナスの数奇数→１こあまる
"""
b = [abs(a[i]) for i in range(n)]


"""
マイナスの個数は
"""
tmp = 0
for i in a:
	if i < 0:
		tmp += 1

if tmp % 2 == 0:
	print(sum(b))
else:
	sumb = sum(b)
	ans = 0
	for i in range(n):
		ans = max(sumb-2*b[i], ans)
	print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
mi = float('INF')
t=0
for i in range(n):
    if abs(a[i])<mi:
        mi=abs(a[i])
        t=i
a1=a.copy()
sum1=0
for i in range(n-2):
    if a1[i]<0:
        sum1+=a1[i]*(-1)
        a1[i+1]*=-1
    else:
        sum1+=a1[i]
if a1[n-2]+a1[n-1]>(a1[n-2]+a1[n-1])*(-1):
    sum1+=a1[n-2]+a1[n-1]
else:
    sum1+=(a1[n-2]+a1[n-1])*(-1)

sum2=0
for i in range(n-2):
    if i==t and a[i]<0:
        sum2+=a[i]
        continue
    elif i==t and a[i]>0:
        sum2-=a[i]
        a[i+1]*=-1
        continue
    if a[i]<0:
        sum2+=a[i]*(-1)
        a[i+1]*=-1
    else:
        sum2+=a[i]
if a[n-2]+a[n-1]>(a[n-2]+a[n-1])*(-1):
    sum2+=a[n-2]+a[n-1]
else:
    sum2+=(a[n-2]+a[n-1])*(-1)
print(max(sum1,sum2))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
neg_parity = sum(a < 0 for a in A) % 2
A = sorted(a if a >= 0 else -a for a in A)
print(sum(A) - neg_parity * 2 * A[0])

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
neg_cnt = sum(a < 0 for a in A)
A = sorted(a if a >= 0 else -a for a in A)
if neg_cnt % 2:
    print(sum(A) - 2 * A[0])
else:
    print(sum(A))

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
a = np.array([int(i) for i in input().split()])
if 0 in a or len(a[a < 0]) % 2 == 0:
    print(int(np.abs(a).sum()))
else:
    m = np.abs(a).min()
    print(int(np.abs(a).sum() - 2 * m))

Submission status: Accepted

Solution code:
def main():
    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]
    if c % 2 == 0:
        print(sum(A))
    else:
        print(sum(A) - 2 * min(A))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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


if count%2!=0:
    print(sum(arr)-2*min(arr))
else:
    print(sum(arr))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n, *s = map(int, open(0).read().split())
v = list(map(lambda x: abs(x), s))
c = 0
for i in s:
    c += i < 0
if c % 2 == 0:
    print(sum(v))
else:
    print(sum(v) - 2*min(v))

Submission status: Accepted

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

ans = 0
s = 0
m = float('inf')

for i in A:
    i = abs(i)
    s += i
    m = min(m, i)

minus_num = len(list(filter(lambda x: x < 0, A)))

if minus_num % 2 == 0:
    print(s)
else:
    print(s-2*m)

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])

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

Submission status: Accepted

Solution code:
import numpy as np
import heapq

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

if len([i for i in A if i < 0]) % 2 == 0:
    A = np.abs(A)
    ans = sum(A)
else:
    A = np.abs(A)
    A = list(A)
    heapq.heapify(A)
    a = heapq.heappop(A)
    ans = sum(A) - a
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
ls = list(map(int,input().split()))
p = 0
k = float("inf")
q = 0
for i in range(n):
  p += abs(ls[i])
  if k > abs(ls[i]):
    k = abs(ls[i]) 
di = [e for e in ls if e <= 0]
if len(di) % 2== 0:
  print(p)
else:
  print(p-2*k)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
p = sum(ai < 0 for ai in a)
ans = sum(map(abs, a))
if p & 1:
    ans -= 2 * min(abs(ai) for ai in a)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = [0] + list(map(int, input().split()))

dp = [[0, 0] for _ in range(N+1)]
# dp[i][j]: j=0 -> A[1] + ... + A[i] の MAX
#           j=1 -> A[i] and A[i+1] を反転させた状態での和の最大値

dp[0][0] = 0
dp[0][1] = -float("inf")

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

print(dp[N][0])

Submission status: Accepted

Solution code:
from sys import stdin, setrecursionlimit
from bisect import bisect_right


def main():
    input = stdin.buffer.readline
    n = int(input())
    a = list(map(int, input().split()))
    a.sort()

    idx = bisect_right(a, 0)

    if idx % 2 == 0:
        print(sum(a[idx:]) - sum(a[:idx]))
    elif idx == n:
        print(a[idx - 1] - sum(a[:idx - 1]))
    else:
        print(sum(a[idx:]) - sum(a[:idx]) - 2 * min(-a[idx - 1], a[idx]))


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Submission status: Accepted

Solution code:
import sys

input = lambda: sys.stdin.readline().rstrip()


def solve():
    N = int(input())
    A = list(map(int, input().split()))
    pa = [abs(a) for a in A]

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

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

    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Accepted

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


def main():
    N = int(input())
    A = list(map(int, input().split()))
    A.sort()
    ans = sum(A)
    for i in range(0,N-1,2):
        # print(i)
        tmp = ans - 2*(A[i]+A[i+1])
        if tmp > ans:
            ans = tmp
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

minus_cnt = 0
for i in range(n):
  if a[i] < 0:
    minus_cnt += 1
    a[i] *= -1
  
s = sum(a)
a.sort()
if(minus_cnt % 2 == 1):
  s -= a[0] * 2

print(s)

Submission status: Accepted

Solution code:
#他の数字はそのままで，任意の2数の符号を同時に反転させることが可能．マイナスの数の偶奇で場合分け
n = int(input())
a = list(map(int,input().split()))
aabs = [i if i>=0 else i*(-1) for i in a]
MIN = min(aabs)
ans = 0
mcount = 0

for i in range(n):
  if a[i] < 0:
    mcount += 1
    
if mcount%2 == 0:
  for i in a:
    ans += abs(i)
  print(ans)
else:
  for i in a:
    ans += abs(i)
  print(ans - 2*MIN)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
aabs = [i if i>=0 else i*(-1) for i in a]
MIN = min(aabs)
ans = 0
mcount = 0

for i in range(n):
  if a[i] < 0:
    mcount += 1
    
if mcount%2 == 0:
  for i in a:
    ans += abs(i)
  print(ans)
else:
  for i in a:
    ans += abs(i)
  print(ans - 2*MIN)

Submission status: Accepted

Solution code:
import sys

N = int(sys.stdin.readline().strip())
A = map(int, sys.stdin.readline().strip().split())

ans = 0
neg_count = 0
zero_count = 0
a_min = float("inf")
for a_i in A:
    ans += abs(a_i)
    a_min = min(a_min, abs(a_i))
    if a_i == 0:
        zero_count += 1
    elif a_i < 0:
        neg_count += 1

#print(ans)
if neg_count % 2 == 0 or zero_count > 0:
    print(ans)
else:
    #print(a_min)
    print(ans - 2 * a_min)

Submission status: Accepted

Solution code:
import sys, re
from math import ceil, floor, sqrt, pi, factorial#, gcd
from copy import deepcopy
from collections import Counter, deque
from heapq import heapify, heappop, heappush
from itertools import accumulate, product, combinations, combinations_with_replacement
from bisect import bisect, bisect_left
from functools import reduce
from decimal import Decimal, getcontext
# input = sys.stdin.readline 
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_list(N): return [s_list() for _ in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 6)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    n = i_input()
    num_list = i_list()

    hifu_count = 0
    zero_count = num_list.count(0)

    for num in num_list:
        if num < 0:
            hifu_count += 1

    count = 0
    for num in num_list:
        count += abs(num)

    if zero_count >= 1 or hifu_count % 2 == 0:
        pass
    else:
        min_num = INF
        for num in num_list:
            min_num = min(min_num, abs(num))
        count -= min_num * 2
        
    print(count)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from sys import stdin, stdout
input = stdin.readline
print = stdout.write
n = int(input())
arr = [int(i) for i in input().split()]
sm = sum(abs(i) for i in arr)
mn = min(abs(i) for i in arr)
nc = sum(i < 0 for i in arr)
if nc % 2 == 0: print(str(sm))
else: print(str(sm - 2 * mn))

Submission status: Accepted

Solution code:
import numpy as np
n=int(input())
a=np.array(list(map(int,input().split())))
ans=sum(np.abs(a))
if 0 in a:
    print(ans)
else:
    b=np.where(a<0)
    if np.size(b)%2==0:
        print(ans)
    else:
        ans-=2*min(np.abs(a))
        print(ans)

Submission status: Accepted

Solution code:
# 0が一つでもあれば、すべて正の数にできる
# そうでない場合、負の数の数が奇数の場合、負の数を一つ（最も絶対値が小さい）だけ残す

import sys
readline = sys.stdin.readline

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

ans = 0
minabs = 10 ** 9 + 1
haszero = False
minuscnt = 0
for i in range(len(A)):
  if A[i] == 0:
    haszero = True
  elif A[i] < 0:
    minuscnt += 1
  ans += abs(A[i])
  if abs(A[i]) < minabs:
    minabs = abs(A[i])
    
if haszero:
  print(ans)
elif minuscnt % 2 == 1:
  print(ans - minabs * 2)
else:
  print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

minus = 0

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


ans = 0
minA = float('inf')
for a in A:
    ans += abs(a)
    if minA > abs(a):
        minA = abs(a)

if 0 in A or minus % 2 == 0:
    print(ans)
    exit(0)

ans -= 2 * minA
print(ans)

Submission status: Accepted

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

    #print(A)

if A[N-1] >= 0:
    print(sum(A))
    exit()

A = A[::-1]
A[0] *= -1
idx = A.index(min(A))
#print(idx)
A[0] *= -1

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

    #print(A)
    
print(sum(A))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations # (string,3) 3回
from collections import deque
from collections import defaultdict
import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#

import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
n = I()
A = readInts()
minus = 0
for i in range(n):
    if A[i] < 0:
        minus += 1
A = sorted(A,key = lambda x:abs(x))
#print(A)
#print(A)
sumA = sum(list(abs(v) for v in A))
#print(sumA)
if minus % 2:
    print(sumA - abs(A[0])*2)
else:
    print(sumA)

Submission status: Accepted

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

mns = len([i for i in A if i < 0])

if mns % 2 == 0:
  ans = sum([abs(i) for i in A])
else:
  ans = sum([abs(i) for i in A]) - 2 * min([abs(i) for i in A])
  
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def resolve():
    N = int(input())
    A = [0]+list(map(int, input().split()))
    dp = [[None for _ in range(2)] for _ in range(N+1)]
    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], 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 '__main__' == __name__:
    resolve()

Submission status: Accepted

Solution code:
def resolve():
    N = int(input())
    A = list(map(int, input().split()))
    absA = list(map(abs, A))
    negcnt = 0
    for a in A:
        if a < 0:
            negcnt += 1
    if 0 in A or negcnt%2==0:
        print(sum(absA))
    else:
        print(sum(absA)-2*min(absA))



if '__main__' == __name__:
    resolve()

Submission status: Accepted

Solution code:
n = int(input())
sum_abs_a = 0
min_abs_a = 10 ** 9
minus_odd = False
for a in map(int, input().split()):
  if a < 0:
    minus_odd = not minus_odd
    a = -a
  sum_abs_a += a
  min_abs_a = min(min_abs_a, a)
if minus_odd:
  print(sum_abs_a - 2 * min_abs_a)
else:
  print(sum_abs_a)

Submission status: Accepted

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

dp = [[0,0] for i in range(n+1)]
dp[0][1] = (-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 = int(input())
a = list(map(int, input().split()))
cnt = 0
m = float('inf')
for i in range(n):
    if a[i] < 0:
        a[i] *= -1
        cnt += 1
    m = min(m, abs(a[i]))
if cnt % 2:
    print(sum(a) - 2 * m)
else:
    print(sum(a))

Submission status: Accepted

Solution code:
n = int(input())
dat = list(map(int, input().split()))
maxval = -9999999999999999999
maxind = -1
numminus = 0
dat2 = []

for i in range(n):
    if dat[i] < 0:
        numminus += 1
    dat2.append(abs(dat[i]))

dat2.sort()
#print(numminus)
#print(dat2)

if numminus %2 == 0:
    print(sum(dat2))
else:
    dat2[0] *= -1
    print(sum(dat2))

Submission status: Accepted

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

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

if x%2 == 0:
    print(sum(li))
else:
    print(sum(li)-min(li)*2)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Mon Jun  1 13:59:38 2020

@author: NEC-PCuser
"""

N = int(input())
A = list(map(int, input().split()))
minusCount = 0
ans = 0
miniumAbs = 10 ** 9
for i in range(0, len(A)):
    if (A[i] < 0):
        minusCount += 1
    if (miniumAbs > abs(A[i])):
        miniumAbs = abs(A[i])
    ans += abs(A[i])
if minusCount %2 == 0:
    print(ans)
else:
    print(ans - 2 * miniumAbs)

Submission status: Accepted

Solution code:
n,*a=map(int,open(0).read().split())
b=list(map(abs,a))
print(sum(b)-sum(i<0 for i in a)%2*min(b)*2)

Submission status: Accepted

Solution code:
n,*a=map(int,open(0).read().split())
b=sum(i<0 for i in a)%2
a=list(map(abs,a))
print(sum(a)-min(a)*2*b)

Submission status: Accepted

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

Submission status: Accepted

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

count = 0
for x in a:
    if x == 0:
        print(sum(abs_a))
        exit()
    elif x < 0:
        count += 1
if count % 2 == 1:
    mins = min(abs_a)
    print(sum(abs_a) - 2 * mins)
else:
    print(sum(abs_a))

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,copy
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n = inp()
a = inpl()
cnt = 0
for i in range(n):
    if a[i] < 0:
        cnt += 1
a = [abs(x) for x in a]
a.sort()
if cnt%2:
    a[0] = -a[0]
print(sum(a))

Submission status: Accepted

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

dp = [[0] * 2 for _ in range(N+1)]#dp[i][0] = A1からAiの最大値(Ai+1は反転なし)

dp[0][0] = 0
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 = int(input())
A = list(map(int,input().split()))
Sum = 0
Min = float("inf")
cnt = 0
zero = 0

for i in A:
    Min = min(Min,abs(i))
    Sum  += abs(i)
    if i < 0:
        cnt += 1
    elif i == 0:
        zero += 1

if cnt % 2 == 0:
    print(Sum)
elif zero > 0:
    print(Sum)
else:
    print(Sum-2*Min)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
negative = []
positive = []
cnt=0
for i in range(n):
  if a[i] < 0:
    cnt += 1
    negative.append(a[i])
  else:
    positive.append(a[i])
if cnt % 2 == 0:
  print(sum(map(lambda x: abs(x), a)))
elif cnt == len(a):
  a.sort()
  b = a[-1]
  a.remove(a[-1])
  print(b+sum(map(lambda x: abs(x), a)))
else:
  negative.sort()
  positive.sort()
  if abs(negative[-1]) < positive[0]:
    a.remove(negative[-1])
    print(negative[-1]+sum(map(lambda x: abs(x), a)))
  else:
    a.remove(positive[0])
    print(-positive[0]+sum(map(lambda x: abs(x), a)))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
abs_min = 10 ** 9
minus = 0
total = 0
for x in A:
    if x < 0:
        minus += 1
        x = - x
    if abs_min > x:
        abs_min = x
    total += x
if minus % 2 == 0:
    print(total)
else:
    print(total - abs_min * 2)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
c = 0
m = 10**10
flag = 0
ans = 0
for i in a:
    if i == 0:
        flag = 1
    if i < 0:
        c += 1
    ans += abs(i)
    m = min(m,abs(i))
if c%2 == 0:
    flag = 1
if flag:
    print(ans)
else:
    print(ans-(2*m))

Submission status: Accepted

Solution code:
def main():
    s = open(0).read()
    b = s.count('-')
    z = s.count(' 0 ')
    _, *c, = map(int, s.replace('-', '').split())

    d = min(c)
    e = sum(c)

    if b % 2 == 0 or z > 0:
        print(e)
    else:
        print(e - 2 * d)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    s = open(0).read()
    b = s.count('-')
    _, *c, = map(int, s.replace('-', '').split())
    z = c.count(0)
    d = min(c)
    e = sum(c)

    if b % 2 == 0 or z > 0:
        print(e)
    else:
        print(e - 2 * d)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    n, *a = map(int, open(0).read().split())
    b = [i for i in a if i <= 0]
    *ab, = map(abs, a)
    c = sum(ab)
    if len(b) % 2 == 0:
        print(c)
    else:
        print(c - 2 * min(ab))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    n, *a = map(int, open(0).read().split())
    b = [i for i in a if i <= 0]
    *ab, = map(lambda x: abs(x), a)
    c, d = sum(a), sum(b)
    if len(b) % 2 == 0:
        print(c - 2 * d)
    else:
        print(c - 2 * d - 2 * min(ab))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import numpy as np

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

# 負の数が奇数か偶数か
cnt = np.where(a < 0)[0].shape[0]
a = np.abs(a)

print(a.sum() if cnt % 2 == 0 else a.sum() - a.min() * 2)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.sort()
negative=[]
positive=[]
for ele in a:
    if ele<0:
        negative.append(ele)
    else:
        positive.append(ele)
if len(negative)==0:
    print(sum(a))
else:
    if len(negative)%2==0:
        print(-sum(negative)+sum(positive))
    else:
        if len(positive)==0:
            print(-sum(negative)+2*negative[-1])
        elif -negative[-1]>positive[0]:
            print(-sum(negative)+sum(positive[1:])-positive[0])
        else:
            print(-sum(negative[:-1])+sum(positive)+negative[-1])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache


#############
# Constants #
#############

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

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)


#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

#####GCD#####
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

#############
# Main Code #
#############

N = I()
A = IL()

minus = 0
newA = []
for a in A:
  if a<0:
    minus += 1
  newA.append(abs(a))
  
if minus%2 == 0:
  print(sum(newA))
else:
  print(sum(newA)-2*min(newA))