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:
int(input())
a=[int(i) for i in input().split()]
b=[abs(int(i)) for i in a]
c=[int(i) for i in a if i<0]
b.sort()
print(sum(b)-[0,b[0]*2][len(c)%2])

Submission status: Accepted

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

cnt = 0
for i in range(N):
    if A[i] < 0:
        cnt += 1
    
if cnt % 2 == 0:
    ans = sum(absA)
else:
    ans = sum(absA) - min(absA) * 2

print(ans)

Submission status: Accepted

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

cnt = 0
for i in range(N):
    if A[i] <= 0:
        cnt += 1
    
if cnt % 2 == 0:
    ans = sum(absA)
else:
    ans = sum(absA) - min(absA) * 2

print(ans)

Submission status: Accepted

Solution code:
N = int( input())
A = list( map( int, input().split()))
S = [ -A[i] for i in range(N) if A[i] <= 0]
T = list( map( abs, A))
ans = sum(T)

if len(S)%2 != 0:
    ans -= min(T)*2
print(ans)

Submission status: Accepted

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


A_minus = [a for a in A if a < 0]
A_abs = [abs(a) for a in A]

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
count_plus=0
count_minus=0
sum_all = 0

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

if count_minus % 2 == 0:
    print(sum_all)
else:
    print(sum_all - (min(A)*2))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
new_a = []
sum = 0
minus_count = 0
zero_flag = 0

for i in range(n):
	new_a.append(abs(a[i]))

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

if zero_flag == 1:
	for i in range(n):
		sum += abs(a[i])
elif minus_count % 2 == 0:
	for i in range(n):
		sum += abs(a[i])
else:
	for i in range(n):
		sum += new_a[i]
	sum = sum - min(new_a) * 2

print(sum)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
new_a = []
sum = 0
minus_count = 0
zero_flag = 0
min_a = 0

for i in range(n):
	new_a.append(abs(a[i]))

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

if zero_flag == 1:
	for i in range(n):
		sum += abs(a[i])
elif minus_count % 2 == 0:
	for i in range(n):
		sum += abs(a[i])
else:
	for i in range(n):
		sum += new_a[i]
	sum = sum - min(new_a) * 2

print(sum)

Submission status: Accepted

Solution code:
from fractions import gcd
def get_result(L, N):
    L_abs = [abs(x) for x in L]
    A = sum(L_abs)
    B = min(L_abs)
    neg_count = 0
    for elem in L:
        if elem < 0:
            neg_count += 1
    if neg_count % 2 == 0:
        return A
    else:
        return (A-2*B)


N = int(input())
L = [int(s) for s in input().split(" ")]
result = get_result(L, N)
print(result )

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

sum = 0
min = 1000000000
flag = 1
for i in range(n):
  sum += abs(A[i])
  if min > abs(A[i]):
    min = abs(A[i])
  if A[i] < 0:
    flag *= -1
    
if flag == 1:
  print(sum)
else:
  print(sum - min * 2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split()))
    c = 0
    for a in A:
        if 0 > a:
            c += 1
    ans = 0
    absA = [abs(a) for a in A]
    if c % 2 == 0:
        ans = sum(absA)
    else:
        ans = sum(absA) - 2 * min(absA)
    print(ans)
    

main()

Submission status: Accepted

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

negative_count = len(list(filter(lambda x: x < 0, As)))

if negative_count % 2 is 0:
    print(sum(list(map(lambda x: abs(x), As))))
else:
    abs_list = list(map(lambda x: abs(x), As))
    min = min(abs_list)
    abs_list.remove(min)
    print(sum(abs_list) - min)

Submission status: Accepted

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

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

if count%2 == 1:
    print(sum(abs_list) - min(abs_list)*2)
else:
    print (sum(abs_list))

Submission status: Accepted

Solution code:
# D

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

minus_num = len([x for x in A if x < 0])
abs_list = [abs(x) for x in A]
if minus_num % 2:
    print(sum(abs_list) - min(abs_list)*2)
else:
    print(sum(abs_list))

Submission status: Accepted

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

dp_p={}
dp_m={}
dp_p[0]=0
dp_m[0]=-9999999999

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

print(dp_p[N])

Submission status: Accepted

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

ans = 0

count = 0
existZero = False
for i in range(N):
    if A[i] == 0:
        existZero = True
    elif A[i] < 0:
        count += 1

    A[i] = abs(A[i])

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

abs_list = [abs(a) for a in A]
count = 0
ans = 0

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

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

print(ans)

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

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

print(total)

