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:
# -*- coding: utf-8 -*-
"""
Created on Mon Jun  1 13:59:38 2020

@author: NEC-PCuser
"""

N = int(input())
A = list(map(int, input().split()))
minusCount = 0
ans = 0
miniumAbs = 10 * 9
for i in range(0, len(A)):
    if (A[i] < 0):
        minusCount += 1
    if (miniumAbs > abs(A[i])):
        miniumAbs = abs(A[i])
    ans += abs(A[i])
if minusCount %2 == 0:
    print(ans)
else:
    print(ans - 2 * miniumAbs)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Mon Jun  1 13:59:38 2020

@author: NEC-PCuser
"""

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

for i in range(0, len(A) - 1):
    if ((A[i] < 0 and A[i + 1] < 0) or (A[i] * A[i + 1] < 0 and A[i] + A[i + 1] < 0)):
        A[i] *= -1
        A[i + 1] *= -1
for i in range(0, len(A) - 1):
    if ((A[i] < 0 and A[i + 1] < 0) or (A[i] * A[i + 1] < 0 and A[i] + A[i + 1] < 0)):
        A[i] *= -1
        A[i + 1] *= -1

ans = 0
for i in range(0, len(A)):
    ans += A[i]
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Mon Jun  1 13:59:38 2020

@author: NEC-PCuser
"""

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

for i in range(0, len(A) - 1):
    if ((A[i] < 0 and A[i + 1] < 0) or (A[i] * A[i + 1] < 0 and A[i] + A[i + 1] < 0)):
        A[i] *= -1
        A[i + 1] *= -1

ans = 0
for i in range(0, len(A)):
    ans += A[i]
print(ans)

Submission status: Wrong Answer

Solution code:
n,*a=map(int,open(0).read().split())
for i in range(n-1):
    if 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:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,copy
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n = inp()
a = inpl()
a = [abs(x) for x in a]
a.sort()
a[0] = -a[0]
print(sum(a))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
negative = []
positive = []
cnt=0
for i in range(n):
  if a[i] < 0:
    cnt += 1
    negative.append(a[i])
  else:
    positive.append(a[i])
if cnt % 2 == 0:
  print(sum(map(lambda x: abs(x), a)))
elif cnt == len(a):
  a.sort()
  a.remove(a[-1])
  print(a[-1]+sum(map(lambda x: abs(x), a)))
else:
  negative.sort()
  positive.sort()
  if abs(negative[-1]) < positive[0]:
    a.remove(negative[-1])
    print(negative[-1]+sum(map(lambda x: abs(x), a)))
  else:
    a.remove(positive[0])
    print(-positive[0]+sum(map(lambda x: abs(x), a)))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
c = 0
m = 10*10
flag = 0
ans = 0
for i in a:
    if i == 0:
        flag = 1
    if i < 0:
        c += 1
    ans += abs(i)
    m = min(m,abs(i))
if c%2 == 0:
    flag = 1
if flag:
    print(ans)
else:
    print(ans-(2*m))

Submission status: Wrong Answer

Solution code:
def main():
    s = open(0).read()
    b = s.count('-')
    *c, = map(int, s[2:].replace('-', '').split())
    z = c.count(0)
    d = min(c)
    e = sum(c)

    if b % 2 == 0 or z:
        print(e)
    else:
        print(e - 2 * d)

    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    s = open(0).read()
    b = s.count('-')
    z = s.count(' 0 ')
    *c, = map(int, s[2:].replace('-', '').split())
    d = min(c)
    e = sum(c)

    if b % 2 == 0 or z:
        print(e)
    elif z:
        print(e - 2 * d)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    s = open(0).read()
    b = s.count('-')
    *c, = map(int, s[2:].replace('-', '').split())
    d = min(c)
    e = sum(c)
    
    if b % 2 == 0:
        print(e)
    else:
        print(e - 2 * d)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import numpy as np
from collections import deque
from collections import defaultdict
import heapq
import collections
import itertools
import bisect


mod = 10 ** 9 + 7
N = int(input())
A = np.array(list(map(int, input().split())))
ans = 0
# print(len(A < 0))
if (len(A[A < 0]) % 2) == 0:
    for a in A:
        ans += abs(a)
    print(ans)
