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

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

absA = list(map(abs, A))
if count % 2 == 0:
    print(sum(absA))
else:
    min_num = min(absA)
    print(sum(absA) - 2 * min_num)

Submission status: Accepted

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

B = False
if all(A):
    # print("not contain")
    M = 0
    for i in range(N):
        if A[i] < 0:
            A[i] *= -1
            M += 1
    # print(M)
    if M%2 == 0:
        B = True
    else:
        B = False

else:
    for i in range(N):
        if A[i] < 0:
            A[i] *= -1
    B = True

if B:
    print(sum(A))
else:
    print(str(sum(A)-2*min(A)))

Submission status: Accepted

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

a.sort()
i = 0
tmp = 0
while i + 1 < n and a[i+1] < 0:
      a[i] *= -1
      a[i+1] *= -1
      i += 2

a.sort()
if a[1] < -a[0]:
    a[1] *= -1
    a[0] *= -1


sum = 0
for i in range(n):
      sum += a[i]
print(sum)

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:
    a = min(A)
    print(sum(A)-2*a)

Submission status: Accepted

Solution code:
n=int(input())
a=[int(x) for x in input().split()]
a.sort()
b=[x for x in a if x>0]
c=[x for x in a if x<=0]
if len(b)==0:
    if len(c)%2==0:
        print(sum(c)*-1)
    else:
        print(sum(c[:-1])*-1+c[-1])
elif len(c)%2==0:
    s=sum(b)
    s-=sum(c)
    print(s)
else:
    c.append(b.pop(0))
    s=sum(b)
    s-=sum(c[:-2])
    if c[-1] > c[-2]*-1:
        s+=c[-1]
        s+=c[-2]
        print(s)
    else:
        s-=c[-1]
        s-=c[-2]
        print(s)

Submission status: Accepted

Solution code:
N = int(input())
ans = 0
plus = []
minus = []
A =[]
zero =0
for i in  [int(n) for n in input().split()]:
    A.append(abs(i))
    if i > 0:
        plus.append(i)
    elif i<0:
        minus.append(i)
    else:
        zero += 1
if len(minus)%2 == 0:
    print(sum(plus)-sum(minus))
else:
    if zero > 0:
        print(sum(plus)-sum(minus))
    else:
        odd = min(A)
        print(sum(A)-2*odd)

Submission status: Accepted

Solution code:
def d_flipping_signs(N, A):
    minus_count = 0
    _a = []
    for a in A:
        if a >= 0:
            _a.append(a)
        else:
            minus_count += 1
            _a.append(abs(a))
    ans = sum(_a)
    if minus_count % 2 == 1:
        ans -= 2 * min(_a)
    return ans

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

Submission status: Accepted

Solution code:
def main():

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

    B = [abs(a) for a in A]
    if 0 in A:
        print(sum(B))
    else:

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

        if count % 2 == 0:
            print(sum(B))
        else:
            print(sum(B) - 2*min(B))    


main()

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:
    a.sort()
    print(sum(a[1:]) - a[0])

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 = count + 1

if count % 2 == 0:
    ans = 0
    for i in range(n):
        ans = ans + abs(a[i])
    print(ans)
else:
    ans = 0
    for i in range(n):
        ans = ans + abs(a[i])
    l = []
    for i in range(n):
        l.append(ans - 2*abs(a[i]))
    print(max(l))

Submission status: Accepted

Solution code:
n=input()
n=int(n)
a=input().split()
a=[int(i) for i in a]
z=[i if i>0 else i*-1 for i in a]
count=0
for i in a:
    if i<0:
        count+=1
if count%2==0:
    print(sum(z))
else:
    m=min(z)*2
    print(sum(z)-m)

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
        
Aabs = [abs(i) for i in A]

if count % 2 == 0:
    print(sum(Aabs))
else:
    mini = min(Aabs)
    print(sum(Aabs) - 2*mini)

Submission status: Accepted

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


C = 0
for i, v in enumerate(A):
    if v < 0:
        C += 1
