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()))
m = 11111111111111111111111111111111111111111111111110;count = 0;check = False
for i in range(n):
    if a[i] == 0:check = True
    if m > abs(a[i]):
        m = abs(a[i])
    if a[i] < 0:count += 1
if count % 2 == 0:check = True
s = 0
for i in range(n):
    s += abs(a[i])
if check:print(s)
else:print(s-2*abs(m))

Submission status: Accepted

Solution code:
def calc_abs(n):
    return abs(n)

N = int(input())
A = [int(i) for i in input().split()]
count = len([i for i in A if i < 0])
count_zero = A.count(0)
answer = 0
if(count % 2 != 0 and count_zero == 0):
    abs_list = list(map(calc_abs,A))
    answer = sum(abs_list) - 2 * min(abs_list)
else:
    abs_list = list(map(calc_abs, A))
    answer = sum(abs_list)
print(answer)

Submission status: Accepted

Solution code:
n = int(input())
A = [int(a) for a in input().split()]
B = [1 if _ > 0 else 0 for _ in A]
D = [abs(d) for d in A]
i = B.count(0)

if i % 2 != 0:
    D[D.index(min(D))] *= -1

print(sum(D))

Submission status: Accepted

Solution code:
#conding utf-8
N = int(input())
A = input().split()
ans = 0
t = 0
for i in range(N):
  A[i] = int(A[i])
for i in range(N):
  if A[i] == 0:
    for j in range(N):
      if A[j]<0:
        A[j] = -A[j]
    ans = sum(A)
    break
for j in range(N):
  if A[j]<0:
    t +=1
if t%2 == 0:
  for j in range(N):
    if A[j]<0:
      A[j] = -A[j]
  ans = sum(A)
else:
  for j in range(N):
    if A[j]<0:
      A[j] = -A[j]
  A[A.index(min(A))] = -A[A.index(min(A))]
  ans = sum(A)
      
print(ans)

Submission status: Accepted

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

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

AA=[abs(x)for x in A]
AA=sorted(AA)
if cnt%2==0:print(sum(AA))
else:print(sum(AA)-2*AA[0])

Submission status: Accepted

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

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

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

Submission status: Accepted

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

num_minus = sum([int(a < 0) for a in A])

abs_a = [abs(a) for a in A]
ans = sum(abs_a)
if num_minus % 2 != 0:
    ans -= min(abs_a) * 2
print(ans)

Submission status: Accepted

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

count = 0
s = 1000000000
add = 0
for num in arr:
  if num < 0:
    count += 1
  s = min(s, abs(num))
  add += abs(num)

print(add-(count%2)*abs(s)*2)

Submission status: Accepted

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

zero_count = 0
minus_count = 0
total = 0
minimum = abs(A_list[0])
for i in range(len(A_list)):
    if A_list[i] == 0:
        zero_count += 1
    elif A_list[i] < 0:
        minus_count += 1

    total += abs(A_list[i])
    if A_list[i] != 0:
        minimum = min(minimum, abs(A_list[i]))
#   print("total:{} min:{} zero:{} minus:{}".format(total, minimum, zero_count, minus_count))

if minus_count % 2 == 0 or zero_count > 0:
    print(total)
