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()))
sum = 0
neg = 0
neg_sum = 0
min = float('inf')
max = -float('inf')
for i in range(N):
	sum += A[i]
	if A[i] < 0:
		neg += 1
		neg_sum += A[i]
		if A[i] > max:
			max = A[i]
	else:
		if A[i] < min:
			min = A[i]
if neg % 2 == 1:
	if max + min < 0:
		neg_sum += min
	else:
		neg_sum -= max
print(sum - neg_sum * 2)

Submission status: Accepted

Solution code:
N = int(input())
x = [int(x) for x in input().split()]
dp = [[0 for _ in range(2)] for _ in range(N+1)]
dp[0][0] = 0
dp[0][1] = -10**9
for i in range(N):
  dp[i+1][0] = max(dp[i][0]+x[i],dp[i][1]-x[i])
  dp[i+1][1] = max(dp[i][0]-x[i],dp[i][1]+x[i])
print(dp[N][0])

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
tot = 0
for i in range(N):
    tot += abs(A[i])
if A.count(0)>0:
    print(tot)
else:
    cur = 0
    cnt = 0
    for i in range(N):
        if A[i]>0:
            c = i-cur
            if c%2==1:
                cnt += 1
            cur =i+1
    if A[-1]<0:
        c = N-cur
        if c%2==1:
            cnt += 1
    if cnt%2==0:
        print(tot)
    else:
        cmin = 10**10
        for i in range(N):
            cmin = min(cmin,abs(A[i]))
        print(tot-2*cmin)

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict
import sys,bisect,math,itertools,string,queue
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

n = inp()
a = inpl()
m_cnt = 0
for i in range(n):
    if a[i] < 0:
        m_cnt += 1
        a[i] = abs(a[i])
a.sort()
if m_cnt%2:
    print(sum(a) - a[0]*2)
else:
    print(sum(a))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

n=int(input())
A=list(map(int,input().split()))
count=0
a_n=np.array(A)
abs_a=np.abs(a_n)
sum_a=np.sum(abs_a)
for x in A:
    if x<0:
        count+=1
if count%2==0:
    print(sum_a)
elif count%2==1:
    sum_b=sum_a-2*np.min(abs_a)
    print(sum_b)

Submission status: Accepted

Solution code:
N=int(input())
l=list(map(int,input().split()))
l2=sorted([num for num in l if num<0])
l3=sorted([num for num in l if num>=0])
if len(l2)%2==0:
  ans=sum([abs(n) for n in l])

elif l3!=[]:
  if abs(l2[-1]) < l3[0]:
    x=l2.pop()
  else:
    x=l3.pop(0)
  ans=sum([abs(n) for n in l2])+sum(l3)-abs(x)
else:
  x=l2.pop()
  ans=sum([abs(n) for n in l2])-abs(x)
print(ans)

Submission status: Accepted

Solution code:
N,*A=map(int,open(0).read().split())
x,y=0,-1e10
for a in A:x,y=max(x+a,y-a),max(x-a,y+a)
print(x)

Submission status: Accepted

Solution code:
N, *A = map(int, open(0).read().split())

x, y = 0, -10 ** 10
for a in A:
    x, y = max(x + a, y - a), max(x - a, y + a)
print(x)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
mc = 0
s = 0
m = float('inf')
for i in a:
  if i < 0:
    mc += 1
  s += abs(i)
  m = min(m, abs(i))
if mc % 2 == 0:
  print(s)
else:
  print(s -2 * m)

Submission status: Accepted

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


dp =[[0,0] for _ in range(len(A)+1) ]

dp[0][0] = A[0]
dp[0][1] = -A[0]

max_value= sum(A)
for y in range(len(A)-1):
    dp[y+1][0] = max(dp[y][0]+A[y+1], dp[y][1]-A[y+1])
    dp[y+1][1] = max(dp[y][1]+A[y+1], dp[y][0]-A[y+1]) 

print(dp[N-1][0])

Submission status: Accepted

Solution code:
def slove():
    import sys
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    a = list(map(int, input().rstrip('\n').split()))
    cnt = 0
    for i in range(len(a)):
        if a[i] < 0:
            cnt += 1
            a[i] *= -1
    a.sort()
    if cnt % 2 == 0:
        print(sum(a))
    else:
        print(sum(a) - a[0] * 2)