if C % 2 == 0:
    ans = 0
    for i, v in enumerate(A):
        ans += abs(v)
else:
    A = sorted([abs(a) for a in A])
    ans = sum(A) - 2*A[0]

print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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


import numpy as np
a = np.array(a)

cnt = sum(a < 0)
b = np.abs(a)

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

Submission status: Accepted

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

ans = 0
for i in range(N):
    ans += A[i]
    
if minus == 1:
    ans -= min(A)*2
    
print(ans)

Submission status: Accepted

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

if a[-1] < 0:
    a[-1] *= -1
    print(sum(a) - min(a) * 2)
else:
    print(sum(a))

Submission status: Accepted

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

if ct%2==0:
    print(SUM)
if ct%2==1:
    a=abs(A[0])
    for i in range(N):
        if a>=abs(A[i]):
            a=abs(A[i])
    print(SUM-2*a)

Submission status: Accepted

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

abs_A = [abs(i) for i in A]
sa = sum(abs_A)

nm = len([i for i in A if i<0])
if nm % 2 == 1:
    ma = min(abs_A)
    sa = sa - 2*ma

print(sa)

Submission status: Accepted

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

abs_list = sorted([abs(x) for x in A])[::-1]

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

if abs(min(A)) != min(abs_list):
    A[A.index(min(A))] *= -1
    A[A.index(min(abs_list))] *= -1
    
print(sum(A))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
input = sys.stdin.readline

# bisect_left(lists, 3)
# bisect_right(lists, 3)

def main():
	N = int(input())
	A = input().split()
	A = [int(s) for s in A]

	ans = 0
	dels = 0
	naiCounts = 0
	lasts = -1
	for i, val in enumerate(A):
		if val < 0:
			lasts = i
			naiCounts += 1
		if val == 0:
			lasts = -1
			naiCounts = 0
			break

	if naiCounts % 2 == 0 or naiCounts == 0:
		for i, val in enumerate(A):
			if val < 0:
				ans = ans + (val * -1)
			else:
				ans += val
		print(ans)
		sys.exit()

	tmp = -1
	for i, val in enumerate(A):
		if tmp == -1 or tmp > abs(val):
			tmp = abs(val)
			dels = i
	
	#print(dels)
	for i, val in enumerate(A):
		if i == dels:
			#print(val)
			if val < 0:
				ans += val
			else:
				ans -= val
		else:
			if val < 0:
				ans = ans + (val * -1)
			else:
				ans += val


	print(ans)



def gcd(a, b):
	if a > b:
		a, b = b, a
		
	while a > 0:
		a, b = b % a, a
	return b
	
if __name__ == "__main__":
	main()

Submission status: Accepted

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

Submission status: Accepted

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

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

dp[0][0] = a_list[0]
dp[0][1] = -1*a_list[0] 

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

print(ans)

Submission status: Accepted

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

r = 0
r_min = 1000000000
m_c = 0

for i in A:
    ia = abs(i)
    if i<0:
        m_c+=1
    if r_min > ia:
        r_min = ia
    r+=ia

if m_c%2==0 :
    print(r)
else:
    print(r-r_min*2)

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
l = np.array(list(map(int,input().split()))).astype("i")

m = np.sum(l < 0)

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

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

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
lsta = list(map(int,input().split()))
npa = np.array(lsta)
guki = len(npa[npa<0])%2
npaabs = np.abs(npa)
print(sum(npaabs)-min(npaabs)*2*guki)

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

flag = 0
for i in range(N):
    if A[i] != 0:
        flag = 1
    if flag == 1 and 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()))
count = 0
for i in range(N):
    if A[i] <= 0:
        count += 1
ans = 0
if count%2 == 0 or 0 in A:
    for i in range(N):
        ans += max(A[i],-A[i])
else:
    tmp = -1000000000
    c = 0
    for i in range(N):
        if tmp < min(A[i],-A[i]) < 0:
            tmp = min(A[i],-A[i])
            c = i
    if tmp == -1000000000 and c == 0:
        if tmp != A[0]:
            tmp = 0
            c = A.index(0)
    
    for i in range(N):
        if i != c:
            ans += max(A[i],-A[i])
        else:
            ans += tmp