else:
    print(total - minimum*2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

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

fu = np.sum(A < 0)

if fu == 0:
    print(np.sum(A))
elif fu%2 == 0:
    print(np.sum(abs(A)))
else:
    b = min(abs(A))
    print(np.sum(abs(A)) - 2*b)

Submission status: Accepted

Solution code:
import operator
n = int(input())
lisa = list(map(int,input().strip().split()))
lisb = []
suma = 0
for val in lisa:
	suma+=abs(val)
#print(suma)
m = 0
for i,val in enumerate(lisa):
	if val < 0:
		m+=1
		lisa[i] = abs(val)
		lisb.append((val,i))
#print(lisa)
#print(lisb)
if m%2==0:
	print(suma)
else:
	minb = min(lisb,key = operator.itemgetter(0))
	mina = min(lisa)
	print(suma-2*min(-minb[0],mina))

Submission status: Accepted

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

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

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

    print(sum(A))
else:
    abs_min_ind = 0
    abs_min = 10**9
    for i in range(N):
        if A[i] < 0:
           A[i] = A[i] * -1
        if abs_min > abs(A[i]):
            abs_min = abs(A[i])
            abs_min_ind = A.index(A[i])

    A[abs_min_ind] = A[abs_min_ind] * -1

    print(sum(A))

Submission status: Accepted

Solution code:
n = int(input())

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

ans = 0
cost = float('inf')
count = 0

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

if 0 not in a:
    # count = max(count,n-count)
    if count % 2 != 0:
        ans -= 2*cost

print(ans)

Submission status: Accepted

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

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

if odd%2==1:
  print(ans-2*min(a))
else:
  print(ans)

Submission status: Accepted

Solution code:
import math


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

nums = A[:]
import math

gcd_queue = [
    (0, nums[0]),
]
for n in nums[1:]:
    tmp_queue = []
    for q in gcd_queue:
        tmp_queue.append((q[0] + q[1], n))
        tmp_queue.append((q[0] + q[1]*-1, n*-1))
    gcd_queue = set(tmp_queue[:])
    #print(gcd_queue)
    tmp = {}
    for q in gcd_queue:
        if str(q[1]) in tmp:
            tmp[str(q[1])] = max(q[0], tmp[str(q[1])])
        else:
            tmp[str(q[1])] = q[0]
    #print(tmp)
    gcd_queue = []
    for k, v in tmp.items():
        #print(k, v)
        gcd_queue.append((v, int(k)))
    

gcd_queue = list(gcd_queue)
ans = gcd_queue[0][0] + gcd_queue[0][1]
for q in gcd_queue:
    ans = max(ans, q[0]+q[1])
print(ans)

Submission status: Accepted

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


count = len([i for i in A if i < 0])
B = []
for i in range(N):
    B.append(abs(A[i]))

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

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
cnt = 0
minus = 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 ] ) - min([ abs(a) for a in A ] )*2)

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)
z = sum(1 for a in A if a == 0)

s = sum(abs(a) for a in A)

if n % 2 == 0 or z >= 1:
    print(s)
else:
    x = min(abs(a) for a in A)
    print(s - 2 * x)

Submission status: Accepted

Solution code:
def abs(i):
    if i <0:
        i = -i
    return i
N = int(input())
As = [int(Ai) for Ai in input().split()]
count = 0
for i in As:
    if i <= 0:
        count += 1
As_prime = [abs(Ai) for Ai in As]
if count % 2 == 0:
    print(sum(As_prime))
else:
    print(sum(As_prime)-2*min(As_prime))

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
cnt=0
ans=0
b=[]
for i in range(n):
  if a[i]<0:
    cnt+=1
    b.append(-a[i])
  else:
    b.append(a[i])
b.sort()
b.reverse()

if cnt%2==1:
  for i in range(n-1):
    ans+=b[i]
  ans-=b[n-1]
else:
  for i in range(n):
    ans+=b[i]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
Alist = list(map(int, input().split()))
c = 0
n0 = 0
Amin = 1000000000

for i in range(N):
    if abs(Alist[i]) < Amin :
        Amin = abs(Alist[i])
    if Alist[i] < 0 :
        c += 1
    elif Alist[i] == 0 :
        c = 0
        Amin = 0
        n0 = i
    Alist[i] = abs(Alist[i])

if n0 == 0 :
    if c % 2 == 0:
        print(sum(Alist))
    else:
        print(sum(Alist) - 2 * Amin)
