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: Wrong Answer

Solution code:
N = int(input().rstrip())
A = list(map(int, input().rstrip().split()))
print(A)
for i in range(len(A)):
  if i == len(A) - 2:
    break
  if A[i] >= 0 and A[i+1] >= 0:
    pass
  else:
    s = A[i] + A[i+1]
    if s < -s:
      A[i] = -1 * A[i]
      A[i+1] = -1 * A[i+1]
print(sum(A))

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(int,input().split()))
if n == 2:
    i = 0
    if (A[i] >= 0 and A[i+1] <= 0) and abs(A[i]) < abs(A[i+1]):
        A[i] = -A[i]
        A[i+1] = -A[i+1]
    elif (A[i] <= 0 and A[i+1] >= 0) and abs(A[i]) > abs(A[i+1]):
        A[i] = -A[i]
        A[i+1] = -A[i+1]
    elif A[i] <= 0 and A[i+1] <= 0:
        A[i] = -A[i]
        A[i+1] = -A[i+1]
else:
    for i in range(n-2):
        if A[i] >= 0 and A[i+1] >= 0 and A[i+2] >= 0:
            continue
        elif A[i] >= 0 and A[i+1] >= 0 and A[i+2] <= 0:
            continue
        elif A[i] >= 0 and A[i+1] <= 0 and A[i+2] >= 0:
            if abs(A[i+2]) <= abs(A[i+1]) and A[i+2] <= A[i]:
                A[i+2] = -A[i+2]
                A[i+1] = -A[i+1]
            elif abs(A[i]) <= abs(A[i+1]) and A[i] <= A[i+2]:
                A[i] = -A[i]
                A[i+1] = -A[i+1]
        elif A[i] <= 0 and A[i+1] >= 0 and A[i+2] >= 0:
            if abs(A[i]) >= abs(A[i+1]):
                A[i] = -A[i]
                A[i+1] = -A[i+1]
        elif A[i] >= 0 and A[i+1] <= 0 and A[i+2] <= 0:
            continue
        elif A[i] <= 0 and A[i+1] >= 0 and A[i+2] <= 0:
            A[i] = -A[i]
            A[i+2] = -A[i+2]
        elif A[i] <= 0 and A[i+1] <= 0 and A[i+2] >= 0:
            A[i] = -A[i]
            A[i+1] = -A[i+1]
        elif A[i] <= 0 and A[i+1] <= 0 and A[i+2] <= 0:
            A[i] = -A[i]
            A[i+1] = -A[i+1]
        #print(A)
    i = n-2
    if (A[i] >= 0 and A[i+1] <= 0) and abs(A[i]) < abs(A[i+1]):
        A[i] = -A[i]
        A[i+1] = -A[i+1]
    elif (A[i] <= 0 and A[i+1] >= 0) and abs(A[i]) > abs(A[i+1]):
        A[i] = -A[i]
        A[i+1] = -A[i+1]
    elif A[i] <= 0 and A[i+1] <= 0:
        A[i] = -A[i]
        A[i+1] = -A[i+1]
print(sum(A))

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np

N = int(input())

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

if np.sum(A==0) != 0:
# Aに0が含まれる場合
    # 最後に0が出てくる場所
    z = np.max(np.where(A==0))
    B = A[z:N]
    if np.sum(B < 0)%2 ==0:
        print(np.abs(A).sum())
    else:
        print(np.abs(A[0:N-1]).sum() - (A[-1]))

# Aに0が含まれない場合
else:
    if np.sum(A < 0)%2 ==0:
        print(np.abs(A).sum())
    else:
        print(np.abs(A[0:N-1]).sum() - (A[-1]))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = input().split(" ")
A = [int(i) for i in A]
count = 0
temp = A[:]
for i in range(N):
    if A[i] < 0:
        count += 1
    if temp[i] < 0:
        temp[i] = abs(temp[i])
if count % 2 == 0:
    print(sum(temp))
    #print("G")
else:
    print(temp)
    print(sum(temp)-min(temp)-min(temp))

Submission status: Wrong Answer

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

for i in range(n-1):
    if a[i] <= 0 and a[i+1] <= 0:
        a[i] = -a[i]
        a[i+1] = -a[i+1]
        
for i in range(n-2):
    if a[i] < 0 and a[i+1] >= 0 and a[i+2] <= 0:
        a[i] = -a[i]
        a[i+2] = -a[i+2]
        
