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:
def solve(n, a_list):
    total = [abs(item) for item in a_list]
    negative = [abs(item) for item in a_list if item < 0]
    # print(negative)
    if len(negative) % 2 == 0:
        return sum([abs(item) for item in a_list])
    else:
        minimum = sorted(total)[0]
        return sum([abs(item) for item in a_list]) - 2 * minimum


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


def main():
    n, a_list = input_from_console()
    print(solve(n, a_list))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
a_list = [int(i) for i in input().split()]
minus_count_even_flug = True
zero_exist_flug = False
  
for a in a_list:
    if a < 0:
        minus_count_even_flug = not minus_count_even_flug
    if a == 0:
        zero_exist_flug = True
     
a_abs_list = [abs(a) for a in a_list]
sum_a_abs_list = sum(a_abs_list)
if minus_count_even_flug or zero_exist_flug:
    print(sum_a_abs_list)
else:
    print(sum_a_abs_list - 2 * min(a_abs_list))

Submission status: Accepted

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

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

# かっこいい解法版
# Ai, Ai+1を反転して-Ai,-Ai+1であるが、これを繰り返すことで、
# -Ai, Ai+1, Ai+2,....,Aj-1, -Aj というように、
# i < jとなるi,jのみを反転させ、間はそのままという操作が可能。
# ここから、負の数が偶数個であれば、全てを正にすることができる
# 負の数が奇数個であれば、絶対値が一番小さい数を残して残りを正にすればよい

ng = [a for a in A if a < 0]    # 負の数の抽出
ab = [abs(a) for a in A]    # 絶対値の和

if len(ng) % 2 == 0:
    print(sum(ab))
else:
    mn = min(ab)
    print(sum(ab)-2*mn)

Submission status: Accepted

Solution code:
N=int(input())
A=[int(x) for x in input().split()]
B=[x for x in A if x<0]
absA=[abs(x) for x in A]
if len(B)%2==0:
  minus=0
else:
  minus=min(absA)
  
print(sum(absA)-minus*2)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split(" ")))
    zero = 0
    neg = 0
    m = float("inf")
    for i in range(N):
        if A[i] == 0:
            zero += 1
        if A[i] < 0:
            neg += 1
        A[i] = abs(A[i])
        m = min(m, A[i])

    neg = neg % 2 
    if neg == 0:
        result = sum(A)
    else:
        if zero:
            result = sum(A)
        else:
            result = sum(A) - 2*m
    print(result)
    return 

if __name__ == "__main__":
    main()

Submission status: Accepted

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

count = 0
m = 0
target = -1
for i in range(N):
    if nums[i] < 0:
        count += 1
    if abs(nums[i]) < m or target == -1:
        target = i
        m = abs(nums[i])
pos = [abs(i) for i in nums]
if count % 2 != 0:
    pos[target] = -abs(pos[target])

print(sum(pos))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin
 
readline = stdin.readline
 
def main():
    _, *A = map(int, open(0).read().split())
 
    B = tuple(map(abs, A))
    C = tuple(a for a in A if a < 0)
    print(sum(B) - min(B)*2*(len(C)%2))
 
 
if __name__ == "__main__":
    main()

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
Negative = False
if count % 2 == 0:
    Negative = True

for i in range(N):
    A[i] = max(A[i],-A[i])

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

print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
N = int(stdin.readline().rstrip())
A = [int(_) for _ in stdin.readline().rstrip().split()]
minus = []
for i in range(N):
  if A[i] < 0:
    minus.append(A[i])
abs_A = [abs(i) for i in A]
if len(minus) % 2 == 0:
  print(sum(abs_A))
else:
  print(sum(abs_A)-min(abs_A)*2)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
N = int(input())
Alist = list(map(int, input().split()))
memo = [{} for i in range(N)]

def ddp(cur_N, ai):
    if ai in memo[cur_N]:
        return memo[cur_N][ai]
    if cur_N >=N-1:
        return ai
    aj = Alist[cur_N+1]
    ret = max(ai + ddp(cur_N+1, aj), -1*ai + ddp(cur_N+1, -1*aj))
    memo[cur_N][ai] = ret
    return ret
