Problem:


Score : 400 points

Problem Statement

There are N integers, A_1, A_2, ..., A_N, arranged in a row in this order.

You can perform the following operation on this integer sequence any number of times:

Operation: Choose an integer i satisfying 1 \leq i \leq N-1. Multiply both A_i and A_{i+1} by -1.

Let B_1, B_2, ..., B_N be the integer sequence after your operations.

Find the maximum possible value of B_1 + B_2 + ... + B_N.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

-10^9 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible value of B_1 + B_2 + ... + B_N.

Sample Input 1

3
-10 5 -4

Sample Output 1

19

If we perform the operation as follows:

Choose 1 as i, which changes the sequence to 10, -5, -4.

Choose 2 as i, which changes the sequence to 10, 5, 4.

we have B_1 = 10, B_2 = 5, B_3 = 4. The sum here, B_1 + B_2 + B_3 = 10 + 5 + 4 = 19, is the maximum possible result.

Sample Input 2

5
10 -4 -8 -11 3

Sample Output 2

30

Sample Input 3

11
-1000000000 1000000000 -1000000000 1000000000 -1000000000 0 1000000000 -1000000000 1000000000 -1000000000 1000000000

Sample Output 3

10000000000

The output may not fit into a 32-bit integer type.



Sample input:
3
-10 5 -4


Sample output:
19

Submission status: Accepted

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


def m():
    abs_A = [abs(a) for a in A]
    mines_cnt = 0
    for a in A:
        if a < 0:
            mines_cnt += 1
    if mines_cnt % 2 == 0:
        return sum(abs_A)
    return sum(abs_A) - min(abs_A)*2


print(m())

Submission status: Accepted

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

plus=0
minus=0
for i in A:
    if i<0:
        minus+=1


value=999999999999999999999999999999
for num1,i in enumerate(A):
    if abs(i)<value:
        absmin=num1
        value=abs(i)

count=0
if minus%2==0:
    for i in A:
        count+=abs(i)
else:
    for num,i in enumerate(A):
        if num==absmin:
            count+=-abs(i)
        else:
            count+=abs(i)
        #print(count)

print(count)

Submission status: Accepted

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

l_minus = [i for i in a if i <= 0]
l_plus = [i for i in a if i > 0]

ans = 0
if len(l_minus) % 2 == 0:
    l_minus_edit = [-i for i in l_minus]
else:
    l_minus_edit = [-i for i in l_minus]
    temp = l_plus[:]
    temp.extend(l_minus_edit)
    l_minus_edit.append(-min(temp) * 2)

ans = sum(l_plus) + sum(l_minus_edit)
print(ans)

Submission status: Accepted

Solution code:
n = input()
a = list(map(int, input().split(' ')))
p = 0
z = False
ret = 0
for i in range(len(a)):
    if a[i] == 0:
        z = True
    if a[i] < 0:
        p = (p + 1) % 2
        a[i] = -a[i]
    ret = ret + a[i]
a.sort()
if z == True or p == 0:
    print(ret)
else:
    print(ret-2*a[0])

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
b = []
count1 = 0
count2 = 0
for s in range(n):
	b.append(abs(a[s]))
	if a[s] < 0:
		count1 += 1
	elif a[s] == 0:
		count2 += 1
if count1 % 2 == 0 or count2 != 0:
	print(sum(b))
else:
	print(sum(b) - min(b)*2)

Submission status: Accepted

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

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

negative_number = len([n for n in A_list if n < 0])

A_list_abs = [abs(n) for n in A_list]
A_list_abs.sort()
total = sum(A_list_abs)

if negative_number % 2 == 0:
    print (total)