for i in range(n-1):
    if a[i] < 0:
        if a[i]+a[i+1] < -(a[i]+a[i+1]):
            a[i] = -a[i]
            a[i+1] = -a[i+1]
            
print(sum(a))

Submission status: Wrong Answer

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):
			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: Wrong Answer

Solution code:

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

import numpy as np
A = np.array(A)
B = A

for i in range(len(A)-1):
    if A[i] < 0 and A[i+1] < 0:
        A[i] = A[i] * -1
        A[i+1] = A[i+1] * -1
        print("h")
    elif A[i] < 0 :
        if A[i] + A[i+1] < 0:
            A[i] = A[i] * -1
            A[i+1] = A[i+1] * -1
            print("k")
            print(i)
            print(A[i])
            print(A[i+1])

print(sum(A))

Submission status: Wrong Answer

Solution code:
import sys,collections as cl
import fractions
from functools import reduce
import copy
Max = sys.maxsize
def l():
	return list(map(int,input().split()))
def m():
	return map(int,input().split())
def s(x):
	a = []
	aa = x[i]
	su = 1
	for i in range(len(x)-1):
		if aa == x[i+1]:
			a.append([aa,su])
			aa = x[i+1]
			su = 1
		else:
			su += 1
	a.append([aa,su])
	return a
def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)
def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm(*numbers):
    return reduce(lcm_base, numbers, 1)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)


n = int(input())
su = 0

a = l()

co = 0
k = 0
a.sort()
f = 0
for i in range(n):
	if a[i] <= 0:
		co += 1
if co % 2 == 0:
	for i in range(n):
		su += abs(a[i])
	print(su)
else:
	for i in range(n):
		if f == 2:
			break
		if a[i] >= 1 and f == 0:
			kk = a[i-1]
			f = 1
		if a[i] >= 1:
			if abs(a[0]) >= a[i]:
				k = a[i]*-2
			else:
				k = kk*2
			f = 2
		elif i == n-1:
			k = a[i]*2
	for i in range(n):
		su += abs(a[i])
	su += k
	print(su)

Submission status: Wrong Answer

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

for i in range(N-1):
    if A[i] <= 0:
        j = i + 1
        while j < N:
            sumA = sum(A[i:j+1])
            if sumA < 0:
                A[i] = -1 * A[i]
                A[j] = -1 * A[j]
                j = N
            j += 1
if A[-2] + A[-1] < 0:
    A[-2] = -1 * A[-2]
    A[-1] = -1 * A[-1]

print(sum(A))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

print(sum(a))

Submission status: Wrong Answer

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]

print(sum(A))

Submission status: Wrong Answer

Solution code:
import numpy as np
N=int(input())
A=np.array(list(map(int,input().split())))
ans=np.sum(np.abs(A))-2*np.min(np.abs(A))
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split()))
    B = 0
    for i in range(len(A) - 1):
        if 0 >= A[i]:
            A[i] *= -1
            A[i + 1] *= -1
        B += A[i]
    print(B + A[-1])


main()

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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] = -1 * a[i]
        a[i+1] = -1 * a[i+1]

print(np.sum(a))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
A_=A.copy()
for i in range(N-1):
    if A[i]>=0:
        continue
    else:
        A[i]*=-1
        A[i+1]*=-1

    if A_[N-1-i]>=0:
        continue
    else:
        A_[N-1-i-1]*=-1
        A_[N-1-i]*=-1

sA = sum(A)
sA_ = sum(A_)
if sA > sA_:
    print(sA)
else:
    print(sA_)

Submission status: Wrong Answer

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: Wrong Answer

Solution code:
A=int(input())
B=[_ for _ in map(int,input().split())]
"マイナスの数を求める"
minus=0
for i in B:
  if i<=0:
    minus=+1
"マイナスの数が偶数の場合、絶対値Bを全部足す"
ans=0
if minus%2==0:
  for i in B:
    if i<=0:
      ans=+(-1)*i
    else:
      ans+=i
  print(ans)

else:
  S=0
  for i in range(A):
    B[i]=abs(B[i])
  
  for i in B:
    S+=i
  if (S-2*min(B))<0:
    print(0)
  else:
    print(S-2*min(B))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
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 and a[n-2] < abs(a[n-1]):
    a[n-2] = a[n-2] * -1
    a[n-1] = a[n-1] * -1
print(sum(a))

Submission status: Wrong Answer

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

A_1 = A.copy()

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

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

print(max(sum(A), sum(A_1)))

