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

INF = 10**6

ind = INF
for i in range(len(A)):
    if A[i]>=0:
        ind = i
        break
    else:
        A[i] *= -1

if ind == INF:
    ind = N

if ind %2 == 0:
    print(sum(A))
elif ind==N:
    print(sum(A)-2*A[ind-1])
else:
    print(sum(A)-2*min(A[ind], A[ind-1]))

Submission status: Accepted

Solution code:
import bisect
# from collections import Counter, defaultdict, deque, namedtuple
# import copy
# from heapq import heappush, heappop, heapify
# from fractions import gcd
# import itertools
# from operator import attrgetter, itemgetter
# import math

import sys

# import numpy as np

ipti = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)

def main():
    n = int(input())
    a = list(map(int,ipti().split()))
    a.sort()
    neg_count = bisect.bisect_left(a, 0)

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

    if neg_count % 2 == 0:
        ans = sum(a_abs)
    else:
        ans = sum(a_abs[1:]) - a_abs[0]

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

ans = 0
minus_cnt = 0
min_abs = float("inf")
for a in A:
    if a < 0:
        minus_cnt += 1
    ans += abs(a)
    min_abs = min(min_abs, abs(a))

if minus_cnt % 2 == 0:
    print(ans)
else:
    print(ans - min_abs * 2)

Submission status: Accepted

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

a.sort()
for i in range(1,n,2):
    if a[i] + a[i-1] < 0:
        a[i]*=(-1)
        a[i-1]*=(-1)
    else:
        break
    #print((a[i],a[i-1]))
print(sum(a))

Submission status: Accepted

Solution code:
# coding: utf-8
N=int(input())
A=list(map(int,input().split()))
B=[A[i] for i in range(N)]
cnt=0

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

if cnt%2==0:
    ans=sum(B)
else:
    ans=sum(B)-2*min(B)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(S) for S in input().split()]
c = 0
for x in range(n):
  if a[x] < 0:
    a[x] *= -1
    c += 1
    
ans = sum(a)

if c % 2 == 1:
  ans -= 2*min(a)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
abs_min = abs(A[0])
sum_abs = 0
cnt = 0
for x in A:
    abs_min = min(abs_min,abs(x))
    sum_abs += abs(x)
    if x < 0:
        cnt += 1
print(sum_abs if cnt%2 == 0 else sum_abs-2*abs_min)

Submission status: Accepted

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

Submission status: Accepted

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

    n_minus = 0
    mn = float('inf')
    for a in A:
        if a < 0:
            n_minus += 1
        mn = min(mn, abs(a))
    
    abs_sum = sum(map(abs, A))
    if n_minus % 2 == 0:
        print(abs_sum)
    else:
        print(abs_sum - (2 * mn))
    

if __name__ == '__main__':
    solve()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

def main():
    n=int(input())
    a=sorted(list(map(int,input().split())))
    mnum=0
    for i,x in enumerate(a):
        if x>=0:
            mnum=i
            break
    m,p=-a[i-1],a[i]
    if i%2==0:
        print(-sum(a[:i])+sum(a[i:]))
    else:
        print(-sum(a[:i])+sum(a[i:])-2*min(m,p))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

cnt_minus = len([a[i] for i in range(n) if a[i] < 0])

if cnt_minus % 2 == 0:
    print(sum([abs(a[i]) for i in range(n)]))
else:
    mi = min([abs(a[i]) for i in range(n)])
    print(sum([abs(a[i]) for i in range(n)]) - 2 * mi)

Submission status: Accepted

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

minuses = 0
for i in a:
    if i < 0:
        minuses += 1

if minuses % 2 == 0:
    print(sum(a_abs))
else:
    print(sum(a_abs) - 2 * min(a_abs))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
neg = len([x for x in a if x < 0])
# print(neg)
pos = []
for ai in a:
    pos.append(abs(ai)) 
pos.sort()

ans = sum(pos)
if neg % 2 != 0:
    ans -= pos[0] * 2

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
# + - - - - + even -> + + + + + + 全部プラス
# + - - - - - + odd -> どっか一個が -

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