print(ans)

Submission status: Accepted

Solution code:
import re
import sys
from fractions import gcd
from functools import reduce
from itertools import product

import numpy as np

sys.setrecursionlimit(200000)


def input():
    return sys.stdin.readline()[:-1]


def ii(t: type = int):
    return t(input())


def il(t: type = int):
    return list(map(t, input().split()))


def imi(N: int, t: type = int):
    return [ii(t) for _ in range(N)]


def iml(N: int, t: type = int):
    return [il(t) for _ in range(N)]


def solve():
    N = ii()
    A = np.asarray(il())
    B = A[A < 0]
    if len(B) % 2 == 0:
        return sum(np.abs(A))
    sorted_A = sorted(np.abs(A))
    return sum(sorted_A[1:]) - sorted_A[0]


if __name__ == "__main__":
    print(solve())

Submission status: Accepted

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

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


minn = 10**10
cnt = 0
ans = 0
for i in range(n):
    temp = abs(a[i])
    minn = min(temp, minn)

    ans += temp

    if a[i]<0:
        cnt += 1



if cnt % 2 == 0:
    print(ans)
else:
    print(ans - 2*minn)

Submission status: Accepted

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

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

Submission status: Accepted

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

import fractions as f
import copy

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

a_abs = list(map(abs, a))

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

if fu%2 == 0:
    max = sum(a_abs)
else:
    min_abs = a_abs.index(min(a_abs))
    max = sum(a_abs) - 2 * a_abs[min_abs]

print(max)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

if minus_count%2 == 0 or 0 in a:
    for i in range(len(a)):
        ans += abs(a[i])
else:
    for i in range(len(a)):
        ans += abs(a[i])
    a = [e for e in a if e != 0]
    a.sort(key=abs)
    ans -= abs(a[0])*2
print(ans)

Submission status: Accepted

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

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

ans = 0
m = 10 ** 9 + 1
for i in range(N - 1):
    if A[i] < 0:
        A[i] = -A[i]
        A[i + 1] = -A[i + 1]
    ans += A[i]
    if A[i] < m:
        m = A[i]

if A[N - 1] < 0:
    m = min(m, abs(A[N - 1]))
    ans += abs(A[N - 1])
    ans -= 2 * m
else:
    ans += A[N - 1]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split(" ")))
abs_list = list(map(abs, A))
abs_list.sort()
pos = list(filter(lambda x: x > 0, A))
neg = list(filter(lambda x: x < 0, A))
pos.sort()
neg.sort()

# A has 0
if abs_list[0] == 0:
	print(sum(abs_list))
elif len(neg) % 2 == 0:
	print(sum(abs_list))
elif len(pos) > 0 and abs(pos[0]) < abs(neg[-1]):
	print(sum(abs_list) - 2*pos[0])
else:
	print(sum(abs_list[1:])-abs_list[0])

Submission status: Accepted

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

count = 0
for i in range(N):
    if B[i] < 0:
        count += 1
        B[i] *= -1
  
res = 0
if count%2 == 1:
    B.sort()
    for i in range(1, N):
        res += abs(B[i])

    res -= B[0]
else:
    for i in range(N):
        res += abs(B[i])
        
print(res)

Submission status: Accepted

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

minus = 0


A = [int(element) for element in a]

A_abs = [abs(element) for  element in A]

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


if minus % 2 == 0:
    result = sum(A_abs)

else:
    result = sum(A_abs) - 2*min(A_abs)

print(result)

Submission status: Accepted

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

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

sum = 0
for i in range(n):
    sum += a[i]

a.sort()

if time % 2 == 0:
    print(sum)
else:
    print(sum - a[0] * 2)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-


def main():
  input()
  s = 0
  m = 1e9
  c = 0

  for v in map(int, input().split()):
    s += abs(v)
    m = min(abs(v), m)

    if v < 0:
      c = 1 - c

  print(s - m * c * 2, flush=True)