Submission status: Accepted

Solution code:
from functools import reduce
N=int(input())
NS=[int(i) for i in input().split()]
Nminus = 0
Nzero = 0
minabs = 9223372036854775807
abssum = 0
for i in NS:
    if i < 0:
        Nminus += 1
    elif i == 0:
        Nzero += 1
    minabs = min(abs(i), minabs)
    abssum += abs(i)

if Nminus % 2 == 0 or Nzero > 0:
    print(abssum)
else:
    print(abssum - 2*minabs)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
t = 0
for c in a: 
  if c < 0: t = 1 - t
if t:
  t = abs(a[0])
  s = 0
  for c in a:
    t = min(t, abs(c))
    s += abs(c)
  print(s - t - t)
else:
  s = 0
  for c in a:
    s += abs(c)
  print(s)

Submission status: Accepted

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


ans = 0
num_minus = 0
min_num = 1e+9
for i in range(N):
    ans += abs(A[i])
    min_num = min(min_num, abs(A[i]))
    if A[i] < 0: num_minus += 1

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

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
x,m,num = 10**9,0,0
for i in a:
    if i<0: m+=1
    x = min(abs(i),x)
    num+=abs(i)
if x and m%2: print(num-2*x)
else: print(num)

Submission status: Accepted

Solution code:
# 解けなかった...orz

N = int(input())
A = [int(i) for i in input().split()]
abs_A = list(map(abs, A))

SUM = sum(abs_A)

minus = len(list(filter(lambda a: a < 0, A)))
if minus % 2 == 0:
    print(SUM)
else:
    m = min(abs_A)
    print(SUM - 2*m)

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

Submission status: Accepted

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

if x % 2 == 0:
    for i in range(N):
        if A[i] < 0:
            A[i] = A[i] * -1
    print(sum(A))
       
else:
    if 0 in A:
        for i in range(N):
            if A[i] < 0:
                A[i] = A[i] * -1
        print(sum(A))  
            
    else:
        for i in range(N):
            if A[i] < 0:
                A[i] = A[i] * -1
        dele = min(A) 
        A.remove(min(A))

        print(sum(A) - dele)

Submission status: Accepted

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

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

A_abs = list(map(abs, A))
minmum = min(A_abs)

if cnt % 2 == 0:
    ans = sum(A_abs)
else:
    ans = sum(A_abs) - 2*minmum

print(ans)

Submission status: Accepted

Solution code:
def flipping_signs(N: int, A: list)->int:
    # 負の数が偶数この場合は全部正にできる。
    # 奇数個の場合は必ず一つ正にできないので絶対値の一番小さいもの
    # を残して他は全部正にする。
    min_abs = float('inf')
    s = 0
    neg_n = 0
    for a in A:
        s += abs(a)
        if a < 0:
            neg_n += 1
        min_abs = min(abs(a), min_abs)

    # print(neg_n, min_abs)
    if neg_n & 1:
        return s - 2*min_abs
    return s


if __name__ == "__main__":
    N = int(input())
    A = [int(s) for s in input().split()]

    ans = flipping_signs(N, A)
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# 解説を見て書いたコード
N = int(input())
data = list(map(int, input().split()))
count = 0

def ary_abs(data):
    for i in range(len(data)):
        data[i] = abs(data[i])


for i in data:
    if i <= 0:
        count += 1

if count % 2 == 0:
    # 負が 偶数 個のとき
    # 全てを正にした和
    ary_abs(data)
    print(sum(data))
else:
    # 負が 奇数 個のとき
    # どれか一つを負(最小のもの)
    ary_abs(data)
    print(sum(data) - min(data) * 2)

Submission status: Accepted

Solution code:
def d_flipping_signs_dp(N, A):
    dp = [[0] * 2 for _ in range(N + 1)]
    dp[0][0] = 0
    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])
    return dp[N][0]

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

Submission status: Accepted

Solution code:
def d_flipping_signs(N, A):
    minus_count_parity = len([1 for a in A if a < 0]) % 2
    a = list(map(abs, A))
    return sum(a) + (-2 * min(a) if minus_count_parity == 1 else 0)

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

Submission status: Accepted

Solution code:
# coding:utf-8                                                                                         

import math
                                                                                                       