else:
    print (total - 2*A_list_abs[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:
    print(sum(B))
else:
    print(sum(B) - min(B) * 2)

Submission status: Accepted

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

minus = sum(x < -1 for x in a) % 2
plus = sum(abs(x) for x in a)
mina = min(absa)

if minus == 0:
  print(plus)
else:
  print(plus - mina * 2)

Submission status: Accepted

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

cntMinus = sum([1 for a in lstA if a < 0])
lstB = [abs(a) for a in lstA]

if cntMinus % 2 == 0:
    ans = sum(lstB)
else:
    ans = sum(lstB) - 2 * min(lstB)
print(ans)

Submission status: Accepted

Solution code:
import numpy

N = int( input() )
A = list( map( int ,input().split()) )
cnt = sum( lop < 0 for lop in A)

nA = numpy.array(A)
nA = numpy.abs(nA)

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

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
a = [int(i) for i in input().split()]
a = np.array(a, dtype='int64')
k = np.sum(a < 0)
a = np.absolute(a)
a.dtype ='uint64'
a.sort()

if k % 2 == 0:
    print(int(np.sum(a)))
else :
    print(int(np.sum(a)- a[0]*2))

Submission status: Accepted

Solution code:
n = int(input())
lia = list(map(int,input().split()))
count = 0

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

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

Submission status: Accepted

Solution code:
# coding: utf-8

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

is_zero = len([a for a in A if a == 0]) > 0
if is_zero:
    print(sum([abs(a) for a in A]))
else:
    minus = len([a for a in A if a < 0])
    abs_list = [abs(a) for a in A]
    if minus % 2 == 0:
        print(sum(abs_list))
    else:
        min_value = min(abs_list)
        print(sum(abs_list) - min_value * 2)

Submission status: Accepted

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

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

if cnt % 2 == 0 or 0 in A:
    print(ans)
else:
    print(ans - 2 * absmin)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
INF = float("inf")


def solve(N: int, A: "List[int]"):
    d = [[a > 0, abs(a)] for i, a in enumerate(A)]
    d.sort(key=lambda x: x[1], reverse=True)
    AA = [dv[1] if dv[0] else -dv[1] for dv in d]
    # print(AA)
    s = 0
    for i in range(N-1):
        if AA[i] < 0:
            AA[i] *= -1
            AA[i+1] *= -1
        s += AA[i]
    # print(AA)
    print(s+AA[-1])
    return


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())
t = list(map(int, input().split()))
mins= [x for x in t if x<0]

if len(mins)%2 ==0:
    print(sum(map(abs,t)))
else:
    print(sum(map(abs,t))-2*min(map(abs,t)))

Submission status: Accepted

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

abs_min = float('inf')
ans = 0
neg_cnt = 0
zero_cnt = 0
for ai in a:
	if abs(ai)<abs_min:
		abs_min = abs(ai)
	if ai<0:
		neg_cnt += 1
	if ai==0:
		zero_cnt += 1
	ans += abs(ai)

if zero_cnt>0:
	print(ans)
elif neg_cnt%2 == 0:
	print(ans)
else:
	print(ans-abs_min*2)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from collections import defaultdict
from sys import setrecursionlimit
setrecursionlimit(10**9)
def inpl(): return list(map(int, input().split()))
N = int(input())
A = inpl()

def calc(i):
    if i == N-2:
        return (A[i], A[i+1]), (-A[i], -A[i+1])
    else:
        X, Y = calc(i+1)
        return ([A[i], max(X[0] + X[1], Y[0] + Y[1])],
                [-A[i], max(-X[0] + X[1], -Y[0] + Y[1])])

print(max(map(sum, calc(0))))

Submission status: Accepted

Solution code:

N = int(input())
A = [int(i) for i in input().split()]
cnt = 0
for a in A:
    if a<0:
        cnt += 1
A = [abs(a) for a in A]
if cnt % 2 == 1:
    print(sum(A)-2*min(A))
else:
    print(sum(A))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import fractions
from functools import reduce

n = int(input())

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

count= 0
maxi = 10**10
for i in range(n):
    num = a[i]
    if a[i] < 0:
        count +=1
    maxi = min(maxi,abs(num))

if count % 2 == 0:
    print(sum(list(map(lambda x:abs(x),a))))
else:
    print(sum(list(map(lambda x:abs(x),a)))- 2 * maxi)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
hint=-1
flg=-1
for i in range(N):
  if A[i]==0:
    hint=1
  elif A[i]<0:
    flg= flg*-1
  A[i]=abs(A[i])
A.sort()
ans=0
if hint==1 or hint*flg==1:
  print(sum(A))
else:
  print(sum(A[1:])-A[0])

Submission status: Accepted

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

if(0 in L):
    print(sum(list(map(abs,L))))
    sys.exit()

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

#print(count,min)

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

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    *A, = map(int, input().split())
    minuscount = 0
    A.sort(key=abs)
    for i in A:
        if i < 0:
            minuscount += 1
    maxi = 0
    for i in A:
        maxi += abs(i)
    if minuscount % 2 == 0:
        print(maxi)
    else:
        print(maxi - 2*abs(A[0]))
main()

Submission status: Accepted

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

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


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

print(ans)

Submission status: Accepted

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

minus = 0
abs_sum = 0
is_zero = False
abs_min = 10**9+1
for i in range(n):
    if a[i] < 0:
        minus += 1
    abs_sum += abs(a[i])
    abs_min = min(abs_min, abs(a[i]))
    if a[i] == 0:
        is_zero = True
if is_zero or minus%2==0:
    print(abs_sum)
elif minus%2 == 1:
    print(abs_sum-abs_min*2)

Submission status: Accepted

Solution code:
import sys

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

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

if minus_count%2==0:
	print  sum( map(lambda x: abs(x), A) )
else:
	if 0 in A:
		print  sum( map(lambda x: abs(x), A) )
	else:
		B=map(lambda x: abs(x), A)
		B.sort()
		print sum(B[1:])-B[0]

Submission status: Accepted

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

zeroFlag = False
kosuu = 0
absMin = 10000000000
ans = 0
for i in range(n):
    if(a[i]<0):
        kosuu += 1
    elif(a[i]==0):
        zeroFlag = True
    if(abs(a[i])<absMin):
        absMin = abs(a[i])
    ans += abs(a[i])

if(zeroFlag):
    print(ans)
else:
    if(kosuu%2==0):
        print(ans)
    else:
        ans -= 2*absMin
        print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#D
import numpy as np
import math
import re

n = int(input())
a = input()
a = re.split(" ",a)
a = list(map(int,a))
count = 0
sum = 0
for i in range(n):
    if(a[i] < 0):
        count += 1
if(count %2 == 0):
    for j in a:
        sum += abs(j)
    print(sum)
else:
    min = 10**9
    t = 0
    for i in range(n):
        if(min > abs(a[i])):
            min = abs(a[i])
            t = i
    for j in range(n):
        if j != t:
            sum += abs(a[j])
        else:
            sum -= abs(a[j])
    
    print(sum)

Submission status: Accepted

Solution code:
read_ints = lambda : map(int, raw_input().strip().split())

n = input()
a = read_ints()

neg = 0
mn = float('inf')
s = 0
for num in a:
    mn = min(mn, abs(num))
    s += abs(num)
    if num < 0: neg += 1

if neg % 2 == 1:
    print s - 2 * mn
else:
    print s

Submission status: Accepted

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

for i in a:
  if i < 0:
    x += 1
    y = min(y, -i)
    ans += -i
  else:
    y = min(y, i)
    ans += i

if x % 2 == 0:
  print(ans)
else:
  print(ans - 2 * y)

Submission status: Accepted

Solution code:
import math
N = int(input())
A = list(map(int, input().strip().split()))
minus = 0
abs_min = abs(A[0])
sum = 0
for i in range(0, N):
    sum += abs(A[i])
    if A[i] < 0:
        minus += 1
    if abs(A[i]) < abs_min:
        abs_min = abs(A[i])
if minus % 2 != 0:
    sum -= abs_min*2
print("{}".format(sum))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

# if 0 in a
if 0 in a or len([i for i in a if i < 0]) % 2 == 0:
    print(sum(map(abs,a)))
else :
    tmp = sorted(map(abs,a))
    print(sum(tmp[1:])-tmp[0])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
mai = 0
for i in range(N):
    if A[i] < 0:
        mai += 1
        A[i] = -A[i]
if mai % 2 == 0:
    print(sum(A))
else:
    A = sorted(A)
    print(sum(A)-2*A[0])

Submission status: Accepted

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

n_neg = 0
for A in As:
    if A < 0:
        n_neg += 1

Bs = list(map(abs, As))
if n_neg % 2 == 0:
    ans = sum(Bs)

else:
    Bs = sorted(Bs)
    Bs[0] = -Bs[0]
    ans = sum(Bs)
print(ans)

Submission status: Accepted

Solution code:
from fractions import gcd
import numpy as np

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

if len(A[A < 0]) % 2 == 0:
    print(np.sum(np.abs(A)))
else:
    minabs_idx = np.argmin(np.abs(A))
    a = np.sum(np.abs(A)) - np.abs(A[minabs_idx])
    b = np.abs(A[minabs_idx])
    print(a - b)

Submission status: Accepted

Solution code:
import sys
import numpy as np

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

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

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

Submission status: Accepted

Solution code:
import numpy as np
N =int(raw_input())
A =map(int ,raw_input().split())
B =np.abs(A)
B = np.array(B,dtype=np.int64)
C = min(B)
D = (sum(B))
E = D - 2*C
k = 0
for i in range(N):
    if A[i]<0:
        k = k+1
if k%2 == 0:
    print D
else:
    print E

Submission status: Accepted

Solution code:
def is_even(x):
    half = x//2
    if half * 2 == x:
        return 1
    else:
        return 0

def abs(x):
    if x >= 0:
        return x
    else:
        return -x

if __name__ == "__main__":
    N = int(input())
    A = list(map(int,input().split()))
    minus_count = 0
    B = 0
    for a in A:
        if a < 0:
            minus_count += 1
    if is_even(minus_count):
        for a in A:
            B += abs(a)
    else:
        C = [abs(a) for a in A]
        c = min(C)
        B = sum(C) - 2 * c
    print(B)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=0
m=10**10
flg=0
last=0
for i in range(n):
    if a[i]==0:
        flg=1
cnt=len([x for x in a if x<0])

if cnt%2==0 or flg==1:
    for i in range(n):
        ans+=abs(a[i])
else:
    for i in range(n):
        ans+=abs(a[i])
        if m>abs(a[i]):
            m=abs(a[i])
    ans-=2*m
print(ans)

Submission status: Accepted

Solution code:
from functools import reduce
read = input
n = int(read())
A = list(map(int, read().split()))
if any(map(lambda x:x==0, A)):
    A = map(abs, A)
    print(sum(A))
else:
    cnt = reduce(lambda ac,x:ac + 1 if x < 0 else ac, A, 0)
    if cnt % 2 == 0:
        A = list(map(abs, A))
        print(sum(A))
    else :
        A = list(map(abs, A))
        print(sum(A) - 2*min(A))

Submission status: Accepted

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

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

b = []
for j in range(len(a)):
    b.append(abs(a[j]))

if cnt % 2 != 0:
        b.append(-2*min(b))

print(sum(b))

Submission status: Accepted

Solution code:
#

import itertools
from collections import Counter
from collections import defaultdict
import bisect

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

    dp = [[0] * 2 for _ in range(N)]
    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])

    dp[N - 1][0] = dp[N - 2][0] + A[N-1]
    dp[N - 1][1] = dp[N - 2][1] - A[N-1]

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
a = input()
l = map(int, raw_input().split())
c = 0
ll = []