Submission status: Wrong Answer

Solution code:
n = int(input())
num = list(map(int,input().split()))
g = 0
for i in range(1,n-1):
    if num[i-1]<0:
        num[i-1]*=-1
        num[i]*=-1
if num[-2]<0 and abs(num[-2])>abs(num[-1]) or num[-1]<0 and abs(num[-2])<abs(num[-1]):
    num[-2]*=-1
    num[-1]*=-1
print(sum(num))

Submission status: Wrong Answer

Solution code:
import sys,collections as cl
import fractions
from functools import reduce
import copy
Max = sys.maxsize
def l():
	return list(map(int,input().split()))
def m():
	return map(int,input().split())
def s(x):
	a = []
	aa = x[i]
	su = 1
	for i in range(len(x)-1):
		if aa == x[i+1]:
			a.append([aa,su])
			aa = x[i+1]
			su = 1
		else:
			su += 1
	a.append([aa,su])
	return a
def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)
def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm(*numbers):
    return reduce(lcm_base, numbers, 1)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)


n = int(input())
su = 0

a = l()

co = 0
k = 0
a.sort()
f = 0
for i in range(n):
	if a[i] <= 0:
		co += 1
if co % 2 == 0:
	for i in range(n):
		su += abs(a[i])
	print(su)
else:
	for i in range(n):
		if f == 2:
			break
		if a[i] >= 0 and f == 0:
			kk = a[i-1]
			f = 1
		if a[i] >= 0:
			if abs(a[0]) >= a[i]:
				k = a[i]*-2
			else:
				k = kk*2
			f = 2
		elif i == n-1:
			k = a[i]*2
	for i in range(n):
		su += abs(a[i])
	su += k
	print(su)

Submission status: Wrong Answer

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 += A[N - 1]
    ans -= m
else:
    ans += A[N - 1]
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

print(sum(A))

Submission status: Wrong Answer

Solution code:
import sys,collections as cl
import fractions
from functools import reduce
import copy
Max = sys.maxsize
def l():
	return list(map(int,input().split()))
def m():
	return map(int,input().split())
def s(x):
	a = []
	aa = x[i]
	su = 1
	for i in range(len(x)-1):
		if aa == x[i+1]:
			a.append([aa,su])
			aa = x[i+1]
			su = 1
		else:
			su += 1
	a.append([aa,su])
	return a
def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)
def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm(*numbers):
    return reduce(lcm_base, numbers, 1)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)


n = int(input())
su = 0

a = l()

co = 0
k = 0
a.sort()
print(a)
f = 0
for i in range(n):
	if a[i] <= 0:
		co += 1
if co % 2 == 0:
	for i in range(n):
		su += abs(a[i])
	print(su)
else:
	for i in range(n):
		if f == 2:
			break
		if a[i] >= 0 and f == 0:
			kk = a[i-1]
			f = 1
		if a[i] >= 0:
			if abs(a[0]) >= a[i]:
				k = a[i]*-2
			else:
				k = kk*2
			f = 2
		elif i == n-1:
			k = a[i]*2
	for i in range(n):
		su += abs(a[i])
	su += k
	print(su)

Submission status: Wrong Answer

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:]))

Submission status: Wrong Answer

Solution code:
def abs(i):
    if i <0:
        i = -i
    return i
N = int(input())
As = [int(Ai) for Ai in input().split()]
for i in range(N-1):
    if As[i] < 0 and As[i+1] < 0:
        As[i] = -As[i]
        As[i+1] = -As[i+1]
    elif As[i] < 0 and As[i+1] >= 0:
        if abs(As[i]) >= As[i+1]:
            As[i] = -As[i]
            As[i+1] = -As[i+1]
    elif As[i] >= 0 and As[i+1] < 0:
        if i+2 < N and As[i] <= As[i+2] and As[i] < - As[i+1]:
            As[i] = -As[i]
            As[i+1] = -As[i+1]
print(sum(As))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
total = 0
for i in range(n-1):
    if a[i]<0and a[i+1]<0:
        a[i]*=-1
        a[i+1]*=-1
# print(a)
for i in range(n-1):
    if a[i]>0:
        total+=a[i]
    else:
        before = a[i]+a[i+1]
        after = a[i]*(-1) + a[i+1]*(-1)
        if before<=after:
            a[i] *= -1
            a[i + 1] *= -1
        # print('a:',a)
        total+=a[i]
total+=a[-1]
print(total)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