print(ddp(0, Alist[0]))

Submission status: Accepted

Solution code:
N=int(input())
A = [int(j) for j in input().split()]
ans=0
min_A=10**5

A_count= len([a for a in A if a <0])
if A_count%2==0:
    for a in A:
        if a <0:
            ans+= -1*a
        else:
            ans+=a


elif A_count%2!=0:
    for a in A:
        ans+=abs(a)
        min_A=min(min_A,abs(a))
    ans=ans-2*min_A
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A= list(map(int, input().split()))
minus = 0
A_abs = []
for i in range(N):
    A_abs.append(abs(int(A[i])))
    if(A[i] < 0):
        minus += 1

if minus % 2 == 0:
    print(sum(A_abs))
else:
    print(sum(A_abs) - 2*min(A_abs))

Submission status: Accepted

Solution code:
N = input()
A = map(int, raw_input().split())
B = []
flag = 2
flag2 = 0
k = 0
minabs = 0
for i in range(N):
	k = abs(A[i])
	B.append(k)
	if(k != 0 and (k < minabs or minabs == 0)):
		minabs = k
	if(i == N - 1):
		if(A[i] == 0 or flag2 == 1):
			flag2 = 1
		elif((flag == 1 and A[i] < 0) or (flag == 0 and A[i] > 0)):
			flag2 = 1
		else:
			flag2 = 0
	else:
		if(flag == 2):
			if(A[i] > 0):
				flag = 0
			elif(A[i] == 0):
				flag2 = 1
			else:
				flag = 1
		elif((A[i] > 0 and flag == 0) or (A[i] < 0 and flag == 1)):
			flag = 0
		elif((A[i] < 0 and flag == 0) or (A[i] > 0 and flag == 1)):
			flag = 1
		else:
			flag2 = 1
if(flag2 == 1):
	print(sum(B))
else:
	print(sum(B)- minabs * 2)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
a = (list(map(int, input().split())))
aa=[]
minus = 0
for i in range(n):
    if a[i] < 0:
        aa.append(-a[i])
        minus += 1
    else: aa.append(a[i])

sum_a = sum(aa)
if minus%2==0:
    print(sum_a)
else:
    print(sum_a-2*min(aa))

Submission status: Accepted

Solution code:
import sys,collections as cl
import fractions
from functools import reduce
import copy
Max = sys.maxsize
def l():
	return list(map(int,input().split()))
def m():
	return map(int,input().split())
def s(x):
	a = []
	aa = x[i]
	su = 1
	for i in range(len(x)-1):
		if aa == x[i+1]:
			a.append([aa,su])
			aa = x[i+1]
			su = 1
		else:
			su += 1
	a.append([aa,su])
	return a

n = int(input())
su = 0

a = l()

co = 0
k = 0
a.sort()
f = 0
for i in range(n):
	if a[i] <= 0:
		co += 1
if co % 2 == 0:
	for i in range(n):
		su += abs(a[i])
	print(su)
else:
	for i in range(n):
		if a[i] == 0:
			break
		if a[i] > 0:
			kk = a[i-1]
		if a[i] > 0:
			if abs(a[i-1]) >= a[i]:
				k = a[i]*(-2)
			else:
				k = kk*2
			break
		elif i == n-1:
			k = a[i]*2
	for i in range(n):
		su += abs(a[i])
	su += k
	print(su)

Submission status: Accepted

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

negs = [num for num in numbers if num <= 0]
abss = []

count = 0
for num in numbers:
  if num >= 0:
    count += num
    abss.append(num)
  else:
    count -= num
    abss.append(-num)
    
if len(negs) % 2 == 0:
  print(count)
else:
  print(count - 2*min(abss))

Submission status: Accepted

Solution code:
N = int(input())
a = input().split()
num=[]
numabs=[]
tex=[]
count=0
count2=0
for i in range(N):
	num.append(int(a[i]))
	numabs.append(abs(num[i]))
	if num[i] < 0 :
		count +=1
	elif num[i] == 0:
		count2 +=1