for i in l:
	if i < 0:
		c += 1
		ll.append(i * -1)
	else:
		ll.append(i)

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

Submission status: Accepted

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

'''考え方

特定の配置なら、全部の数字を足せる。
そうでないなら、一番絶対値が小さい値だけ排除、となりそう。

・どこにいようと、0がいたら全ての値を合計できる。


・手順
負の値について、奇数個ブロックが2個あれば消滅できる。
奇数個ブロックが奇数個しかないときは、絶対値が一番小さい値を除外する。
'''


min_abs = 1000000000000
cnt_in_block = 0
cnt_blocks = 0
sum_all = 0


for i,a in enumerate(A):
  # 負の値
  if a < 0:
    cnt_in_block += 1
  
  # 正の値
  else:
    # 切り替わり直後
    if i > 0 and A[i-1] < 0:
      if cnt_in_block % 2 != 0:
        cnt_blocks += 1
    
      # reset
      cnt_in_block = 0
    
  tmp = abs(a)
  if min_abs > tmp:
    min_abs = tmp
  sum_all += abs(a)
  
  # 一番最後が負の値の場合、ブロック数として計算する
  if N -1 == i and cnt_in_block > 0:
    if cnt_in_block % 2 != 0:
      cnt_blocks += 1

  
if cnt_blocks % 2 == 1:
  out = sum_all - 2*min_abs