else:
    abs_min = A[0]
    for a in A:
        ans += abs(a)  # aaa
        abs_min = min(abs_min, abs(a))
    print(ans-abs(abs_min)*2)

# print(A)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
i = 10**9
s = 0
z = 0
c = 0
for b in a:
  if b < 0:
    s += 1
  elif b == 0:
    z += 1
  if abs(b) < i:
    i = abs(b)
  c += abs(b)
print(s,z,c)
if z > 0 or s%2 == 0:
  print(c)
else:
  print(c-2*i)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
i = 10**9
s = 0
z = 0
c = 0
for b in a:
  if b < 0:
    s += 1
  elif b == 0:
    z += 1
    break
  if abs(b) < i:
    i = abs(b)
  c += abs(b)
if z > 0 or s%2 == 0:
  print(c)
else:
  print(c-2*i)

Submission status: Wrong Answer

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

for i in range(n):
    if (a[i] <= 0):
        num_minus += 1
    num_min = min(num_min, abs(a[i]))
    num_sum += abs(a[i])

if (num_minus % 2 == 0):
    print (num_sum)
else:
    print(num_sum-num_min*2)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import numpy as np
from collections import deque
from collections import defaultdict
import heapq
import collections
import itertools
import bisect


mod = 10 ** 9 + 7
N = int(input())
A = list(map(int, input().split()))

for i in range(N-2):
    if (A[i] * A[i + 1] < 0):  # 正負のとき
        if (A[i] < A[i+1]):
            A[i] *= -1
            A[i+1] *= -1
        elif (A[i] >= A[i + 1]):
            if (A[i] > A[i + 2]):
                continue
    elif ((A[i] <= 0 and A[i + 1] <= 0)):
        A[i] *= -1
        A[i + 1] *= -1
if (A[-2] * A[-1] < 0):
    if (-min(A[-2], A[-1]) > max(A[-2], A[-1])):
        A[-2] *= -1
        A[-1] *= -1
elif (A[-2] <= 0 and A[-1] <= 0):
    A[-2] *= -1
    A[-1] *= -1

# print(A)
print(sum(A))

Submission status: Wrong Answer

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

