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:
#!/usr/bin/env python3

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

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

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

# print(num_minus)
# print(a_plus)
if num_minus % 2 == 0:
    print(sum(a_plus))
else:
    print(sum(a_plus)-2*min(a_plus))

Submission status: Accepted

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

ans=0;x=10**9;minus_n=0
for a in A:
  if a<0:
    minus_n+=1
    ans+=-a
  else:
    ans+=a
    
  if x>abs(a):
    x=abs(a)

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

Submission status: Accepted

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

cnt = 0
f = 0

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

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

Submission status: Accepted

Solution code:
import numpy as np

n = int(input()) 

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

minus = 0
zero = 0
for num in nums:
    if num == 0:
        zero += 1
        break
    elif num < 0:
        minus += 1

nums = np.abs(nums)
nums.sort()

if minus % 2 and not zero:
    ans = sum(nums) - 2 * nums[0]

else:
    ans = sum(nums)

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
import collections

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

a=np.array(A, dtype=np.float64)
asign=np.sign(A)
c = collections.Counter(asign)
ar = np.array(c[-1], dtype=np.float64)
ac = a * asign

if c[-1] % 2 == 0:
    print(int(sum(ac)))
else:
    print(int(sum(ac)-min(ac)*2))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import numpy as np
from collections import deque
from collections import defaultdict
import heapq
import collections
import itertools
import bisect


mod = 10 ** 9 + 7
N = int(input())
A = np.array(list(map(int, input().split())), dtype=np.int64)
lis = []
ans = 0
# print(len(A < 0))
if (len(A[A < 0]) % 2) == 0:
    abs_min = 0
    for a in A:
        ans += abs(a)
    print(ans)
else:
    abs_min = A[0]
    for a in A:
        ans += abs(a)  # aaa
        abs_min = min(abs(abs_min), abs(a))
    print(ans-abs(abs_min)*2)
# print(np.sum(lis) - abs_min*2)
# print(A)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
i = 10**9
s = 0
z = 0
c = 0
for b in a:
  if b < 0:
    s += 1
  elif b == 0:
    z += 1
  if abs(b) < i:
    i = abs(b)
  c += abs(b)
if z > 0 or s%2 == 0:
  print(c)
else:
  print(c-2*i)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
num_sum = 0
num_min = abs(a[0])
num_minus = 0

for i in range(n):
    if (a[i] <= 0):
        num_minus += 1
    num_min = min(num_min, abs(a[i]))
    num_sum += abs(a[i])

if (num_minus % 2 == 0):
    print (num_sum)
else:
    print(num_sum-num_min*2)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
num_sum = 0
num_min = abs(a[1])
num_minus = 0

for i in range(n):
    if (a[i] <= 0):
        num_minus += 1
    num_min = min(num_min, abs(a[i]))
    num_sum += abs(a[i])

if (num_minus % 2 == 0):
    print (num_sum)
else:
    print(num_sum-num_min*2)

Submission status: Accepted

Solution code:
import numpy as np
import sys
input = sys.stdin.readline
N = int(input())
A = [int(x) for x in input().split()]
A_count_mynus =[i for i in A if i<0]
A_plus =[abs(i) for i in A]

#Aの内マイナスの要素数が偶数奇数で場合分け
ans =0
if len(A_count_mynus) %2 ==0:
    ans =sum(A_plus) #好きなだけ－1を乗じれば全て正
else:
    A_plus.sort()
    ans = -A_plus[0] +sum(A_plus[1:]) #最小値のみマイナス、他はプラス
print(ans)

Submission status: Accepted

Solution code:
import numpy as np

def main():
    n = int(input())
    a = list(map(int, input().split()))
    A = np.array(a)
    B = np.where(A < 0, A * -1, A)
    cnt = np.count_nonzero(A < 0)
    #print(B)
    s = B.sum()
    if cnt % 2 == 0:
        print(s)
    else:
        m = np.amin(B)
        print(s - m * 2)
        

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
minus = 0
m = float("inf")
s = 0
for i in map(int,input().split()):
    if i < 0:
        minus += 1
        m = min(m,-i)
        s -= i
    else:
        m = min(m,i)
        s += i
if minus % 2 == 0:
    print(s)
else:
    print(s-2*m)

Submission status: Accepted