else:
    if c % 2 == 0:
        print(sum(Alist))
    else:
        print(sum(Alist) - 2 * Amin)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    N = int(input())
    A_list = list(map(int, input().split()))

    minus_cnt = 0

    for A in A_list:
        if A < 0:
            minus_cnt += 1

    answer = 0

    if minus_cnt % 2 == 0:
        for A in A_list:
            answer += abs(A)
    else:
        min_abs_A = pow(10, 9)
        for A in A_list:
            answer += abs(A)
            if min_abs_A > abs(A):
                min_abs_A = abs(A)
        answer -= min_abs_A * 2

    print(answer)

Submission status: Accepted

Solution code:
# AtCoder Beginner Contest 125
# https://atcoder.jp/contests/abc125
import sys

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in list(ss)]
ss2nnn = lambda ss: [s2nn(s) for s in list(ss)]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline() for _ in range(n)]
ii2nnn = lambda n: ss2nnn(ii2ss(n))

def main(N, An):
    n0 = An[0]
    n1 = -An[0]
    for i in range(1, N-1):
        n0t = max(n0 + An[i], n1 - An[i])
        n1t = max(n0 - An[i], n1 + An[i])
        n0 = n0t
        n1 = n1t
    n0 += An[N-1]
    n1 -= An[N-1]
    print(max(n0, n1))

N = i2n()
An = i2nn()
main(N, An)

Submission status: Accepted

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


def main():
    n = int(input())
    a = list(map(int, input().split()))
    minus_count = 0
    abs_min = float('inf')
    ans = 0

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

        abs_min = min(abs_min, abs(ai))
        ans += abs(ai)

    if minus_count % 2 == 1:
        ans -= abs_min * 2

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#flipping signs
N=int(input())
A=list(map(int,input().split()))
n_plus=sum(x>0 for x in A)
min_abs=1000000000
if n_plus%2==N%2:
    ans=sum(abs(x) for x in A)
else:
    for x in A:
        x_abs=abs(x)
        if x_abs<min_abs:
            min_abs = x_abs
    ans=sum(abs(x) for x in A)-min_abs*2
print(ans)

Submission status: Accepted

Solution code:
# 入力が10**5とかになったときに100ms程度早い
import sys
read = sys.stdin.readline


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


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


def read_matrix(H):
    '''
    H is number of rows
    '''
    return [list(map(int, read().split())) for _ in range(H)]


def read_map(H):
    '''
    H is number of rows
    文字列で与えられた盤面を読み取る用
    '''
    return [read() for _ in range(H)]


def read_col(H, n_cols):
    '''
    H is number of rows
    n_cols is number of cols

    A列、B列が与えられるようなとき
    '''
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])

    return ret


N = read_a_int()
A = read_ints()

# 最初にすべてのマイナスが外せないか試して、
# abs最小の数に-をつけて合計すればいいんだ！

# 問題はどうやってすべてのマイナスがはずせるか判別するか
# +-+,---っていうパターンがあったらアウト
# flg = False
minuscount = 0
for a in A:
    if a < 0:
        minuscount += 1


anstmp = sum([abs(a) for a in A])
if minuscount % 2 == 0:
    print(anstmp)
    exit()

# else
mi = 10 ** 9
for a in A:
    mi = min(mi, abs(a))

print(anstmp-2*mi)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import collections
ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())
nsl = lambda: map(str, sys.stdin.readline().split())

n = ni()
a = nl()
count = 0
for i in range(len(a)):
    if a[i] < 0:
        count += 1
        a[i] = -a[i]
ans = 0
if count % 2 == 0:
    ans = sum(a)
else:
    ans = sum(a) - 2 * min(a)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
As = list(map(int,input().split()))
n = sum(x<0 for x in As)
ans = 0
if n%2 == 0:
    for i in range(N):
        ans += abs(As[i])
else:
    abs_list = []
    for i in range(N):
        abs_list.append(abs(As[i]))
        ans += abs(As[i])
    ans = ans-2*min(abs_list)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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


curmin = 1 + 10**9
negatives = 0
cursum = 0

