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 = sorted(list(map(int, input().split())))
absA = [abs(a) for a in A]
if 0 in A:
    print(sum(absA))
else:
    num_minus = 0
    for a in A:
        if a < 0:
            num_minus += 1
        else:
            break

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = sorted(map(int, input().split()))
print(sum(map(lambda a, b: abs(a + b), a[::2], a[1::2])) + (a[-1] if n % 2 == 1 else 0))

Submission status: Accepted

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

N = int(input())
A = list(map(int, input().split()))
cnt = 0
m = 10**18

for Ai in A:
    if Ai<0:
        cnt += 1
    
    m = min(m, abs(Ai))

if cnt%2==0:
    print(sum(abs(Ai) for Ai in A))
else:
    print(sum(abs(Ai) for Ai in A)-2*m)

Submission status: Accepted

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

negCnt = 0
absSum = 0
minAbs = float('inf')
for val in a:
    negCnt += val < 0
    absVal = abs(val)
    absSum += absVal
    minAbs = min(minAbs, absVal)
print(absSum - 2 * minAbs if negCnt & 1 else absSum)

Submission status: Accepted

Solution code:
N = input()
AA = input()
A = list(map(int, AA.replace('-', '').split()))
HC = AA.count('-')
if HC % 2 == 0:
    print(sum(A))
else:
    print(sum(A) - sorted(A)[0] * 2)

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int, input().split(' ')))
absl = [abs(i) for i in l]
mmm = len([True for i in l if i < 0])
if mmm % 2 == 0:
  print(sum(absl))
else:
  print(sum(absl) - min(absl)*2)

Submission status: Accepted

Solution code:
N = int(input())
As = [int(s) for s in input().split(' ')]
count_minus = len([a for a in As if a < 0])
As2 = [abs(a) for a in As]
As2.sort()
ret = sum(As2)

if count_minus % 2 == 1:
    ret -= As2[0] * 2

print(ret)

Submission status: Accepted

Solution code:
import os, sys, re, math

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

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

print(sum(A))

Submission status: Accepted

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

Submission status: Accepted

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

B_sum = 0
B = 10 ** 9
cnt = 0

for i in range(N):
  A = A_list[i]
  if A < 0:
    cnt += 1
    A = -A
  B_sum += A
  B = min(B, A)
  
if cnt % 2 == 0:
  print(B_sum)
else:
  print(B_sum - 2 * B)

Submission status: Accepted

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

*_a, = map(abs, a)
if sum(map(lambda x: x < 0, a))%2 == 0:
    print(sum(_a))
else:
    mi = min(_a)
    print(sum(_a)-2*mi)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int, input().split()))
minus=0
for i in range(n):
	if a[i]<0:
		minus+=1
		a[i]=-a[i]
if minus%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()))
minus_num = 0
for i in range(n):
    if a[i] < 0:
        minus_num += 1
if minus_num % 2 == 0:
    ans = 0
    for i in range(n):
        ans += abs(a[i])
else:
    min_abs = abs(a[0])
    ans = 0
    for i in range(n):
        ans += abs(a[i])
        min_abs = min(min_abs, abs(a[i]))
    ans = ans - min_abs * 2
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(n):
    if a[i] < 0:
        ne_num += 1
if ne_num % 2 == 0:
    print(sum(list(map(abs, a))))
    exit()

min_b = 10**9
for i in range(n):
    if abs(a[i]) < min_b:
        min_b = abs(a[i])
print(sum(list(map(abs, a))) - 2*min_b)

Submission status: Accepted

Solution code:
N = int(input())
A=sorted(list(map(int,input().split())))
dp = [[0 for i in range(N)] for j in range(2)]
dp[0][0] = A[0]
dp[1][0] = -A[0]
for i in range(1,N-1):
    dp[0][i] = max(dp[0][i-1]+A[i],dp[1][i-1]-A[i])
    dp[1][i] = max(dp[0][i-1]-A[i],dp[1][i-1]+A[i])