Solution code:
n = int(input())
a_list = list(map(int,input().split()))
abs_list = []
minus = 0
check = False
for a in a_list:
  abs_list.append(abs(a))
  if a < 0:
    minus += 1
  if a == 0:
    check = True
if minus % 2 == 0 or check == True:
  print(sum(abs_list))
else:
  print(sum(abs_list) - 2 * min(abs_list))

Submission status: Accepted

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

count = 0
for i in range(n):
  if a[i] < 0:
    count += 1
    
if count % 2 == 0:
  print(sum(absa))
else:
  absa.sort()
  temp = absa.pop(0)
  print(sum(absa)-temp)

Submission status: Accepted

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

ans=0
m_cnt=0
min_abs=10**18
for i in a:
    if i<0:
        m_cnt+=1
    tmp=abs(i)
    ans+=tmp
    min_abs=min(min_abs, tmp)
if m_cnt%2==0:
    print(ans)
else:
    print(ans-2*min_abs)

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 i in range(N):
    ans += abs(A[i])
  print(ans)
else:
  minabs = 10**9
  for i in range(N):
    minabs = min(minabs, abs(A[i]))
  for i in range(N):
    ans += abs(A[i])
  print(ans - 2*minabs)

Submission status: Accepted

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

def read():
    N = int(input().strip())
    A = list(map(int, input().strip().split()))
    return N, A


def solve(N, A):
    abs_a = list(map(abs, A))
    min_a = min(abs_a)
    for i in range(N-1):
        if A[i] < 0:
            A[i] = -A[i]
            A[i+1] = -A[i+1]
    if A[N-1] < 0:
        return sum(abs_a) - min_a * 2
    else:
        return sum(abs_a)


if __name__ == '__main__':
    inputs = read()
    print(solve(*inputs))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
cnt = 0
m = float('inf')
for i in range(n):
    if a[i] < 0:
        cnt += 1
    if abs(a[i]) < m:
        m = abs(a[i])
ans = 0
for i in range(n):
    ans += abs(a[i])
if cnt % 2 == 1:
    ans -= m * 2
print(ans)

Submission status: Accepted

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

hu = []
sei = []
for i in a:
	if i < 0:
		hu.append(i)
	else:
		sei.append(i)
hu.sort()
sei.sort()
if len(hu)%2 == 0:
	ans = sum(sei) + (-1)*sum(hu)
else:
	if len(sei) == 0:
		ans = (-1)*sum(hu[:-1]) + hu[-1]
		print (ans)
		exit()
	else:
		b = sei[0]
	if len(hu) == 0:
		ans = sum(sei[1:] + sei[-1])
		print (ans)
		exit()
	else:
		c = hu[-1]
	if abs(b) <= abs(c):
		b *= -1
		c *= -1
	ans = sum(sei[1:]) + (-1)*sum(hu[:-1])
	ans += (b+c)
print (ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
# from collections import defaultdict, deque
# from math import sqrt, gcd, factorial, tan, pi, sin, cos
def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline()[:-1]


def solve():
    n = int(input())
    a = [int(x) for x in input().split()]
    mn = 1e9
    c = 0
    z = 0
    sm = 0
    for e in a:
        mn = min(mn, abs(e))
        if e < 0:
            c += 1
        if e == 0:
            z += 1
        sm += abs(e)
    if not z:
        if c % 2:
            sm -= 2 * mn

    print(sm)
    
        




t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()



"""
1 -> (1, 2) -> (2 ** 3, 3) -> (3, 1)

"""

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

minus = len([x for x in a if x < 0])
ans = sum(abs(x) for x in a)
if minus % 2 != 0:
    m = min(abs(x) for x in a)
    ans -= m*2

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
li = list(map(int,input().split()))
cnt = 0
new_li = []
for l in li:
    if l<0:
        cnt += 1
    new_li.append(abs(l))
if cnt%2 == 0:
    print(sum(new_li))
else:
    print(sum(new_li)-(2*(min(new_li))))

Submission status: Accepted

Solution code:
n = int(input())
a_list = list(map(int,input().split()))
dp = [[0] * 2 for _ in range(n-1)]
dp[0][0] = a_list[0] + a_list[1]
dp[0][1] = -1*(a_list[0] + a_list[1])

for i in range(1,n-1):
    dp[i][0] = max(dp[i-1][0] + a_list[i+1] , dp[i-1][1] + a_list[i+1])
    dp[i][1] = max(dp[i-1][0] - a_list[i+1] -2*a_list[i], dp[i-1][1] - a_list[i+1]+2*a_list[i])
    
print(max(dp[n-2][0] ,dp[n-2][1]))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
num = 10000000000000000000000000
ans = 0
cnt = 0
for i in range(N):
    a = abs(A[i])
    num = min(num,a)
    ans += a
    if A[i] != a:
        cnt += 1
if cnt%2  == 1:
    ans -= num*2
print(ans)

Submission status: Accepted

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

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

if m % 2 == 1:
    print(sum(b) - 2*min(b))
else:
    print(sum((b)))

Submission status: Accepted

Solution code:
n = int(input())

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

min_signs = 0

for num in a:
    if num < 0:
        min_signs += 1

a = list(map(abs, a))

if min_signs % 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()))
neg = sum([1 for i in range(N)  if A[i] < 0])
s = [abs(A[i]) for i in range(N)]
ans = sum(s)
if neg % 2 == 0:
  print(ans)
