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

Minus=0
Zero=0
L_absolute=[]

for i in LA:
    if i<0:
        L_absolute.append(i*(-1))
        Minus+=1
    elif i==0:
        Zero=1
    else:
        L_absolute.append(i)
        
L_absolute.sort()

if Zero==1:
    ans=sum(L_absolute)
elif Minus%2==1:
    ans=sum(L_absolute)-2*L_absolute[0]
else:
    ans=sum(L_absolute)
    
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
ans = 0
min_chk = 10**10
minus_chk = 0
for i in input().split():
    i = int(i)
    if i < 0 :
        minus_chk += 1
    num = abs(i)
    min_chk = min(num,min_chk)
    ans += num
if min_chk != 0:
    if minus_chk%2 == 1:
        print(ans-2*min_chk)
    else:
        print(ans)
else:
    print(ans)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
A.sort()
idx=N
for i in range(N):
    if A[i]>0:
        idx=i
        break
if idx==0:
    print(sum(A))
elif idx==N:
    print(sum(A[(N//2)*2:])-sum(A[:(N//2)*2]))
else:
    x=sum(A[(idx//2)*2:])-sum(A[:(idx//2)*2])
    y=sum(A[((idx+1)//2)*2:])-sum(A[:((idx+1)//2)*2])
    z=sum(A[(1+idx//2)*2:])-sum(A[:(1+idx//2)*2])
    print(max(x,y,z))

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
b=0
ans=0
m=abs(A[0])
for a in A:
    ans+=abs(a)
    m=min(m,abs(a))
    if a<0:
        b+=1

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
al=list(map(int,input().split()))
nummin=sum(a<0 for a in al)
asum=0
amin=10**9
for a in al:
  a=abs(a)
  asum+=a
  amin=min(amin,a)
if nummin%2==1:
  ans=asum-2*amin
else:
  ans=asum
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
count_t = list(filter(lambda x: x < 0, a))
count = len(count_t)
s = []
for i in a:
    if i < 0:
        s.append(i * -1)
    else:
        s.append(i)

if count % 2 == 0 or a.count(0) > 0:
    print(sum(s))
else:
    print(sum(s) - min(s) * 2)

Submission status: Accepted

Solution code:
#https://atcoder.jp/contests/abc125/tasks/abc125_d

from functools import reduce

def positivesum(lists):
	for i in range(len(lists)):
		if lists[i]<0:
			lists[i]=lists[i]*(-1)
	return(sum(lists))
	

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

#負数の数をカウント
count=0
for i in range(n):
	if alist[i]<0:
		count+=1


if count==n and count%2==1:
	print(-sum(alist)+2*max(alist))

elif count%2==0:
	print(positivesum(alist))
else:
	alist.sort()
	x=max(alist[count-1],-alist[count])
	print(positivesum(alist)+x*2)

Submission status: Accepted

Solution code:

def main():
    _ = input()
    A = list(map(int, input().split()))
    A_list = []
    count = 0

    for a in A:
        if a < 0:
            count += 1
            A_list.append(a * -1)
        else:
            A_list.append(a)
    if count % 2 == 0:
        print(sum(A_list))
    else:
        print(sum(A_list)-min(A_list) * 2)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:

def main():
    _ = input()
    A = list(map(int, input().split()))
    A_list = []
    count = 0
    for a in A:
        if a < 0:
            count += 1
            A_list.append(a * -1)
        else:
            A_list.append(a)
    if count % 2 == 0:
        print(sum(A_list))
    else:
        print(sum(A_list)-min(A_list) * 2)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
A = sorted(map(int, input().split()), key=lambda x : abs(x))
count = sum(x < 0 for x in A)
if count % 2 == 0:
    print(sum(abs(x) for x in A))
else:
    print(-abs(A[0]) + sum(abs(x) for x in A[1:]))

Submission status: Accepted

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

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

A = [abs(a) for a in A]
A.sort()

if sign < 0:
    print(sum(A)-2*A[0])
else:
    print(sum(A))

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())


n = ni()
a = list(li())

absmin = 10**18
mnscnt = 0
ans = 0

for ai in a:
    if ai < 0:
        mnscnt += 1

    absmin = min(absmin, abs(ai))
    ans += abs(ai)

if mnscnt % 2:
    print(ans - 2*absmin)
else:
    print(ans)

Submission status: Accepted

Solution code:
from functools import reduce
N = int(input())
A = list(map(int,input().split()))
B = list(map(lambda x: x+0.5, A))
C = reduce(lambda a, b: a*b, B)
D = sorted(list(map(abs, A)))
if C < 0:
	print(sum(D)-2*D[0])
else:
	print(sum(D))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
'''
allinputs = iter(input().splitlines())
input = lambda : next(allinputs)
#'''

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

sum_abs = 0
min_abs = 10 ** (9)
count_minus = 0
for i in range(N):
	if A[i] < 0:
		count_minus += 1
		sum_abs -= A[i]
		if -A[i] < min_abs:
			min_abs = -A[i]
	else:
		sum_abs += A[i]
		if A[i] < min_abs:
			min_abs = A[i]

if count_minus % 2 == 0:
	print(sum_abs)
else:
	print(sum_abs - 2 * min_abs)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

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

count=0
ans=0

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

if count%2==0:
    ans=sum(a)

else:
    a.sort()
    ans=sum(a)-a[0]*2

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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

count=0
ans=0

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

if count%2==0:
    ans=sum(a)

else:
    a.sort()
    ans=sum(a)-a[0]*2

print(ans)

Submission status: Accepted

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

minus = 1
for i,a in enumerate(A) :
    if a < 0 :
        minus *= -1
        A[i] = -a

ans = sum(A)

A.sort()

if minus == -1 :
    ans -= A[0] * 2

print(ans)

Submission status: Accepted

Solution code:
import numpy as np

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

A = np.array(A)
idx_minabs = np.argmin(np.abs(A))
n_minus = np.sum(A < 0) - (A[idx_minabs] < 0)

ans = np.sum(np.abs(A)) - np.abs(A[idx_minabs]) + (2*((n_minus+1)%2)-1)*A[idx_minabs]

print(ans)

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-
import numpy as np


n = int(raw_input())

A_list = map(int,raw_input().split())

minus_sum = sum(x<0 for x in A_list)

arr_A = np.array(A_list)
abs_arr = np.abs(arr_A)
ABS_list = abs_arr.tolist()
#print ('ABS_list',ABS_list)

_min = min(ABS_list)
min_id = [i for i, x in enumerate(ABS_list) if x == _min]

if(minus_sum%2 == 0):
	print sum(ABS_list)
else:
	print sum(ABS_list)-ABS_list[min_id[0]]-ABS_list[min_id[0]]

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-
import numpy as np

n = int(raw_input())

A_list = map(int, raw_input().split())
A_list_np = np.array(A_list)

minus_sum = np.sum(A_list_np < 0)

if(minus_sum%2 == 0):
	print np.sum(np.abs(A_list_np))
else:
	print np.sum(np.abs(A_list_np))-np.min(np.abs(A_list_np)) * 2

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
A.sort()
eo = False #False = even
suma = sum(A)
lta = 10**10
for j in range(n):
    i = A[j]
    if i <= 0: #0以下
        if j == n - 1 and not eo: break
        eo ^= True
        lta = i
        suma += 2*(-i)
        #print(i,"を+にした")
    else: #0以上なら
        if lta == 10**10: break
        if eo: #奇数回変更歴なら
            if abs(i) > abs(lta):
                suma += lta*2
            else:
                suma += 2*(-i)
         #   print("oddかつ",abs(i),">",abs(lta),"だったので元に戻した")
        break
print(suma)

Submission status: Accepted

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

# どちらも同じ結果となる：
negative_values = list(filter(lambda x:x<0 , a) )
# negative_values = [x for x in a if x<0]
num_negative = len(negative_values)

if num_negative % 2 == 0 :
	ans = sum(list(map(abs, a)))
	print(ans)
else:
	# 絶対値が最小のものを負にしたものが答え
	abs_array = list(map(abs, a))
	ans = sum(abs_array) - 2 * min(abs_array)
	print(ans)

Submission status: Accepted

Solution code:
N = int(input())
# N < 10 ** 5
A = sorted([int(x) for x in input().split()])
# - 10 ** 9 < A < 10 ** 9

B = [-a for a in A if a < 0]
D = [a for a in A if a >= 0]

if len(B) % 2 == 1:
    B[-1] = -B[-1]
    if D and -B[-1] > D[0]:
        B[-1] = -B[-1]
        D[0] = -D[0]

print(sum(B)+sum(D))

Submission status: Accepted

Solution code:
N = int(input())

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

ans = 0
A_min = 10 ** 10
if len([a for a in A if a < 0]) % 2 == 0:
    for a in A:
        ans += abs(a)
    print(ans)
else:
    for a in A:
        ans += abs(a)
        A_min = min(abs(a), A_min)
    print(ans - 2 * A_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

ans = 0
MIN = 10 ** 9
if count % 2:
    for i in range(N):
        ans += abs(A[i])
        MIN = min(abs(A[i]), MIN)
    ans = ans - 2 * MIN
else:
    for i in range(N):
        ans += abs(A[i])

print(ans)

Submission status: Accepted

Solution code:
#写経

n=input()
a=list(map(int,input().split()))
b=[abs(x)for x in a]

#sum(x<0 for x in a)%2 ここで負の値の個数数えてるのが！
print(sum(b)-2*min(b)if sum(x<0 for x in a)%2 else sum(b))

Submission status: Accepted

Solution code:
# abcde	s=input()	s='abcde'
# abcde	s=list(input())	s=['a', 'b', 'c', 'd', 'e']
# 5(1つだけ)	a=int(input())	a=5
# 1 2	| x,y = s_inpl()   |	x=1,y=2
# 1 2 3 4 5 ... n 　	li = input().split()	li=['1','2','3',...,'n']
# 1 2 3 4 5 ... n 　	li = inpl()	li=[1,2,3,4,5,...,n]
# FFFTFTTFF 　	li = input().split('T')	li=['FFF', 'F', '', 'FF']

# INPUT
# 3
# hoge
# foo
# bar
# ANSWER
# n=int(input())
# string_list=[input() for i in range(n)]

import math
import copy
from collections import defaultdict
from collections import Counter
from collections import deque
# 直積 A={a, b, c}, B={d, e}:のとき，A×B={(a,d),(a,e),(b,d),(b,e),(c,d),(c,e)}: product(A, B)
from itertools import product
# 階乗 P!: permutations(seq), 順列 {}_len(seq) P_n: permutations(seq, n)
from itertools import permutations
# 組み合わせ {}_len(seq) C_n: combinations(seq, n)
from itertools import combinations
from bisect import bisect_left, bisect_right
# import numpy as np

def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W

# 四方向: 右, 下, 左, 上
dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]

def i_inpl(): return int(input())
def s_inpl(): return map(int,input().split())
def l_inpl(): return list(map(int, input().split()))
INF = float("inf")

############
############
############


N = i_inpl()
A = l_inpl()

# dp[i][0]: i-1まで-1を偶数回乗算
# dp[i][1]: i-1まで-1を奇数回乗算
dp = [[-INF] * 2 for _ in range(N+1)]
dp[0][0] = 0

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

print(dp[N][0])

Submission status: Accepted

Solution code:
import numpy as np
from collections import defaultdict
from functools import reduce

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

S = reduce(lambda x, y: abs(x)+abs(y), A)


if len(list(filter(lambda x: x<0, A)))%2 == 0:
    print(S)
else:
    print(S - 2*min(abs(x) for x in A))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
abs_A = [abs(i) for i in A]
neg_A = [i for i in A if i < 0]

if len(neg_A) % 2 == 0:
	ans = sum(abs_A)
else:
	min = min(abs_A)
	ans = sum(abs_A) - 2 * min

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int, input().split()))
abs_a = []
#負の数を数えながら、絶対値でどれが一番小さいかを計算
min_a = 10**9
num_minus = 0
for i in range(n):
    abs_ai = abs(a[i])
    if a[i]<0:
        num_minus+=1
    if abs_ai<min_a:
        min_a=abs(abs_ai)
    abs_a.append(abs_ai)
ans=sum(abs_a)
if num_minus%2!=0:
    ans = ans-2*min_a
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

abs_min=abs(a[0])
abs_sum=0
cnt=0
for val in a:
	if val<0:
		cnt+=1
	abs_sum+=abs(val)
	abs_min=min([abs_min,abs(val)])

if cnt%2==0:
	print(abs_sum)
else:
	print(abs_sum-2*abs_min)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split(' ')))
ABS_LIST = list(map(abs,A))
ABS_SUM = sum(ABS_LIST)
if len([x for x in A if x < 0])%2 != 0:
    print(ABS_SUM-min(ABS_LIST)*2)
else:
    print(ABS_SUM)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split(' ')))
sum = 0
min = 1000000000
minus = 0
for n in range(N):
    n_abs = abs(A[n])
    sum += n_abs
    if min > n_abs:
        min = n_abs
    if A[n] < 0:
        minus += 1
if minus%2 != 0:
    sum -= 2*min
print(sum)

Submission status: Accepted

Solution code:
# 数列の個数
num_count = int(input())

# 数列を定義
nums_list = list(map(int, input().split()))

max_num = 0
zero_count = 0
minus_count = 0

for num in nums_list:
    max_num += abs(num)

    if num == 0:
        zero_count += 1
    if num < 0:
        minus_count += 1

if zero_count == 0 and minus_count % 2 != 0:
    max_num -= min(list(map(abs, nums_list))) * 2

print(max_num)

Submission status: Accepted

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

abs_list = list(map(abs, A))
abs = sum(abs_list)
x = len([i for i in A if i < 0]) 

if x % 2 ==0:
  print(abs)
 
else:
  min_abs = min(abs_list)
  print(abs - 2*min_abs)

Submission status: Accepted

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

for i in a:
    if i > 0:
        ans += i
        if m > i:
            m = i
    else:
        ans -= i
        c += 1
        if m > -i:
            m = -i

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int,input().split()))
k = 0
p = 0
for i in l:
    if i < 0:
        k += 1

for i in l:
    p += abs(i)
if k % 2 ==0:
    print(p)
else:
    print(p-(2*min(list(map(abs,l)))))

Submission status: Accepted

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

b = []

c = 0
for a in A:
    if a < 0:
        c += 1
    b.append(abs(a))

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

num_of_minus = sum([a < 0 for a in A])
abs_A = [abs(a) for a in A]
ans = sum(abs_A)

if num_of_minus % 2 == 0:
  print(ans)
else:
  print(ans - 2 * min(abs_A))

Submission status: Accepted

Solution code:
import numpy as np

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

wh = len([i for i in a if i <= 0])

if wh % 2 == 0 :
    print(sum(np.abs(na)))
else :
    m = min(np.abs(na))
    print(sum(np.abs(na)) - 2*m)

Submission status: Accepted

Solution code:
n = int(input())
arr = list(map(int, input().split()))
abs_arr = list(map(abs, arr))

if len(list(filter(lambda i: i < 0, arr))) % 2 == 1:
    print(sum(abs_arr) - min(abs_arr) * 2)
else:
    print(sum(abs_arr))

Submission status: Accepted

Solution code:
# 絶対値が最も小さい数に向けて、両端から始め、その数より遠い側の数がマイナスなら入れ替えると言う動作を行う
# すると絶対値が最小の数以外は全て正になる
# あとは、絶対値が最小の数が正か負かを判断するのみ
# 絶対値が最小の数の符号は、全体のうちマイナスが奇数個なら負で、偶数個なら正

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

minus = 0
a_min = abs(a[0])
a_sum = 0

for x in a:
  if x < 0:
    minus += 1
    x = -x
  if x < a_min:
    a_min = x
  a_sum += x

if minus % 2 == 0:
  print(a_sum)
else:
  print(a_sum - 2 * a_min)

Submission status: Accepted

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

minus_num = [i for i in A if i < 0]
abs_num = map(abs, A)
min_num = min(abs_num)

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

if len(minus_num) % 2 != 0:
    A.append(min_num * -2)

print(sum(A))

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
 
if 0 in A:
    out = 0
    for i in A:
        out += abs(i)
    print(out)
else:
    count = 0
    for i in A:
        if i < 0:
            count += 1
    
    if count%2 == 0:
        out = 0
        for i in A:
            out += abs(i)
        print(out)
    else:
        count = 10 ** 9
        for i in A:
            if abs(i) < count:
                count = abs(i)
        
        out = 0
        for i in A:
            out += abs(i)
        out = out - count*2
        
        print(out)

Submission status: Accepted

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

ans = 0

#nns は　負の整数の数を表す
nns = len([x for x in a if x < 0])

ans = sum([abs(x) for x in a])

if nns%2 != 0:
  ans -= 2*min(abs(i) for i in a)
  
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
dp_table = [0, -10 ** 10]
temp = [0,0]
for i in range(n):
    temp[0] = dp_table[0]
    temp[1] = dp_table[1]
    dp_table[0] = max(temp[0]+a[i],temp[1]-a[i])
    dp_table[1] = max(temp[0]-a[i],temp[1]+a[i])
    
print(dp_table[0])

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=[abs(i) for i in a]
m=len([i for i in a if i<0])
M=len([i for i in a if i>0])
ans=0
if (m%2==1 and M%2==0) or (m%2==M%2==1):
    ans=sum(b)-2*min(b)
else:
    ans=sum(b)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
dp = [0, -10**20]
tmp = [0, 0]

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
dp = [[0,0] for i in range(N+1)]
dp[0][1] = -10**20

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

print(dp[-1][0])

Submission status: Accepted

Solution code:
from sys import stdin


def main():
    _ = int(stdin.readline().rstrip())
    As = [int(x) for x in stdin.readline().rstrip().split()]
    mcount = len([x for x in As if x < 0])
    if mcount % 2 == 0:
        print(sum([abs(x) for x in As]))
    else:
        min_ = min([abs(x) for x in As])
        sum_ = sum([abs(x) for x in As])
        print(sum_ - (min_ * 2))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def sub():
    N, As = [input() for _ in range(2)]
    N = int(N)
    As = [int(e) for e in As.split(" ")]

    dp = {(-1, 0): 0, (-1, 1): -1e10}
    for i in range(0, N):
        dp[(i, 0)] = max(dp[(i - 1, 0)] + As[i], dp[(i - 1, 1)] - As[i])
        dp[(i, 1)] = max(dp[(i - 1, 0)] - As[i], dp[(i - 1, 1)] + As[i])
    print("{}".format(dp[(N - 1, 0)]))


sub()

Submission status: Accepted

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

num_of_minus = sum([a < 0 for a in A])
abs_A = [abs(a) for a in A]

ans = sum(abs_A)

if num_of_minus % 2 == 0:
    print(ans)
else:
    print(ans - 2 * min(abs_A))

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
pbox=[]
nbox=[]
if 0 in A:
  count=0
  for i in A:
    count += abs(i)
  print(count)
else:
  for i in A:
    if i > 0:
      pbox.append(i)
    else:
      nbox.append(i)
  if len(nbox)%2==0:
    count=0
    for i in A:
      count += abs(i)
    print(count)
  else:
    box=[]
    for i in A:
      box.append(abs(i))
    mi=min(box)
    count=0
    for i in A:
      count += abs(i)
    print(count-2*mi)

Submission status: Accepted

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

i = 0
j = 0
total = 0
count = 0
min = abs(A[0])

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

if count % 2 == 1:
    total = total - 2 * min

print(total)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
pbox = []
nbox = []
if 0 in a:
    count = 0
    for i in a:
        count += abs(i)
    print(count)
else:
    for i in a:
        if i > 0:
            pbox.append(i)
        else:
            nbox.append(i)
    if len(nbox) % 2 == 0:
        count = 0
        for i in a:
            count += abs(i)
        print(count)
    else:
        box = []
        for i in a:
            box.append(abs(i))
        mi = min(box)
        count = 0
        for i in a:
            count += abs(i)
        print(count - 2*mi)

Submission status: Accepted

Solution code:

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

func = lambda x : 1 if( x > 1) else 0 if( x == 0) else -1

signs = [func(x) for x in lst]
lst = [abs(x) for x in lst]

if(signs.count(-1)%2 == 1):
    m = min(lst)
    lst.remove(m)
    s = sum(lst) - m
    print(s)
else:
    print(sum(lst))

Submission status: Accepted

Solution code:
#Conding: Utf-8

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

count=num=total=0
b=[]

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

if count%2==0 or num>0:
    for j in a:
        total=total+abs(j)
    print(total)
else:
    for k in a:
        total=total+abs(k)
    for l in a:
        b.append(abs(l))

    print(total-2*min(b))

Submission status: Accepted

Solution code:
from functools import reduce
from operator import add

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

m = 0 #-の個数
z = 0 #0の個数

for i in range(n):
  if lst[i] == 0:
    z += 1
  if lst[i] < 0:
    m += 1

ans = 0
l = list(map(abs, lst))
if m%2 == 1 and z == 0:
  ans = reduce(add,l) - 2 * min(l)
else:
  ans = reduce(add,l)
print(ans)

Submission status: Accepted

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

if sum([x<0 for x in a])%2 == 0:
    print(sum([x if x > 0 else -x for x in a]))
else:
    print(sum([x if x > 0 else -x for x in a]) - min(list(map(abs, a))) * 2)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=[abs(x) for x in a]
ret=0
for i in range(n):
  if a[i]!=b[i]:
    ret+=1
ans=sum(b)
if ret%2:
  ans-=2*min(b)
print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate
from math import*
from collections import deque
from collections import defaultdict
from itertools import permutations
from heapq import*
from collections import Counter
from itertools import*
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf = 10**18
mod = 10**9+7
from functools import reduce
import sys
sys.setrecursionlimit(10**7)

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

Submission status: Accepted

Solution code:
import sys
from functools import lru_cache
sys.setrecursionlimit(10**7)
INF = 10 ** 18

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def II(): return int(sys.stdin.readline())

N = II()
A_li = LI()
N = len(A_li)

@lru_cache(maxsize=None)
def dp(i,j):
    if i == 0:
        if j == 0: return A_li[0]
        elif j == 1: return -A_li[0]
    else:
        if j == 0: return max(dp(i-1, 0) + A_li[i], dp(i-1, 1) - A_li[i])
        elif j == 1: return max(dp(i-1, 0) - A_li[i], dp(i-1, 1) + A_li[i])

print(dp(N-1,0))

Submission status: Accepted

Solution code:
import sys
from functools import lru_cache
sys.setrecursionlimit(10**7)
INF = 10 ** 18
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def II(): return int(sys.stdin.readline())

N = II()
A_li = LI()
N = len(A_li)

@lru_cache(maxsize=None)
def dp(i,j):
    if i == 0:
        if j == 0: return 0
        elif j == 1: return -INF
    else:
        if j == 0: return max(dp(i-1,0) + A_li[i-1], dp(i-1,1) - A_li[i-1])
        if j == 1: return max(dp(i-1,0) - A_li[i-1], dp(i-1,1) + A_li[i-1])

print(dp(N,0))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def II(): return int(sys.stdin.readline())

N = II()
A_li = LI()

import numpy as np

A_abs = np.abs(A_li)
A_neg_count = sum([1 if i<0 else 0 for i in A_li])

if 0 in A_li or A_neg_count%2 == 0: print(sum(A_abs))
else: print(sum(A_abs) - 2 * min(A_abs))

Submission status: Accepted

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

s = 0
sign = 1
m = 1e10
for i in range(N):
    abs_val = abs(AN[i])

    s += abs_val
    sign = sign * (1 if abs_val == AN[i] else -1)
    m = min(m, abs_val)

if sign < 0: 
    print(s - 2 * m)
else:
    print(s)