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:
import sys
import heapq
import re
from itertools import permutations
from bisect import bisect_left, bisect_right
from collections import Counter, deque
from fractions import gcd
from math import factorial, sqrt, ceil
from functools import lru_cache, reduce
INF = 1 << 60
MOD = 1000000007
sys.setrecursionlimit(10 ** 7)

# UnionFind
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

def is_prime(n):
    if n == 1:
        return False
    for i in range(2,int(n**0.5)+1):
        if n % i == 0:
            return False
    return True


# ワーシャルフロイド (任意の2頂点の対に対して最短経路を求める)
# 計算量n^3 (nは頂点の数)
def warshall_floyd(d, n):
    #d[i][j]: iからjへの最短距離
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = min(d[i][j],d[i][k] + d[k][j])
    return d


# ダイクストラ
def dijkstra_heap(s, edge, n):
    #始点sから各頂点への最短距離
    d = [10**20] * n
    used = [True] * n #True:未確定
    d[s] = 0
    used[s] = False
    edgelist = []
    for a,b in edge[s]:
        heapq.heappush(edgelist,a*(10**6)+b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        #まだ使われてない頂点の中から最小の距離のものを探す
        if not used[minedge%(10**6)]:
            continue
        v = minedge%(10**6)
        d[v] = minedge//(10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
    return d

# 素因数分解
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr

# 2数の最小公倍数
def lcm(x, y):
    return (x * y) // gcd(x, y)

# リストの要素の最小公倍数
def lcm_list(numbers):
    return reduce(lcm, numbers, 1)

# リストの要素の最大公約数
def gcd_list(numbers):
    return reduce(gcd, numbers)

# 素数判定



# limit以下の素数を列挙
def eratosthenes(limit):
    A = [i for i in range(2, limit+1)]
    P = []

    while True:
        prime = min(A)
        
        if prime > sqrt(limit):
            break
            
        P.append(prime)
            
        i = 0
        while i < len(A):
            if A[i] % prime == 0:
                A.pop(i)
                continue
            i += 1
            
    for a in A:
        P.append(a)
            
    return P

# 同じものを含む順列
def permutation_with_duplicates(L):

    if L == []:
        return [[]]

    else:
        ret = []

        # set（集合）型で重複を削除、ソート
        S = sorted(set(L))

        for i in S:

            data = L[:]
            data.remove(i)

            for j in permutation_with_duplicates(data):
                ret.append([i] + j)

        return ret


# ここから書き始める
n = int(input())
a = list(map(int, input().split()))
dp = [[0 for j in range(2)] for i in range(n)]
for i in range(n):
    if i == 0:
        dp[i][0] = a[i]
        dp[i][1] = -a[i]
    else:
        dp[i][0] = max(dp[i - 1][0] + a[i], dp[i - 1][1] - a[i])
        dp[i][1] = max(dp[i - 1][0] - a[i], dp[i - 1][1] + a[i])
ans = dp[n - 1][0]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
cnt = 0
sumnum = 0
minnum = float('inf')
for i in A:
    if i < 0:
        i *= -1
        cnt += 1
    minnum = min(minnum, i)
    sumnum += i
if cnt%2 == 0:
    print(sumnum)
else:
    print(sumnum-2*minnum)

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][0]= 0 
dp[0][1]= -10**10

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


print(dp[n][0])

Submission status: Accepted

Solution code:
N = int(input())

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

total = 0
minus = 0

l = []
for i in range(N):
  total += abs(A[i])
  l.append(abs(A[i]))
  if A[i] < 0:
    minus += 1

ans = total
if minus % 2 == 1:
  ans = total - 2 * min(l)

print(ans)

Submission status: Accepted

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

min_num=float('inf')
minus_num=0

reverse_sum=0
for i in range(n):
    a = a_inputs[i]
    if a<0:
        minus_num+=1
        reverse_sum-=a
    else:
        reverse_sum+=a
    if min_num>abs(a):
        min_num=abs(a)
if minus_num%2==0:
    print(reverse_sum)