else:
  print(ans - 2*min(s))

Submission status: Accepted

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


def get_input() -> tuple:
    """
    標準入力の取得

    Returns:\n
        tuple: 標準入力
    """
    N = int(input())
    A = list(map(int, input().split()))

    return N, A


def main(N: int, A: list) -> None:
    """
    求解処理.\n
    数列内の負の数が奇数か偶数かで場合分けを行う.

    Args:\n
        N (int): 整数列内の要素数
        A (list): 整数列
    """
    minus_sign_count = len([a for a in A if a < 0])
    B = [abs(a) for a in A]
    if minus_sign_count % 2 == 1:
        # 絶対値が最小の要素の正負を反転する
        B[B.index(min(B))] *= -1
    result = sum(B)

    # 結果出力
    print(result)


if __name__ == "__main__":
    N, A = get_input()
    main(N, A)

Submission status: Accepted

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

# 標準入力の取得
N = int(input())
A = list(map(int, input().split()))

# 求解処理
# 数列A内に負の数が奇数/偶数で場合分け
sign_count = len([a for a in A if a < 0])
result = int()
B = [abs(a) for a in A]
if sign_count % 2 == 1:
    B[B.index(min(B))] *= -1
result = sum(B)

# 結果出力
print(result)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
m_cnt = 0
for a in A:
    if a < 0:
        m_cnt += 1

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

ans = 0
for a in A:
    ans += abs(a)