for item in A:
    # find minimum
    # find num of negatives

    curmin = min(curmin, abs(item))

    if item < 0:
        negatives += 1

    cursum += abs(item)

if negatives % 2 == 1:
    cursum -= 2 * curmin

print(cursum)

Submission status: Accepted

Solution code:
inp = input()
inn = [int(e) for e in input().split()]
c = []
for d in inn:
    if d > 0:
        c.append(1)

    else:
        c.append(0)
k = [abs(d) for d in inn]


if c.count(0) % 2 == 1:
     k.sort()
     a = k.pop(0)
     aa = sum(k) - a
     print(aa)
else:
    k.sort()
    aa = sum(k)
    print(aa)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
c = 0
s = 0
for i in range(n):
	s += abs(a[i])

for i in range(n):
	if a[i] == 0:
		print(s)
		quit()
	elif a[i] < 0:
		c += 1
	else:
		pass

if c%2 == 0:
	pass
else:
	a.sort(key = lambda x: abs(x))
	s = s - 2*abs(a[0])
print(s)

Submission status: Accepted

Solution code:
from numpy import sign
n=int(input())
a=list(map(int,input().split()))
a_abs=list(map(abs,a))
a_abs.sort()

plmi=1
for i in a:
    plmi*=sign(i)

if plmi==-1:
    ans=sum(a_abs[1:])-a_abs[0]
elif plmi==1 or plmi==0:
    ans=sum(a_abs)
print(ans)

Submission status: Accepted

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

aA = [abs(a) for a in A]
asum = sum(aA)
c = sum([(a < 0) for a in A])

if c % 2:
	print(asum - min(aA) * 2)
else:
	print(asum)

Submission status: Accepted

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


def solve(N: int, A: "List[int]"):
    dp = [[0] * 4 for i in range(N)]

    dp[0][0] = A[0]
    dp[0][1] = -float('inf')
    dp[0][2] = -float('inf')
    dp[0][3] = -A[0]

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


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    solve(N, A)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
t=0
hunokazu=0
while t<N:
    if A[t]<0:
        hunokazu=hunokazu+1
    t=t+1
q=[]
qq=0
while qq<N:
    if A[qq]>=0:
        q.append(A[qq])
    else:
        q.append(A[qq]*(-1))
    qq=qq+1
if hunokazu%2==0:
    k=0
    ans=0
    while k<N:
        ans=ans+q[k]
        k=k+1
else:
    k=0
    ans=0
    while k<N:
        ans=ans+q[k]
        k=k+1
    ans=ans-min(q)*2
print(ans)

Submission status: Accepted

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

negcnt = 0
zcnt = 0
for k in a:
    if k < 0:
        negcnt += 1
    elif k == 0:
        zcnt += 1

ans = 0
if (negcnt + zcnt) % 2 == 0:
    for k in a:
        ans += abs(k)
    print(ans)
else:
    negmin = (10**9+1)
    for k in a:
        if abs(k) < negmin:
            negmin = abs(k)
    for k in a:
        ans += abs(k)
    #print(ans,negmin)
    print(ans - 2*negmin)

Submission status: Accepted

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

Submission status: Accepted

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

def abs_list_sum(numbers):
    result = 0
    for v in numbers:
        result += abs(v)
    return result


def abs_min(numbers):
    result = abs(numbers[0])
    for v in numbers:
        if result > abs(v):
            result = abs(v)
    return result

def main():
    n = int(input())
    a = list(map(int, input().split()))
    # count negative
    negative_count = 0
    for v in a:
        if v < 0:
            negative_count += 1
    if negative_count % 2 == 0:
        print(abs_list_sum(a))
    else:
        minimum = abs_min(a)
        print(abs_list_sum(a) - 2 * minimum)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

minus_count = 0
plus_list = []
for x in n_list:
  if x < 0:
    minus_count += 1
  plus_list.append(abs(x))
    
if minus_count % 2 == 0 or 0 in plus_list: #even
  print(sum(plus_list))