else:
    print(reverse_sum-2*min_num)

Submission status: Accepted

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

count = 0
ans = 0
for i in range(n):
  if lst[i] < 0:
    count += 1
    lst[i] = abs(lst[i])
  ans += lst[i]
  
if count % 2 == 1:
  ans -= 2*min(lst)
  
print(ans)

Submission status: Accepted

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

def main():
    N = int(readline())
    A = list(map(int, readline().rstrip().split()))
    dp = [[0, 0] for _ in range(N+1)]
    dp[0][1] = - 10 ** 10
    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__':
    main()

Submission status: Accepted

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

ans=0
mn=0
mi=abs(a[0])
for i in range(n):
    if a[i]<=0:
        mn += 1
    s=abs(a[i])
    ans += s
    mi=min(s,mi)
if mn%2==1:
    ans -= mi*2
print(ans)

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import numpy as np
import functools
import math
import scipy
import fractions
import itertools

def solve():
    n = int(input())
    a = list(map(int, input().split()))
    hoge = 0
    ans = 0
    for i in range(n):
        if a[i] < 0:
            hoge += 1
    if 0 in a or hoge % 2 == 0:
        for i in range(n):
            ans += abs(a[i])
    else:
        huga = []
        for i in range(n):
            a[i] = abs(a[i])
            ans += abs(a[i])
            if a[i] > 0:
                huga.append(a[i])
        ans -= min(huga)*2
    print(ans)
    return 0

if __name__ == "__main__":
    solve()

Submission status: Accepted

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

Submission status: Accepted

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


cnt=0
minimum=10**10
tot=0
for i in range(N):
    if A[i]<0:
        cnt+=1
        A[i]=-A[i]
    minimum=min(minimum,A[i])
    tot+=A[i]

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

Submission status: Accepted

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

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

if count_minus % 2 == 0:
    print(sum(As))
else:
    print(sum(As) - 2*min(As))

Submission status: Accepted

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

num_minus = 0
Min = float('inf')
Sum = 0
for i in range(n):
    if a_ls[i] < 0:
        num_minus += 1
    Min = min(abs(a_ls[i]),Min)
    Sum += abs(a_ls[i])
if num_minus % 2 == 0:
    ans = Sum 
else:
    ans = Sum - 2 * Min
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
A=[int(a) for a in input().split()]
ABS_SUM=sum(abs(a) for a in A)
ans=0
if 0 in A:
  ans=ABS_SUM
else:
  pal=sum(1 for a in A if a<0)
  if pal%2==0:
    ans=ABS_SUM
  else:
    ABS_MIN=min(abs(a) for a in A)
    ans=ABS_SUM-ABS_MIN*2
print(ans)

Submission status: Accepted

Solution code:
import numpy as np

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

neg = np.count_nonzero(A < 0)
zero = int(0 in A)

# 負の数が奇数個のときは全部正の数にできない。sum( abs(A) ) - 2*min(abs(A))
# 負の数が偶数個の時 or ０があるときはできる。

if zero > 0 or (neg % 2) == 0:
    ans = sum(abs(A))
else:
    ans = sum(abs(A)) - 2 * min(abs(A))
    # print(S, idx, A[idx], res)

print(ans)

Submission status: Accepted

Solution code:
import numpy as np

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

b = a[a <= 0]
a = np.abs(a)
s = np.sum(a)
if(len(b)%2==1):
    a = np.sort(a)
    s -= a[0]*2
print(s)

Submission status: Accepted

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

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

D = B+C
ans = sum(D)

if len(C) % 2 == 1:
    D.sort()
    ans -= 2*D[0]

print(ans)

Submission status: Accepted

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

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

min_abs = 10**9 + 1
for a in A:
    min_abs = min(min_abs, abs(a))

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

if minus % 2 == 0:
    print(abs_sum)
else:
    print(abs_sum - 2 * min_abs)

Submission status: Accepted

Solution code:
n = int(input())

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

neg = sum(1 for x in a if x<0)

ans = 0