num_negative = np.count_nonzero(A < 0)
A = np.abs(A)
if num_negative % 2 == 0:
    print(A.sum())
else:
    print(A.sum() - A.min() * 2)

Submission status: Accepted

Solution code:
#!/usr/bin/python3
# -*- coding:utf-8 -*-

def main():
  n = int(input())
  As = list(map(int, input().strip().split()))
  nminus = 0
  for i in range(n):
    if As[i] < 0:
      As[i] = abs(As[i])
      nminus += 1
  if As.count(0) >= 1 or nminus%2==0:
    print(sum(As))
  else:
    print(sum(As)-2*min(As))

if __name__=='__main__':
  main()

Submission status: Accepted

Solution code:
import math

def main():
    n = int(input())
    a = [int(t) for t in input().split()]

    m = 0
    if 0 in a or len([x for x in a if x<0])%2==0:
        m = sum([abs(x) for x in a])
    else:
        a_ = [abs(x) for x in a]
        m = sum(a_)-2*min(a_)

    print(m)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
dp = [[0,0] for _ in range(N+1)]
dp[0][1] = -1000000007
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:
def solve(a):
    abs_a = [abs(ai) for ai in a]
    ans = sum(abs_a)

    if len([ai for ai in a if ai < 0]) % 2:
        ans -= min(abs_a) * 2

    return ans


if __name__ == '__main__':
    int(input())
    a = list(map(int, input().split()))

    print(solve(a))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
A.sort()
t = 0
i = 0
while True:
    if A[i]<0:
        A[i] = -1*A[i]
        t+=1
        if i==N-1:
            if t%2==0:
                break
            elif t%2==1:
                A[i] = -1*A[i]
                break
    elif A[i]>=0:
        if t%2==0:
            break
        elif t%2==1:
            X = -1*A[i]+A[i-1]
            Y = -1*A[i-1]+A[i]
            if X>=Y:
                A[i] = -1*A[i]
            else:
                A[i-1] = -1*A[i-1]
            break
    i+=1

print(sum(A))

Submission status: Accepted

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

dp=[[0 for i in range(2)] for i in range(n+1)]
dp[0][0]=0
dp[0][1]=-10**9+1

for i in range(0,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])
#解説AC

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
dp = [[0] * 2 for i in range(n)]
dp[0][0],dp[0][1] = a[0], -a[0]
dp[1][0], dp[1][1] = a[0] + a[1], -a[0] + -a[1]
#dp[i][0]は選ばないdp[i][1]は選ぶ
for i in range(2, n):
    dp[i][0] = max((dp[i-1][0] + a[i]), (dp[i-1][1] + a[i]))
    dp[i][1] = max((dp[i-1][0] + 2 * -a[i-1] + -a[i]), (dp[i-1][1] + 2 * a[i-1] + -a[i]))

#print(dp)
print(max(dp[n-1][0], dp[n-1][1]))

Submission status: Accepted

Solution code:
import math
import copy
from copy import deepcopy
import sys
import fractions
import numpy as np
from functools import reduce
# import statistics
import heapq
sys.setrecursionlimit(100001)

# input = sys.stdin.readline
# sys.setrecursionlimit(10**6)

# ===FUNCTION===

def getInputInt():
    inputNum = int(input())
    return inputNum


def getInputListInt():
    outputData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE===

n = int(input())
data = getInputListInt()

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

data.sort()
if cnt % 2 == 1:
    sum_num -= 2*data[0]

print(sum_num)

Submission status: Accepted

Solution code:
#125_D
N=int(input())
A=list(map(int,input().split()))
abs_A=[abs(a) for a in A]
sum_abs_A=sum(abs_A)

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

print(sum_abs_A if neg%2==0 else sum_abs_A-2*min(abs_A))

Submission status: Accepted

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

ans,cnt,mini=[],0,float('inf')
for a in A:
  if abs(a)!=a: cnt+=1
  ans.append(abs(a))
  mini=min(mini,abs(a))