def main(n,a):                                                                                         
    n = int(n)                                                                                         
    a = a.split()
    cnt = 0
    minus = 0                                                                                          
    b = []                                                                                             
    for i in a:                                                                                        
        a[cnt] = int(i)                                                                                
        b.append(abs(int(i)))                                                                          
        if a[cnt] < 0:
            minus += 1                                                                                 
        cnt += 1 
    if minus % 2 != 0: 
        mini = min(b)                                                                                  
    else:       
        mini = 0 
    total = 0   
    for i in b: 
        total += abs(i)
    total -= 2 * abs(mini)                                                                             
    print total     
    return total
                                                                                                       
if __name__ == "__main__":                                                                             
    n = raw_input()
    a = raw_input()                                                                                    
    main(n,a)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
a_min = 0
minus_count = 0
for i in a:
    if i < 0:
        minus_count += 1
a = [abs(e) for e in a]
if minus_count % 2 != 0:
    a_min = a.pop(a.index(min(a)))
ans = sum(a) - a_min
print(ans)

Submission status: Accepted

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

minus_count = 0

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

total = 0
min_num = 10 ** 9

for a in A:
    total += abs(a)
    min_num = min(min_num, abs(a))

if minus_count % 2 == 1:
    total -= 2 * min_num

print(total)

Submission status: Accepted

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

B = [abs(a) for a in A]
if len([a for a in A if a < 0]) % 2 == 1:
    print(sum(B) - 2 * min(B))
else:
    print(sum(B))

Submission status: Accepted

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

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

    a_sum, a1, a2 = 0, A[0], A[1]
    b_sum, b1, b2 = 0, -A[0], -A[1]
    for i in range(2, N):
        #print(a_sum, a1, a2)
        #print(b_sum, b1, b2)
        #print(A[i])
        

        if (a_sum + a1 + a2 + A[i]) > (b_sum + b1 + b2 + A[i]):
            new_a_sum = a_sum + a1
            new_a1 = a2
            new_a2 = A[i]
        else:
            new_a_sum = b_sum + b1
            new_a1 = b2
            new_a2 = A[i]

        if (a_sum + a1 - a2 - A[i]) > (b_sum + b1 - b2 - A[i]):
            new_b_sum = a_sum + a1
            new_b1 = -a2
            new_b2 = -A[i]
        else:
            new_b_sum = b_sum + b1
            new_b1 = -b2
            new_b2 = -A[i]

        a_sum, a1, a2 = new_a_sum, new_a1, new_a2
        b_sum, b1, b2 = new_b_sum, new_b1, new_b2
        
    print(max(a_sum+a1+a2, b_sum+b1+b2))

main()

Submission status: Accepted

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

dp = {(0, 0): 0, (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)])

Submission status: Accepted

Solution code:
import sys
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
import math
from functools import reduce


def input(): return sys.stdin.readline().strip()


def INT(): return int(input())


def MAP(): return map(int, input().split())


def LIST(): return list(map(int, input().split()))

def SUM(x):return sum([abs(x) for x in a ])



sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

n = INT()

a = LIST()

ct = 0

p=[]

for i, x in enumerate(a):
    if x < 0:
        ct +=1
        p.append([i, x])
    elif x == 0:
        ct = 0
        p.clear()


saidai = SUM(a)
a_abs = [abs(x) for x in a]

if ct % 2 == 0:
    print(saidai)
else:
    a_min = min(a_abs)
    print(saidai-2*a_min)

Submission status: Accepted

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

a, b = 0, -1e10
for ai in A:
    a, b = max(a + ai, b - ai), max(a - ai, b + ai)

print(a)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
N = int(input())
A = list(map(int, input().split()))

c = 0
S = 0
m = pow(10, 9)
for a in A:
    if a < 0:
        c += 1
        abs = -a
    else:
        abs = a
    S += abs
    if abs < m:
        m = abs

if c % 2 == 0:
    print(S)
else:
    print(S - 2 * m)

Submission status: Accepted

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

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

A.sort()

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

Submission status: Accepted

Solution code:
def main():
    N = int(input())

    neg_num = 0
    sum_abs = 0
    min_abs = 100000
    for i in input().split():
        number = int(i)
        if number < 0:
            neg_num +=1

        sum_abs += abs(number)
        min_abs = min(min_abs, abs(number))

    if neg_num % 2 == 0:
        print(sum_abs)
    else:
        print(sum_abs - min_abs*2)
    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import fractions
from functools import reduce

n = int(input())

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

dp = [[a[0]],[-10**10]]

for i in range(n-1):
    dp[0].append(max(dp[0][i] +a[i+1], dp[1][i] + a[i+1]))
    dp[1].append(max(dp[0][i] - a[i+1]- 2*a[i], dp[1][i]-a[i+1] + 2*a[i]))

