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:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math

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

A.sort()
cnt_mi = 0
if A[0] >= 0:
    ans = sum(A)
    print(ans)
    exit()

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

if cnt_mi%2 == 0:
    ans = sum(A)
    print(ans)
    exit()

A.sort()

ans = sum(A)-2*A[0]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = sorted(list(map(int,input().split())))
t = 0
for e in A:
    if e < 0:
        t += 1
B = sorted([abs(e) for e in A])

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

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    arr = list(map(int, input().split()))
    mi = 1000000000
    s = 0
    cnt = 0
    for i in range(n):
        if arr[i] < 0:
            cnt += 1
            s -= arr[i]
            mi = min(mi, -arr[i])
        else:
            s += arr[i]
            mi = min(mi, arr[i])
    if cnt & 1:
        print(s - 2 * mi)
    else:
        print(s)
    return 0

main()

Submission status: Accepted

Solution code:
n=int(input())
a=[int(x) for x in input().split()]
num_minus=0
a_modified=[]
for ai in a:
    if ai<0:
        num_minus+=1
        a_modified.append(-ai)
    else:
        a_modified.append(ai)
if num_minus%2==0:
    print(sum(a_modified))
else:
    print(sum(a_modified)-2*sorted(a_modified)[0])
# print(sorted(a_modified)[0])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#
import sys
import math
import numpy as np
import itertools


# ひとつ入力
n = int(input())
# リストに入れる
a = [int(i) for i in input().split()] 

fugou = 0
saishou = 999999999999
goukei = 0

for i in range(n):
    if a[i] < 0:
        fugou += 1
    goukei += abs(a[i])
    saishou = min(saishou,abs(a[i]))
if fugou % 2 == 0:
    print(goukei)
else:
    print(goukei - 2*saishou)

Submission status: Accepted

Solution code:
# 2019/04/27
# AtCoder Beginner Contest 125 - D

# Input
n = int(input())
d = list(map(int,input().split()))

mcnt = 0
absmin = 10**10
abssum = 0

# Sum abs & Count minus & Search abs(min)
for i in range(n):
    abssum += abs(d[i])
    if d[i] < 0:
        mcnt += 1
    if absmin > abs(d[i]):
        absmin = abs(d[i])
if mcnt % 2 == 0:
    ans = abssum
else:
    ans = abssum - (absmin * 2)

# Output
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
A = list(map(int, input().split()))
A = np.array(A, dtype = 'int64')
k = np.where(A < 0)
l = len(k[0])
A = np.abs(A)
if l % 2 == 0:
    print(np.sum(A))
else:
    print(np.sum(A) - 2 * np.min(A))

Submission status: Accepted

Solution code:
N = int(input())
A = sorted([int(x) for x in input().split()])
ans, p = 0, 0
for i in range(N):
    p = i
    if A[i] >= 0:
        break
for i in range(N):
    if i < p:
        ans -= A[i]
    else:
        ans += A[i]
if p % 2 != 0:
    ans += max(2*A[p-1],-2*A[p])
print(ans)

Submission status: Accepted

Solution code:
#全体-絶対値一番小さいやつ*2
N = int(input())
A = list(map(int,input().split()))
# N=3
# A = [-10,5,-4]
ans = 0
minnum = abs(A[0])
numminus = 0
for i in range(N):
    if A[i]<0:
        numminus+=1
    ans += abs(A[i])
    minnum = min(minnum,abs(A[i]))
    #print(minnum,ans)
if numminus%2 == 1:
    ans -= minnum*2
print(ans)

Submission status: Accepted

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

minuses = sum(i < 0 for i in ls)

if minuses % 2 == 0:
	print(sum(abs(i) for i in ls))
else:
	absls = [abs(i) for i in ls]
	minimum = min(absls)
	print(sum(absls) - minimum * 2)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time
 
sys.setrecursionlimit(10**7)
inf = 10**20
MOD = 998244353
#########################
N = int(input())
a = list(map(int,input().split()))
sumOfAbsValues = 0
minimumValueAbs = 10**10
minusSignParity = 0
zeroExists = False
for value in a:
    if value == 0:
        zeroExists = True
    absValue = abs(value)
    sumOfAbsValues += absValue
    minimumValueAbs = min(minimumValueAbs,absValue)
    if value < 0:
        minusSignParity += 1