else:
  print(sum(plus_list) - 2 * min(plus_list))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
abss = [abs(x) for x in a]
ans = sum(abss)
minus = [-x for x in a if x <= 0]

if len(minus) % 2 == 0:
    print(ans)
    exit(0)

ans -= 2 * min(abss)

print(ans)

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

Submission status: Accepted

Solution code:
n = int(input())
a_lst = []
cnt = 0
for elem in input().split():
    if int(elem) < 0:
        cnt += 1
        a_lst.append((-1) * int(elem))
    else:
        a_lst.append(int(elem))

if cnt % 2 == 0:
    print(sum(a_lst))
else:
    print(sum(a_lst) - 2 * sorted(a_lst)[0])

Submission status: Accepted

Solution code:
n = int(input())
a = [int(x) for x in input().split()]
cnt = 0
for i in range(len(a)):
	if a[i] < 0:
		cnt += 1
		a[i] *= -1

if cnt % 2 == 0:
	print(sum(a))
else:
	a[a.index(min(a))] *= -1
	print(sum(a))

Submission status: Accepted

Solution code:
import numpy as np

def rev(i):
    if i==0:
        return 1
    return 0

N=int(input())
A=np.array(list(map(int,input().split())))
s=[]
for i in range(N):
    if A[i]>=0:
        s.append(1)
    else:
        s.append(0)

s[::-1]
for i in range(N-1):
    if s[i]==0:
        s[i]=1
        s[i+1]=rev(s[i+1])
if np.sum(s)!=N:
    ans=np.sum(np.abs(A))-2*np.min(np.abs(A))
    print(ans)
else:
    print(np.sum(np.abs(A)))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int , input().split()))
Pos_min =  10**9
Neg_max = -10**9
Sum = 0

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

if M % 2 == 1:
	for n in range(N):
		if A[n] < 0:
			Neg_max = max(Neg_max , A[n])
		else:
			Pos_min = min(Pos_min , A[n])
#	print("Neg_max = " + str(Neg_max))
#	print("Pos_min = " + str(Pos_min))
	
	if abs(Neg_max) < abs(Pos_min):
		A.remove(Neg_max)
		Sum += Neg_max
	else:
		A.remove(Pos_min)
		Sum -= Pos_min
	
	for n in range(N-1):
		Sum += abs(A[n])

else:
	for n in range(N):
		Sum += abs(A[n])

print(Sum)

Submission status: Accepted

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

a.sort(reverse=False)

for i in range(n-1):

    if a[i] < 0:

        if a[i+1] >= 0:

            if -a[i] > a[i+1]:
                a[i] = -a[i]
                a[i+1] = -a[i+1]
                b.append(a[i])

                for j in range(i+1,n-1):
                    b.append(a[j])
                break

            else:
                b.append(a[i])

                for j in range(i+1,n-1):
                    b.append(a[j])
                break

        a[i] = -a[i]
        a[i+1] = -a[i+1]
        b.append(a[i])

    else:
        b.append(a[i])

ans = sum(b) + a[n-1]

print(ans)

Submission status: Accepted

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

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

if cnt % 2 == 0:
    print(sum(map(abs, A_list)))
else:
    A_list_abs = list(map(abs, A_list))
    minimum = min(A_list_abs)
    A_list_abs.remove(minimum)
    print(sum(A_list_abs) - minimum)

Submission status: Accepted

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

neg = [x for x in a if x < 0]

b = len(neg) % 2 == 0

if b:
    print(sum([abs(x) for x in a]))
else:
    a.remove(max(neg))
    flip = [abs(x) for x in a]
    if -max(neg) > min(flip):
        print(sum(flip) - min(flip) * 2- max(neg))
    else:
        print(sum(flip) + max(neg))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

flag = False
for i in range(n):
    if a[i] < 0:
        if flag == False:
            flag = True
        else :
            flag = False
        a[i] = -1 * a[i]