else:
  out = sum_all
  
print(out)

Submission status: Accepted

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

X = len([a for a in A if a < 0])
if X % 2 == 0:
    print(sum([abs(a) for a in A]))
else:
    MIN = float('inf')
    for a in A:
        if abs(a) < MIN:
            MIN = abs(a)
    print(sum([abs(a) for a in A]) - 2 * MIN)

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 == 1:
    ans -= 2*min(A)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

n_minus = 0
n_zero = 0
for x in a:
  if x == 0:
    n_zero += 1
  elif x < 0:
    n_minus += 1
r = 0
for x in a:
    r += abs(x)
if n_zero == 0 and n_minus % 2 == 1:
  e = abs(a[0])
  for x in a:
  	e = min(e, abs(x))  
  r -= e * 2
print(r)

Submission status: Accepted

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

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N=INT()
A=LIST()

mn=INF
cnt=0
zero=False
for i in range(N):
    mn=min(mn, abs(A[i]))
    if A[i]<0:
        cnt+=1
    if A[i]==0:
        zero=True
    A[i]=abs(A[i])

if zero:
    print(sum(A))
elif cnt%2==0:
    print(sum(A))
else:
    print(sum(A)-mn*2)

Submission status: Accepted

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

total = 0
is_odd = 0
min_number = 1000000001