if __name__ == '__main__':
  main()

Submission status: Accepted

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

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

dp[1][0] = sum(arr[:2])
dp[1][1] = -sum(arr[:2])

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    result = sub()
    print(result)


def sub():
    N = int(input())
    As = [int(i) for i in input().split()]

    abssum = 0
    negative_count = 0
    is_zero_contained = False
    absmin = 10**9

    for i in range(N):
        val = As[i]
        if val > 0:
            abssum += val
            absmin = min(absmin, val)
        elif val < 0:
            abssum -= val
            negative_count += 1
            absmin = min(absmin, -val)
        else:  # 0
            is_zero_contained = True
            absmin = 0

    if is_zero_contained:
        return abssum
    elif negative_count % 2 == 0:
        return abssum
    else:
        return abssum - 2 * absmin

    return abssum


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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

    # マイナスが奇数の時、好きな数字をマイナスにできる
    # マイナスが偶数の時、全てプラスにできる
    minus_num = 0
    for a in A:
        if a < 0:
            minus_num += 1
    
    ans = 0
    lowest = 10**9+1
    for a in A:
        ans += abs(a)
        if lowest >= abs(a):
            lowest = abs(a)
    
    if minus_num%2 == 0:
        print(ans)
    else:
        ans -= lowest*2
        print(ans)


if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
m = 0
b = []
for i in range(N):
    if a[i]<0:
        m += 1
for i in range(N):
    b.append(abs(a[i]))
if m%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()))
b=list(map(abs,a))
c=0
for i in range(n):
     if a[i]<0:
        c += 1
if c%2==0:
    print(sum(b))
else:
    d=sorted(b)
    print(sum(d)-2*d[0])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
A.sort()
ans = 0
hoge = 10000000000000000000
# マイナスの個数が偶数かつ、Nが偶数なら全部の絶対値で良い気がする
# マイナスの個数が偶数なら全部の和でいい
n = sum(x < 0 for x in A)
if n % 2 == 0:
    for i in range(N):
        ans += abs(A[i])
    print(ans)
    exit()
else:
    for i in range(N):
        ans += abs(A[i])
        if hoge > abs(A[i]):
            hoge = abs(A[i])
    print(ans - hoge*2)
    exit()
# マイナスの個数が奇数なら被害を抑えるために、絶対値の一番小さいやつを-にする

Submission status: Accepted

Solution code:
N = int(input())
A = input().split(' ')
for i in range(N):
  A[i] = int(A[i])
cnt = 0
for i in range(N):
  if A[i]<0:
    cnt += 1
if cnt%2==0:
  rlt = 0
  for i in range(N):
    rlt += abs(A[i])
else:
  sml = 0
  for i in range(N-1):
    if abs(A[sml])>abs(A[i+1]):
      sml = i+1
  rlt = 0
  for i in range(N):
      if i != sml:
        rlt += abs(A[i])
      else:
        rlt += -(abs(A[i]))
print(rlt)

Submission status: Accepted

Solution code:
import numpy as np

def spaceinput():
    return list(map(int,input().split(" ")))
    
N=int(input())
a=np.array(spaceinput())


cnt_minus=0
cnt_zero=0
for sa in a:
    if sa==0:
        cnt_zero+=1
    if sa<0:
        cnt_minus+=1


a_abs=np.abs(a)
suma=np.sum(a_abs)


if cnt_minus%2==0 or cnt_zero>0:
    res=suma
else:
    res=suma-2*np.min(a_abs)

print(res)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
N = int(input())
Alist = [int(x) for x in input().split()]
Abslist = [abs(x) for x in Alist]

if 0 in Alist:
    ans = sum(Abslist)
else:
    count = 0
    for i in Alist:
        if i < 0:
            count += 1
    if count % 2 == 0:
        ans = sum(Abslist)
    else:
        ans = sum(Abslist) - 2* min(Abslist)

print(ans)

Submission status: Accepted

Solution code:
import numpy as np

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

ans = np.zeros(1, dtype=np.uint64)
even_odd = 0
mini = 1000000000