if cnt%2==0:
  print(sum(ans))
else:
  print(sum(ans)-(mini*2))

Submission status: Accepted

Solution code:
n=int(input())

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

s = 0
mini = 10000000000000
count = 0
for i in t:
    if i < 0:
        count += 1
    s += abs(i)
    mini = min(mini, abs(i))
if count%2 == 0:
    print(s)
else:
    print(s-mini*2)

Submission status: Accepted

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

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

Submission status: Accepted

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

ans = 0
m = 10**10
k = 0
for i in range(N):
    ans += abs(A[i])
    m = min(m,abs(A[i]))
    if A[i] < 0:
        k += 1

if k % 2 == 0:
    print(ans)
else:
    print(ans - 2*m)

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
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()
mic = 0
for i in range(n):
    if a[i]<0: 
        mic += 1
        a[i] *= -1
if mic%2:
    a.sort()
    print(sum(a[1:])-a[0])
else:
    print(sum(a))

Submission status: Accepted

Solution code:
import numpy as np
input()
A = np.array(input().split(), dtype=np.int64)
S = np.sign(A)
A = np.sort(np.abs(A))
a = A[0]
print(A.sum() if 0 in S or not ((S == -1).sum() & 1) else A.sum() - a * 2)

Submission status: Accepted

Solution code:
import numpy as np
input()
A = np.array(input().split(), dtype=np.int64)
S = np.sign(A)
A = np.abs(A)
if 0 in S or not ((S == -1).sum() & 1):
    print(A.sum())
else:
    A.sort()
    print(A.sum() - A[0] * 2)

Submission status: Accepted

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

total = 0
ct = 0
min_num = 10**9
for i in l:
    total += abs(i)
    ct += 1 if i < 0 else 0
    min_num = min(min_num,abs(i))

if ct%2:
    print(total - min_num*2)
else:
    print(total)

Submission status: Accepted

Solution code:
n = int(input())

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

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

Submission status: Accepted

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

neg_cnt = 0
zero_cnt = 0

max_neg_v = -float("inf")
min_pos_v = float("inf")

for i in range(len(a)):
    if a[i] < 0:
        neg_cnt += 1
        max_neg_v = max(max_neg_v, a[i])
    elif a[i] == 0:
        zero_cnt += 1
    else:
        min_pos_v = min(min_pos_v, a[i])

if neg_cnt % 2 == 0 or zero_cnt > 0:
    print(sum(list(map(abs, a))))
else:
    print(max(sum(list(map(abs, a)))-2*min_pos_v, sum(list(map(abs, a)))+2*max_neg_v))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
 
#整数input
def ii(): return int(sys.stdin.readline().rstrip()) #int(input())
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) #list(map(int,input().split()))
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]
#文字列input
def ss(): return sys.stdin.readline().rstrip() #input()
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) #list(input().split())
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]
ninf = -float("inf")
#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？


n=ii()
arr=limii()

dp = [[0]*(2) for _ in range(n+1)]

dp[0][1]=ninf
for i in range(n):
    dp[i+1][0]=max(dp[i][0]+arr[i],dp[i][1]-arr[i])
    dp[i+1][1]=max(dp[i][0]-arr[i],dp[i][1]+arr[i])

print(dp[n][0])

Submission status: Accepted

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

a, b, c = 0, 0, 0
for i in A:
    if i<0:
        a+=1
    elif i>0:
        c+=1
    else:
        b+=1

if b:
    ans = sum(map(abs, A))
elif c:
    for i,j in enumerate(A):
        if j>=0:break

    if a%2==0:
        ans = sum(map(abs, A))
    else:
        for i,j in enumerate(A):
            if j>=0:break
        ans = sum(map(abs, A)) + 2 * max(-abs(A[i-1]), -abs(A[i]))
else:
    if a%2 == 0:
        ans = sum(map(abs, A))
    else:
        ans = sum(map(abs, A[:-1]))+A[-1]

print(ans)

Submission status: Accepted

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