if m_cnt % 2 == 1:
    ans -= abs(min(A))*2

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
dp = [[0]*2 for i in range(n+1)]
dp[0][1] = -float('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 numpy as np
N = int(input())
A = np.array(list(map(int, input().split())))
my_sum = np.sum(np.abs(A))
if np.count_nonzero(A < 0) % 2 == 0:
    print(my_sum)
else:
    print(my_sum - 2 * np.min(np.abs(A)))

Submission status: Accepted

Solution code:
import itertools
import math
import fractions
import functools
import copy

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

pm = [0,0]

for i in range(n):
    if a[i] == 0:
        print(sum(list(map(abs,a))))
        quit()
    elif a[i] > 0:
        pm[0] += 1
    else:
        pm[1] += 1
if pm[1] % 2 != 0:
    print(sum(list(map(abs,a))) - 2 * min(list(map(abs,a))))
else:
    print(sum(list(map(abs,a))))

Submission status: Accepted

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

n_nega = sum(aa<0 for aa in a)
sumabs = sum(abs(aa) for aa in a)
if n_nega%2==0:
    print(sumabs)
else:
    mi=min( abs(aa) for aa in a)
    print(sumabs-mi*2)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
if 0 in a:
  print(sum(map(abs, a)))
  exit()
minus = len([i for i in range(n) if a[i]<0 ])
if minus % 2 == 0:
  print(sum(map(abs, a)))
else:
  a = sorted(a, key=lambda x: abs(x))
  print(sum(map(abs, a[1:]))-abs(a[0]))

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
A = list(map(int,input().split()))
a = np.abs(A)
mainasu = 0

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

if mainasu%2 == 0:
  ans = sum(a)
else:
  a = sorted(a)
  ans = sum(a[1:n]) - a[0]
  
print(ans)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

def main():
    N = int(input())
    A = list(map(int, input().split()))
    ans = 0; cnt = 0; tmp = float('inf'); flag = False
    for a in A:
        ans += abs(a)
        tmp = min(tmp, abs(a))
        if a < 0:
            cnt += 1
        elif a == 0:
            flag = True
        else:
            continue
    if cnt%2 == 0:
        flag = True
    if flag:
        print(ans)
    else:
        print(ans - 2*tmp)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from copy import copy, deepcopy
# from functools import reduce
# from heapq import heapify, heappop, heappush
# from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product
# import math
# import numpy as np  # Pythonのみ！
# from operator import xor
# import re
# from scipy.sparse.csgraph import connected_components  # Pythonのみ！
# ↑cf.  https://note.nkmk.me/python-scipy-connected-components/
# from scipy.sparse import csr_matrix
# import statistics # Pythonのみ
# import string
import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

def resolve():
    
    N=int(input())
    A=list(map(int,input().split()))
    B=copy(A)
    cnt=0
    for i in range(N):
        if A[i]<0:
            cnt+=1
            B[i]=(-1)*A[i]
    B.sort()
    ans=sum(B)
    if cnt%2==1:
        ans-=B[0]*2
    
    print(ans)

    
resolve()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
cnt_list = [0, 0, 0]
total = 0
low = 10**9
for a in A:
    low = min(low, abs(a))
    total += abs(a)
    if a == 0:
        cnt_list[1] += 1
    elif a > 0:
        cnt_list[2] += 1
    else:
        cnt_list[0] += 1
if (cnt_list[1] > 0)or(cnt_list[0] % 2 == 0):
    print(total)
else:
    print(total-2*low)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

import sys
# import math
# from string import ascii_lowercase, ascii_upper_case, ascii_letters, digits, hexdigits
# import re                                    # re.compile(pattern) => ptn obj; p.search(s), p.match(s), p.finditer(s) => match obj; p.sub(after, s)
# from operator import itemgetter              # itemgetter(1), itemgetter('key')
# from collections import deque                # deque class. deque(L): dq.append(x), dq.appendleft(x), dq.pop(), dq.popleft(), dq.rotate()
# from collections import defaultdict          # subclass of dict. defaultdict(facroty)
# from collections import Counter              # subclass of dict. Counter(iter): c.elements(), c.most_common(n), c.subtract(iter)
# from heapq import heapify, heappush, heappop # built-in list. heapify(L) changes list in-place to min-heap in O(n), heappush(heapL, x) and heappop(heapL) in O(lgn).
# from heapq import nlargest, nsmallest        # nlargest(n, iter[, key]) returns k-largest-list in O(n+klgn).
# from itertools import count, cycle, repeat   # count(start[,step]), cycle(iter), repeat(elm[,n])
# from itertools import groupby                # [(k, list(g)) for k, g in groupby('000112')] returns [('0',['0','0','0']), ('1',['1','1']), ('2',['2'])]
# from itertools import starmap                # starmap(pow, [[2,5], [3,2]]) returns [32, 9]
# from itertools import product, permutations  # product(iter, repeat=n), permutations(iter[,r])
# from itertools import combinations, combinations_with_replacement
# from itertools import accumulate             # accumulate(iter[, f])
# from functools import reduce                 # reduce(f, iter[, init])
# from functools import lru_cache              # @lrucache ...arguments of functions should be able to be keys of dict (e.g. list is not allowed)
# from bisect import bisect_left, bisect_right # bisect_left(a, x, lo=0, hi=len(a)) returns i such that all(val<x for val in a[lo:i]) and all(val>-=x for val in a[i:hi]).
# from copy import deepcopy                    # to copy multi-dimentional matrix without reference
# from fractions import gcd                    # for Python 3.4 (previous contest @AtCoder)


def main():
    mod = 1000000007                # 10^9+7
    inf = float('inf')              # sys.float_info.max = 1.79...e+308
    # inf = 2 ** 64 - 1               # (for fast JIT compile in PyPy) 1.84...e+19
    sys.setrecursionlimit(10**6)    # 1000 -> 1000000
    def input(): return sys.stdin.readline().rstrip()
    def ii():    return int(input())
    def mi():    return map(int, input().split())
    def mi_0():  return map(lambda x: int(x)-1, input().split())
    def lmi():   return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():    return list(input())
    
    
    n = ii()
    L = lmi()
    minus = len(list(filter(lambda x: x<0, L)))
    pos = list(map(abs, L))
    if minus % 2 == 0:
        print(sum(pos))
    else:
        print(sum(pos) - min(pos) * 2)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
A = np.array(list(map(int, input().split())))
b = np.abs(A)
if np.count_nonzero(A < 0) % 2 == 0:
    print(np.sum(b))
else:
    print(np.sum(b)-np.min(b)*2)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
ans = 0
minus = 0
mimax = - float('inf')
B = []
for i in range(N):
    if A[i] < 0:
        minus += 1
        B.append(-A[i])
    else:
        B.append(A[i])

if minus % 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()))
if 0 in A:
  A = list(map(abs,A))
  print(sum(A))