list.sort(a, reverse=True)        
sum_a = 0

for i in range(n):
    if i == n - 1 and flag:
        sum_a -= a[i]
    else :
        sum_a += a[i]
print(sum_a)

Submission status: Accepted

Solution code:
#import sys
#input = sys.stdin.read

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

abslsta=[abs(v) for v in lsta]
min_index=abslsta.index(min(abslsta))

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

if lsta[n-1]<0:
    if min_index==n-1:
        print(sum(lsta))

    else:
        print(sum(lsta)-lsta[n-1]*2-lsta[min_index]*2)

else:
    print(sum(lsta))

Submission status: Accepted

Solution code:
n = int(input())
tmp = input().split()
a = []
minus_count = 0
abs_sum = 0
min_num = min_num = abs(int(tmp[0]))
for i in range(n):
    b = int(tmp[i])
    a.append(b)
    if (b < 0):
        minus_count += 1
    abs_sum += abs(b)
    if abs(b) < min_num:
        min_num = abs(b)

ans = 0
if minus_count % 2 == 0:
    ans = abs_sum
else:
    ans = abs_sum - (min_num * 2)

print(ans)

Submission status: Accepted

Solution code:
def main():
  n = int(input())
  vs = list(map(int, input().split()))
  avs = [abs(v) for v in vs]
  mvs = [v for v in vs if v < 0]
  
  mvs_len = len(mvs)
  avs_sum = sum(avs)
  avs_min = min(avs)
  if mvs_len % 2 == 0:
    return avs_sum
  else:
    return avs_sum - avs_min * 2
    
  
print(main())

Submission status: Accepted

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

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

ans = 0
if count & 1 == 0:
    for a in alist:
        ans += abs(a)
else:
    mina = abs(alist[0])
    for a in alist:
        mina = min(mina, abs(a))
    for a in alist:
        ans += abs(a)
    ans -= mina * 2


print(ans)

Submission status: Accepted

Solution code:
def main():
    input = sys.stdin.readline
    try:
        n = int(input())
        a = list(map(int, input().split()))
        if len([i for i in a if i < 0]) % 2 == 0:
            print(sum((abs(i) for i in a)))
        else:
            arr = [abs(i) for i in a]
            print(sum(arr) - min(arr) * 2)
        
    finally:
        input = None

import sys; sys.setrecursionlimit(50000)
if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def solve():
    N = int(input())
    A = [int(i) for i in input().split()]
    absA = [abs(Ai) for Ai in A]
    rA = [-1 if Ai < 0 else 1 for Ai in A]
    # すべてひっくり返せるか, 端だけ負になるかを検索する
    for i in range(N - 1):
        if rA[i] < 0:
            rA[i] *= -1
            rA[i + 1] *= -1
    # すべてひっくり返せるならすべての合計
    if rA[-1] == 1:
        result = sum(absA)
    else:
        # 無理なら一番小さい値を引いた数
        result = sum(absA) - (min(absA) * 2)
    print(result)


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

ans = 0

count = 0

is_zero = False

mint = abs(a[0])

for i in range(0, n):
	if(a[i] == 0):
		is_zero = True
	if(a[i] < 0):
		a[i] = a[i] * -1
		count += 1
	ans += a[i]
	mint = min(mint, a[i])

if(count % 2 != 0 and is_zero == False):
	ans -= 2 * mint
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a = list(map(int,input().split()))
minus_cnt = len([x for x in a if x<0])
abs_a = list(map(abs,a))
# print(a)
# print(abs_a)
# print(minus_cnt)
if 0 in abs_a:
    print(sum(abs_a))
elif minus_cnt %2 == 0:
    print(sum(abs_a))
else:
    print(sum(abs_a)-2*min(abs_a))

Submission status: Accepted