if neg%2:
	for i in range(len(a)):
		if i == 0:
			exc = abs(a[i])
		else:
			exc = min(exc, abs(a[i]))
	ans = sum(abs(x) for x in a)-exc*2
else:
	ans = sum(abs(x) for x in a)

print(ans)

Submission status: Accepted

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

cnt = 0
total = 0
min_num = float('inf')
for a in A:
    if a<0:
        cnt = cnt + 1
    min_num = min(min_num, abs(a))
    total = total + abs(a)

if cnt%2==0:
    print(total)
else:
    print(total-2*min_num)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
minux_count = 0
minimum_abs = 10**9+1
if_zero_exists = False
for a in A:
    if a < 0:
        minux_count += 1
    if a == 0:
        if_zero_exists = True
    if abs(a) < minimum_abs:
        minimum_abs = abs(a)

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

if if_zero_exists or minux_count % 2 == 0:
    print(answer)
else:
    print(answer - 2*minimum_abs)

Submission status: Accepted

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

DP = [[0, 0] for _ in range(N+1)]
DP[0][1] = -10**15 

for i in range(N):
    for j in range(2):
        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()))
B=A[:]
count=0
zero=0
for i in range(N):
  if(A[i]<0):
    count+=1
  elif(A[i]==0):
    zero+=1
for i in range(N):
  B[i]=abs(A[i])
if(zero==0 and count%2==1):
  print(sum(B)-2*min(B))
else:
  print(sum(B))

Submission status: Accepted

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

if c%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()))
count = 0
for i in range(n):
    if a[i] < 0:
        count += 1
        a[i] *= -1
if count % 2 == 0:
    print(sum(a))
else:
    min_a = min(a)
    ans = sum(a)-min_a*2
    print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil
from operator import itemgetter
from copy import deepcopy
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
inf = 10**17
mod = 10**9 + 7

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

Submission status: Accepted

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

for num in a:
    if num < 0:
        cnt+=1
        b.append(abs(num))
    else:
        b.append(num)

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

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
A = np.array(list(map(int, input().split())))
my_min = 10 ** 10
A_rev = []
for i in range(N - 1):
    if A[i] < 0:
        A[i] *= -1
        if i < N - 1:
            A[i + 1] *= -1
    A_rev.append(A[i])
    my_min = min(my_min, abs(A[i]))
A_rev.append(A[-1] if A[-1] >= 0 else A[-1] * -1)
my_min = min(my_min, abs(A[-1]))
if np.count_nonzero(A >= 0) == N:
    print(sum(A))
else:
    print(sum(A_rev) - my_min * 2)

Submission status: Accepted

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

dp1 = a_l[0] + a_l[1] # last num is not changed
dp2 = -a_l[0] - a_l[1] # last num is changed

for i in range(2,n):
    dp1, dp2 = \
        max(dp1 + a_l[i], dp2 + a_l[i]), \
            max(dp2 + a_l[i-1]*2 - a_l[i], dp1 - a_l[i-1]*2 - a_l[i])

print(max(dp1,dp2))

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    n=int(input())
    A=[int(_) for _ in input().split()]
    cunt,ans=0,0
    min_a=10**9
    for a in A:
        if a<0:
            cunt+=1
            ans+=-a
            min_a=min(-a,min_a)
        else:
            ans+=a
            min_a=min(a,min_a)
    if cunt%2==1:
        print(ans-2*min_a)
    else:
        print(ans)

if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
cm=len([ai for ai in a if ai < 0])
a=[abs(ai) for ai in a]
if cm%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()))
num_minus = 0
minimum = 10 ** 6
ans = 0

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

if num_minus % 2 == 0:
    print(ans)
else:
    print(ans - minimum * 2)

Submission status: Accepted

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

Submission status: Accepted

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

mn = 0
ma = abs(a[0])
ans = 0
for i in a:
    if i < 0:
        mn += 1
    ans += abs(i)
    if abs(i) < ma:
        ma = abs(i)

if mn%2 == 1:
    ans -= ma*2

print(ans)