for i in A:
    if(i < 0):
        even_odd = (even_odd + 1) % 2
    ans += abs(i)
    if(mini > abs(i)):
        mini = abs(i)
        
if(even_odd == 0):
    print(ans[0])
else:
    print((ans - (mini * 2))[0])

Submission status: Accepted

Solution code:
N = int(input())
*A, = map(int, input().split())
a = 0
for i in range(N-1):
    if A[i] < 0:
        A[i] = -A[i]
        A[i+1] = -A[i+1]
    a += A[i]
if A[N-1] >= 0:
    print(a+A[N-1])
else:
    a = [abs(v) for v in A]
    a.sort()
    print(sum(a)-a[0]*2)

Submission status: Accepted

Solution code:
from collections import Counter
N=int(input())
A=list(map(int,input().split()))
judge=Counter([('0' if k==0 else '-1' if k<0 else '1') for k in A])

new_list=[i if i>=0 else -i for i in A]
if ((judge['0'] != 0) or (judge['-1'] % 2 == 0)):
    print(sum(new_list))
else:
    print(sum(new_list)-2*min(new_list))

Submission status: Accepted

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

num = 0
for i in range(n):
    if a[i] < 0:
        num = num + 1
if num % 2 == 0:
    ans = sum(b)
else:
    ans = sum(b) - (b[0]*2)
print(ans)

Submission status: Accepted

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

A = sorted(A)
sumA = sum(A)

for n in range(0, N-1, 2):
    v = A[n] + A[n+1]
    if v < 0:
        sumA = sumA - (2* v)
    else:
        break
print(sumA)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
A.sort(key=lambda a: abs(a))

minus_count = sum(a < 0 for a in A)
has_zero = (0 in A)
abs_sum = sum(abs(a) for a in A)

if has_zero or minus_count%2==0:
    print(abs_sum)
else:
    print(abs_sum - 2*abs(A[0]))

Submission status: Accepted

Solution code:
num= int (input())
nl = list ( map ( int , input ().split()))
fugo = [0 if i < 0 else 1 for i in nl]
mi = fugo.count(0)
nla =[abs(x) for x in nl]
nlsum = sum (nla)
if mi % 2 == 1 :
    minus = min(nla) * -2
    nlsum += minus
print(nlsum)

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 = (cnt+1)%2

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

Submission status: Accepted

Solution code:
import sys

N = int(sys.stdin.readline())

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

abstotal = 0
count_minus = 0
abs_A = []
for i in range(N):
    abstotal += abs(A[i])
    abs_A.append(abs(A[i]))
    if A[i] < 0:
        count_minus += 1

ans = abstotal
if count_minus%2 == 1:
    ans = abstotal - 2 * min(abs_A)

print(ans)

Submission status: Accepted

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

sum1 = 0
for i in list1:
    if(i<0):
        sum1 += 1

nl1 = np.array(list1)
np.abs(nl1)
if(sum1%2 == 0):
    a = sum(np.abs(nl1))
else:
    a = sum(np.abs(nl1))-2*min(np.abs(nl1))

print(a)

Submission status: Accepted

Solution code:
import math

N = int(input())
A = list(map(int, input().strip().split(' ')))
B = []

sum_A = abs(A[0])
min_A = abs(A[0])
mins = 1 if A[0] < 0 else 0
for i in range(1, N):
    sum_A += abs(A[i])
    min_A = min(min_A, abs(A[i]))
    if A[i] < 0:
        mins += 1 if A[i] < 0 else 0

if mins % 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().split()))
count = 0
point = 0 


for i in range(N):
    if A[i]==0:
        count = 1
        break
    elif A[i] < 0:
        point += 1

if count==1 or point%2==0:
    print(sum(np.abs(A)))

else:
    A1 = np.abs(A)
    A2 = min(A1)
    print(sum(A1)-2*A2)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
B = [abs(i) for i in A]
for i in range(N-1):
    if(A[i]<0):
        A[i] = -A[i]
        A[i+1] = -A[i+1]