count = 0
for i in range(N):
    if B[i] < 0:
        count += 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(A[i])
        
print(res)

Submission status: Wrong Answer

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

nums = A[:]
import math

gcd_queue = [
    (0, nums[0]),
    (0, nums[0]*-1)
]
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 = tmp_queue[:]
#print(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: Wrong Answer

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):
			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: Wrong Answer

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

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
import copy

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

ansA = 0
for i in range(N-2):
  if A[i] < 0:
    A[i+1] *= -1
  ansA += abs(A[i])
ansA += abs(A[-1]+A[-2])
ansB = 0
for i in reversed(range(2, N)):
  if X[i] < 0:
    X[i-1] *= -1
  ansB += abs(X[i])
ansB += abs(X[0]+X[1])
print(max(ansA, ansB))

Submission status: Wrong Answer

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 -= m
else:
    ans += A[N - 1]
print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

from pprint import pprint

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

for i in range(0, n - 1):
    if a[i] < 0 and a[i + 1] < 0:
        a[i] *= -1;
        a[i + 1] *= -1;
    elif (a[i] < 0) and (a[i] + a[i + 1] < 0):
        a[i] *= -1;
        a[i + 1] *= -1;

print(sum(a))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split(" ")))
res = []
for i in range(n-1):
    tmp = a[:]
    tmp[i] *= -1
    tmp[i+1] *= -1
    res.append(sum(tmp))
print(max(res))

Submission status: Wrong Answer

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

for i in range(len(a) - 1):
  if a[i] < 0 and a[i+1] < 0:
    a[i] *= -1
    a[i+1] *= -1
  elif a[i] < 0 and abs(a[i]) > abs(a[i+1]):
    a[i] *= -1
    a[i+1] *= -1
  elif a[i+1] < 0 and abs(a[i]) < abs(a[i+1]):
    if i < n-2 and abs(a[i]) > abs(a[i+2]):
      continue
    else:
      a[i] *= -1
      a[i+1] *= -1
      
print(sum(a))

Submission status: Wrong Answer

Solution code:
n = int(input())

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import os
import math
import itertools
import math
from functools import reduce
# 
# f = open('input.txt', 'r')
# sys.stdin = f

n = int(input())
nums = [int(i) for i in input().split()]
num_abs = [abs(i) for i in nums]
print(sum(num_abs))
# sums = []
# for i in range(1, n):
#     print(i)
#     tmp = nums.copy()
#     tmp[i-1] *= -1
#     tmp[i] *= -1
#     sums.append(sum(tmp))
# print(sums)

Submission status: Wrong Answer

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 i < lasts:
			continue

		if tmp -1 or 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: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = 0
last = 0
for i in range(n - 1):
    flag = False
    if i == n - 2:
        if last:
            if last <= 0:
                if a[i + 1] <= 0:
                    flag = True
                elif -last >= a[i + 1]:
                    flag = True
            else:
                if a[i + 1] <= 0 and -a[i + 1] >= last:
                    flag = True
            if flag:
                ans -= last
                ans -= a[i + 1]
            else:
                ans += last
                ans += a[i + 1]
        else:
            if a[i] <= 0:
                if a[i + 1] <= 0:
                    flag = True
                elif -a[i] >= a[i + 1]:
                    flag = True
            else:
                if a[i + 1] <= 0 and -a[i + 1] >= a[i]:
                    flag = True
            if flag:
                ans -= a[i]
                ans -= a[i + 1]
            else:
                ans += a[i]
                ans += a[i + 1]
    else:
        if last:
            if last <= 0:
                if a[i + 1] <= 0:
                    flag = True
                elif -last >= a[i + 1]:
                    flag = True
            if flag:
                ans -= last
                last = -a[i + 1]
            else:
                ans += last
                last = 0
        else:
            if a[i] <= 0:
                if a[i + 1] <= 0:
                    flag = True
                elif -a[i] >= a[i + 1]:
                    flag = True
            if flag:
                ans -= a[i]
                last = -a[i + 1]
            else:
                ans += a[i]
                last = 0