Submission status: Accepted

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

minus=0
minabs=10**10
SUM=0
iszero=False
for a in A:
    if a<0:
        minus+=1
    if a==0:
        iszero=True
    minabs=min(minabs, abs(a))
    SUM+=abs(a)

if iszero or minus%2==0:
    print(SUM)
else:
    print(SUM-2*minabs)

Submission status: Accepted

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

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

ans = sum(a_list)
if count % 2 == 1:
    a_list.sort()
    ans -= a_list[0] * 2
print(ans)

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from heapq import heapify, heappop, heappush
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
A = LIST()
is_zero = False
if 0 in A:
    is_zero = True
minus = 0
plus = 0

for a in A:
    if a < 0:
        minus += 1
    elif a > 0:
        plus += 1    
ans = 0
for a in A:
    ans += abs(a)
tmp = INF
if is_zero == True or minus % 2 == 0:
    print(ans)
else:
    for a in A:
        if abs(a) < tmp:
            tmp = abs(a)
    print(ans - 2 * tmp)

Submission status: Accepted

Solution code:
def main():
    inf = 10 ** 14 + 1

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

    stay = 0  # -1を乗算しない
    done = -inf  # -1を乗算した

    for x in a:
        stay, done = max(stay + x, done - x), max(stay - x, done + x)

    print(stay)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

if abs(A[0]) < abs(A[-1]):
    A = A[::-1]
B = list(map(abs, A))
B.sort()
C = [x for x in A if x < 0]
n = len(C)
if n % 2 == 0:
    print(sum(B))
else:
    print(sum(B[1:]) - B[0])

Submission status: Accepted

Solution code:
import bisect
n=int(input())
a=sorted(list(map(int,input().split())))
ind=bisect.bisect_left(a,0)


if ind==0 :
  print(sum(a))
  
elif ind==n:
  if ind%2==1:
    print(-sum(a)+2*a[-1])
  else:
    print(abs(sum(a)))

elif ind%2==0:

  print(-sum(a[:ind])+sum(a[ind:]))
else:
  print(-sum(a[:ind])+sum(a[ind:])-2*min(abs(a[ind]),abs(a[ind-1])))

Submission status: Accepted

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

absA = []
all_sum = 0
minus_term = 0
for i in A:
  absA.append(abs(i))
  all_sum += abs(i)
  if i < 0:
    minus_term += 1
    
if minus_term % 2 == 0:
  print(all_sum)
else:
  print(all_sum - 2 * min(absA))

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

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n, *a = map(int, read().split())
cnt = 0
for i, aa in enumerate(a):
    if aa < 0:
        a[i] = -aa
        cnt += 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()))
ans=0
minuscnt,zero_cnt=0,0
for aa in a:
  if aa<0:
    minuscnt+=1
  elif aa==0:
    zero_cnt+=1
sum_a=0
min_a=10**18
for aa in a:
  sum_a+=abs(aa)
  min_a=min(min_a,abs(aa))

if minuscnt%2==0 or zero_cnt>0:
  print(sum_a)