for i in range(N):
    abs_num = abs(A[i])
    total += abs_num
    if abs_num < min_number:
        min_number = abs_num
    if A[i] < 0:
        is_odd = 1 - is_odd

if is_odd == 1:
    total -= min_number * 2

print(total)

Submission status: Accepted

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

A_plus, A_minus = [], []
for a in A:
    if a>0: A_plus.append(a)
    else: A_minus.append(a)

if len(A_minus)%2==0:
    print(sum(A_plus) - sum(A_minus))
elif len(A_plus)==0:
    print(sum(A_plus) - sum(A_minus) + max(A_minus)*2)    
else:
    if min(A_plus) > -max(A_minus):
        print(sum(A_plus) - sum(A_minus) + max(A_minus)*2)
    else:
        print(sum(A_plus) - sum(A_minus) - min(A_plus)*2)

Submission status: Accepted

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

cnt = 0
b = []
for ai in a:
    if ai <= 0:
        b.append(-ai)
        cnt += 1
    else:
        b.append(ai)
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 = list(map(int,input().split()))

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

a = sorted(list(map(abs,a)))
ans = sum(a)
if neg%2 == 1:
    ans -= 2*a[0]
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin

n = int(stdin.readline().rstrip())
an = list(map(int, input().split()))

m = []
p = []
z = 0

for i in range(n):
  if(an[i]<0):
    m.append(an[i])
  if(an[i]>0):
    p.append(an[i])
  if(an[i]==0):
    z=1

if ( z==1 ):
  ans = sum(p) - sum(m)
elif ( len(m) == 0):
    ans = sum(p)
else:
  if ( len(p) == 0):
    if ( len(m)%2 == 0):
      ans = - sum(m)
    else:
        minm = -max(m)
        ans = -sum(m) - (minm*2)
  else:
    if ( len(m)%2 == 0):
      ans = sum(p) - sum(m)
    else:
      minm = 0
      if ( min(p) < (-max(m)) ):
        minm = min(p)
      else:
        minm = -max(m)
      ans  = sum(p) - sum(m) - (minm*2)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
lst_A =list(map(int, input().split()))
neg_count = 0
zero_count = 0
total = 0

if lst_A[0] < 0:
    neg_count += 1
elif lst_A[0] == 0:
    zero_count +=1
total += abs(lst_A[0])
memo = abs(lst_A[0])
i = 1
while i < N:
    if lst_A[i] < 0:
        neg_count += 1
    elif lst_A[i] == 0:
        zero_count +=1
    total += abs(lst_A[i])
    memo = min(memo, abs(lst_A[i]))
    i += 1