if count%2 ==0 or count2 > 0:
	res = sum(numabs)
else:
	res =sum(numabs)-2*min(numabs)
print(res)

Submission status: Accepted

Solution code:
from sys import stdin

readline = stdin.readline

_, *aa = map(int, open(0).read().split())
r = 0
mi = 1000000000
m = 0
for a in aa:
    if a < 0:
        m += 1
    b = abs(a)
    r += b
    mi = min(b, mi)
print(r - mi * 2 * (m % 2))

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
l=[]
cnt=0
for i in range(N):
    if A[i]<0:
        cnt+=1
        l.append(A[i])
wa=0
l_ab=[]
for i in range(N):
    wa+=abs(A[i])
    l_ab.append(abs(A[i]))
if cnt%2!=0:
    wa-=2*min(l_ab)

print(wa)

Submission status: Accepted

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

m = 0
su = 0
mi = 10 ** 10
for ai in A:
    if ai < 0:
        m += 1
    mi = min(mi, abs(ai))
    su += abs(ai)

if m % 2 == 1:
    print(su - 2 * mi)
else:
    print(su)

Submission status: Accepted

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

Submission status: Accepted

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

s = sum(map(abs, A))
if sum(1 for a in A if a < 0) % 2 == 0:
    print(s)
else:
    print(s - 2 * min(map(abs, A)))

Submission status: Accepted

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

zero = 0
minus = 0
ans = 0

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

if zero or (minus % 2 == 0) :
    ans = sum(A)
else :
    ans = sum(A) - min(A) * 2

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
num = list(map(int,input().split()))
numabs = [abs(i) for i in num]
cnt = 0           
for i in num :
    if i < 0 :
        cnt += 1

if cnt % 2 == 0 :
    print(sum(numabs))

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

Submission status: Accepted

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

Submission status: Accepted

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

is_even = True
exists_zero = False
min_a = 10 ** 9
for i, a in enumerate(a_list):
    if a == 0:
        exists_zero = True
    else:
        is_even = is_even ^ (a < 0)
    if min_a > abs(a):
        min_a = abs(a)
    a_list[i] = abs(a)

all_plus = is_even | exists_zero
if all_plus:
    print(sum(a_list))