for i in range(N-1):
    abs_a = list(map(abs, A))
    min_a = min(abs_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:
        print(sum(abs_a) - min_a * 2) 
    else:
        print(sum(abs_a))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

# check
print(sum([a if a >= 0 else a * -1 for a in A]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
if 0 in a:
  print(sum(map(abs, a)))
  exit()
minus = len([i for i in range(n) if a[i]<0 ])
if minus % 2 == 0:
  print(sum(map(abs, a)))
else:
  a = sorted(a, key=lambda x: abs(x))
  print(sum(map(abs, a[1:]))-a[0])

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
if 0 in a:
  print(sum(map(abs, a)))
  exit()
minus = len([i for i in range(n) if a[i]<0 ])
if minus % 2 == 0:
  print(sum(map(abs, a)))
else:
  a = sorted(a, key=lambda x: abs(x))
  print(sum(map(abs, a[1:])))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
if 0 in a:
  print(sum(map(abs, a)))
  exit()
minus = len([i for i in range(n) if a[i]<0 ])
if minus % 2 == 0:
  print(print(sum(map(abs, a))))
else:
  a = sorted(a, key=lambda x: abs(x))
  print(sum(map(abs, a[1:])))

Submission status: Wrong Answer

Solution code:
import math
import time
from collections import defaultdict, deque
from sys import stdin, stdout
from bisect import bisect_left, bisect_right
n=int(input())
a=list(map(int,stdin.readline().split()))
for i in range(n-1):
    if(a[i]<a[i+1] and abs(a[i])>abs(a[i+1])):
        a[i]*=-1
        a[i+1]*=-1
    elif(a[i]>a[i+1] and abs(a[i])<abs(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()))
if 0 in A:
  A = list(map(abs,A))
  print(sum(A))
else:
  count=0
  min_A =100000000000000000
  for i in range(N):
    if A[i]<0:
      count=count+1
    if abs(A[i])<min_A:
      min_A = abs(A[i])
  A = list(map(abs,A))
  print(sum(A)-min_A*2)

Submission status: Wrong Answer

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

B = [abs(i) for i in A]
minus = [int(i<0) for i in A]
if sum(minus)%2 == 0:
    print(sum(B))
else:
    print(sum(B))
    print(sum(B)-2*min(B))

Submission status: Wrong Answer

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

B = [abs(i) for i in A]
if N%2 == 0:
    print(sum(B))
else:
    print(sum(B)-min(B))

Submission status: Wrong Answer

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

#全部の数がプラスになってくれればうれしい
#何かマイナスが残るならそれを最小に抑えたい
#－＋－ or＋－＋だと、いずれかがマイナスにならざるを得ない
for i in range(N-1):
  one=A[i]
  two=A[i+1]
  if A[i]*A[i+1]>=0:
    A[i]=abs(A[i])
    A[i+1]=abs(A[i+1])
  else:
    if abs(A[i])>abs(A[i+1]):
      A[i]=abs(A[i])
      A[i+1]=-1*abs(A[i+1])
    else:
      A[i]=-1*abs(A[i])
      A[i+1]=abs(A[i+1])
print(sum(A))

Submission status: Wrong Answer

Solution code:
DEBUG = False

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

index_max_abs_val = N-1
flag_zero = not A[N-1]
for i in range(N-1):
    if not A[i]:
        flag_zero = True
    if index_max_abs_val > abs(A[i]):
        index_max_abs_val = i
    if A[i] < 0:
        A[i] *= -1
        A[i+1] *= -1
    if DEBUG:
        print("A: {}".format(A))
        print("index_max_abs_val: {}, flag_zero: {}".format(index_max_abs_val, flag_zero))

if flag_zero:
    A[N-1] = abs(A[N-1])
elif A[N-1] < 0:
    A[N-1] *= -1
    A[index_max_abs_val] *= -1

if DEBUG:
    print("A: {}".format(A))
print(sum(A))

Submission status: Wrong Answer

Solution code:
DEBUG = False

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

index_max_abs_val = N-1
flag_zero = not A[N-1]
for i in range(N-1):
    if not A[i]:
        flag_zero = True
    if index_max_abs_val < abs(A[i]):
        index_max_abs_val = i
    if A[i] < 0:
        A[i] *= -1
        A[i+1] *= -1
    if DEBUG:
        print("A: {}".format(A))
        print("index_max_abs_val: {}, flag_zero: {}".format(index_max_abs_val, flag_zero))

if flag_zero:
    A[N-1] = abs(A[N-1])
elif A[N-1] < 0:
    A[N-1] *= -1
    A[index_max_abs_val] *= -1

if DEBUG:
    print("A: {}".format(A))
print(sum(A))

Submission status: Wrong Answer

Solution code:
N=int(input())
A = list(map(int, input().split()))
ans=0
hoge=1
tmp=1

if abs(A[0]) < abs(A[N-1]):
    A.reverse()

for i in range(N-1):
    ans+=abs(A[i])
    if A[i]==0:
        hoge=-1
    else:
        tmp = int(tmp*A[i]/abs(A[i]))
    
if hoge==-1:
    ans+=abs(A[N-1])
else:
    ans+=tmp*A[N-1]

print(ans)

Submission status: Wrong Answer

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

def main():
    N = int(readline())
    A = list(map(int, readline().rstrip().split()))
    for i in range(N-2):
        if A[i] < 0:
            A[i] *= -1
            A[i+1] *= -1
    
    if A[-2]*-1 + A[-1]*-1 > A[-2] + A[-1]:
        A[-2] *= -1
        A[-1] *= -1
    
    print(sum(A))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
An = list(map(int, input().split()))
print(sum(An) - min(An))

Submission status: Wrong Answer

Solution code:
import numpy as np

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

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

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

# import sys

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

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

# print(neg, posi, zero)
# S := 隣接の数の絶対値の差の絶対値 abs( abs(A[i])-abs(A[i+1]) )

S = abs(abs(A[1:]) - abs(A[:-1]))
# print(S)

# 負の数が奇数個のときは全部正の数にできない。abs( abs(A[i])-abs(A[i+1]) )が最大のもの + 残りの絶対値の和
# 負の数が偶数個の時 or ０があるときはできる。
if zero > 0 or (neg % 2) == 0:
    ans = sum(abs(A))
else:
    idx = np.argmax(S)
    res = abs(abs(A[idx]) - abs(A[idx + 1]))
    ans = sum(abs(A)) - sum(abs(A[idx : idx + 2])) + res
    # print(S, idx, A[idx], res)

print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())

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

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

ans = 0

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

print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().split()))
B=A[:]
count=0
zero=0
for i in range(N):
  if(A[i]<0):
    count+=1
  elif(A[i]==0):
    zero+=1