dp[0][-1] = max(dp[0][-2]+A[-1],dp[1][-2]-A[-1])
print(max(dp[0][-1],dp[1][-1]))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
MIN_A = min(map(abs, A))
MINUS = 0

for i in A:
  if i < 0:
    MINUS += 1

if min(A) > 0:
  print(sum(A))
  exit(0)
  
ans = 0
if MINUS % 2 == 0:
  for a in A:
    ans += abs(a)
else:
  for a in A:
    ans += abs(a)
  ans -= MIN_A * 2

print(ans)

Submission status: Accepted

Solution code:
import numpy as np

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

if np.sum([a < 0]) % 2 == 0 or np.sum([a == 0]) > 0:
    print(np.sum(np.abs(a)))
else:
    print(np.sum(np.abs(a)) - np.min(np.abs(a)) * 2)

Submission status: Accepted

Solution code:
import math
import numpy as np
from fractions import gcd
import fractions
import statistics
import collections
from functools import reduce
import itertools
from collections import defaultdict

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

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

if (cnt%2 == 0 or cnt0>=1):
    print(sum(np.abs(np.array(A))))
else:
    print(sum(np.abs(np.array(A)))-min(np.abs(np.array(A)))*2)

Submission status: Accepted

Solution code:
from _collections import deque
from _ast import Num


def parser():
    while 1:
        data = list(input().split(' '))
        for number in data:
            if len(number) > 0:
                yield (number)


input_parser = parser()


def gw():
    global input_parser
    return next(input_parser)


def gi():
    data = gw()
    return int(data)


MOD = int(1e9 + 7)

import numpy
from collections import deque
from math import sqrt
from math import floor
# https://atcoder.jp/contests/abc114/tasks/abc114_c
# C - 755
"""
need to consider the case that ticket is not enough to lower everything
"""

N = gi()
nps = []
ns = []

for i in range(N):
    a = gi()
    if a < 0:
        ns.append(-a)
    else:
        nps.append(a)

ns.sort()
nps.sort()

base = sum(ns) + sum(nps)

if len(ns) % 2:
    ans = base - 2 * ns[0]
    if len(nps):
        ans = max(ans, base - 2 * nps[0])
else:
    ans = base

print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split(" ")))
    negative = 0
    result = 0
    abs_minimum = 10**9
    for a in A:
        abs_minimum = min(abs_minimum, abs(a))
        if a < 0:
            negative += 1
    if negative % 2 == 0:
        for a in A:
            result += abs(a)
    else:
        for a in A:
            if abs(a) == abs_minimum:
                result += min(a, -a)
            else:
                result += abs(a)
    print(result)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math
def calculate(arr1,arr2,zeroExist):
    if (len(arr1) % 2 == 0) or (zeroExist == True):
        return sum(arr2) + sum(arr1)
    else:
        return sum(arr2) + sum(arr1) - 2 * (calculateAbsSmallest(arr1,arr2))


def calculateAbsSmallest(arr1,arr2):
    arr1.sort()
    arr2.sort()
    if len(arr2) == 0:
        return arr1[0]
    return min(arr1[0],arr2[0])

N = int(input())

arr1 = []
arr2 = []
zeroExist = False
for s in input().split(" "):
    if int(s) == 0:
        zeroExist = True
    if int(s) < 0:
        arr1.append(abs(int(s)))
    else:
        arr2.append(int(s))

arr1 = list(arr1)
arr2 = list(arr2)

res = calculate(arr1,arr2,zeroExist)
print(res)

Submission status: Accepted

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

minus_cnt = 0
zero_cnt = 0
for ai in A:
    if ai == 0:
        zero_cnt += 1
    elif ai < 0:
        minus_cnt += 1

A = [abs(ai) for ai in A]

sm_a = sum(A)
min_a = min(A)

if zero_cnt > 0:
    print(sm_a)
else:
    if minus_cnt % 2 == 0:
        print(sm_a)
    else:
        r = sm_a - 2 * min_a
        print(r)

Submission status: Accepted

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

abs_a = list(map(abs, a))
sm_abs_a = sum(abs_a)
min_a = min(abs(a[i]) for i in range(len(a)))

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