if zeroExists:
    ans = sumOfAbsValues
elif minusSignParity%2 == 0:
    ans = sumOfAbsValues
else:
    ans = sumOfAbsValues - 2*minimumValueAbs
print(ans)

Submission status: Accepted

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

out = 0
negative_cnt = 0
min_abs = 1000000000
for _a in a:
    tmp = abs(_a)
    if _a <= 0:
        negative_cnt += 1
    min_abs = min(min_abs, tmp)
    out += tmp

if negative_cnt % 2 != 0:
    out -= min_abs * 2

print(out)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = []
kosu = 0
mini = 10**9

for i in range(n):
    if a[i] < 0:
        b.append(-a[i])
        kosu += 1
    else:
        b.append(a[i])
    mini = min(mini, b[i])

ans = sum(b)

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

Submission status: Accepted

Solution code:
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

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

Submission status: Accepted

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

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 * a[i]

if minus % 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()))

ans = 0
abs_mina = 10 ** 10
import copy
copy_a = copy.copy(a)
sum_a = 0

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

for i in a:
    ans += abs(i)
    abs_mina = min(abs_mina, abs(i))

if copy_a[-1] < 0:
    ans -= abs_mina * 2


print(ans)

Submission status: Accepted

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

minuc = 0
for i in range(N):
    if A[i] < 0:
        minuc += 1
ans = 0
if minuc % 2 == 0:
    for i in range(N):
        ans += abs(A[i])

else:
    for i in range(N):
        A[i] = abs(A[i])
    mina = min(A)
    for i in range(N):
        if mina == A[i]:
            ans -= A[i]
        else:
            ans += A[i]
print(ans)

Submission status: Accepted

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

b = list(filter(lambda x: x<0, a))
n = len(b)

c = list(map(abs, a))
d = c
s = sum(c)
if n & 1 == 1:
    s -= min(d)*2

print(s)

Submission status: Accepted

Solution code:
N = int(input())
import numpy as np
li = np.array(list(map(int,input().split())))	#1 2 3 4 5 ... n  li=[1,2,3,4,5,...,n]
li_abs = abs(li)
M = np.argmin(li_abs)
if sum(li < 0)%2 == 0:
    print(sum(li_abs))
else:
    for i in range(N-1):
        tmp = N-1-i
        if tmp == M:
            if li[tmp] >= 0:
                li[tmp] = li[tmp] * (-1)
                li[tmp - 1] = li[tmp - 1] * (-1)
        else:
            if li[tmp] < 0:
                li[tmp] = li[tmp]*(-1)
                li[tmp-1] = li[tmp-1]*(-1)

    print(sum(li))

Submission status: Accepted

Solution code:
n = int(input())
lists = list(map(int,input().split()))
ans = 0
count =0
min_num = 999999
for i in lists:
    if i<0:
        count+=1
if(count%2==0):
    for i in lists:
        ans += abs(i)
else:
    for i in lists:
        if abs(i) < min_num:
                min_num = abs(i)
    for i in lists:
        ans += abs(i)
    ans -= min_num*2
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
count=0
a_abs=[0 for j in range(n)]
min_abs=abs(a[0])
for i in range(n):
    a_abs[i]=abs(a[i])
    if a[i]<0:
        count+=1
    if abs(a[i])<min_abs:
        min_abs=abs(a[i])
        

if count%2==0:
    ans=sum(a_abs)
else:
    ans=sum(a_abs)-2*min_abs
    
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
b = [abs(aa) for aa in a]
b.sort()
plus_a = len([aa for aa in a if aa > 0])
zero_a = a.count(0)
minus_a = n - plus_a - zero_a
if minus_a % 2 == 0:
    ans = sum(b)
else:
    ans = sum(b[1:]) - b[0]
print(ans)

Submission status: Accepted

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

cnt = 0

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

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

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

Submission status: Accepted

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

from collections import deque
import heapq
import math
import bisect