for i in range(N):
  B[i]=abs(A[i])
if(zero==0 and count%2==1):
  print(sum(B)-2*min(B[0],B[-1]))
else:
  print(sum(B))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
for i in range(n-1):
    le = a[i]+a[i+1]
    if le<=0:
        if (a[i]<0 and a[i+1]<0)or (a[i]<0 and a[i+1]>0):
            a[i]*= -1
            a[i+1]*= -1
        else:
            if i+2<n:
                ri = a[i+1]+a[i+2]
                if le>=ri:
                    a[i+1]*= -1
                    a[i+2]*= -1
                else:
                    a[i]*= -1
                    a[i+1]*= -1
            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()))

for i in range(n-2):
    if abs(abs(a[i]) - abs(a[i+1])) >= abs(abs(a[i+1]) - abs(a[i+2])):
        if a[i]+a[i+1] < (-1)*(a[i]+a[i+1]):
            a[i]*=(-1)
            a[i+1]*=(-1)

if a[-1]< 0 or a[-2] < 0:
    if a[-1]+a[-2] < (-1)*(a[-1]+a[-2]):
        a[-1]*=(-1)
        a[-2]*=(-1)

ans=sum(a)

print(ans)

Submission status: Wrong Answer

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

for i in range(N - 1):
    if i < N - 2:
        if A[i] < 0:
            A[i] *= -1
            A[i + 1] *= -1
    else:
        if -1 * (A[i] + A[i + 1]) > 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()))
for i in range(n-2):
    if a[i]<0:
        a[i]*=-1
        a[i+1]*=-1
if a[-1]+a[-2]<0:
    a[-1]*=-1
    a[-2]*=-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:
        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 abs(A[0]) < abs(A[-1]):
    A = A[::-1]
B = list(map(abs, A))
B.sort()
C = [x for x in A if x < 0]
n = len(C)
if (N % 2 == 0 and n % 2 == 1) or (N % 2 == 1 and n % 2 == 0):
    print(sum(B))
else:
    print(sum(B) - 2*B[0])

Submission status: Wrong Answer

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


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

elif ind%2==0:

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

Submission status: Wrong Answer

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


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

elif ind%2==0 or a[ind]==0:

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from collections import deque
from heapq import heapify,heappop,heappush,heappushpop
from copy import copy,deepcopy
from itertools import permutations,combinations
from collections import defaultdict,Counter
from bisect import bisect_left,bisect_right
# from math import gcd,ceil,floor,factorial
from fractions import gcd
from functools import reduce
from pprint import pprint

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

def mylistinput(n):
    return [ list(myinput()) for _ in range(n) ]

def mycol(data,col):
    return [ row[col] for row in data ]

def mysort(data,col,reverse_flag):
    data.sort(key=lambda x:x[col],reverse=reverse_flag)
    return data

def mymax(data):
    M = -1*float("inf")
    for i in range(len(data)):
        m = max(data[i])
        M = max(M,m)
    return M

def mymin(data):
    m = float("inf")
    for i in range(len(data)):
        M = min(data[i])
        m = min(m,M)
    return m

def myoutput(ls,space=True):
    if space:
        if len(ls)==0:
            print(" ")
        elif type(ls[0])==str:
            print(" ".join(ls))
        elif type(ls[0])==int:
            print(" ".join(map(str,ls)))
        else:
            print("Output Error")
    else:
        if len(ls)==0:
            print("")
        elif type(ls[0])==str:
            print("".join(ls))
        elif type(ls[0])==int:
            print("".join(map(str,ls)))
        else:
            print("Output Error")

n = int(input())
a = list(myinput())
b = copy(a)
c = a[::-1]

ans1 = sum(a)

i = 0
while i<(n-1):
    s = b[i]
    t = b[i+1]
    if s>=0 and t>=0:
        i += 2
    elif s>=0 and t<=0:
        i += 1
    elif s<=0 and t>=0:
        b[i] = -1*s
        b[i+1] = -1*t
        i += 1
    elif s<=0 and t<=0:
        b[i] = -1*s
        b[i+1] = -1*t
        i += 2