if(A[N-1]<0):
    flag = False
else:
    flag = True

if(flag):
    print(sum(A))
else:
    m = min(B)
    print(sum(B)-2*m)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
s = 1
for i in A:
  if i < 0:
    s *= -1
Aabs = list(map(abs,A))

if s == 1:
  print(sum(Aabs))
else:
  print(sum(Aabs)-2*min(Aabs))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

import sys


def parse(file):
    n = int(file.readline().strip())
    nums = map(int, file.readline().split())
    return n, nums


def main(n, nums):
    minimum_abs = 1e20
    negatives = 0
    ans = 0

    for num in nums:
        if num < 0:
            a = - num
            negatives += 1
        else:
            a = num

        if a < minimum_abs:
            minimum_abs = a
        ans += a

    if negatives % 2 == 1:
        ans -= (minimum_abs * 2)

    return ans


if __name__ == '__main__':
    print(main(*parse(sys.stdin)))

Submission status: Accepted

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

t = sum(x < 0 for x in s)
p = 0
p1 = abs(s[0])
q = abs(s[0])

if t % 2 == 0:
    for i in range(n):
        p += abs(s[i])
    print(p)
else:
    for i in range(n-1):
        p1 += abs(s[i+1])
        q = min(q,abs(s[i+1]))
    print(p1-q*2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
import re

n = int(raw_input())
a = raw_input().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

Submission status: Accepted

Solution code:
n = int(input())
data = list(map(int,input().split()))
nega = 0
for d in data:
  if d <= 0:
    nega += 1
    
data = sorted([abs(d) for d in data])
    
if nega % 2 == 0:
  res = sum(data)
  print(res)
  
else:
  res = sum(data[1:])
  print(res - data[0])

Submission status: Accepted

Solution code:
# coding: utf-8

n = int(input())
a = list(map(int, input().split()))
a_ = [0] * n
for i in range(n):
    a_[i] = abs(a[i])

output = 0
all_plus = False
minus_kisuu = False
for i in range(n):
    if a[i] == 0:
        all_plus = True
        break
    if a[i] < 0:
        minus_kisuu = not minus_kisuu

if all_plus or not minus_kisuu:
    for i in range(n):
        output += a_[i]
else:
    for i in range(n):
        output += a_[i]
    output -= min(a_) * 2

print(output)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

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

aa = list(map(lambda x: abs(x), A))
c = len(list(filter(lambda x: x < 0, A)))
s = sum(aa)
if c % 2 == 1:
    s -= min(aa) * 2
print(s)

Submission status: Accepted

Solution code:
import numpy as np
N = (int(input())+1)
A = [int(i) for i in input().split()]
B = np.abs(A)
C = 0
for i in range(N-1):
  if A[i]<0 : C+=1

C %= 2
min = min(B)

if C==0: print(sum(B))
else : print(sum(B)-min*2)

Submission status: Accepted

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

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

# print(neg)

if neg % 2 == 0:
    print(sum(absl))
else:
    print(sum(absl) - min(absl) * 2)

Submission status: Accepted

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

m = 10**9+1
minus_count = 0
for i in range(n):
    if a[i] < 0:
        minus_count += 1 
    if m > abs(a[i]):
        m = abs(a[i])
    a[i] = abs(a[i])
if minus_count % 2 == 0:
    print(sum(a))
else:
    print(sum(a) - m*2)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
i = 0
while i < n-1:
  if a[i] <= 0 and a[i+1] <= 0:
    a[i] = -1*a[i]
    a[i+1] = -1*a[i+1]
    i += 2
  elif a[i] > 0:
    i += 1
  elif a[i] <= 0 and a[i+1] > 0:
    a[i] = -1*a[i]
    a[i+1] = -1*a[i+1]
    i += 1
if a[n-1] < 0:
  a[n-1] = -1*a[n-1]
  print(sum(a)-2*min(a))
else:print(sum(a))

Submission status: Accepted

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

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

if cnt % 2 == 0:
    print(s)
else:
    print(s - 2 * x)