if __name__ == '__main__':
    slove()

Submission status: Accepted

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

ans = 0
tmp = float('inf')
cnt = 0
for a in A:
    if a >= 0:
        ans += a
        tmp = min(tmp, a)
    else:
        ans -= a
        tmp = min(tmp, -a)
        cnt += 1
        
if cnt % 2 == 1:
    print(ans - tmp*2)
else:
    print(ans)

Submission status: Accepted

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

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

print(dp[n][0])

Submission status: Accepted

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

odd = 0

for a in arr:
    if a < 0:
        odd += 1

if odd %2 == 0:
    print(np.sum(np.abs(arr)))
else:
    # print(np.sum(np.abs(arr)))
    # print(np.min(np.abs(arr)))
    print(np.sum(np.abs(arr))-2*np.min(np.abs(arr)))

Submission status: Accepted

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

neg = 0
for i in range(N):
    if A[i] < 0:
        neg += 1
        A[i] *= -1
A.sort()
ans = 0
if neg % 2 == 1:
    A[0] *= -1
for i in A:
    ans += i
print(ans)

Submission status: Accepted

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

cnt = 0
min = 10**9
total = 0

for i in A:
    total += abs(i)
    if abs(i) < min:
            min = abs(i)
    if i < 0:
        cnt += 1
if cnt%2 == 1:
    print(total - 2*min)
else:
    print(total)

Submission status: Accepted

Solution code:
n = int(input())
a = []
mc = 0

for x in input().split():
    i = int(x)
    if i < 0:
        mc += 1
        i = -i
    a.append(i) 


s = sum(a)
print(s if mc % 2 == 0 else s - min(a)*2)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

i = 0
while i < n:
    dp[i+1][0] = max(dp[i][0] + lst[i], dp[i][1] - lst[i])
    dp[i+1][1] = max(dp[i][0] - lst[i], dp[i][1] + lst[i])
    i += 1

print(dp[n][0])

Submission status: Accepted

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

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

Submission status: Accepted

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

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

dp[0][0]=0
dp[0][1]=-3*10**9

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

Submission status: Accepted

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

neg_count = 0
neg_candidate = abs(A[0])
for a in A:
    if a <= 0:
        neg_count += 1
    neg_candidate = min(neg_candidate, abs(a))

B = list(map(abs, A))

if neg_count % 2 == 0:
    print(sum(B))

else:
    print(sum(B)-2*neg_candidate)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = [int(a) for a in input().split()]
    dp = [[0, 0] for _ in range(N)]
    dp[1][0] = A[0]+A[1]
    dp[1][1] = -(A[0]+A[1])
    for i in range(2, N):
        dp[i][0] = max(dp[i-1][0] + A[i], dp[i-1][1] + A[i])
        dp[i][1] = max(dp[i-1][0]-2*A[i-1]-A[i], dp[i-1][1]+2*A[i-1]-A[i])
    # print(A)
    # print(dp)
    return max(dp[N-1])


if __name__ == '__main__':
    print(main())

Submission status: Accepted

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

Submission status: Accepted

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

_minus=0
for i in range(n):
    if a[i]<0:
        _minus+=1
abs_a = [abs(g) for g in a]

if _minus%2==0:
    print(sum(abs_a))
else:
    print(sum(abs_a) - 2*min(abs_a))

Submission status: Accepted

Solution code:
n = int(input())
an = [int(x) for x in input().split()]
abs_an = [abs(x) for x in an]

if len([x for x in an if x < 0])%2 == 0:
    print(sum(abs_an))
else:
    print(sum(abs_an)-2*min(abs_an))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
#import math #python 3.5~
import fractions #fractions.gcd() python~3.4
sys.setrecursionlimit(1000000)

def main(): 

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def main():
    import sys
    from fractions import gcd
    input = sys.stdin.readline
    N = int(input())
    A = list(map(int, input().split()))
    tmp = float("inf")
    S = 0
    count = 0
    for i in A:
        if tmp > abs(i):
            tmp = abs(i)
        if abs(i) != i:
            count += 1
        S += abs(i)
    if count%2 == 0:
        print(S)
    else:
        print(S-2*tmp)
    
main()

Submission status: Accepted

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

Submission status: Accepted

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

alist=sorted(alist)
ng=len(alist)
if alist[0]>=0:
    print(sum(alist))