m = 0
ab = []
for i in a:
    if i < 0:
        m+=1
        i = i*-1
    ab.append(i)

if m%2 == 0:
    print(sum(ab))
else:
    ab.sort()
    print(sum(ab[1:])-ab[0] )

Submission status: Accepted

Solution code:
import sys
from fractions import gcd

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

minus_count=0
A_sum=0
A_min=float("inf")
for a in A:
    if a<0:
        minus_count+=1
    a=abs(a)
    A_min=min(A_min,a)
    A_sum+=a

if minus_count%2==0:
    print A_sum
else:
    print A_sum-A_min*2

Submission status: Accepted

Solution code:
n = int(input())
L = list(map(int,input().split()))
L1 = len([x for x in L if x < 0])
L.sort()
for i in range(n):
  if L[i] < 0:
    L[i] = -L[i]
#print(L)
if L1%2 == 0:
  print(sum(L))
else:
  print(sum(L)-2*min(L))

Submission status: Accepted

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

minus = 0
sm = 0
mn = abs(ls[0])

for i in range(n):
    if ls[i] < 0:
        minus += 1
    
    z = abs(ls[i])
    if z < mn:
        mn = z
    
    sm += z

if minus % 2 == 0:
    print(sm)
elif minus % 2 == 1:
    print(sm - 2 * mn)

Submission status: Accepted

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


zero_included = (0 in set(a))
negative_count = sum([item < 0 for item in a])
a_abs = sorted([abs(item) for item in a])

if zero_included or negative_count % 2 == 0:
    print(sum(a_abs))
else:
    print(sum(a_abs[1:]) - a_abs[0])

Submission status: Accepted

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

if A.count(0) >= 1:
    ans = 0
    for i in range(n):
        ans += abs(A[i])
else:
    ans = 0
    parity = 0
    absmin = 10**10
    for i in range(n):
        if A[i] < 0:
            parity += 1
        absmin = min(absmin, abs(A[i]))
        ans += abs(A[i])
    if parity % 2 == 0:
        pass
    else:
        ans = ans - absmin * 2
print(ans)

Submission status: Accepted

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

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

cnt = 0
min_abs = 10 ** 15
for a in A:
    if a < 0:
        cnt += 1
    min_abs = min(min_abs, abs(a))
        
if cnt % 2 == 0:
    print(sum(map(abs, A)))
else:
    print(sum(map(abs, A)) - min_abs * 2)

Submission status: Accepted

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

cnt_m,cnt_z=0,0
for i in range(n):
  if num[i]<0:
    cnt_m+=1
  elif num[i]==0:
    cnt_z+=1

num1=[abs(i) for i in num]  

if cnt_m%2==0:
  ans=sum(num1)
else:
  if cnt_z>0:
    ans=sum(num1)
  else:
    ans=sum(num1)-2*min(num1)
    #print(7,sum(num1),min(num1))
print(ans)

Submission status: Accepted

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

min_abs = abs(a[0])  # a[0], ..., a[n-1]中の絶対値の最小値
sum = 0   # b[0] +  ... + b[n-1]
p_neg = 0  # a[0], ..., a[n-1]中の負の数の数が偶数なら0、奇数なら1
zero_exists = False  # a[0], ..., a[n-1]の中に0があるかどうか

for i in range(n):
    min_abs = min( min_abs, abs(a[i]))
    if a[i] < 0:
        p_neg = 1 - p_neg
        sum += -a[i]
    elif a[i] == 0:
        zero_exists = True
    else:
        sum += a[i]

if zero_exists or p_neg == 0:
    pass
else:
    sum -= 2 * min_abs

print(sum)

Submission status: Accepted

Solution code:
N=input()
A=list(map(int,input().split()))
c=sum([1 for i in A if i<0])
A=list(map(abs,A))
print(sum(A)-min(A)*(1-(-1)**c))

Submission status: Accepted

Solution code:
N = int(input())
A=list(map(int,input().split()))
cnt=sum([1 for i in A if i<0])
A=list(map(abs,A))
ret = sum(A)-min(A)*(1-(-1)**cnt)
print(ret)