def main():
    N = int(input())
    A = list(map(int, input().split()))
    dp = [[-1]*2 for i in range(N+1)]

    dp[0][0] = A[0]
    dp[0][1] = -A[0]
    for i in range(N-2):
        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(max(dp[N-2][0]+A[N-1], dp[N-2][1]-A[N-1]))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def solve():
    N = int(input())
    A = list(map(int,input().split()))
    minus = len([a for a in A if a < 0])
    zero = len([a for a in A if a == 0])
    absA = [abs(a) for a in A]
    low = sorted(absA)[0]
    if minus % 2 == 0:
        return sum(absA)
    elif zero >= 1:
        return sum(absA)
    return sum(absA) - low * 2

print(solve())

Submission status: Accepted

Solution code:
n = int(input())
a = [int(x) for x in input().split()]
plus = True
absmin = 10000000000
s = 0
for i in a:
    if i < 0:
        plus = not plus
    absi = abs(i)
    if absi < absmin:
        absmin = absi
    s += absi
if plus:
    print(s)
else:
    print(s - absmin * 2)

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

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

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

if count_minus % 2 == 0:
    print(sum(b))
if count_minus % 2 == 1:
    print(sum(b) - 2 * min(b))

Submission status: Accepted

Solution code:
from numpy import*
n,*a=map(int,open(0).read().split())
a=array(a)
if len(a[a<0])%2==0:
    print(sum(abs(a)))
else:
    a=sorted(abs(a))
    print(sum(a[1:])-a[0])

Submission status: Accepted

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

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

a.sort()
sum=0

if cnt%2==0:
  for j in range(n):
    sum+=a[j]
else:
  for j in range(n-1):
    sum+=a[j+1]
  sum-=a[0]

print(sum)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(_) for _ in input().split()]
nega = 0
for i in range(n):
    if a[i] < 0:
        nega += 1
        a[i] = -1*a[i]
a.sort()
if nega % 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_minus=0
abs_min=10**9
for i in range(n):
    if a[i]<0:
        count_minus=count_minus+1
    if abs(a[i])<abs_min:
        abs_min=abs(a[i])
sum=0
if count_minus%2==0:
    for i in range(n):
        sum=sum+abs(a[i])
else:
    for i in range(n):
        sum=sum+abs(a[i])
    sum=sum-2*abs_min
print(sum)

Submission status: Accepted

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

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

if ct % 2:
    ans = sum(A) - 2*min(A)
else:
    ans = sum(A)

print(ans)

Submission status: Accepted

Solution code:
import numpy

n = int(input())
a = []
a = numpy.array([ int(e) for e in input().split(" ")])



if sum(a < 0)%2 == 0:
    print(numpy.abs(a).sum())
else:
    tmp = numpy.abs(a)
    print( tmp.sum() -2*min(tmp))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 27 20:57:36 2019