else:
    now=0
    switch=0
    while now<ng and switch==0:
        if alist[now]<0:
            now+=1
        else:
            switch=1
    if now%2==1:
        hidari=now-1
        wa=0
        for al in alist:
            wa+=abs(al)
        if ng==hidari+1:
            wa-=abs(alist[hidari]*2)
        else:
            wa-=min(abs(alist[hidari]), abs(alist[hidari+1]))*2
        print(wa)
    else:
        hidari=now
        wa=0
        for al in alist:
            wa+=abs(al)
        print(wa)

Submission status: Accepted

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

import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline

#n,x = [int(i) for i in readline().split()]
n = int(input())
a = [int(i) for i in readline().split()]

neg = len([i for i in a if i < 0])
if neg%2 == 0:
    ans = sum(abs(i) for i in a)
else:
    ans = sum(abs(i) for i in a) - 2*min(abs(i) for i in a)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
absA = sorted([abs(A[i]) for i in range(N)], reverse = True)
minuscount = 0
for i in range(N):
  if A[i] < 0:
    minuscount += 1
if minuscount % 2 == 0:
  print(sum(absA))
else:
  print(sum(absA[: -1]) - absA[-1])

Submission status: Accepted

Solution code:
import sys
import numpy as np
input = sys.stdin.readline

def main():
	N = int(input())
	A = list(map(int,input().split()))
	A.sort()
	count = 0
	m = 10**10
	if 0 in A:
		A = np.array(A)
		print(sum(abs(A)))
	else:
		for i in range(N):
			if A[i]<0:
				count += 1
			m = min(m,abs(A[i]))
		A = np.array(A)
		if count%2 == 0:
			print(sum(abs(A)))
		else:
			print(sum(abs(A))-2*m)

if __name__ == "__main__":
	main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

m= [0]*n
d = 0
for i in range(n):
  m[i] = abs(l[i])
  if l[i] < 0:
    d += 1

if d%2 == 0:
  print(sum(m))
else:
  m.sort()
  print(sum(m)-2*m[0])

Submission status: Accepted

Solution code:
inf = 10**9
n = int(input())
lst = list(map(int,input().split()))
cnt = 0
m = inf
total = 0
for i in range(n):
    if lst[i] < 0:
        cnt += 1
    if abs(lst[i]) < m:
        m = abs(lst[i])
    total += abs(lst[i])
if cnt%2==0:
    print(total)
else:
    print(total - 2*m)

Submission status: Accepted

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

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

dp[0][0]=a[0]
dp[0][1]=-a[0]

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np


def solve(a_list):
    negative_flag = False
    for a in a_list:
        if a < 0:
            negative_flag = not negative_flag

    # time O(N log(N))
    sorted_list = sorted(np.abs(a_list), key=abs)
    if negative_flag:
        sorted_list[0] = -sorted_list[0]
    return sum(sorted_list)


def main():
    n = int(input())
    a_list = list(map(int, input().split()))

    ans = solve(a_list)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

dp = [[0, -100000000000] for i in range(n+1)]

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

Submission status: Accepted

Solution code:
n = int(input())
a = [int(x) for x in input().split()]
count = len([x for x in filter(lambda n: n<0, a)])
b = [x for x in map(abs, a)]

p = min(b)
q = sum(b)
if count % 2 != 0:
    ans = q - 2 * p
else:
    ans = q
print(ans)

Submission status: Accepted

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

minabs = min(list(map(abs,Alist)))
sumabs = sum(list(map(abs,Alist)))

c_ne = 0
c_ze = 0
for i in range(N):
    if Alist[ i ] < 0:
        c_ne += 1
    if Alist[ i ] == 0:
        c_ze += 1

if c_ne % 2 == 0:
    print( sumabs )
elif c_ze > 0:
    print( sumabs )
else:
    print( sumabs - minabs * 2)

Submission status: Accepted

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

dp = [[[0] for j in range(2)] for i in range(N-1)]
dp[0][0] = sum(A)
A_ = A.copy()
A_[0] = A_[0]*-1
A_[1] = A_[1]*-1
dp[0][1] = sum(A_)

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

count=0
total=0
min_=10**9
for i in range(n):
  total+=abs(a[i])
  if a[i]<0:
    count+=1
  if abs(a[i])<min_:
    min_=abs(a[i])
    