else:
    print(sum(a_list) - min_a * 2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
w = input().split(" ")

a = []
m = 0
k = False
for i in range(n):
    a.append(int(w[i]))
    if a[i] < 0:
        a[i] *= -1
        k = not (k)
    if i == 0:
        m = a[i]
    if a[i] < m:
        m = a[i]

sum = sum(a)
if k:
    sum -= m*2
print(sum)

Submission status: Accepted

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

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

for i in range(n):
  abs_a.append(abs(a[i]))
  
if minus % 2 == 0:
  print(sum(abs_a))
else:
  min_abs_a = min(abs_a)
  print(sum(abs_a)-min_abs_a*2)

Submission status: Accepted

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

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
if cnt % 2 == 0:
    print(sum(map(lambda a: abs(a), A)))
else:
    mina = min(map(lambda a: abs(a), A))
    print(sum(map(lambda a: abs(a), A))-2*mina)

Submission status: Accepted

Solution code:

N = int(input())
A = [int(i) for i in input().split()]
if A[1] > 0:
	total_p = A[0]
	total_n = -A[0]
	B = [A[1],-A[1]]
else:
	total_p = -A[0]
	total_n = A[0]
	B = [-A[1],A[1]]
for i in range(2,N):
	if A[i] > 0:
		(total_p,total_n) = (max(total_p+B[0],total_n+B[1]),max(total_p+B[1],total_n+B[0]))
		B = [A[i],-A[i]]
	else:
		(total_p,total_n) = (max(total_p+B[1],total_n+B[0]),max(total_p+B[0],total_n+B[1]))
		B = [-A[i],A[i]]
print(max(total_n+B[1],total_p+B[0]))

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()

n = ni()
li = na()
ct = 0
s1 = 0
li2 = []
m = -1000000000000000000
for i in range(n):
    s1 += abs(li[i])
    li2.append(abs(li[i]))
    if li[i] < 0:
        ct += 1
if ct % 2 == 0:
    print(s1)
else:
    print(s1-2*min(li2))

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
a2 = [ abs(i) for i in a]
if cnt%2 == 0:
    print(sum(a2))
else:
    print(sum(a2)-min(a2)*2)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
minus = 0
mina = 10 ** 5
for a in A:
    if a < 0:
        minus += 1
    mina = min(mina, abs(a))
print(sum(map(abs, A)) - mina * 2 if minus % 2 else sum(map(abs, A)))

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
res = 0
A = [abs(a) for a in A]
A.sort()
for a in A:
        res += a
if cnt%2 == 1:
    res -= 2*A[0]
print(res)

Submission status: Accepted

Solution code:
N = int(input())
A_arr = list(map(int, input().split()))
A_abs = [abs(A_arr[i]) for i in range(N)]

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

ans = 0
if 0 in A_arr:
    for i in range(N):
        ans += A_abs[i]

else:
    if count % 2 == 0:
        for i in range(N):
            ans += A_abs[i]
    else:
        for i in range(N):
            ans += A_abs[i]
        ans -= 2*min(A_abs)

print(ans)

Submission status: Accepted

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

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

counter = 0
min_list = []
for i in range(N):
    if a_list[i] < 0:
        counter += 1
        min_list.append(a_list[i])

if counter % 2 == 0:
    abc = sum(min_list)
    abc *= -1
    ans = sum(a_list)
    print(ans + 2*abc)

if counter % 2 == 1:
    for i in range(N):
        if a_list[i] < 0:
            a_list[i] *= -1
    min_mun = min(a_list)
    ans = sum(a_list) - 2*min_mun
    print(ans)

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

Solution code:
import sys

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

    S = 0
    min_ = 10**9 + 1
    neg = 0
    for a in A:
        S += abs(a)
        min_ = min(min_, abs(a))

        if a < 0:
            neg += 1

    if neg % 2 == 0:
        return S
    else:
        return S - 2 * min_


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

Submission status: Accepted

Solution code:
def flipping(n,a):
    c = list(filter(lambda x:x<0,a))
    b = list(map(lambda x:abs(x),a))
    if len(c)%2==0:
        return sum(b)
    if len(c)%2!=0:
        return sum(b)-2*min(b)
    return b
n = int(input())
a=list(map(int, input().split()))
print(flipping(n,a))

Submission status: Accepted

Solution code:
n = int(input())
a = list(int(n) for n in input().split())
b = [0] * n
num_of_minus = 0

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

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

print(ans)

Submission status: Accepted

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

if len(np.where(A<0)[0])%2 == 0:
  print(np.abs(A).sum())
else:
  print(np.abs(A).sum() - np.abs(A).min()*2)

Submission status: Accepted

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

count = np.sum(A_array < 0)
# print(count)
A_array_abs = np.abs(A_array)
# print(A_array_abs)
A_array_abs = np.sort(A_array_abs)
# print(A_array_abs)
if A_array_abs[0] == 0 or count % 2 == 0:
    print(np.sum(A_array_abs))
else:
    print(np.sum(A_array_abs[1:]) - A_array_abs[0])

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
A = np.array([int(i) for i in input().split()])
negative_count = np.sum(A<0)

abs_A = list(map(abs, A))
if(negative_count%2==0):
    print(int(np.sum(abs_A)))
else:
    print(int(np.sum(abs_A) - (min(abs_A)*2)))

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split(" ")))
A_p = [ a for a in A if a > 0 ]
A_n = [ -1*a for a in A if a < 0 ]
A_0 = [ a for a in A if a == 0 ]

l_p = len(A_p)
l_n = len(A_n)
l_0 = len(A_0)