@author: Yamazaki Kenichi
"""

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

flg = 1
m = 10**9
ans = 0
for c in A:
    flg *= -1 if c < 0 else 1
    m = min(m, abs(c))
    ans += abs(c)

ans = ans - (2*m if flg < 0 else 0)

print(ans)

Submission status: Accepted

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

count = 0
num = 0
absl = []

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

for j in range(n):
  absl.append(abs(a[j]))

num = sum(absl)

if count % 2 == 0:
  ans = num
else:
  ans = num - min(absl) * 2

print(ans)

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())
an = np.array([int(i) for i in input().split(" ")])
suma = 0
suma = np.sum(an <= 0)
minus = np.empty(0)
if suma % 2 == 0:
    print(np.sum(np.abs(an)))
else:
    print(int(np.sum(np.abs(an)) - 2 * np.min(np.abs(an))))

Submission status: Accepted

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

Submission status: Accepted

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

dpP = [0] * N
dpN = [0] * N
dpP[0] = As[0]
dpN[0] = -As[0]

for i, A in enumerate(As[1:-1], 1):
    dpP[i] = max(dpP[i-1]+A, dpN[i-1]-A)
    dpN[i] = max(dpP[i-1]-A, dpN[i-1]+A)

dpP[N-1] = dpP[N-2]+As[N-1]
dpN[N-1] = dpN[N-2]-As[N-1]

print(max(dpP[N-1], dpN[N-1]))

Submission status: Accepted

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

pos = [a for a in As if a > 0]
neg = [a for a in As if a < 0]

if len(neg) % 2 == 0:
    print(sum(pos + [0]) - sum(neg + [0]))
else:
    print(sum(pos) - sum(neg) - 2 * min(map(abs, As)))

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int, input().split()))
temp=0
nummin=0
minabs=10**9
ans=0

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

Submission status: Accepted

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

def readInts():
  return [int(s) for s in input().split(" ")]

def main():
  N = readInts()[0]
  A = readInts()
  minus = 0
  m = abs(A[0])
  s = 0
  for a in A:
    if a < 0:
      minus += 1
    if abs(a) < m:
      m = abs(a)
    s += abs(a)

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

if __name__ == "__main__":
  main()

Submission status: Accepted

Solution code:
N=int(input())
LA=[int(x) for x in input().split()]
for i in range(len(LA)-1):
    if LA[i]>=0:
        pass
    else:
        LA[i]*=-1
        LA[i+1]*=-1

if LA[-1]<0:
    LA[-1]*=-1
    print(sum(LA)-min(LA)*2)
else:
    print(sum(LA))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = []
count = 0  #Aのなかの負の要素の個数
for i in range(N):
	if A[i] != abs(A[i]):
		count += 1
	B.append(abs(A[i]))
if count % 2 == 0:
	print(sum(B))
else:
	print(sum(B) - min(B) * 2)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int, input().split()))
ans=0
mn=10**9
hu=0
for i in range(n):
    if(a[i]<0):
        hu+=1
    tmp=abs(a[i])
    ans+=tmp
    mn=min(mn,tmp)
if(hu%2==1):
    ans-=mn*2
print(ans)

Submission status: Accepted

Solution code:
def input_ints():
    return list(map(int, input().split()))

def main():
    n = int(input())
    a = input_ints()
    c = 0
    for i in range(n):
        if a[i] < 0:
            c += 1
            a[i] *= -1
    ans = sum(a)
    if c % 2:
        ans -= 2 * min(a)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

minus = [i<0 for i in a]
a_abs = list(map(abs,a))

ans = sum(a_abs)
if sum(minus) %2 == 1:
    ans -= min(a_abs)*2
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,datetime,random
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())

N = int(input())
AA = inpl()

cnt = 0
plus_min = INF
mins_max = -INF
ans = 0

for a in AA:
    if a < 0:
        cnt += 1
        mins_max = max(mins_max,a)
        ans -= a
    else:
        plus_min = min(plus_min,a)
        ans += a

if cnt%2 == 0 :
    print(ans)
else:
    ans = max( ans + mins_max*2 , ans - plus_min*2 )
    print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
cnt = sum([a<0 for a in A])
B = [abs(a) for a in A]
ans = sum(B)
if cnt%2:
    ans -= 2*min(B)
print(ans)

Submission status: Accepted

Solution code:
import sys
import math
import collections
import itertools
import array
import inspect

# Set max recursion limit
sys.setrecursionlimit(1000000)


# Debug output
def chkprint(*args):
    names = {
        id(v): k
        for k, v in inspect.currentframe().f_back.f_locals.items()
    }
    print(', '.join(
        names.get(id(arg), '???') + ' = ' + repr(arg) for arg in args))


# Binary converter
def to_bin(x):
    return bin(x)[2:]


def li_input():
    return [int(_) for _ in sys.stdin.readline().split()]


def gcd(n, m):
    if n % m == 0:
        return m
    else:
        return gcd(m, n % m)


def gcd_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = gcd(v, L[i])

    return v


def lcm(n, m):
    return (n * m) // gcd(n, m)


def lcm_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = lcm(v, L[i])

    return v

def comb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2, r + 1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p - 1, r, p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def bisearch(L, target):
    low = 0
    high = len(L) - 1
    
    while low <= high:
        mid = (low + high) // 2
        guess = L[mid]
        if guess == target:
            return True
        elif guess < target:
            low = mid + 1
        elif guess > target:
            high = mid - 1
    if guess != target:
        return False

# --------------------------------------------

dp = None


def main():
    N = int(input())
    A = li_input()
    B = [abs(a) for a in A]

    n_negative = len([a for a in A if a < 0])

    if n_negative % 2 == 1:
        print(sum(B) - 2 * min(B))
    else:
        print(sum(B))


main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
cnt = 0
ans = []

for i in a:
    if i < 0:
        cnt += 1
    ans.append(abs(i))

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
import bisect


def solve(N: int, A: "List[int]"):
    A.sort()
    count_minus = bisect.bisect_left(A, 0)
    A = [abs(x) for x in A]
    A.sort()
    ret = sum(A)
    if count_minus % 2 != 0:
        ret -= A[0] * 2
    print(ret)
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    solve(N, A)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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

if k%2 == 0:
    print(sum)
else:
    c = 10000000000
    for i in range(n):
        c = min(c,abs(a[i]))
    print(sum-2*c)

Submission status: Accepted

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

m = 1000000000
s = 0
f = 0

for i in range(N):
    if A[i] < 0:
        f += 1
    if m > abs(A[i]):
        m = abs(A[i])
    s += abs(A[i])
if f % 2 == 0:
    print(s)
else:
    print(s - m * 2)

Submission status: Accepted

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

memo = [[0, 0] for _ in range(n)]

memo[0][0] = A[0]
memo[0][1] = -A[0]

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

memo[n - 1][0] = memo[n - 2][0] + A[n - 1]
memo[n - 1][1] = memo[n - 2][1] - A[n - 1]

print(max(memo[-1]))

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
Alist = list(map(int, input().split())) 
Blist = list(map(abs, Alist))
Blist.sort()
parity = 1
for a in Alist:
    parity = parity*np.sign(a)
if parity>0:
    print(sum(Blist))
else:
    print(sum(Blist[1:])-Blist[0])

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
ans = []
num = 0
for i,x in enumerate(A):
    if x < 0:
        ans.append(-x)
        num += 1
    else:
        ans.append(x)
if num%2 == 0:
    print(sum(ans))
else:
    print(sum(ans)-2*min(ans))

Submission status: Accepted

Solution code:
import math

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

minus_count = 0
min_A = abs(A[0])
min_i = 0
for i in range(N):
    if A[i] < -1:
        minus_count += 1
    abs_a = abs(A[i])
    if min_A > abs_a:
        min_i = i
        min_A = abs_a

A2 = [abs(a) for a in A]
    
if minus_count % 2 == 0:
    out = sum(A2)
else:
    A2[min_i] *= -1
    out = sum(A2)

print(out)

Submission status: Accepted

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

dp=[[0]*2 for i in range(len(A))]


dp[0][0]=A[0]
dp[0][1]=A[0]

for i in range(1,len(A)):
    if i!=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]-2*A[i-1]-A[i],dp[i-1][1]+2*A[i-1]-A[i])  
    else:
        dp[i][0]=A[i-1]+A[i]
        dp[i][1]=-A[i-1]-A[i]              
print(max(dp[-1][0],dp[-1][1]))

Submission status: Accepted

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

cnt = 0
for x in a:
    if x > 0:
        break
    cnt += 1

if cnt % 2 == 0:
    print(sum(abs(x) for x in a))
else:
    y = [abs(x) for x in a]
    y.sort()
    print(sum(y[1:]) - y[0])

Submission status: Accepted

Solution code:
N = int(input())

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

nega_count = [1 for x in A if x < 0]
nega_count = sum(nega_count)

B = [abs(x) for x in A]
if nega_count % 2 == 0:
  print(sum(B))
else:
  b = B.pop(B.index(min(B)))
  print(sum(B) - b)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin
from collections import deque, defaultdict as ddic
from itertools import combinations, permutations, product
import bisect
import heapq


rs = lambda: stdin.readline().strip()
ri = lambda: int(rs())
rsl = lambda: stdin.readline().split()
rim = lambda: map(int, rsl())
ril = lambda: list(rim())

N = ri()
A = list(rim())
B = [abs(elem) for elem in A]

low = min(B)
minus = 0
for i in range(len(A)):
    if A[i] < 0:
        minus += 1

if minus % 2 == 0:
    print(sum(B))
else:
    print(sum(B) - (low*2))

Submission status: Accepted

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

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


A_abs = [abs(a) for a in A]
if minus_num % 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()))
an = list(map(abs,a))
cnt=0
for x in range(n):
    if a[x] < 0:
        cnt += 1

if cnt%2 == 0:
    print(sum(map(abs,a)))
else:
    print(sum(map(abs,a)) - 2*min(an))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
a_list = [int(i) for i in input().split()]
b_list = [abs(i) for i in a_list]
b_list.sort()
minus_number = 0
for i in a_list:
	if i<0:
		minus_number+=1
if minus_number%2==0:
	print(sum(b_list))
else:
	print(sum(b_list)-2*b_list[0])

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
plus = 0
minus = 0
cur = float('inf')
S = 0
for i in range(N):
  if A[i] > 0:
    plus += 1
    S += A[i]
    if A[i] < cur:
      cur = A[i]
  elif A[i] < 0:
    minus += 1
    S += -A[i]
    if -A[i] <= cur:
      cur = -A[i]
  else:
    cur = A[i]
if minus%2 == 0:
  print(S)
else:
  if cur != float('inf'):
    print(S-2*cur)
  else:
    print(S)

Submission status: Accepted

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

ma = sorted(x for x in A if x < 0)
pa = sorted(x for x in A if x >= 0)

# print(ma, pa)

if len(ma) % 2 == 0:
    result = -sum(ma) + sum(pa)
else:
    x = -ma[-1]
    if len(pa):
        x = min(x, pa[0])
    result = -sum(ma) + sum(pa) - x * 2

print(result)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
A = np.array([int(x) for x in input().split(' ')])

absA = abs(A)
if sum(A <= 0) % 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()))

abs_min = 10**10
minus_cnt = 0
for i in range(n):
    abs_min = min(abs(a[i]), abs_min)
    if a[i] < 0:
        minus_cnt += 1
        a[i] *= -1

if minus_cnt %2 == 0:
    print(sum(a))
else:
    print(sum(a) - abs_min*2)

Submission status: Accepted

Solution code:
N = int(raw_input())
A = map(long, raw_input().split())

minus_count = 0
min_abs = 10 ** 9
sum = 0
for a in A:
    if a < 0:
        minus_count += 1
    abs_a = abs(a)
    if abs_a < min_abs:
        min_abs = abs_a
    sum += abs_a

if minus_count % 2 > 0:
    sum -= 2*min_abs

print sum

Submission status: Accepted

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

c %= 2

if d > 0:
    c = 0

#print(c)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int, input().split()))
pos=[]
nega=[]

for i in range(n):
    if a[i]>=0:
        pos.append(a[i])
    else:
        nega.append(a[i])
if len(pos)==0:
    if len(nega)%2==0:
        print(abs(sum(a)))
    else:
        print(abs(sum(a))+max(a)*2)
elif len(nega)==0:
    print(sum(a))
elif len(nega)%2==0:
    print(sum(pos)-sum(nega))
else:
    if min(pos)>-max(nega):
        print(sum(pos)-sum(nega)+max(nega)*2)
    else:
        print(sum(pos)-sum(nega)-min(pos)*2)

Submission status: Accepted

Solution code:
 
N = int(input())
A = list(map(int, input().split()))
mn_count = len([0 for i in A if i < 0])
AA = [abs(i) for i in A]
mn = min(AA) if mn_count % 2 else 0
#print(AA, mn, mn_count)
print(sum(AA) - mn*2)

Submission status: Accepted

Solution code:
import sys
def p(*a):
  s=" ".join(map(str,a))
  #print(s)
  sys.stderr.write(s+"\n")



N=int(input())
A = [int(i) for i in input().split()]	# 1 2 3 4 5 ...
ABS=[]

iCount=0
iABSMin=10**9+1

for i in range(N):
	a = A[i]
	if a<0: iCount+=1
	
	b = abs(a)
	ABS.append(b)
	
	iABSMin=min(iABSMin,b)
	
	

if iCount%2==0:
	print(sum(ABS))
	exit()


ans=sum(ABS)-(iABSMin*2)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

if sum(a < 0 for a in A) % 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 = [int(i) for i in input().split()]

m = 10 ** 9
c = 0
ans = 0

for x in a:
    m = min(abs(x), m)
    ans += abs(x)
    if x < 0:
        c += 1

if c % 2 == 1:
    ans -= m * 2

print(ans)