ans2 = sum(b)

# i = 0
# while i<(n-1):
#     s = c[i]
#     t = c[i+1]
#     if s>=0 and t>=0:
#         i += 2
#     elif s>=0 and t<=0:
#         i += 1
#     elif s<=0 and t>=0:
#         c[i] = -1*s
#         c[i+1] = -1*t
#         i += 1
#     elif s<=0 and t<=0:
#         c[i] = -1*s
#         c[i+1] = -1*t
#         i += 2
# ans3 = sum(c)

# print(a)
# print(b)
# print(c)
# print(ans1,ans2,ans3)
ans = max(ans1,ans2)
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
from heapq import heapify,heappop,heappush,heappushpop
from copy import copy,deepcopy
from itertools import permutations,combinations
from collections import defaultdict,Counter
from bisect import bisect_left,bisect_right
# from math import gcd,ceil,floor,factorial
from fractions import gcd
from functools import reduce
from pprint import pprint

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

def mylistinput(n):
    return [ list(myinput()) for _ in range(n) ]

def mycol(data,col):
    return [ row[col] for row in data ]

def mysort(data,col,reverse_flag):
    data.sort(key=lambda x:x[col],reverse=reverse_flag)
    return data

def mymax(data):
    M = -1*float("inf")
    for i in range(len(data)):
        m = max(data[i])
        M = max(M,m)
    return M

def mymin(data):
    m = float("inf")
    for i in range(len(data)):
        M = min(data[i])
        m = min(m,M)
    return m

def myoutput(ls,space=True):
    if space:
        if len(ls)==0:
            print(" ")
        elif type(ls[0])==str:
            print(" ".join(ls))
        elif type(ls[0])==int:
            print(" ".join(map(str,ls)))
        else:
            print("Output Error")
    else:
        if len(ls)==0:
            print("")
        elif type(ls[0])==str:
            print("".join(ls))
        elif type(ls[0])==int:
            print("".join(map(str,ls)))
        else:
            print("Output Error")

n = int(input())
a = list(myinput())
b = copy(a)
c = a[::-1]

ans1 = sum(a)

i = 0
while i<(n-1):
    s = b[i]
    t = b[i+1]
    if s>=0 and t>=0:
        i += 2
    elif s>=0 and t<=0:
        i += 1
    elif s<=0 and t>=0:
        b[i] = -1*s
        b[i+1] = -1*t
        i += 1
    elif s<=0 and t<=0:
        b[i] = -1*s
        b[i+1] = -1*t
        i += 2
ans2 = sum(b)

i = 0
while i<(n-1):
    s = c[i]
    t = c[i+1]
    if s>=0 and t>=0:
        i += 2
    elif s>=0 and t<=0:
        i += 1
    elif s<=0 and t>=0:
        c[i] = -1*s
        c[i+1] = -1*t
        i += 1
    elif s<=0 and t<=0:
        c[i] = -1*s
        c[i+1] = -1*t
        i += 2
ans3 = sum(c)

# print(a)
# print(b)
# print(c)
# print(ans1,ans2,ans3)
ans = max(ans1,ans2,ans3)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

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

for i in range(len(A)-1):
  before = A[i] + A[i+1]
  after = -1*A[i] + (-1)*A[i+1]

  if A[i] >= 0 and i != len(A)-1:
    continue

  if A[i] < 0 and i != len(A)-1:
    A[i] *= -1
    A[i+1] *= -1 
    continue

  if after >= before:
    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()))

for i in range(len(A)-1):
  before = A[i] + A[i+1]
  after = -1*A[i] + (-1)*A[i+1]

  if A[i] >= 0:
    continue

  if A[i] < 0 and i != len(A)-1:
    A[i] *= -1
    A[i+1] *= -1 
    continue

  if after >= before:
    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):
    a[i] = abs(a[i])
    ans += a[i]
if 0 in a:
    print(ans)
else:
    print(ans - 2*min(a))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

sum=0
min=10**9
minus=0
for i in a:
  if i>=0:
    sum=sum+i
    if min>i:
      min=i
  else:
    minus=minus+1
    sum=sum-i
    if min>-i:
      min=-i
print(sum if minus%2==0 else sum-min)

Submission status: Wrong Answer

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