else:
  count=0
  min_A =100000000000000000
  for i in range(N):
    if A[i]<0:
      count=count+1
    if abs(A[i])<min_A:
      min_A = abs(A[i])
  A = list(map(abs,A))
  if count%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()))

B = [abs(i) for i in A]
minus = [int(i<0) for i in A]
if sum(minus)%2 == 0:
    print(sum(B))
else:
    print(sum(B)-2*min(B))

Submission status: Accepted

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]

from collections import defaultdict, deque
from sys import exit
import heapq
import math
import copy
from bisect import bisect_left, bisect_right

import sys
# sys.setrecursionlimit(1000000)
# list(map(int, input().split()))
mod = 10 ** 9 + 7

N = getN()
A = getList()
alta = []
cntminus = 0
for i in A:
    if i < 0:
        cntminus += 1
    alta.append(abs(i))
if cntminus % 2 == 0:
    print(sum(alta))
else:
    print(sum(alta) - 2 * min(alta))

Submission status: Accepted

Solution code:
from operator import itemgetter
import sys
sys.setrecursionlimit(1000000)
ans=0

n=int(input())
a=[]
b=[]
c=0
for i in input().split():
    x=int(i)
    if x<0:
        c+=1
    a.append(x)
    b.append(abs(x))

b.sort()
ans=sum(b[1:])
if c%2==1:
    ans+=-b[0]
else:
    ans+=b[0]
print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = [int(_) for _ in input().split()]
    dp = [[0] * 2 for _ in range(N-1)]
    dp[0][0] = A[0] + A[1]
    dp[0][1] = -(A[0] + A[1])
    for i in range(1, N-1):
        dp[i][0] = max(dp[i-1][0]+A[i+1], dp[i-1][1]+A[i+1])
        dp[i][1] = max(dp[i-1][0]-2*A[i]-A[i+1], dp[i-1][1]+2*A[i]-A[i+1])
    print(max(dp[N-2][0], dp[N-2][1]))
    return

if __name__ == '__main__':
    main()

Submission status: Accepted

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

m = 10**20
c = 0
for a in A:
    if a < 0:
        c += 1
    m = min(m, abs(a))

if c % 2 == 0:
    ans = sum([abs(a) for a in A])
else:
    ans = sum([abs(a) for a in A]) - m*2

print(ans)

Submission status: Accepted

Solution code:
DEBUG = False

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

index_min_abs_val = N-1
flag_zero = not A[N-1]
if DEBUG:
    print("index_max_abs_val: {}, flag_zero: {}".format(index_min_abs_val, flag_zero))
for i in range(N-1):
    if not A[i]:
        flag_zero = True
    if abs(A[index_min_abs_val]) > abs(A[i]):
        index_min_abs_val = i
    if A[i] < 0:
        A[i] *= -1
        A[i+1] *= -1
    if DEBUG:
        print("i: {}, A: {}".format(i, A))
        print("index_max_abs_val: {}, flag_zero: {}".format(index_min_abs_val, flag_zero))

if flag_zero:
    A[N-1] = abs(A[N-1])
elif A[N-1] < 0:
    A[N-1] *= -1
    A[index_min_abs_val] *= -1

if DEBUG:
    print("A: {}".format(A))