if l_p == 0:
    if l_n%2 == 0:
        print(sum(A_n+[0]))
    elif l_0 > 0:
        print(sum(A_n+[0]))
    else:
        tmp_n=min(A_n)
        A_n.remove(tmp_n)
        print(sum(A_n+[0])-tmp_n)
elif l_n == 0:
    print(sum(A_p+[0]))

elif l_n%2 == 0:
    print(sum(A_p)+sum(A_n))
else:
    if l_0 > 0:
        print(sum(A_p)+sum(A_n))
    else:
        if min(A_n) > min(A_p):
            tmp_n=min(A_p)
            A_p.remove(tmp_n)
            print(sum(A_p)+sum(A_n)-tmp_n)    
        else:
            tmp_n=min(A_n)
            A_n.remove(tmp_n)
            print(sum(A_p)+sum(A_n)-tmp_n)

Submission status: Accepted

Solution code:
import numpy as np

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

n = np.sum(A < 0)
b = np.abs(A)
s = np.sum(b)

if n % 2:
    print(s - 2 * np.min(b))
else:
    print(s)

Submission status: Accepted

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

Submission status: Accepted

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

sign = 1
abs_sum = 0
min_abs = int(1e9)

for a in a_list:
    tmp = abs(a)
    abs_sum += tmp
    min_abs = min(min_abs, tmp)
    sign *= (a >= 0) * 2 - 1

if sign > 0:
    print(abs_sum)
else:
    print(abs_sum - 2 * min_abs)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline
n = int(input())
a_list = list(map(int, input().split()))
new_list = []
cnt = 0
for a in a_list:
    if a < 0:
        cnt += 1
        a = -a
    new_list.append(a)
if cnt%2 == 0:
    print(sum(new_list))
else:
    print(sum(new_list)-min(new_list)*2)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(s) for s in input().split()]
abss = [abs(a) for a in A]
signs = sum(1 if a < 0 else 0 for a in A)
if signs % 2 == 0:
    print(sum(abss))
else:
    minabs = min(abss)
    print(sum(abss) - 2 * minabs)

Submission status: Accepted

Solution code:
import numpy as np
import sys
 
n = int(input())
l = np.array([int(x) for x in input().split()])
ans = 0
if 0 in l:
  for i in range(n):
    ans += abs(l[i])
  print(ans)
  sys.exit()
m = abs(l[0])
p = 0
for i in range(n):
  if l[i] < 0:
    p += 1
  if m > abs(l[i]):
    m = abs(l[i])
for i in range(n):
  ans += abs(l[i])
if p % 2 == 0:
  print(ans)
else:
  print(ans - m*2)

Submission status: Accepted

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

index=0
count=0
plSum=0
miSum=0
miniO=10000000
while index < len(listA):
    if listA[index] < 0:
        count+=1
        miSum-=listA[index]
    else:
        plSum+=listA[index]
    if abs(listA[index]) < miniO:
        miniO = abs(listA[index])
    index+=1
if count%2==0:
    print(plSum+miSum)
else:
    print(plSum+miSum-2*miniO)

Submission status: Accepted

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

if 0 in a:
  b = list(map(abs, a))
  print(sum(b))
  
else:
  if (a[a<0].size % 2) == 0:
    b = list(map(abs, a))
    print(sum(b))
  else:
    b = list(map(abs, a))
    b = sorted(b)
    b.reverse()
    c = b.pop()
    print(sum(b)-c)

Submission status: Accepted

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

count = 0
minus = 0
total = 0
for i in range(n):
    if a[i] == 0:
        count = i
if count == 0:
    while count < n:
        if a[count] < 0:
            minus += 1
        count += 1
else:
    minus = 0

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

print(total)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

N =int(input())
B = list(map(int,input().split()))
count=0
for i in range(N):
    if B[i]< 0:
        B[i] =-B[i]
        count+=1
#print(sum(B))
if count%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()))
s = sum([abs(a[i]) for i in range(n)])
cnt = 0
for i in range(n):
        if a[i] < 0:
                cnt += 1
if cnt % 2 == 0:
        ans = s
else:
        ans = s - 2 * min ([abs(a[i]) for i in range(n)])