sum=0
min=10**9
for i in a:
  if i>=0:
    sum=sum+i
    if min>i:
      min=i
  else:
    sum=sum-i
    if min>-i:
      min=-i
print(sum if len(a)%2==0 else sum-min)

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split()))
    Ap = [i for i in A if i >= 0]
    An = [i for i in A if i < 0]
    if len(An) % 2 == 0:
      return sum(Ap) - sum(An)
    return sum(Ap) - sum(sorted(An)[:-1])
print(main())

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split()))
    Ap = [i for i in A if i >= 0]
    An = [i for i in A if i < 0]
    if len(An) % 2 == 0:
      return sum(Ap) - sum(An)
    return sum(Ap) - sum(sorted(An)[1:])
print(main())

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split()))
    negatives = 0
    zeros = 0
    for a in A:
        if a < 0:
            negatives += 1
        if a == zeros:
            zeros += 1
    ans = sum(map(abs, A))
    if negatives % 2 == 0 or zeros != 0:
        print(ans)
        return

    a = min(abs(A[0]), abs(A[1]))
    b = min(abs(A[-2]), abs(A[-1]))
    ans -= 2*min(a, b)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split()))
    for i in range(1, N-1):
        a0 = A[i - 1]
        a1 = A[i]
        if a0 < 0:
            A[i - 1] *= -1
            A[i] *= -1
    a0 = A[-2]
    a1 = A[-1]
    if a0 < 0 and a1 < 0:
        A[-2] *= -1
        A[-1] *= -1
    elif a0 < 0:
        if abs(a0) > a1:
            A[-2] *= -1
            A[-1] *= -1
    elif a1 < 0:
        if abs(a1) > a0:
            A[-2] *= -1
            A[-1] *= -1
    print(sum(A))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

mini=float("inf")
summ=0
for i in range(N):
  mini = min(mini,abs(A[i]))
  summ += abs(A[i])
  
print(summ-2*mini)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
for i in a:
    i=abs(i)
print(sum(a))

Submission status: Wrong Answer

Solution code:
# python3.4.2用
import math
import fractions
import bisect
import collections
import itertools
import heapq
import string
import sys
import copy
from decimal import *
from collections import deque
sys.setrecursionlimit(10**7)
MOD = 10**9+7
INF = float('inf') #無限大
def gcd(a,b):return fractions.gcd(a,b) #最大公約数
def lcm(a,b):return (a*b) // fractions.gcd(a,b) #最小公倍数
def iin(): return int(sys.stdin.readline()) #整数読み込み
def ifn(): return float(sys.stdin.readline()) #浮動小数点読み込み
def isn(): return sys.stdin.readline().split() #文字列読み込み
def imn(): return map(int, sys.stdin.readline().split()) #整数map取得
def imnn(): return map(lambda x:int(x)-1, sys.stdin.readline().split()) #整数-1map取得
def fmn(): return map(float, sys.stdin.readline().split()) #浮動小数点map取得
def iln(): return list(map(int, sys.stdin.readline().split())) #整数リスト取得
def iln_s(): return sorted(iln()) # 昇順の整数リスト取得
def iln_r(): return sorted(iln(), reverse=True) # 降順の整数リスト取得
def fln(): return list(map(float, sys.stdin.readline().split())) # 浮動小数点リスト取得
def join(l, s=''): return s.join(l) #リストを文字列に変換
def perm(l, n): return itertools.permutations(l, n) # 順列取得
def perm_count(n, r): return math.factorial(n) // math.factorial(n-r) # 順列の総数
def comb(l, n): return itertools.combinations(l, n) # 組み合わせ取得
def comb_count(n, r): return math.factorial(n) // (math.factorial(n-r) * math.factorial(r)) #組み合わせの総数
def two_distance(a, b, c, d): return ((c-a)**2 + (d-b)**2)**.5 # 2点間の距離
def m_add(a,b): return (a+b) % MOD
def lprint(l): print(*l, sep='\n')
def sieves_of_e(n):
    is_prime = [True] * (n+1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i * 2, n+1, i): is_prime[j] = False
    return is_prime

N = iin()
A = iln()

dp = [[0 for _ in range(2)] for _ in range(N+1)]

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

# 最後の数字は操作できないため、dp[N][0]を出力する
print(dp[N][0])

Submission status: Wrong Answer

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