if cnt%2 == 0:
    print(sm_abs_a)
else:
    print(sm_abs_a - min_a*2)

Submission status: Accepted

Solution code:
def main():
    _ = int(input())
    A = [int(i) for i in input().split()]
    cnt_minus = 0
    zero_flag = False
    for i, a in enumerate(A):
        if a < 0:
            cnt_minus += 1
        elif a == 0:
            zero_flag = True

    ans = 0
    if cnt_minus % 2 == 0 or zero_flag:
        ans = sum(abs(a) for a in A)
    else:
        ans = sum(abs(a) for a in A) - min(abs(a) for a in A) * 2
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
dp = [[0 for i in range(N-1)] for j in range(2)]
dp[0][0] = A[0]
dp[1][0] = -A[0]
for i in range(1,N-1):
    dp[0][i] = max(dp[0][i-1]+A[i],dp[1][i-1]-A[i])
    dp[1][i] = max(dp[0][i-1]-A[i],dp[1][i-1]+A[i])
print(max(dp[0][-1]+A[-1],dp[1][-1]-A[-1]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
a=[int(i) for i in input().split()]
a.sort()
j=n
for i in range(n):
    if a[i]>=0:
        j=i
        break
a=list(map(abs,a))
a.sort()
if j%2==0:
    print(sum(a))
else:
    print(sum(a)-2*a[0])

Submission status: Accepted

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

ans = 0
m = 10 ** 9 + 1
cnt = 0
for a in A:
    if a < 0:
        cnt ^= 1
        a *= -1
    m = min(m, a)
    ans += a

if cnt:
    ans -= 2 * m

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
D=0
zcnt=0
fcnt=0
m=10**9
for i in range(N):
  D+=abs(A[i])
  m=min(m,abs(A[i]))
  if A[i]<0:
    fcnt+=1
  elif A[i]==0:
    zcnt+=1

if ((fcnt%2)==1) and (zcnt==0):
  print(D-2*m)  
else:
  print(D)

Submission status: Accepted

Solution code:
n = int(input())
check = []
l = []
li = list(map(int,input().split()))
for a in li:
    if a > 0:
        check.append(1)
        l.append(a)
    elif a < 0:
        check.append(-1)
        l.append(-a)
    else:
        check.append(0)
        l.append(a)
for i in range(n-1):
    if check[i] == -1:
        check[i] = 1
        check[i+1] *= -1

if check[-1] == -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()))

mc = 0
absum = 0
absmin = 10**9+7

for a in A:
    if a < 0:
        mc += 1
    absum += abs(a)
    absmin = min(absmin,abs(a))

if mc%2 == 0:
    ans = absum
else:
    ans = absum-absmin*2

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
List = list(map(int,input().split()))
counter = 0
min = abs(max(List))
sum = 0
for item in List:
  if item < 0:
    counter += 1
  if abs(item) <= min:
    min = abs(item)
  sum += abs(item)
if counter % 2 == 0:
  Answer = sum
else:
  Answer = sum - min*2
print(Answer)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
na = [abs(i) for i in a]
m = min(na)
s = sum(na)
cnt = 0

for i in a:
    if i < 0:
        cnt += 1
if cnt%2 == 0 or m == 0:
    ans = s
else:
    ans = s-2*m

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
na = [abs(i) for i in a]
m = min(na)
s = sum(na)

if m == 0:
    ans = s
else:
    cnt = 0
    for i in a:
        if i < 0:
            cnt += 1
    if cnt%2 == 0:
        ans = s
    else:
        ans = s-2*m

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
s=sum(map(abs,a))
m=min(list(map(abs,a)))
z=[]
for i in range(n):
    if a[i]<0:
        z.append(a[i])

if len(z)%2:
    print(s-2*m)
else:
    print(s)

Submission status: Accepted

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