print(ans)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n = ni()
a = na()
c=0
for i in a:
    if i < 0:
        c+=1

b = [abs(i) for i in a]

if c%2 == 0:
    print(sum(b))
    exit()

b.sort()
print(sum(b)-2*b[0])

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a = list(map(int, input().rstrip().split(' ')))
    total = 0
    n_count = 0
    n_max = -10000000000
    p_min = 10000000000
    for i in range(n):
        if a[i] < 0:
            n_count += 1
            if n_max < a[i]:
                n_max = a[i]
        else:
            if p_min > a[i]:
                p_min = a[i]
    if n_count % 2 == 0:
        total = sum(map(abs, a))
    else:
        total = sum(map(abs, a)) - 2 * min(p_min, abs(n_max))
    print(total)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

amin = 10 ** 9 + 1
cntm = 0
asum = 0
for a in A:
    if a < 0:
        cntm += 1
        amin = min(amin, -a)
        asum -= a
    else:
        amin = min(amin, a)
        asum += a
#    print("cntm={},amin={},asum={}".format(cntm,amin,asum))

if(cntm % 2):
    print(asum - 2 * amin)
else:
    print(asum)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
sei=[0]*n
mai=0
zero=0
pr=0
for i in range(n):
  sei[i]=max(a[i],-a[i])
  pr+=sei[i]
  if a[i]<0:
    mai +=1
  elif a[i]==0:
    zero+=1
  else:
    pass
#ゼロと負の数を数える
if mai%2==0 or zero >=1:
    print(pr)
else:
    print(pr-2*min(sei))

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
res = 0
if cnt%2 == 0:
    A = [abs(a) for a in A]
    for a in A:
        res += a
else:
    g = abs(A[0])
    for a in A:
        if abs(a) <= g:
            g = abs(a)
        res += abs(a)
    res -= 2*g
print(res)

Submission status: Accepted

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

minus_count_even_flug = 1
zero_exist_flug = -1

for a in a_list:
    if a < 0:
        minus_count_even_flug *= -1
    if a == 0:
        zero_exist_flug = 1

a_abs_list = [abs(a) for a in a_list]
sum_a_abs_list = sum(a_abs_list)
if minus_count_even_flug == 1 or zero_exist_flug == 1:
    print(sum_a_abs_list)
else:
    print(sum_a_abs_list - 2 * min(a_abs_list))

Submission status: Accepted

Solution code:
n = int(input())
lis = list(map(int,input().split()))
numm = 0
ans = 0
nu = 10 ** 9
for num in lis:
  nu = min(nu,abs(num))
  ans += abs(num)
  if num < 0:
    numm += 1
if numm % 2 == 1:
  print(ans-nu*2)
else:
  print(ans)

Submission status: Accepted

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

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

print(sum(A))

Submission status: Accepted

Solution code:
n = int(input())
aa = list(map(int,input().split()))
oddNum = 0
mainasuNum = 0
min_abs = abs(min(aa))
for a in aa:
    min_abs = abs(a) if abs(a) < min_abs else abs(min_abs)
    if a < 0:
        mainasuNum += abs(a)
        oddNum += 1
if oddNum%2 == 0:
    print(sum(aa)+mainasuNum*2)
else:
    print(sum(aa)+mainasuNum*2 - min_abs*2)

Submission status: Accepted

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

minNum = 0

for x in range(0, len(aList)):
	minNum += 1 if aList[x] < 0 else 0
	aList[x] = -aList[x] if aList[x] < 0 else aList[x]

aMin = min(aList)


even = minNum%2==0

if even:
	print(sum(aList))
else:
	print(sum(aList) - (aMin*2))

Submission status: Accepted

Solution code:
N=int(input())

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

o=0

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

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

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

Submission status: Accepted

Solution code:
import numpy as np

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

A = np.array(A)

plus = A[A >= 0]
minus = A[A < 0]

if len(minus) % 2 == 0:
    ans = np.sum(plus) - np.sum(minus)