print(max(dp[0][n-1],dp[1][n-1]))

Submission status: Accepted

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

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

print(dp[N][0])

Submission status: Accepted

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

dp = np.zeros((len(A_list)+1, 2))
dp[0][1] = - 10**5

for idx, a in enumerate(A_list):
    dp[idx+1][0] = max(dp[idx][0] + a, dp[idx][1] - a)
    dp[idx+1][1] = max(dp[idx][0] - a, dp[idx][1] + a)
print(int(dp[N][0]))

Submission status: Accepted

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

num_negative = sum([1 for x in A if x < 0])
absA = [abs(x) for x in A]
if num_negative % 2 == 0:
    print(sum(absA))
else:
    print(sum(absA)-min(absA)*2)

Submission status: Accepted

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

ans = 0
minus_cnt = 0
min_num = 10000000000000
for i in a:
  if i < 0:
    minus_cnt += 1
  if abs(i) < min_num:
    min_num = abs(i)

case = minus_cnt % 2

for i in a:
   ans += abs(i)

if case == 1:
  ans -= 2 * min_num

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:
        A[i] = -A[i]
        cnt += 1
if cnt % 2 == 0:
    print(sum(A))
else:
    print(sum(A) - 2*min(A))

Submission status: Accepted

Solution code:
N = int(input())
A = list(int(i) for i in input().split())
count_m = 0
count_z = 0

for i in A:
  if i < 0:
    count_m += 1
  elif i == 0:
    count_z += 1

A_abs = [abs(i) for i in A]
if count_m%2 == 0 or (count_m+count_z)%2 == 0:
  print(sum(A_abs))
  
else:
  A_min = A_abs.index(min(A_abs))
  A_abs[A_min] = A_abs[A_min]*(-1)
  print(sum(A_abs))

Submission status: Accepted

Solution code:
from sys import stdin
##  input functions for me
def ria(sep = ''):
    if sep == '' :
        return list(map(int, input().split())) 
    else: return list(map(int, input().split(sep)))
def rsa(sep = ''):
    if sep == '' :
        return input().split() 
    else: return input().split(sep)
def ri(): return int(input())
def rd(): return float(input())
def rs(): return input()
##

## main ##
N = ri()
A = ria()
inf = int(1e18)

dp = [0] * 2
dp[0] = [-inf] * (N + 1)
dp[1] = [-inf] * (N + 1)

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

ma = max(dp[0][N - 1] + A[N - 1], dp[1][N - 1] - A[N - 1])

print(ma)

Submission status: Accepted

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

k = 0
tmp = abs(a[n - 1])

for i in range(n - 1):
    if abs(a[i]) < tmp:
        tmp = abs(a[i])

    if a[i] < 0:
        a[i] *= -1
        a[i + 1] *= -1

    k += a[i]

if a[n - 1] < 0:
    if abs(a[n - 1]) == tmp:
        k += a[n - 1]
    else:
        k += abs(a[n - 1])
        k -= tmp * 2
else:
    k += a[n - 1]

print(k)

Submission status: Accepted

Solution code:
N=int(input())
A=list(int(x) for x in input().split())
minus_cnt=0
zero_cnt=0

B=[]
for x in A:
	if x<0:
		minus_cnt+=1
	if x==0:
		zero_cnt+=1
	B.append(max(x,-x))

if (minus_cnt%2==1 and zero_cnt==0):
	print(sum(B)-min(B)*2)
else:
	print(sum(B))

Submission status: Accepted

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

if sum(1 for x in a if x < 0) % 2 == 0:
    print(sum(np.abs(a)))
else:
    print(sum(np.abs(a)) - min(np.abs(a)) * 2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
b = list(map(int,input().split()))
count_m = 0
b_abs = [abs(b[i]) for i in range(n)]
for i in range(n):
  if b[i] < 0:
    count_m += 1
if count_m % 2 == 0:
  print(sum(b_abs))
else:
  b_min = min(b_abs)
  min_i = b_abs.index(b_min)
  if min_i == 0:
    print(sum(b_abs[1:]) - b_abs[0])
  elif min_i == n-1:
    print(sum(b_abs[:n-1]) - b_abs[n-1])
  else:
    print(sum(b_abs[0:min_i]+b_abs[min_i+1:]) - b_abs[min_i])

Submission status: Accepted

Solution code:
from collections import deque

N = int(input())
A = list(map(int, input().split()))
minAbs = 10**10
countMinus = 0
S = 0
for a in A:
  countMinus += (1 if a < 0 else 0)
  S += abs(a)
  minAbs = min(minAbs, abs(a))
print(S-2*(0 if countMinus % 2 == 0 else minAbs))

Submission status: Accepted

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

dp[0][1] = -1e9
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 = [int(i) for i in input().split()]
n = [a for a in A if a < 0]
S = sum([abs(a) for a in A])
if len(n) % 2 == 0:
    print(S)
else:
    print(S - 2 * min([abs(i) for i in A]))

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())
a = np.array(list(map(int, input().split())),dtype="i8")
cm = np.sum(a < 0)
a = np.abs(a)
s = np.sum(a)
if cm % 2:
    s -= np.min(a) * 2