Submission status: Accepted

Solution code:
N = int(input())
A=list(map(int,input().split()))
cnt = 0
B=[None]*N
C=10**9+1
for i in range(len(A)):
  B[i]=abs(A[i])
  C=min(C,B[i])
  if A[i]<0:
    cnt+=1
ret = sum(B)-C+C*(-1)**cnt
print(ret)

Submission status: Accepted

Solution code:
N = int(input())
Alist = list(map(int, input().split()))
dp = [[0, 0] for i in range(N+1)]
dp[0][1] = -(3*10**9)
for i in range(1, N+1):
    Ai = Alist[i-1]
    dp[i][0] = max(dp[i-1][0]+Ai, dp[i-1][1]-Ai)
    dp[i][1] = max(dp[i-1][0]-Ai, dp[i-1][1]+Ai)
print(dp[N][0])

Submission status: Accepted

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

cnt_minus = 0
wa = 0
small = 10 ** 10
for i in range(N):
    if A[i] < 0:
        cnt_minus += 1
    wa += abs(A[i])
    small = min(abs(A[i]), small)
    

if cnt_minus % 2 == 0:
    print(wa)
else:
    print(wa - (2 * small))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
sumabs = 0
minuscnt = 0
zerocnt = 0
minabs = 10**9+1
for i in range(n):
  if a[i] < 0:
    minuscnt += 1
  elif a[i] == 0:
    zerocnt += 1
  sumabs += abs(a[i])
  minabs = min(minabs, abs(a[i]))
if zerocnt > 0 or minuscnt % 2 == 0:
  print(sumabs)
else:
  print(sumabs - minabs * 2)

Submission status: Accepted

Solution code:
N = int(input())
*A, = map(int, input().split())
cnt = sum([1 for i in range(N) if A[i] < 0])
if cnt % 2 == 0:
    print(sum([abs(a) for a in A]))
else:
    A.sort(key=abs)
    print(sum([abs(a) for a in A[1:]]) - abs(A[0]))

Submission status: Accepted

Solution code:
import numpy as np

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

dp = [[0, 0] for i in range(n+1)]
dp[0][1] = -np.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_m, cnt_z = 0, 0

for a in A:
    if a < 0:
        cnt_m += 1
    elif a == 0:
        cnt_z += 1

B = list(map(abs, A))

if cnt_m % 2 == 0 or cnt_z > 0:
    print(sum(B))
else:
    print(sum(B) - min(B)*2)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
m=list(map(abs,l))
print(sum(m)-sum(i<0 for i in l)%2*min(m)*2)

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int,input().split()))
m = sorted(list(map(abs,l)))
print(sum(m) - sum(i<0 for i in l)%2*m[0]*2)

Submission status: Accepted

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

minus=0
abssum=0
absmin=10**9
for i in range(0,N):
    if A[i]<0:
        minus=minus+1
    if abs(A[i])<absmin:
        absmin=abs(A[i])
    abssum=abssum+abs(A[i])

if minus%2==0:
    print(abssum)
else:
    print(abssum-2*absmin)

Submission status: Accepted

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

minus_cnt=0
zero_flag=False
mn = 10**10
sm = 0

for i in range(N):
    if a[i] == 0:
        zero_flag=True
    if a[i] < 0:
        minus_cnt+=1
    if mn > abs(a[i]):
        mn = abs(a[i])
    sm+=abs(a[i])

if zero_flag or minus_cnt%2==0:
    print(sm)
else:
    print(sm-2*mn)

Submission status: Accepted

Solution code:
import bisect
def main():
    N = int(input())
    A = list(map(int, input().split()))
    A.sort()
    i = bisect.bisect_left(A, 0)
    j = bisect.bisect_right(A, 0)
    if i % 2 == 0:
        print(sum([abs(i) for i in A]))
    else:
        if i != j:
            print(sum([abs(i) for i in A]))
        elif i == N:
            print(sum([abs(i) for i in A]) - 2 * abs(A[-1]))
        else:
            if abs(A[i-1]) > A[i]:
                print(sum([abs(i) for i in A]) - 2 * abs(A[i]))
            else:
                print(sum([abs(i) for i in A]) - 2 * abs(A[i-1]))