dp = [[-INF for _ in range(2)] for _ in range(N+1)]
dp[0][0] = dp[0][1] = sum(A)
# print(dp)
for i in range(N-1):
    dp[i+1][0] = max(dp[i+1][0], dp[i][0])
    dp[i+1][0] = max(dp[i+1][0], dp[i][1])

    dp[i+1][1] = max(dp[i+1][1], dp[i][0] - (A[i] + A[i+1]) + (-A[i]-A[i+1]))
    if i > 0:
        dp[i+1][1] = max(dp[i+1][1], dp[i][1] - (-A[i] + A[i+1]) + (A[i]-A[i+1]))

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

Submission status: Accepted

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

c = 0
f = 0
for i in range(N):
    if A[i] < 0:
        c += 1
    elif A[i] == 0:
        f = 1
    A[i] = abs(A[i])
    
if f == 1 or c % 2 == 0:
    ans = sum(A)
else:
    ans = sum(A) - 2 * min(A)
    
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

def main():
    N = int(input())
    A = [int(x) for x in input().split()]
    count = 0
    ans = 0
    mi = float("inf")

    for a in A:
        ans += abs(a)
        mi = min(mi, abs(a))
        if a <= 0:
            count += 1

    if count & 1:
        print(ans - mi * 2)
    else:
        print(ans)

    
    

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
mn = 10**9+1
minus = 0
zero = 0
ans = 0
for i in a:
    if abs(i) < mn:
        mn = abs(i)
    if i < 0:
        minus += 1
    if i == 0:
        zero += 1
    ans += abs(i)

tmp = mn if zero == 0 and minus % 2 else 0
print(ans - tmp * 2)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from operator import itemgetter
sys.setrecursionlimit(10000000)
INF = 10**30

def main():
    n = int(input().strip())
    v = list(map(int, input().strip().split()))
    s = 0
    m = INF
    su = 0
    for i in range(n):
        if v[i] < 0:
            s += 1
        if abs(v[i]) < m:
            m = abs(v[i])
        su += abs(v[i])
    if s % 2 == 0:
        print(su)
    else:
        print(su - 2 * m)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
cnt=0
for i in range(n):
    if a[i]<0:
        cnt+=1
        a[i]*=(-1)
if cnt%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()))

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

if minus%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()))
NS = sum(int(a < 0) for a in A)
S = sum(abs(a) for a in A)
m = min(abs(a) for a in A)
print(S - 2 * m if NS & 1 else S)

Submission status: Accepted

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

minus_count = 0
A_abs = sorted(list(map(abs, A)))

for i in A:
    if i < 0:
        minus_count += 1

if minus_count % 2 == 0:
    print(sum(A_abs))
else:
    print(sum(A_abs) - A_abs[0] * 2)

Submission status: Accepted

Solution code:
N = int(input())

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

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

if count % 2 == 1:
  print(abs_sum - 2*min_value)
else:
  print(abs_sum)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
cnt = 0
target = 10**10
sum_A = 0
for a in A:
	if a < 0:
		cnt+=1
		sum_A+=-a
	else:
		sum_A+=a
		
	if target > abs(a):
		target = abs(a)

if cnt%2==0:
	print(sum_A)
else:
	print(sum_A - 2*target)

Submission status: Accepted

Solution code:
import bisect
import cmath
import heapq
import itertools
import math
import operator
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter, mul, add, xor

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353

_ = int(sys.stdin.buffer.readline())
A = list(map(int, sys.stdin.buffer.readline().split()))

A = np.array(A, dtype=int)
cnt = np.count_nonzero(A < 0)
A = np.abs(A)
if cnt & 1:
    ans = A.sum() - A.min() * 2
else:
    ans = A.sum()
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def solve():
    N, *A = map(int, open(0).read().split())
    
    dp = [[0 for _ in range(2)] for _ in range(N+1)]
    dp[0][1] = -10**15
    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])


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs = sys.stdin.buffer.readlines


# mod=10**9+7
# rstrip().decode('utf-8')
# map(int,input().split())
import numpy as np