if count%2==0:
  print(total)
else:
  print(total-2*min_)

Submission status: Accepted

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


def solve(N: int, A: "List[int]"):
    B = [abs(a) for a  in A]
    m = len([a for a in A if a < 0])
    if m % 2 == 0:
        print(sum(B))
    else:
        print(sum(B) - 2*min(B))
    return


# 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()))

A_abs = [abs(i) for i in A]
A_abs_sum = sum(A_abs)
if 0 in A or len([i for i in A if i < 0]) % 2 == 0:
  print(A_abs_sum)
else:
  print(A_abs_sum - min(A_abs) * 2)

Submission status: Accepted

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

a_abs = [abs(i) for i in a]
a_abs_sum = sum(a_abs)
if 0 in a or len([i for i in a if i < 0]) % 2 == 0:
  print(a_abs_sum)
else:
  print(a_abs_sum - min(a_abs) * 2)

Submission status: Accepted

Solution code:
# ABC125D - Flipping Signs
def main():
    # neg % 2 == 0 -> all negatives disappear
    # else -> move all neg to the smallest one (abs(i))
    # if 0 in A -> move all neg there but min will be 0
    N = int(input())
    A = tuple(map(int, input().split()))
    mi = abs(min(A, key=abs))
    abs_sum = sum(abs(i) for i in A)
    neg = sum(i < 0 for i in A)
    ans = abs_sum if neg % 2 == 0 else abs_sum - mi * 2
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ABC125D - Flipping Signs
def main():
    # neg % 2 == 0 -> all negatives disappear
    # 0 in A -> move all neg there
    # else -> move all neg to the smallest one (abs(i))
    N = int(input())
    A = tuple(map(int, input().split()))
    mi = abs(min(A, key=abs))
    abs_sum = sum(abs(i) for i in A)
    neg = sum(i < 0 for i in A)
    if 0 in A or neg % 2 == 0:
        print(abs_sum)
    else:
        print(abs_sum - mi * 2)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ABC125D - Flipping Signs
def main():
    # neg % 2 == 0 -> all negatives disappear
    # 0 in A -> move all neg there
    # else -> move all neg to the smallest one (abs(i))
    N = int(input())
    A = tuple(map(int, input().split()))
    mi = min(abs(i) for i in A)
    abs_sum = sum(abs(i) for i in A)
    neg = sum(i < 0 for i in A)
    if 0 in A or neg % 2 == 0:
        print(abs_sum)
    else:
        print(abs_sum - mi * 2)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ABC125D - Flipping Signs
def main():
    N = int(input())
    A = tuple(map(int, input().split()))
    mi = min(abs(i) for i in A)
    abs_sum = sum(abs(i) for i in A)
    neg = sum(i < 0 for i in A)
    if 0 in A or neg % 2 == 0:
        print(abs_sum)
    else:
        print(abs_sum - mi * 2)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import itertools

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

n=getint()
a=getints()
inf = 1 << 60

cache=[[-inf] * 2 for _ in range(n)]

def solve(pos, minus):
    if pos == n - 1:
        return -a[pos] if minus else a[pos]
    res = cache[pos][minus]
    if res > -inf:
        return res
    now = a[pos] if minus == 0 else -a[pos]
    res1 =  now + solve(pos + 1, 0)
    res2 = -now + solve(pos + 1, 1)
    res  = max(res1, res2)
    cache[pos][minus] = res
    return res

for i in range(n-1,-1,-1):
    solve(i, 0)
    solve(i, 1)

res = solve(0, 0)
print(res)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
ans = sum(map(abs,A))
flag = False
sign = 0
mi = 10 ** 9
for a in A:
    if a == 0:
        flag = True
        break
    if a < 0:
        sign += 1
    if abs(a) < mi: mi = abs(a)
if flag:
    print(ans)
else:
    if sign % 2 == 0:
        print(ans)
    else:
        print(ans-2*mi)

Submission status: Accepted

Solution code:
import fractions

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

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

if(count == 0 and flag==0):
    flag = 1

abss = [abs(a[i]) for i in range(n)]

if(flag==1):
    print(sum(abss))
else:
    print(sum(abss) - 2*min(abss))

Submission status: Accepted

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

count_0 = 0
count_minus = 0

abs_a = a