print(s)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from functools import reduce

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

def isMinus(x):
  if x<0:
    return 1
  else:
    return 0

#signature count
s = sum([isMinus(i) for i in alist]) % 2
#    print(alist)\\

if s == 0:
  ans = sum(map(abs, alist))
else:
  ans = sum(map(abs, alist)) - min(list(map(abs, alist)))*2

print("{}".format(ans))

Submission status: Accepted

Solution code:
if __name__ == "__main__":
    _, *aa = map(int, open(0).read().split())
    bb = tuple(map(abs, aa))
    m = tuple(a for a in aa if a < 0)
    print(sum(bb) - min(bb)*2*(len(m)%2))

Submission status: Accepted

Solution code:
_, *aa = map(int, open(0).read().split())
bb = tuple(map(abs, aa))
m = tuple(a for a in aa if a < 0)
print(sum(bb) - min(bb)*2*(len(m)%2))

Submission status: Accepted

Solution code:
def main():
    _, *aa = map(int, open(0).read().split())
 
    bb = tuple(map(abs, aa))
    m = tuple(a for a in aa if a < 0)
    print(sum(bb) - min(bb)*2*(len(m)%2))
 
 
if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split()))
 
    B = list(map(abs, A))
    C = [a for a in A if a < 0]
    if len(C) % 2 == 0:
        print(sum(B))
    else:
        print(sum(B) - min(B)*2)
    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from sys import stdin

readline = stdin.readline

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

Submission status: Accepted

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

Submission status: Accepted

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

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:
    ans = 0
    for a in A:
        ans += max(a, -a)
    print(ans)
else:
    ans = 0
    smallest = float("inf")
    for a in A:
        ans += max(a, -a)
        smallest = min(smallest, abs(a))
    
    ans -= 2 * smallest
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

s = 0
mc = 0
mh = 1e10
for i in A:
  if i <= 0:
    mc += 1
  if abs(i) <= mh:
    mh = abs(i)
  s += abs(i)

if (mc % 2 == 1):
  mv = min(A[0],A[-1])
  #print('mv',mv)
  s -= mh*2
print(s)

Submission status: Accepted

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

n = sum(1 for a in A if a < 0)
s = sum(abs(a) for a in A)
m = min(abs(a) for a in A)

if n % 2 == 0:
    print(s)
else:
    print(s - 2 * m)

Submission status: Accepted

Solution code:
n = int(input())
aa = list(map(int, input().split()))
abs_aa = sorted([abs(a) for a in aa])
minus_count = sum([1 for a in aa if a < 0])
if minus_count % 2:
    abs_aa[0] *= -1
print(sum(abs_aa))

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in 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) - min(A)*2)

Submission status: Accepted

Solution code:
import numpy as np 
N = int(input())
A =list(map(int, input().strip().split()))
Ap = np.abs(A)
i = 0
m = 0
s = 0
M = np.sum(Ap)
while i <= N - 1:
    if A[i] < 0:
        m += 1
    i += 1
if m % 2 ==0:
    print(M)
else:
    Ma = 0
    while s <= N - 1:
        M1 = M - 2 * Ap[s]
        if M1 >= Ma:
            Ma = M1
        s += 1
    print(Ma)

Submission status: Accepted

Solution code:
from sys import stdin

readline = stdin.readline

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
a=abs(l[0])
m=0
d=0
z=0
s=0
for i in range(n):
  if l[i]<0:
    d+=1
  elif l[i]==0:
    z+=1
if d%2==0 or (d+z)%2==0:
  for i in range(n):
    s+=abs(l[i])
  print(s)
else:
  for i in range(1,n):
    if a>=abs(l[i]):
      a=abs(l[i])
      m=i
  s=0
  for i in range(m):
    s+=abs(l[i])
  s+=-abs(l[m])
  for i in range(m+1,n):
    s+=abs(l[i])
  print(s)