def main():
	n=int(input())
	A= np.array(read().split(), np.int64)
	nega=len(A[A<0])
	B=abs(A)
	ans=np.sum(B)
	
	if nega%2:
		ans-=2*np.min(B)
	
	print(ans)
	
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:
        A[i] *= -1
        count += 1

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

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def main():
    n=int(input())
    aa=LI()
    pp=[]
    mm=[]
    for a in aa:
        if a<0:mm.append(a)
        else:pp.append(a)
    if not mm:
        print(sum(pp))
        exit()
    if not pp:
        if len(mm)%2:
            print(-sum(mm)+max(mm)*2)
        else:
            print(-sum(mm))
        exit()
    if len(mm)%2:
        m=max(mm)
        p=min(pp)
        if p+m>=0:print(sum(pp)-sum(mm)+2*m)
        else:print(sum(pp)-2*p-sum(mm))
    else:
        print(sum(pp)-sum(mm))

main()

Submission status: Accepted

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

Submission status: Accepted

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

a1 = a[:]

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

a[N-1] = abs(a[N-1])

print(max(sum(a1), sum(a)-2*min(a)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

if sys.platform =='ios':
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

N = INT()
A = MAP()

B = [abs(a) for a in A]
negatives = sum([1 for a in A if a<0])

#print(B, negatives)

if negatives % 2:
	min_B = min(B)
	ans = sum(B) - (2*min_B)
else:
	ans = sum(B)

print(ans)

Submission status: Accepted

Solution code:
import copy
n=int(input())
a=list(map(int,input().split()))
absa=[]

for i in a:
  absa.append(abs(i))

mi=0
pl=0
for i in a:
  if i<0:
    mi+=1
  else:
    pl+=1

#その配列が-がいるか全部非負にできるかの判定…
def jud(x):
  temp=copy.copy(x)
  for i in range(len(temp)-1):
    if temp[i]>=0:
      continue
    else:
      temp[i]*=-1
      temp[i+1]*=-1
  if temp[-1]>=0:
    return 1
  else:
    return 0

if jud(a)==1:
  print(sum(absa))
else:
  print(sum(absa)-min(absa)*2)

Submission status: Accepted

Solution code:

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

minus_odd = len(list(filter(lambda x: x < 0, A))) % 2 != 0

a = list(map(lambda x: abs(x), A))
if minus_odd:
  mini = min(a)
  print(sum(a) - mini * 2)
else:
  print(sum(a))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
m = 1
z = 0
for i in range(N):
  if A[i] < 0:
    m *= -1
    A[i] = (-1) * A[i]
  if A[i] == 0:
    z = 1
print(sum(A) + (m - 1) * min(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][0] = 0
INF = -10**9
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:
def main():
    N = int(input())
    A = list(map(int, input().split()))
    cnt = 0
    for i in range(N):
        if A[i] < 0:
            cnt += 1
        A[i] = abs(A[i])

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N=int(input())
ls=[int(s) for s in input().split()]
_abs=[0 for _ in range(N)]
zero=0
minus=0
for i in range(N):
  if ls[i]<0:
    minus+=1
    _abs[i]=-ls[i]
  elif ls[i]==0:
    zero+=1
  else:
    _abs[i]=ls[i]
if zero>0:
  print(sum(_abs))
elif minus%2==0:
  print(sum(_abs))
else:
  print(sum(_abs)-2*min(_abs))

Submission status: Accepted

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

n_minus = 0

for a in list_a:
  if a < 0:
    n_minus += 1

if n_minus % 2 == 0:
  print(sum(list(map(abs, list_a))))
else:
  list_a_min = min(list(map(abs,list_a)))
  print(sum(list(map(abs, list_a))) - 2 * list_a_min)

Submission status: Accepted

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

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

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

if cnt%2 == 0:
    print(sum(a2))
else:
    memo = 10**9
    for i in range(n):
        memo = min(abs(a[i]),memo)
    print(sum(a2)-2*memo)

Submission status: Accepted

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

Submission status: Accepted

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

b = []
count = 0
for i in a:
    if i < 0:
        count += 1
    b.append(abs(i))

if count % 2:
    print(sum(b) - 2 * min(b))
else:
    print(sum(b))

Submission status: Accepted

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

dp = [[0] * 2 for i in range(n + 1)]

# dp[i + 1][j] := i 番目までの最大値（j = 1 なら a[i] の符号反転）

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:
N = int(input())
A = list(map(int,input().split()))
absA = [abs(i) for i in A]
minusA = [i for i in A if i<0]
mina = min(absA)
if (0 in A) or (len(minusA)%2 == 0):
    print(sum(absA))
else:
    print(sum(absA) -2*mina)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
c=0
s=0
Az=[]
for i in A:
    if i<0:
        c+=1

    s+=abs(i)
    Az.append(abs(i))
if c%2==0:
    print(s)
else:
    print(s-2*min(Az))

Submission status: Accepted

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

res = 0
min_abs = float('inf')
cnt_neg = 0
for a in A:
    if a < 0:
        cnt_neg += 1
    abs_a = abs(a)
    res += abs_a
    if min_abs > abs_a:
        min_abs = abs_a

if cnt_neg%2==0:
    print(res)
else:
    print(res-2*min_abs)

Submission status: Accepted

Solution code:
n=int(input())
a=[int(i) for i in input().split()]
c=0
for i in range(n):
    if a[i]<0:
        c+=1
a=list(map(abs,a))
#print(c)
if c%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()))
count = 0
for a in A:
    if a < 0:
        count += 1
    elif a == 0:
        count = 0
        break
A = list(map(abs, A))
A.sort()
if count % 2 == 0:
    print(sum(A))
else:
    print(sum(A[1:]) - A[0])

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 list4d(a, b, c, d, e): return [[[[e] * d for j in range(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(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N = INT()
A = LIST()

has_zero = False
cnt = 0
for a in A:
    if a < 0:
        cnt += 1
    elif a == 0:
        has_zero = True

A2 = [abs(a) for a in A]
if has_zero or 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()))

cnt = 0
for Ai in A:
    cnt += int(Ai < 0)

if cnt % 2 == 0:
    print(sum([abs(Ai) for Ai in A]))
else:
    ans_sum = 0
    ans_min = 10 ** 9 + 7
    for Ai in A:
        ans_sum += abs(Ai)
        ans_min = min(ans_min, abs(Ai))
    #
    print(ans_sum - 2 * ans_min)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
import numpy as np

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

B = []
cnt = 0
for i in A:
    if i < 0:
        cnt += 1
    B.append(abs(i))

if cnt % 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()))
c=0
for i in range(n):
    if a[i]<0:
        c+=1
        a[i]=-1*a[i]
aa=list(map(abs,a))
if c%2==0:
    print(sum(aa))
else:
    print(sum(aa)-2*min(aa))

Submission status: Accepted

Solution code:
N=int(input())
L=list(map(int,input().split()))
c=0
s=0
mi=10**10
for i in range(N):
    if(L[i]<0):
        c+=1
    s+=abs(L[i])
    if(abs(L[i])<mi):
        mi=abs(L[i])
if(c%2==0):
    print(s)
    exit()
print(s-(2*mi))

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
num_minus = 0
A_abs = []
for a in A:
    if a < 0:
        num_minus += 1
    A_abs.append(abs(a))
A_abs.sort()
if num_minus % 2 == 0:
    print(sum(A_abs))
else:
    print(sum(A_abs) - 2 * A_abs[0])

Submission status: Accepted

Solution code:
import numpy as np

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

count  = 0
for i in a:
    if i < 0:
        count += 1
    elif i == 0:
        print(sum(np.abs(a)))
        exit()

if count % 2 == 0:
    print(sum(np.abs(a)))
else:
    a = np.abs(a)
    a.sort()
    b = a[0]
    a = np.delete(a, 0, 0)
    print(sum(a) - b)

Submission status: Accepted

Solution code:
from sys import stdin

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

point = 0
mi = 10**10
total = 0

for i in li:
    if i < 0:
        point += 1
    mi = min(mi,abs(i))
    total += abs(i)
if point % 2 == 0:
    print(total)
else:
    print(total-mi*2)