for i in range(n):
    if a[i] < 0:
        count_minus += 1
        abs_a[i] = -1*a[i]
    elif a[i] == 0:
        count_0 += 1

abs_a.sort()
a_sum = sum(abs_a)

if count_minus % 2 == 1 and count_0 == 0:
    print(a_sum - abs_a[0]*2)
else:
    print(a_sum)

Submission status: Accepted

Solution code:
from array import array

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

i_of_minimum_abs = 0
minimum_abs = 10**9
Aarray = array('l', A)

for i in range(N):
    a = Aarray[i]
    if abs(a) < minimum_abs:
        minimum_abs = min(minimum_abs, abs(a))
        i_of_minimum_abs = i

for i in range(i_of_minimum_abs):
    if Aarray[i] < 0:
        Aarray[i] = -Aarray[i]
        Aarray[i+1] = -Aarray[i+1]
        
for i in reversed(list(range(i_of_minimum_abs+1,N))):
    if Aarray[i] < 0:
        Aarray[i] = -Aarray[i]
        Aarray[i-1] = -Aarray[i-1]

print(sum(Aarray))

Submission status: Accepted

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

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

b.sort()
ans = sum(b)
if m % 2 == 1:
    ans -= b[0] * 2

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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

minum = 0
minabs = float("inf")
allsum = 0

for i in A:

    if i < 0:
        minum += 1

    allsum += abs(i)
    minabs = min(minabs,abs(i))

if minum % 2 == 0:
    print (allsum)

else:
    print (allsum - 2 * minabs)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.sort()
b=[]
for k in range(n):
        b.append(abs(a[k]))
b.sort()
for i in range(n):
        if a[i]==0:
                print(sum(b))
                break
      
else:
        for j in range(n):
                if a[j]>0:
                        if j%2==0:
                                print(sum(b))
                                break
                        elif j%2!=0:
                                print(sum(b)-b[0]-b[0])
                                break
        else:
                if n%2==0:
                        print(sum(b))
                else:
                        print(sum(b)-b[0]-b[0])

Submission status: Accepted

Solution code:
#### import ####
import sys
import math
from collections import defaultdict

#### 設定 ####
sys.setrecursionlimit(10**7)
def input():
  return sys.stdin.readline()[:-1]

#### 定数 ####
mod = 10**9 + 7

#### 読み込み ####
def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
  read_all = [tuple(map(int, input().split())) for _ in range(N)]
  return map(list,zip(*read_all))

#################

N = I()

A = III()

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

if num%2==0:
  print(sum(map(abs, A)))
else:
  print(sum(map(abs, A))-2*min(map(abs, A)))

Submission status: Accepted

Solution code:
import math
import copy

n = int(input())
#n, k = [int(i) for i in input().split()]
#table = [[int(i) for i in input().split()] for j in range(n)]
#table = [list(input()) for n in range(h)]
num = [int(i) for i in input().split()]
#num = [int(i) for i in range(n)]
#s = [input() for i in range(n)]
#s = input()
#list = list(s)
#dp = [[0 for _ in range(13)] for _ in range(13)]

dp = [[0 for _ in range(2)] for _ in range(n+1)]
dp[0][1]= -(10**9)
for i in range(n) :
    dp[i+1][0] = max(dp[i][0] + num[i], dp[i][1] - num[i])
    dp[i+1][1] = max(dp[i][0] - num[i], dp[i][1] + num[i])

print(dp[n][0])

Submission status: Accepted

Solution code:
N = int(input())
As = list(map(int, input().split()))
count = len(list(filter(lambda A: A < 0, As)))
As2 = list(map(abs, As))
As2.sort()
#print(count, As2)
if count%2 == 0:
  r = sum(As2)
else:
  r = sum(As2)-2*As2[0]
print(r)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    aa = list(map(int, input().split()))

    pos = [0] * n
    neg = [0] * n

    pos[0] = aa[0]
    neg[0] = -aa[0]

    for i, a in enumerate(aa[1:-1], start=1):
        pos[i] = max(pos[i - 1] + a, neg[i - 1] - a)
        neg[i] = max(pos[i - 1] - a, neg[i - 1] + a)

    pos[n - 1] = pos[n - 2] + aa[n - 1]
    neg[n - 1] = neg[n - 2] - aa[n - 1]

    score = max(pos[n - 1], neg[n - 1])

    print(score)


main()

Submission status: Accepted

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

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