Solution code:
_,a=open(0);b,c=zip(*[(abs(i),i<0)for i in map(int,a.split())]);print(sum(b)-sum(c)%2*min(b)*2)

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
a = np.array([i for i in input().split()]).astype('int64')

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 :
    i = a[:n-1].argmin()
    if a[i] < -a[n-1]:
        a[i] = -a[i]
        a[n-1] = -a[n-1]
print(np.sum(a))

Submission status: Accepted

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

ans = 0

a = []
for i in A:
    ans += abs(i)
    a.append(abs(i))

gusu = False

count = 0
for i in A:
    if i == 0:
        gusu = True
        break
    if 0 > i:
        count += 1

if count % 2 == 0:
    gusu = True

if gusu == True:
    print(ans)
else:
    print(ans - min(a)*2)

Submission status: Accepted

Solution code:
n=int(raw_input())
a=map(int,raw_input().split())
#b=[abs(a[i]) for i in xrange(n-1)]
s=0
a.sort()
for i in xrange(n-1):
	if a[i]<0:
		if a[i+1]<=0 or (a[i+1]>0 and -a[i]>a[i+1]):
			a[i+1]*=-1
			a[i]*=-1
print sum(a)

Submission status: Accepted

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

# if A[0] < 0:
#     if abs(A[0]) >= abs(A[1]):
#         A[0] = -A[0]
#         A[1] = -A[1]

# for i in range(1, N-1):
#     if A[i] < 0:
#         if A[i-1] < A[i+1]:
#             if abs(A[i]) >= abs(A[i-1]) or A[i-1] < 0:
#                 A[i-1] = -A[i-1]
#                 A[i] = -A[i]
#         else:
#             if abs(A[i]) >= abs(A[i+1]) or A[i+1] < 0:
#                 A[i+1] = -A[i+1]
#                 A[i] = -A[i]

cou = 0
min_abs = 10000000000
for i in range(N):
    if A[i] < 0:
        cou += 1
    min_abs = min(min_abs, abs(A[i]))
    A[i] = abs(A[i])

if cou % 2 == 0:
    print(sum(A))
else:
    print(sum(A)-2*min_abs)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
count=0
flag=1
num=10*9+1
for i in range(n):
  if a[i]<0:
    count+=1
    a[i]*=-1
  elif a[i]==0:
    flag=0
if count%2==0 or flag==0:
  print(sum(a))
else:
  print(sum(a)-2*min(a))

Submission status: Accepted

Solution code:
import numpy as np

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

ab = len([n for n in A if n <0]) % 2

B = np.sort(np.abs(A))
if ab == 0:
  print(sum(B))
else:
  print(sum(B[1 :]) - B[0])

Submission status: Accepted

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

minus = 0
abs_min = 10 ** 10
sum_A = sum(list(map(abs, A)))

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

if (minus % 2 == 0):
    print(sum_A)
else:
    print(sum_A - 2 * abs_min)

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

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

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

Submission status: Accepted

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

    aabs = [abs(aa) for aa in a]
    tmp = 0
    minabs = min(aabs)
    for aa in a:
        if aa < 0:
            tmp += 1
    if tmp%2==1:
        print(sum(aabs) - 2* minabs)
    else:
        print(sum(aabs))
    # 3 5 7

if __name__ == '__main__':
    main()

Submission status: Accepted

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

minus = False
for a in A:
    if a < 0:
        minus = not minus

if minus:
    print(sum(map(abs,A)) - min(map(abs,A)) * 2)
else:
    print(sum(map(abs,A)))

Submission status: Accepted

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

sum_A = sum(A)
plus_A_sum = 0
minus_num = 0
Abs = []
for i in range(N):
  if(A[i] < 0): 
    minus_num += 1
    plus_A_sum += (-A[i])
  else:
    plus_A_sum += A[i]
  Abs.append(abs(A[i]))
  
if(minus_num%2 == 0):
  ans = plus_A_sum
else:
  Abs.sort()
  tmp = Abs[0]
  plus_A_sum = plus_A_sum-2*tmp
  ans = plus_A_sum
print(ans)