print(sum(A))

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:
    A[i] *= -1
    count += 1

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

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from itertools import permutations, accumulate, combinations, combinations_with_replacement
from math import sqrt, ceil, floor, factorial
from bisect import bisect_left, bisect_right, insort_left, insort_right
from copy import deepcopy
from operator import itemgetter
from functools import reduce
from fractions import gcd
import sys
def input(): return sys.stdin.readline().rstrip()
def I(): return int(input())
def Is(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def TI(): return tuple(map(int, input().split()))
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def TIR(n): return [TI() for _ in range(n)]
def S(): return input()
def Ss(): return input().split()
def LS(): return list(input())
def SR(n): return [S() for _ in range(n)]
def SsR(n): return [Ss() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
sys.setrecursionlimit(10**6)
MOD = 10**9+7
INF = float('inf')
# ----------------------------------------------------------- #

n = I()
A = LI()
absmin = INF
minus = 0
for a in A:
    absmin = min(absmin, abs(a))
    if a < 0:
        minus += 1

ans = sum([abs(a) for a in A])
if minus % 2 == 0:
    print(ans)
else:
    print(ans - absmin*2)

Submission status: Accepted

Solution code:
import sys
from bisect import bisect_left
def input(): return sys.stdin.readline().strip()


def main():
    N = int(input())
    A = list(map(int, input().split()))
    A.sort()
    
    """
    もし負の数が偶数個存在すればそれらはすべて正の数にできる。
    もし負の数が奇数個存在すればそれは最終的に１個にできる。
    """

    idx = bisect_left(A, 0)
    if idx % 2 == 0:
        print(sum(A[idx:]) - sum(A[:idx]))
    elif idx < len(A) :
        cand1 = sum(A[idx:]) - sum(A[:idx]) + A[idx - 1] * 2
        cand2 = sum(A[idx:]) - sum(A[:idx]) - A[idx] * 2
        print(max(cand1, cand2))
    else:
        print(-sum(A) + A[-1] * 2)
    


if __name__ == "__main__":
    main()

Submission status: Accepted

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

cnt=0
aa=[]
for item in a:
  if item<0:
    cnt+=1
    aa.append(-1*item)
  else:
    aa.append(item)
    
if cnt%2==1:
  print(sum(aa)-2*min(aa))
else:
  print(sum(aa))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

n = int(input())
a = []
f = True
A = map(int, input().split())

for i in A:
    if i < 0:
        f ^= 1
    a.append(abs(i))

a.sort()

ans = sum(a[1:])

print(ans + (a[0] if f else -a[0]))

Submission status: Accepted

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

Submission status: Accepted

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

A_sorted = sorted(A, key=lambda x:abs(x), reverse=True)
abs_min = A_sorted[N - 1]

b_sum = 0
for i in range(N):
    b_sum += abs(A_sorted[i])
    
for j in range(N - 1):
    if A[j] < 0:
        A[j] = -A[j]
        A[j + 1] = -A[j + 1]
        
c_sum = sum(A)

if b_sum == c_sum:
    ans = b_sum
else:
    ans = b_sum - 2 * abs(abs_min)
    
print(ans)

Submission status: Accepted

Solution code:
#最初に置いておくもの
import sys
sys.setrecursionlimit(500000) 
input=sys.stdin.readline
N=int(input())
A = list(map(int, input().split()))

inf=float('inf')
dp=[[inf]*2 for j in range(N+1)]

def chmax(a,b):
    return max(a,b)

def rec(n,m):
    if n==0 and m==0:
        dp[0][0]=0
        return 0
    if n==0 and m==1:
        dp[0][1]=-inf
        return -inf

    if dp[n][m] < inf:
        return dp[n][m]
    
    if m==0:
        res=chmax(rec(n-1,0)+A[n-1] ,rec(n-1,1)-A[n-1] ) 
    if m==1:
        res=chmax(rec(n-1,0)-A[n-1] ,rec(n-1,1)+A[n-1] )
     
    dp[n][m]=res

    return res

print(rec(N,0))

Submission status: Accepted

Solution code:
N=int(input())
A = list(map(int, input().split()))
ans=0
tmp=0
hoge=0
li=[]

for i in range(N):
    li.append(abs(A[i]))
    ans+=abs(A[i])
    if A[i]<0:
        tmp+=1
    if A[i]==0:
        hoge=1

if tmp%2==0 or hoge==1:
    ans=ans
else:
    ans=ans-2*min(li)

print(ans)