else:
    ans = np.sum(plus) - np.sum(minus) - np.min(np.abs(A)) * 2

print(ans)

Submission status: Accepted

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

sum = 0
minus = 0
minn = -1
for a in A:
    b = abs(a)
    sum += b
    if minn == -1 or b < minn:
        minn = b
    if a < 0:
        minus += 1

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

Submission status: Accepted

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

min_m = -10**9+1
min_p = 10**9+1 
ans = 0
cnt = 0

for i in range(n):
  ans += abs(a[i])
  if min_p > a[i] >= 0:
    min_p = a[i]
  elif a[i] < 0:
    if min_m < a[i]:
      min_m = a[i]
    cnt += 1
if cnt % 2:
  if abs(min_m) > abs(min_p):
    print(ans - abs(min_p)*2)
  else:
     print(ans - abs(min_m)*2)
else:
  print(ans)

Submission status: Accepted

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

dp = [[-10**16 for __ in range(4)] for _ in range(N+10)]
A = A+[0,0,0,0,0,0,0]

if N == 2:
    print(max(A[0]+A[1], -A[0]-A[1]))
    exit()

dp[1][0] = A[0]+A[1] 
dp[1][1] = -A[0]-A[1]
# 0 -> ++
# 1 -> --
# 2 -> +-
# 3 -> -+

for i in range(1, N):
    # 0 ->
    dp[i+1][0] = max(dp[i+1][0], dp[i][0]+A[i+1])
    dp[i+1][1] = max(dp[i+1][1], dp[i][0]-A[i+1]-2*A[i])
    # 1 ->
    dp[i+1][3] = max(dp[i+1][3], dp[i][1]+A[i+1])
    dp[i+1][2] = max(dp[i+1][2], dp[i][1]-A[i+1]+2*A[i])
    # 2 ->
    dp[i+1][3] = max(dp[i+1][3], dp[i][2]+A[i+1])
    dp[i+1][2] = max(dp[i+1][2], dp[i][2]-A[i+1]+2*A[i])
    # 3 ->
    dp[i+1][0] = max(dp[i+1][0], dp[i][3]+A[i+1])
    dp[i+1][1] = max(dp[i+1][1], dp[i][3]-A[i+1]-2*A[i])    

print(max(dp[N-1]))

Submission status: Accepted

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

Submission status: Accepted

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

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

A = list(map(abs, A))

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

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
A = list(map(int,input().split()))
n = sum(1 for x in A if x<0)
B = np.abs(A)
if n % 2 == 1:
  print(sum(B) - 2*min(B))
else:
  print(sum(B))

Submission status: Accepted

Solution code:
n = input()
a_list = map(int, raw_input().split())

minus_count = 0
abs_min = 0
has_zero = False
total = 0
for a in a_list:
    if abs_min == 0 or abs(a) < abs_min:
        abs_min = abs(a)
    if a < 0:
        minus_count += 1
        total -= a
    elif a == 0:
        has_zero = True
    else:
        total += a
if not has_zero and minus_count % 2 == 1:
    total -= abs_min * 2
print(total)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
counter = 0
m = []
def abab(l):
    for i in range(len(l)):
        m.append(abs(l[i]))
    return m

for i in range(N):
    if A[i] < 0:
        counter += 1
      
if counter % 2 == 1:
    m = abab(A)
    idx = m.index(min(m))
    m[idx] *= -1
    print(sum(m))
else:
    print(sum(abab(A)))

Submission status: Accepted

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

A.sort()
for i in range(N-1):
    if A[i] <= 0 and A[i+1] <= 0:
        A[i] *= -1
        A[i+1] *= -1
    elif A[i] < A[i+1]*(-1):
        A[i] *= -1
        A[i+1] *= -1

print(sum(A))

Submission status: Accepted

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

zero = False
gu = 1
for a in A:
    if a == 0:
        zero = True
    elif a < 0:
        gu *= -1

anss = [abs(a) for a in A]
if zero | (gu == 1):
    print(sum(anss))

else:
    mina = min(anss)
    print(sum(anss) - 2 * mina)