main()

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
m = len(list(filter(lambda x: x < 0, a)))
s = [abs(i) for i in a]
if m % 2 == 0 or 0 in a:
    print(sum(s))
else:
    print(sum(s) - 2 * min(s))

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
a = np.array(list(map(int,input().split())))
b = a[a<0]
if len(b)%2==0:
    print(int(sum(np.abs(a))))
else:
    print(int(sum(np.abs(a))-2*min(np.abs(a))))

Submission status: Accepted

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

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

nums = [abs(num) for num in nums]
if cnt % 2 == 0:
    print(sum(nums))
else:
    print(sum(nums) - 2 * min(nums))

Submission status: Accepted

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

cnt = 0
b = []
for i in range(n):
    if a[i] < 0:
        cnt += 1
    b.append(abs(a[i]))

if cnt % 2 == 0:
    ans = sum(b)
    print(ans)
else:
    ans = sum(b) - 2 * min(b)
    print(ans)

Submission status: Accepted

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

m = float("inf")
cnt = 0
ans = 0
for x in a:
    if x < 0:
        cnt += 1
    
    m = min(m, abs(x))
    ans += abs(x)

if cnt%2:
    ans -= m*2

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

List=list(map(int,input().split()))
cnt = 0
for i in range(len(List)):
  if List[i] < 0:
    List[i] = int(-List[i])
    cnt +=1
    
if cnt % 2 == 0:
  print(int(sum(List)))
else:
  a = int(min(List))
  b = int(sum(List))
  print(int(b-a*2))

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
mval = float('inf')
minus_cnt = 0
isZero = False
ans = 0

for a in A:
    if a < 0: minus_cnt += 1
    if a == 0: isZero
    mval = min(mval, abs(a))
    ans += abs(a)

if isZero or minus_cnt % 2 == 0: print(ans)
else: print(ans - mval*2)

Submission status: Accepted

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

minus_count = sum([1 for v in aa if v<0])
a = [abs(v) for v in aa]

if minus_count % 2 == 0:    
  print(sum(a))
else:
  print(sum(sorted(a)[1:]) - min(a))

Submission status: Accepted

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

b=list(filter(lambda x:x<0, a))
a = list(map(abs, a))


if len(b)%2==0:
    print(sum(a))
else:
    print(sum(a)-min(a)*2)

Submission status: Accepted

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

minus_cnt = 0
min_abs = 10 ** 15
for i in range(N):
  if A[i] < 0:
    minus_cnt += 1
    A[i] *= -1
  min_abs = min(min_abs, abs(A[i]))

print(sum(A) - 2 * min_abs * (minus_cnt % 2))

Submission status: Accepted

Solution code:
def solve(n, A):
    if A.count(0) > 0:
        return sum(map(abs, A))
    total = 0
    minval = 10 ** 9
    count = 0
    for a in A:
        total += abs(a)
        if a < 0:
            count += 1
        if abs(a) < minval:
            minval = abs(a)
    return total - (2 * minval if count % 2 == 1 else 0)

_n = int(input())
_A = list(map(int, input().split()))
print(solve(_n, _A))

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    _ = int(input())
    A = list(map(int, input().split()))
    A_abs = [abs(a) for a in A]
    mc = sum([1 for a in A  if a < 0])

    ans = sum(A_abs)
    if mc % 2 != 0:
        ans -= min(A_abs) * 2
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

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

class Solution(object):
    def flipping_signs(self,N, A):

        oddeven = 1
        cnt = 0
        B = [0]*N
        for i in range(N):
            B[i] = abs(A[i])
            if A[i] == 0:
                oddeven = 0
            elif A[i] < 0:
                cnt += 1
        if cnt%2 == 0: oddeven = 0

        return sum(B)-2*min(B) if oddeven != 0 else sum(B) 