else:
  print(sum_a-2*min_a)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_col(H):
    '''
    H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) #一列の場合
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return tuple(map(list, zip(*ret)))


def read_matrix(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    # return [list(map(int, read().split())) for _ in range(H)] # 内包表記はpypyでは遅いため


MOD = 10**9 + 7
INF = 2**31  # 2147483648 > 10**9
# default import
from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  # , insort_left, insort_right

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

# 理想としてはすべて+にしたい
# i<=N-1ならばすべて自由にflipできるのでは？ #まえから愚直にflipのシミュレーションをしていっても良いしな
# iを都合のいいように変えたときには、i+1も操作で都合のいいように変えられる。よって後ろに押し付けられる
# つまり後ろの2要素の大小関係のみが重要
# んー前から愚直にシミュレーション
# すべて正にできないときは1個だけ負になるのだから、absの最小を持っておけば良い

N = read_a_int()
A = read_ints()
ans = 0
for i in range(N - 1):
    if A[i] < 0:
        ans += -A[i]
        A[i + 1] = -A[i + 1]
    else:
        ans += A[i]
if A[N - 1] >= 0:
    ans += A[N - 1]
else:
    ans += -A[N - 1]
    min_abs = abs(min(A, key=abs))
    # print(ans, min_abs)
    ans -= min_abs * 2
print(ans)

Submission status: Accepted

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

count_m=0
sum=0

for i in range(N):
    if A[i]<0:
        count_m+=1
        A[i]*=-1
    sum+=A[i]
B=sorted(A)
if count_m%2==0:
    print(sum)
else:
    print(sum-B[0]*2)

Submission status: Accepted

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

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
ans = sum(a) - 2 * min(a) if cnt % 2 != 0 else sum(a)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
neg = 0
sum_abs = 0
minn = float("inf")
for i in a:
    if i < 0:
        neg += 1
    sum_abs += abs(i)
    minn = min(abs(i),minn)
print(sum_abs - (neg%2)*minn*2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
cnt = 0
amin = 10**9+7
tot = 0
for i in range(N):
    if A[i]<0:
        cnt += 1
    amin = min(amin,abs(A[i]))
    tot += abs(A[i])
if cnt%2==0:
    print(tot)
else:
    print(tot-2*amin)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a = [int(x) for x in input().split()]
    count_negative = 0
    for i in range(n):
        if a[i] < 0:
            count_negative += 1
    a = [abs(x) for x in a]
    answer = sum(a)
    if count_negative % 2:
        answer -= 2 * min(a)
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
A = np.array(input().split(), dtype=np.int64)

neg = np.count_nonzero(A[A < 0])
if neg % 2 == 0 or 0 in A:
    ans = np.abs(A).sum()
else:
    tmp = np.abs(A).min()
    ans = np.abs(A).sum() - 2 * tmp

print(ans)

Submission status: Accepted

Solution code:
def main():
    _ = int(input())
    A = [int(i) for i in input().split()]
    sumA = sum(abs(a) for a in A)
    minus_cnt = sum(1 if a < 0 else 0 for a in A)
    if minus_cnt % 2 == 0 or (0 in A):
        print(sumA)
    else:
        min_v = min(abs(a) for a in A)
        print(sumA - min_v*2)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
M = len([0 for a in A if a < 0])

if A.count(0) > 0 or M % 2 == 0:
    print(sum(abs(a) for a in A))
    exit()

print(sum(abs(a) for a in A) - min(abs(a) for a in A) * 2)

Submission status: Accepted

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N=I()
    A=LI()
    A.sort(key=lambda x:abs(x))
    cnt = 0
    ans = 0
    for a in A:
        ans += abs(a)
        cnt += a<0
    if(cnt&1):
        ans-=abs(A[0])*2
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
INF = n*10**10
dp = [[-INF]*2 for i  in range(n+1)]
#i番目まで見たとき、i番目でー１を書けたかがｊのときのmax
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])
ans = dp[n][0]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
  import sys
  input = sys.stdin.readline
  n=int(input())
  a=list(map(int,input().split()))
  b=sorted([abs(i) for i in a])
  cnt=0
  for i in a:
    if i<0:
      cnt+=1
  print(sum(b) if cnt%2==0 else sum(b)-2*b[0])
if __name__=='__main__':
  main()

Submission status: Accepted

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

m = 10 ** 9
cnt = 0
SUM = 0
for i in a:
    if i < 0:
        cnt += 1
        SUM += - i
        if m > -i:m = -i
    else:
        SUM += i
        if m > i:m = i

if cnt % 2 == 0:
    print(SUM)
else:
    print(SUM - 2 * m)

Submission status: Accepted

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

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

# negativeがevenなら行ける、oddならどれか1個は残る？
neg = 0
MIN = float('inf')
ans = 0
for num in nums:
    if num < 0:
        neg += 1
    MIN = min(MIN, abs(num))
    ans += abs(num)

if neg % 2 == 1:
    ans -= MIN * 2
print(ans)