a.reverse()
ans_ = 0
last = 0
for i in range(n - 1):
    flag = False
    if i == n - 2:
        if last:
            if last <= 0:
                if a[i + 1] <= 0:
                    flag = True
                elif -last >= a[i + 1]:
                    flag = True
            else:
                if a[i + 1] <= 0 and -a[i + 1] >= last:
                    flag = True
            if flag:
                ans_ -= last
                ans_ -= a[i + 1]
            else:
                ans_ += last
                ans_ += a[i + 1]
        else:
            if a[i] <= 0:
                if a[i + 1] <= 0:
                    flag = True
                elif -a[i] >= a[i + 1]:
                    flag = True
            else:
                if a[i + 1] <= 0 and -a[i + 1] >= a[i]:
                    flag = True
            if flag:
                ans_ -= a[i]
                ans_ -= a[i + 1]
            else:
                ans_ += a[i]
                ans_ += a[i + 1]
    else:
        if last:
            if last <= 0:
                if a[i + 1] <= 0:
                    flag = True
                elif -last >= a[i + 1]:
                    flag = True
            if flag:
                ans_ -= last
                last = -a[i + 1]
            else:
                ans_ += last
                last = 0
        else:
            if a[i] <= 0:
                if a[i + 1] <= 0:
                    flag = True
                elif -a[i] >= a[i + 1]:
                    flag = True
            if flag:
                ans_ -= a[i]
                last = -a[i + 1]
            else:
                ans_ += a[i]
                last = 0
print(max(ans, ans_))

Submission status: Wrong Answer

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

for i in range(N-1):
    if B[i] < 0:
        B[i] *= -1
        B[i+1] *= -1
    if B[i] == 0 and B[i+1] < 0:
        B[i+1] *= -1
 
flag = False
count = 0
for i in range(N):
    if B[i] < 0:
        flag = True
        count += 1
  
res = 0
if flag:
    B.sort()
    for i in range(count, N):
        res += abs(B[i])
        
    for i in range(count):
        res += B[i]
else:
    for i in range(N):
        res += abs(A[i])
        
print(res)

Submission status: Wrong Answer

Solution code:
def gcd_core(a, b):
    if b == 0:
        return a
    else:
        return gcd_core(b, a % b)


def gcd(arr):
    g = gcd_core(arr[0], arr[1])
    for i in range(2, len(arr)):
        g = gcd_core(g, arr[i])
    return g

n = int(input())

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

m = max(a)
ans = 0

for i in range(n):
    if a[i] != m:
        ans += a[i]*-1
    else:
        ans += m

print(ans)

Submission status: Wrong Answer

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 += A[N-1]
    s = A[:N-1]
    s.sort()
    b = a - s[0]*2+A[N-1]*2
    print(max([a, b]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
for i in range(N-1):
    if A[i]>=0:
        continue
    else:
        A[i]*=-1
        A[i+1]*=-1

A_=A.copy()
A_[N-2]*=-1
A_[N-1]*=-1

sA = sum(A)
sA_ = sum(A_)
if sA > sA_:
    print(sA)
else:
    print(sA_)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

N = int(input())
A = input().split()

sum = 0

for i in range(0, N - 1):

    a = int(A[i])
    a_next = int(A[i + 1])

    if a < 0:
        # 操作
        A[i] = a * -1
        A[i + 1] = a_next * -1

a = int(A[N - 2])
b = int(A[N - 1])

if a + b < -a + (-b):
    # 操作
    A[N - 2] = a * -1
    A[N - 1] = b * -1

for i in range(N):
    sum += int(A[i])

print(sum)

Submission status: Wrong Answer

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

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
    
ans = 0
if cnt % 2 == 0:
    for i in range(N):
        ans += abs(A[i])
else:
    for i in range(N - 2):
        ans += abs(A[i])
    if abs(A[-2]) > abs(A[-1]):
        ans += abs(A[-2])
        if A[-1] < 0:
            ans += A[-1]
        else:
            ans += - A[-1]
    else:
        ans += abs(A[-1])
        if A[-2] < 0:
            ans += A[-2]
        else:
            ans += - A[-2]
    
print(ans)

Submission status: Wrong Answer

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

ans = 0

count = 0

is_zero = False

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]

if(count % 2 != 0 and is_zero == False):
	ans -= 2 * abs(a[n-1])
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split()))
    B = 0
    for i in range(len(A) - 1):
        if 0 > A[i]:
            A[i] *= -1
            A[i + 1] *= -1
        B += A[i]
    print(B + A[-1])


main()

Submission status: Wrong Answer

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])
    if A[i] < 0:
        mins += 1 if A[i] < 0 else 0
    else:
        min_A = min(min_A, abs(A[i]))

if mins % 2 == 0:
    print(sum_A)
else:
    print(sum_A-min_A*2)