def main():

    N0 = int(read())
    A0 = tuple(map(int, read().split()))



    res = Solution().flipping_signs(N0, A0)

    print(res)
 
if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

B = sorted(map(abs, A))
if negative % 2 == 1:
    B[0] = -B[0]

print(sum(B))

Submission status: Accepted

Solution code:
N, *A = map(int, open(0).read().split())
negative_num = sum(1 if a < 0 else 0 for a in A)
positive_num = list(abs(n) for n in A)

if negative_num % 2 == 0:
    print(sum(positive_num))
else:
    print(sum(positive_num) - 2*min(positive_num))

Submission status: Accepted

Solution code:
import sys
import numpy as np 

n = int(sys.stdin.readline().rstrip())
a = np.array(sys.stdin.readline().split(), dtype=np.int64)

def main():
    tmp = np.absolute(a)
    if np.count_nonzero(a) != n:
        return tmp.sum()
    else:
        if np.count_nonzero(a < 0) & 1:
            return tmp.sum() - np.amin(tmp) * 2
        else:
            return tmp.sum()

if __name__ == '__main__':
    ans = main()
    print(ans)

Submission status: Accepted

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

num_negative = sum(1 for x in A if x < 0)
positives = [abs(x) for x in A]
if num_negative % 2 == 0:
    print(sum(positives))
else:
    print(sum(positives) - min(positives) * 2)

Submission status: Accepted

Solution code:
import heapq
N = int(input())
A = list(map(int, input().split()))
heapq.heapify(A)
while True:
  m = heapq.heappop(A)
  n = heapq.heappop(A)
  if m + n >= 0:
    heapq.heappush(A, m)
    heapq.heappush(A, n)
    break
  else:
    heapq.heappush(A, -m)
    heapq.heappush(A, -n)
print(sum(A))

Submission status: Accepted

Solution code:
from numpy import*


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


if 0 in a:
  print(sum(b));exit()
  
  
minus_cnt=sum(a<0)


if minus_cnt%2==0:
  print(sum(b));exit()
  
  
print(sum(b)-min(b)*2)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
zero_in_a=(0 in a) #bool

if zero_in_a:
  tmp=[abs(i) for i in a]
  print(sum(tmp))
  exit()

minus_cnt=0
b=[]
for i in a:
  if i<0:
    minus_cnt+=1
    b.append(-i)
  else:
    b.append(i)

if minus_cnt%2==0:
  print(sum(b))
  exit()
  
c=min(b)
print(sum(b)-2*c)

Submission status: Accepted

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

c = 0

for i in A:
    if i < 0:
        c += 1

s = [abs(i) for i in A]

if c % 2 == 0:
    print(sum(s))
else:
    print(sum(s)-2*min(s))

Submission status: Accepted

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

B = []
i = 0
while i < N:
  a = A[i]
  if a >= 0:
    B.append(a)
    i += 1
  else:
    if i+1 < N:
      if A[i+1] <= 0:
        B.extend([-a, -A[i+1]])
        i += 2
      else:
        B.append(-a)
        i += 1
        A[i] *= -1
    else:
      B.append(a)
      i += 1

if B[-1] >= 0:
  print(sum(B))
else:
  b = - B.pop()
  m = min(B)
  print(sum(B) + b - 2*min(m, b))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
N=int(input())
A=[int(x) for x in sys.stdin.readline().split()]

abssum=0
negative=0
mi=100000000000000
for i in range(N):
	if A[i]<0:
		negative+=1
	abssum+=abs(A[i])
	mi=min(mi,abs(A[i]))

if negative%2==1:
	abssum-=2*mi
print(abssum)

Submission status: Accepted

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

abs_sum=0
nega=0
mini=10**9+7

for i in range(N):
    a=abs(A[i])
    abs_sum+=a
    mini=min(mini,a)
    if A[i]<0:
        nega+=1

if nega%2==1:
    abs_sum-=2*mini
print(abs_sum)