abs_ls = [abs(a) for a in a_ls]
if any([a < 0 for a in a_ls]):
    print(sum(abs_ls) - min(abs_ls) * 2)
else:
    print(sum(abs_ls))

Submission status: Accepted

Solution code:
import bisect
N = int(input())
A = [int(i) for i in input().split()]
A.sort()
idx = bisect.bisect_left(A,0)
ans = 0
if idx%2 == 0:
  ans += abs(sum(A[0:idx]))
  if N > idx:
    ans += sum(A[idx:])
else:
  ans += abs(sum(A[0:idx-1]))
  if idx < N:
    ans += abs(A[idx-1]+A[idx])
  elif idx == N:
    ans += A[idx-1]
  if idx + 1 < N:
    ans += sum(A[idx+1:])
print(ans)

Submission status: Accepted

Solution code:
# 解き直し
# 負数が偶数なら全部+にできる 奇数ならすべてを正数にして一番ちいさいやつを引く
n = int(input())
arr = list(map(int, input().split()))
minus_n = 0
positive_arr = list(sorted(list(map(lambda x: abs(x), arr))))
for a in arr:
    if a < 0:
        minus_n += 1

if minus_n % 2 == 0:
    print(sum(positive_arr))
else:
    print(sum(positive_arr) - positive_arr[0] * 2)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

minus = sum(i <= 0 for i in a) #0以下の数
#m = [i for i in a if i <= 0] #負の数リスト
#s = [i for i in a if i > 0] #正の数リスト
abs = [abs(i) for i in a] #リストの数を絶対値に変換
  
if minus%2 == 0 or 0 in a:
  print(sum(abs))
else:
  abs[abs.index(min(abs))] *= -1
  print(sum(abs))

Submission status: Accepted

Solution code:
N = int(input())

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

has_zero = False
min_abs = 10**5
sum_abs = 0
for i in range(N):
    if A[i] == 0:
        has_zero = True
    sum_abs += abs(A[i])
    if abs(A[i]) < min_abs:
        min_abs = abs(A[i])

minus = (tuple(filter(lambda x: x < 0, A)))
cnt_minus = len(minus)
if cnt_minus % 2 == 0 or has_zero == True:
    print(sum_abs)
else:
    print(sum_abs - 2*min_abs)

Submission status: Accepted

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

def count_negative(n,a):
    count = 0
    for i in range(n):
        if a[i] == 0:
            return 0
        elif a[i] < 0:
            count += 1
    return count

def num_of_min(n,a):
    minimum = sum(map(abs,a))
    
    count = 0    
    tmp = 0
    for i in range(n):
        tmp = abs(a[i])
        if tmp < minimum:
            minimum = tmp
            count = i
        else:
            pass
    return count

def flipping(n,a):
    nc = count_negative(n,a)
    total = 0
    count = 0
    if nc % 2 == 0:
        total = sum(map(abs,a))
    else:
        count = num_of_min(n,a)
        target = a.pop(count)
        
        total = sum(map(abs,a))
        if target > 0:
            total += target*(-1)
        else:
            total += target

    return total

if __name__ == '__main__':
    n = int(raw_input())
    a = map(int,raw_input().split())
    gt = flipping(n,a)
    print("%d" %gt)

Submission status: Accepted

Solution code:
import sys
from collections import deque, defaultdict
import copy
import bisect
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 9)
import math

N = int(input())
A = list(map(int, input().split()))
zeros = 0
minus = 0
B = []
for i in range(len(A)):
	if A[i] < 0:
		minus += 1
	elif A[i] == 0:
		zeros += 1
	B.append(abs(A[i]))


if zeros > 0:
	print(sum(B))
elif minus % 2 == 0:
	print(sum(B))
else:
	print(sum(B) - 2*min(B))

Submission status: Accepted

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

m = 0
B = []
for a in A:
    if a < 0:
        m += 1
    B.append(abs(a))

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

Submission status: Accepted

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

count = 0
minabs = max(max(a),-min(a))
sumabs = 0
for i in range(n):
    if a[i] < 0:
        count += 1
        sumabs += -a[i]
    else:
        sumabs += a[i]
    minabs = min(minabs,abs(a[i]))
if count%2 == 0:
    print(sumabs)
else:
    print(sumabs-2*minabs)

Submission status: Accepted

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