if zero_count > 0:
    total = total
elif neg_count % 2 == 1:
    total = total - memo *2
else:
    total = total
print (total)

Submission status: Accepted

Solution code:
n = int(input())
A = [int(_) for _ in input().split()]
total = sum(A)
neg = 0
pos = 0
neno = []
pono = []
for a in A:
    if a < 0:
        neg += 1
        neno.append(a)
    else:
        pos += 1
        pono.append(a)
neno.sort()
pono.sort()
noneg = -sum(neno) + sum(pono)
if n == 2:
    print(max(-total, total))
else:
    if neg % 2 == 0:
        print(noneg)
    else:
        if pos != 0:
            print(noneg - 2*min(-neno[-1],pono[0]))
        else:
            print(noneg + 2*(neno[-1]))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))  # �������l�����X�g�Ɋi�[
ans = 0

a.sort()
num = 0
#  �}�C�i�X�̌��������Ȃ�S�����ɂ��Ęa���Ƃ�
# ��Ȃ�S�������ɂ��Đ�Βl�������������

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

# num�̓}�C�i�X�̐�
a = list(map(abs, a))
if num % 2 == 0:
    print(sum(a))
else:
    a.sort()
    print(sum(a) - a[0]*2)

Submission status: Accepted

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

s = sum(list(map(lambda x: int(x < 0),A)))
absA = sorted(list(map(lambda a: abs(a), A)))

if s % 2 == 1:
    print(-2*absA[0]+sum(absA))
else:
    print(sum(absA))

Submission status: Accepted

Solution code:

num = int(input())
data = list(map(int, input().split()))

count_fu = 0
count_zero = 0
for i in range(num):
    if data[i] == 0:
        count_zero += 1
    elif data[i] < 0:
        count_fu += 1

sum_data = 0
min_data = abs(data[0])
for i in range(num):
    sum_data += abs(data[i])
    if abs(data[i]) < min_data:
        min_data = abs(data[i])

if count_fu % 2 == 0:
    print(sum_data)
else:
    print(sum_data - 2 * min_data)
    # print(sum_data, min_data)

Submission status: Accepted

Solution code:
n = int(input())
tmp = [i for i in map(int, input().split())]
v_list = []
cnt = 1
for i in tmp:
    if i < 0:
       cnt *= -1
    v_list.append(abs(i))
v_list.sort()
if cnt ==1:
    print(sum(v_list))
else:
    print(sum(v_list[1:]) - v_list[0])

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
As=input().split()
A = np.array([int(s) for s in As])
del(As)
Ans = np.sum(np.abs(A))
co = np.sum(A < 0)
if co%2==0:
    print(Ans)
else:
    Ans = Ans - 2*np.amin(np.abs(A))
    print(Ans)

Submission status: Accepted

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

m = 10**9
n = 0
abs_sum = 0
for i in range(N):
    a = A[i]
    b = abs(a)
    m = min(m, b)
    abs_sum += b
    if a < 0:
        n += 1

if m == 0:
    print(abs_sum)
elif n%2 == 0:
    print(abs_sum)
else:
    print(abs_sum-2*m)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
A = [int(i) for i in input().split()] 
Acnt = 0
for i in range(N):
	if A[i] < 0:
		Acnt += 1

if Acnt % 2 == 1:
	print(np.sum(np.abs(A)) - np.min(np.abs(A))*2)
elif Acnt % 2 == 0:
	print(np.sum(np.abs(A)))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
pos = []
neg = []
cnt = 0
for i in a:
    if i > 0:
        pos.append(i)
    elif i < 0:
        neg.append(i)
    else:
        cnt += 1
if cnt != 0 or len(neg) % 2 == 0:
    print(sum(pos) - sum(neg))
else:
    pos = sorted(pos)
    neg = sorted(neg)
    if pos == []:
      print(-sum(neg[:-1])+neg[-1])
      exit()
    else:
      b = min(pos)
    c = -max(neg)
    pos = pos[1:]
    neg = neg[:-1]
    r = sum(pos) - sum(neg)
    m = max(b - c, c - b)
    print(r + m)

Submission status: Accepted

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

counter=0

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

li2=li

for i in range(N):
    li2[i]=abs(li[i])

if counter % 2==0:
    mi=0

if counter % 2!=0:
    mi=min(li2)

sumli=0
for i in range(N):
    sumli=abs(li[i])+sumli

sumli=sumli -(mi*2)

print(sumli)

Submission status: Accepted

Solution code:
import sys
raw_input = sys.stdin.readline
N = int(raw_input())
L = map(int, raw_input().split())
nm = 0
for i in xrange(N):
	if L[i] < 0:
		nm += 1
		L[i] = abs(L[i])

if nm % 2 == 1 :
	print sum(L) - 2 * min(L)
else:
	print sum(L)

Submission status: Accepted

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

minabs = 10e9+1
totalsum = 0
flag = 1 #0あり：2,偶数：1,不可（バツ奇数）：-1
for a in A:
  absa = abs(a)
  #あとでsumを再度計算しなくて済むよう、先に計算
  totalsum += absa
  #絶対値が最小のものの絶対値をminabsに入れる
  if absa < minabs: minabs = absa
  #0ならオール可が決定
  if a == 0:
    flag = 2
  elif a < 0:
    flag *= -1
    

    
print(totalsum if (flag == 1) or (flag == 2) else totalsum - (minabs*2))

Submission status: Accepted

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

Submission status: Accepted

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

b_list = []

c_list = []
cnt = 0
for a in a_list:
    if a <= 0:
        cnt += 1
    c_list.append(abs(a))

if cnt % 2 != 0:
    print(sum(c_list) - 2 * min(c_list))
else:
    print(sum(c_list))
# print(sum(b_list))

Submission status: Accepted

Solution code:
import numpy as np

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

b = np.sort(np.abs(a))
c = np.sum(a < 0)

if c % 2 == 0:
  total = np.sum(b)
else:
  total = np.sum(b[1:]) - b[0]
  
print(total)

Submission status: Accepted

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

mc = 0
for e in a:
  if e < 0:
    mc += 1


    
f = 0
if mc % 2 == 1:
  a = [abs(i) for i in a]
  a.sort()
  f = 0
  for i in range(1, n):
    f += a[i]
  f -= a[0]
else:
  for e in a:
    if e >= 0:
      f += e
    else:
      f -= e
      
print(f)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
count_minus = 0
min_abs = 10 ** 9 + 1
sum_abs = 0
for i in range(N):
  if A[i] < 0:
    count_minus += 1
  min_abs = min(min_abs, abs(A[i]))
  sum_abs += abs(A[i])
if count_minus % 2 == 0:
  print(sum_abs)
else:
  print(sum_abs - min_abs * 2)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
neg = 0
r=0
for i in a:
  r = r + abs(i)
  if i < 0 :
     neg = neg + 1
if neg % 2 != 0:
  s = 10000000000
  for i in a:
    s = min(s,abs(i))
  r = r - 2*s
  
print(r)

Submission status: Accepted

Solution code:
argN = int(input())
argA = list(map(int,input().split()))

cntP = 0
cntZ = 0
cntM = 0
aryAbsA = []
wkMin = abs(argA[0])
#wkMinIdx = 0
for i in range(argN):
  if argA[i] == 0:
    cntZ += 1
  elif argA[i] > 0:
    cntP += 1
  else:
    cntM += 1
  aryAbsA.append(abs(argA[i]))
  if aryAbsA[i] != 0 and aryAbsA[i] < wkMin:
    wkMin = aryAbsA[i]

if cntZ == 0 and cntM % 2 == 1:
  fix = wkMin
else:
  fix = 0

print(str(sum(aryAbsA) - fix * 2))

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split(' ')))
plus=[A[i] for i in range(N) if A[i]>=0]
minus=[abs(A[i]) for i in range(N) if A[i]<0]

if len(minus)%2 == 0:
	print(sum(plus)+sum(minus))
else:
	newl = sorted(plus + minus)[::-1]
	print(sum(newl[:-1]) - newl[-1])