Problem:


Score : 300 points

Problem Statement

You are given an integer sequence of length N. The i-th term in the sequence is a_i.
In one operation, you can select a term and either increment or decrement it by one.

At least how many operations are necessary to satisfy the following conditions?

For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.

For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.

Constraints

2 ≤ n ≤ 10^5

|a_i| ≤ 10^9

Each a_i is an integer.

Input

Input is given from Standard Input in the following format:

n
a_1 a_2 ... a_n

Output

Print the minimum necessary count of operations.

Sample Input 1

4
1 -3 1 0

Sample Output 1

4

For example, the given sequence can be transformed into 1, -2, 2, -2 by four operations. The sums of the first one, two, three and four terms are 1, -1, 1 and -1, respectively, which satisfy the conditions.

Sample Input 2

5
3 -6 4 -5 7

Sample Output 2

0

The given sequence already satisfies the conditions.

Sample Input 3

6
-1 4 3 2 -5 4

Sample Output 3

8



Sample input:
4
1 -3 1 0


Sample output:
4

Submission status: Wrong Answer

Solution code:
n=int(input())
arr1=list(map(int,input().split()))
arr2=arr1[:]
ans1=0
ans2=0

if arr1[0]<=0:
    arr1[0]=1
    ans1+=abs(arr1[0])+1
if arr2[0]>=0:
    arr2[0]=-1
    ans2+=arr2[0]+1

sum1=arr1[0]
for i in range(1,n):
    tmp=sum1+arr1[i]
    if i%2==1:
        if tmp>=0:
            ans1+=tmp+1
            sum1=-1
        else:
            sum1=tmp
    else:
        if tmp<=0:
            ans1+=abs(tmp)+1
            sum1=1
        else:
            sum1=tmp
sum2=arr2[0]
for i in range(1,n):
    tmp=sum2+arr2[i]
    if i%2==0:
        if tmp>=0:
            ans2+=tmp+1
            sum2=-1
        else:
            sum2=tmp
    else:
        if tmp<=0:
            ans2+=abs(tmp)+1
            sum2=1
        else:
            sum2=tmp
print(min(ans1,ans2))

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(int,input().split()))
ans = 0
x = 0
S = []
S.append(A[0])
for i in range(1,n):
    if S[i - 1] == 0:
        S[i - 1] += 1
    x = A[i] + S[i - 1]
    if S[i - 1] > 0:
        if x >= 0:
            x = - x - 1
            ans += abs(x)
            S.append(-1)
        else:
            S.append(x)
    elif S[i - 1] < 0:
        if x <= 0:
            x = -x + 1
            ans += abs(x)
            S.append(1)
        else:
            S.append(x)
print(ans)

Submission status: Wrong Answer

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

import numpy as np

ans = 0

sum0 = a[0]
sum1 = a[0]

for i in range(1, n):
  sum1 += a[i]
  if np.sign(sum0) != np.sign(sum1) and sum1 != 0: #合計の符号が逆となっており、0でない
    sum0 = sum1
    pass
  elif sum1 == 0: #合計が0になった場合は、符号が逆になるよう１か-1を足す
    sum1 -= 1 * np.sign(sum0)
    ans += 1
    sum0 = sum1
  elif np.sign(sum0) == np.sign(sum1): #符号が同じ場合は、+1か-1になるまで足す
    if np.sign(sum1) == 1: #sum0もsum1もプラスの場合
      ans = ans + sum0 + a[i] + 1
      sum1 = -1
        
    else: #sum0もsum1もマイナスの場合
      ans = ans + abs(sum0 + a[i]) + 1
      sum1 = 1
        
    sum0 = sum1

print(ans)

Submission status: Wrong Answer

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

import numpy as np

ans = 0

sum0 = a[0]
sum1 = a[0]

for i in range(1, n):
  sum1 += a[i]
  if np.sign(sum0) != np.sign(sum1) and sum1 != 0: #合計の符号が逆となっており、0でない
    sum0 = sum1
    pass
  elif sum1 == 0: #合計が0になった場合は、符号が逆になるよう１か-1を足す
    sum1 -= 1 * np.sign(sum0)
    ans += 1
    sum0 = sum1
  elif np.sign(sum0) == np.sign(sum1): #符号が同じ場合は、+1か-1になるまで足す
    if np.sign(sum1) == 1: #sum0もsum1もプラスの場合
      ans += (sum1 + 1) #
      sum1 =  -1
        
    else:
      ans += (abs(sum1) +1)
      sum1 = 1
        
    sum0 = sum1

print(ans)

Submission status: Wrong Answer

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

import numpy as np

ans = 0

sum0 = a[0]
sum1 = a[0]

for i in range(1, n):
  sum1 += a[i]
  if np.sign(sum0) != np.sign(sum1) and sum1 != 0: #合計の符号が逆となっており、0でない
    sum0 = sum1
    pass
  elif sum1 == 0: #合計が0になった場合は、符号が逆になるよう１か-1を足す
    sum1 -= 1 * np.sign(sum0)
    ans += 1
    sum0 = sum1
  elif np.sign(sum0) == np.sign(sum1): #符号が同じ場合は、+1か-1になるまで足す
    if np.sign(sum1) == 1:
      ans += (sum1 + 1)
      sum1 =  -1
        
    else:
      ans += abs(sum1 - 1)
      sum1 = 1
        
    sum0 = sum1

print(ans)

Submission status: Wrong Answer

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

s = []
ss = a[0]
cnt = 0
c = 1
if(a[0] != 0):
    s.append(a[0])
else:
    while (a[c] == 0):
        c += 1
        cnt += 2
    cnt += 1
    if(np.sign(a[c]) == 1):
        ss = -1
        for i in range(c):
            if(i%2 != c%2):
                s.append(-1)
            else:
                s.append(1)
    else:
        ss = 1
        for i in range(c):
            if(i%2 != c%2):
                s.append(1)
            else:
                s.append(-1)
        
for i in range(c, n):
    ss += a[i]
    if(ss == 0):
        if(np.sign(s[i-1]) == -1):
            ss += 1
            cnt += 1 
            s.append(1)
        else:
            ss -= 1
            cnt += 1
            s.append(-1)
    elif(np.sign(s[i-1]) == np.sign(ss)):
        if(np.sign(s[i-1]) == -1):
            cnt += abs(1 - ss)
            ss = 1
            s.append(1)
        else:
            cnt += abs(-1-ss)
            ss = -1
            s.append(-1)
    else:
        s.append(ss)
print(cnt)

Submission status: Wrong Answer

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

s = []
ss = a[0]
cnt = 0
c = 1
if(a[0] != 0):
    s.append(a[0])
else:
    while (a[c] == 0):
        c += 1
        cnt += 2
    cnt -= 1
    if(np.sign(a[c]) == 1):
        ss = -1
        for i in range(c):
            if(i%2 != c%2):
                s.append(-1)
            else:
                s.append(1)
    else:
        ss = 1
        for i in range(c):
            if(i%2 != c%2):
                s.append(1)
            else:
                s.append(-1)
        
for i in range(c, n):
    ss += a[i]
    if(ss == 0):
        if(np.sign(s[i-1]) == -1):
            ss += 1
            cnt += 1
            s.append(1)
        else:
            ss -= 1
            cnt += 1
            s.append(-1)
    elif(np.sign(s[i-1]) == np.sign(ss)):
        if(np.sign(s[i-1]) == -1):
            cnt += 1 - ss
            ss = 1
            s.append(1)
        else:
            cnt += abs(-1-ss)
            ss = -1
            s.append(-1)
    else:
        s.append(ss)
print(cnt)

Submission status: Wrong Answer

Solution code:
def resolve(L):
    # L[0]!=0を起点とする
    cnt = 0
    s = L[0]
    for i in range(1,len(L)):
        a = L[i]
        if(s>0 and s+a>=0):
            L[i] = -s-1
            cnt += (s+a+1)
            s = -1
        elif(s<0 and s+a<=0):
            L[i] = -s+1
            cnt += (-s-a+1)
            s = 1
        else:
            s += a
    return cnt      

def ans(L):
    a = L[0]
    c0,c1=0,0

    if (a>0):
        c0 = resolve(L)
        c1 = (a+1) + resolve([-1]+L[1:])
    elif (a<0):
        c0 = resolve(L)
        c1 = (-a+1) + resolve([1]+L[1:])
    else:
        c0 = 1 + resolve([1]+L[1:])
        c1 = 1 + resolve([-1]+L[1:])
    
    return(min(c0,c1))

def main():
    N = int(input())
    List = [int(x) for x in input().split(' ')]
    print(ans(List))

main()

Submission status: Wrong Answer

Solution code:
def resolve(List):
    # L[0]!=0を起点とする
    L = List
    cnt = 0
    s = L[0]
    for i in range(1,len(L)):
        a = L[i]
        if(s>0 and s+a>=0):
            L[i] = -s-1
            cnt += (s+a+1)
            s = -1
        elif(s<0 and s+a<=0):
            L[i] = -s+1
            cnt += (-s-a+1)
            s = 1
        else:
            s += a
    return cnt      

def ans(List):
    L = List
    a = L[0]
    c0,c1=0,0

    if (a>0):
        c0 = resolve(L)
        c1 = (a+1) + resolve([-1]+L[1:])
    elif (a<0):
        c0 = resolve(L)
        c1 = (-a+1) + resolve([1]+L[1:])
    else:
        c0 = 1 + resolve([1]+L[1:])
        c1 = 1 + resolve([-1]+L[1:])
    
    return(min(c0,c1))

def main():
    N = int(input())
    L = [int(x) for x in input().split(' ')]
    print(ans(L))

main()

Submission status: Wrong Answer

Solution code:
def resolve(List):
    # L[0]!=0を起点とする
    L = List
    cnt = 0
    s = L[0]
    for i in range(1,len(L)):
        a = L[i]
        if(s>0 and s+a>=0):
            L[i] = -s-1
            cnt += (s+a+1)
            s = -1
        elif(s<0 and s+a<=0):
            L[i] = -s+1
            cnt += (-s-a+1)
            s = 1
        else:
            s += a
    return cnt      

def ans(L):
    a = L[0]
    c0,c1=0,0

    if (a>0):
        c0 = resolve(L)
        c1 = (a+1) + resolve([-1]+L[1:])
    elif (a<0):
        c0 = resolve(L)
        c1 = (-a+1) + resolve([1]+L[1:])
    else:
        c0 = 1 + resolve([1]+L[1:])
        c1 = 1 + resolve([-1]+L[1:])
    
    return(min(c0,c1))

N = int(input())
L = [int(x) for x in input().split(' ')]

print(ans(L))

Submission status: Wrong Answer

Solution code:

import sys
input = sys.stdin.readline

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

sum_odd = sum(a[1::2])
sum_eve = sum(a[::2])

ans = 0
sum_a  = 0
for i in range(n):
        
    sum_a = sum_a + a[i]
        
    if sum_a *(-1)**(i+1) < 1:
        kari = 1-sum_a *(-1)**(i+1)
        a[i] += 1*(-1)**(i+1) *(kari)
        sum_a += 1*(-1)**(i+1) *(kari)
            
        ans = ans + abs(kari)
            
ans1 = 0
sum_a = 0
             
for i in range(n):
       
    sum_a = sum_a + a[i]
    if sum_a *(-1)**(i) < 1:
        kari = (1-sum_a *(-1)**(i))
        a[i] += 1*(-1)**(i) * kari
        sum_a += 1*(-1)**(i) * kari

        ans1 = ans1 + abs(kari)


print(min(ans,ans1))

Submission status: Wrong Answer

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

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

sum_odd = sum(a[1::2])
sum_eve = sum(a[::2])

ans = 0
sum_a  = 0
if sum_odd >= sum_eve:#奇数の和がおおきいので、偶数　0,2,4...を負にする
    for i in range(n):
        
        sum_a = sum_a + a[i]
        
        if sum_a *(-1)**(i+1) < 1:
            kari = 1-sum_a *(-1)**(i+1)
            a[i] += 1*(-1)**(i+1) *(kari)
            sum_a += 1*(-1)**(i+1) *(kari)
            
            ans = ans + abs(kari)
            

             
else:
    for i in range(n):
       
        sum_a = sum_a + a[i]
        if sum_a *(-1)**(i) < 1:
            kari = (1-sum_a *(-1)**(i))
            a[i] += 1*(-1)**(i) * kari
            sum_a += 1*(-1)**(i) * kari

            ans = ans + abs(kari)


print(ans)

Submission status: Wrong Answer

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

n = 6
A = [-1, 4, 3, 2, -5, 4]
import numpy as np
b = A.copy()
c = A.copy()

#+-+-+-
def pm(a):
    for i in range(len(a)):
        if i==0:
            if a[i] <= 0:
                a[i] = 1
        elif i%2==0:
            if sum(a[:i+1]) <= 0:
                a[i] = 1 - sum(a[:i])
        elif i%2==1:
            if sum(a[:i+1]) >= 0:
                a[i] = -1 - sum(a[:i])
    return sum(abs(np.array(a) - np.array(b)))

#-+-+-+
def mp(a):
    for i in range(len(a)):
        if i==0:
            if a[i] >= 0:
                a[i] = -1
        elif i%2==0:
            if sum(a[:i+1]) >= 0:
                a[i] = -1 - sum(a[:i])
        elif i%2==1:
            if sum(a[:i+1]) <= 0:
                a[i] = 1 - sum(a[:i])
    return sum(abs(np.array(a) - np.array(b)))
  
print(min(pm(A), mp(c)))

Submission status: Wrong Answer

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

cs = A[0]
ans1 = 0
for na in A[1:]:
    if cs >= 0:
        cs += na
        if cs < 0:
            continue

        ans1 += abs(cs) + 1
        cs = -1
    else:
        cs += na
        if cs > 0:
            continue

        ans1 += abs(cs) + 1
        cs = 1

if A[0] > 0:
    cs = -1
else:
    cs = 1
ans2 = abs(A[0]) + 1
for na in A[1:]:
    if cs >= 0:
        cs += na
        if cs < 0:
            continue

        ans2 += abs(cs) + 1
        cs = -1
    else:
        cs += na
        if cs > 0:
            continue

        ans2 += abs(cs) + 1
        cs = 1

print(min(ans1, ans2))

Submission status: Wrong Answer

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

cs = A[0]
ans1 = 0
for na in A[1:]:
    if cs >= 0:
        cs += na
        if cs < 0:
            continue

        ans1 += cs + 1
        cs = -1
    else:
        cs += na
        if cs > 0:
            continue

        ans1 += -cs + 1
        cs = 1

if A[0] > 0:
    cs = -1
else:
    cs = 1
ans2 = A[0] + 1
for na in A[1:]:
    if cs >= 0:
        cs += na
        if cs < 0:
            continue

        ans2 += cs + 1
        cs = -1
    else:
        cs += na
        if cs > 0:
            continue

        ans2 += -cs + 1
        cs = 1

print(min(ans1, ans2))

Submission status: Wrong Answer

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

cs = A[0]
ans1 = 0
for na in A[1:]:
    if cs >= 0:
        cs += na
        if cs < 0:
            continue

        ans1 += cs + 1
        cs = -1
    else:
        cs += na
        if cs > 0:
            continue

        ans1 += -cs + 1
        cs = 1

cs = A[-1]
ans2 = 0
for na in reversed(A[:-1]):
    if cs >= 0:
        cs += na
        if cs < 0:
            continue

        ans2 += cs + 1
        cs = -1
    else:
        cs += na
        if cs > 0:
            continue

        ans2 += -cs + 1
        cs = 1

print(min(ans1, ans2))

Submission status: Wrong Answer

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

cs = A[0]

ans = 0
for na in A[1:]:
    if cs >= 0:
        cs += na
        if cs < 0:
            continue

        ans += cs + 1
        cs = -1
    else:
        cs += na
        if cs > 0:
            continue

        ans += -cs + 1
        cs = 1

print(ans)

Submission status: Wrong Answer

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

cntA, sumA = 0, 0
for i in range(N):
    sumA += A[i]
    if i % 2 == 0:
        if sumA < 0:
            cntA += abs(sumA) + 1
            sumA += abs(sumA) + 1
    else:
        if sumA > 0:
            cntA += abs(sumA) + 1
            sumA -= abs(sumA) + 1
    
cntB, sumB = 0, 0
for i in range(N):
    sumB += A[i]
    if i % 2 != 0:
        if sumB < 0:
            cntB += abs(sumB) + 1
            sumB += abs(sumB) + 1
    else:
        if sumB > 0:
            cntB += abs(sumB) + 1
            sumB -= abs(sumB) + 1

print(min(cntA, cntB))

Submission status: Wrong Answer

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

cntA, sumA = 0, 0
for i in range(N):
    sumA += A[i]
    if i % 2 == 0:
        if sumA <= 0:
            cntA += abs(sumA) + 1
            sumA += abs(sumA) + 1
    else:
        if sumA >= 0:
            cntA += abs(sumA) + 1
            sumA -= abs(sumA) + 1
            
cntB, sumB = 0, 0
for i in range(N):
    sumB += A[i]
    if i % 2 != 0:
        if sumB >= 0:
            cntB += abs(sumB) + 1
            sumB += abs(sumB) + 1
    else:
        if sumB >= 0:
            cntB += abs(sumB) + 1
            sumB -= abs(sumB) + 1

print(min(cntA, cntB))

Submission status: Wrong Answer

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

cntA, sumA = 0, 0
for i in range(N):
    sumA += A[i]
    if i % 2 == 0:
        if sumA < 0:
            cntA += abs(sumA) + 1
            sumA += abs(sumA) + 1
    else:
        if sumA > 0:
            cntA += abs(sumA) + 1
            sumA -= abs(sumA) + 1
            
cntB, sumB = 0, 0
for i in range(N):
    sumB += A[i]
    if i % 2 != 0:
        if sumB > 0:
            cntB += abs(sumB) + 1
            sumB += abs(sumB) + 1
    else:
        if sumB > 0:
            cntB += abs(sumB) + 1
            sumB -= abs(sumB) + 1

print(min(cntA, cntB))

Submission status: Wrong Answer

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

S = 0
C = 0
S = a[0]
if S > 0:
    pm = 1
elif S < 0:
    pm = 0
else:
    S += 1
    C += 1
    pm = 1
for i in range(1, n):
    S += a[i]
    if pm == 1 and S >= 0:
        C += S + 1
        S -= S + 1
    elif pm == 0 and S <= 0:
        C += -S + 1
        S += -S + 1
    pm = 1 - pm 

T = 0
D = 0
T = a[0]
if T > 0:
    D += T + 1
    T -= T + 1
    pm = 0
elif T < 0:
    D += -T + 1
    T += -T + 1
    pm = 1
else:
    T -= 1
    C += 1
    pm = 0
for i in range(1, n):
    T += a[i]
    if pm == 1 and T >= 0:
        D += T + 1
        T -= T + 1
    elif pm == 0 and T <= 0:
        D += -T + 1
        T += -T + 1
    pm = 1 - pm

print(min(C, D))

Submission status: Wrong Answer

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

S = 0
C = 0
S = a[0]
if S > 0:
    pm = 1
else:
    pm = 0
for i in range(1, n):
    S += a[i]
    if pm == 1 and S >= 0:
        C += S + 1
        S -= S + 1
    elif pm == 0 and S <= 0:
        C += -S + 1
        S += -S + 1
    pm = 1 - pm 

T = 0
D = 0
T = a[0]
if T > 0:
    D += T + 1
    T -= T + 1
    pm = 0
else:
    D += -T + 1
    T += -T + 1
    pm = 1
for i in range(1, n):
    T += a[i]
    if pm == 1 and T >= 0:
        D += T + 1
        T -= T + 1
    elif pm == 0 and T <= 0:
        D += -T + 1
        T += -T + 1
    pm = 1 - pm

print(min(C, D))

Submission status: Wrong Answer

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

S = 0
C = 0
S = a[0]
if S > 0:
    pm = 1
else:
    pm = 0
for i in range(1, n):
    S += a[i]
    if pm == 1 and S >= 0:
        C += S + 1
        S -= S + 1
    elif pm == 0 and S <= 0:
        C += -S + 1
        S += -S + 1
    pm = 1 - pm 
print(C)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def resolve(L):
    # L[0]!=0を起点とする
    cnt = 0
    s = L[0]
    for i in range(1,len(L)):
        a = L[i]
        if(s>0 and s+a>=0):
            L[i] = -s-1
            cnt += (s+a+1)
            s = -1
        elif(s<0 and s+a<=0):
            L[i] = -s+1
            cnt += (-s-a+1)
            s = 1
        else:
            s += a
    return cnt      

def ans(L):
    a = L[0]
    c0,c1=0,0

    if (a>0):
        c0 = resolve(L)
        c1 = (a+1) + resolve([-1]+L[1:])
    elif (a<0):
        c0 = resolve(L)
        c1 = (-a+1) + resolve([1]+L[1:])
    else:
        c0 = 1 + resolve([1]+L[1:])
        c1 = 1 + resolve([-1]+L[1:])
    
    return(min(c0,c1))

N = int(input())
L = [int(x) for x in input().split(' ')]

print(ans(L))

Submission status: Wrong Answer

Solution code:
import sys, os, math, bisect, itertools, collections, heapq, queue
# from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
from decimal import Decimal
from collections import defaultdict, deque

# import fractions

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)
# lcm = lambda x, y: (x * y) // fractions.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    A = il()

    r1 = 0
    S = [0] * N
    S[0] = A[0]
    for n in range(1, N):
        S[n] = S[n - 1] + A[n]
        if n % 2 != 0 and S[n] >= 0:
            r1 += S[n] + 1
            S[n] = -1
        elif n % 2 == 0 and S[n] <= 0:
            r1 += abs(S[n]) + 1
            S[n] = 1

    r2 = 0
    S = [0] * N
    S[0] = A[0]
    for n in range(1, N):
        S[n] = S[n - 1] + A[n]
        if n % 2 != 0 and S[n] <= 0:
            r2 += abs(S[n]) + 1
            S[n] = 1
        elif n % 2 == 0 and S[n] >= 0:
            r2 += S[n] + 1
            S[n] = -1
    print(min(r1, r2))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def first_positive(n,a):
    count = 0
    sum = a[0]
    #正ならTrue
    flag = True

    if a[0] <= 0:
        sum = 1
        count += abs(1 - a[0])

    for i in range(1,n):
        if sum + a[i] < 0 and flag == True:
            sum += a[i]
            flag = False
            continue
        elif sum + a[i] < 0 and flag == False:
            flag = True
            count += abs(1 - sum - a[i])
            sum = 1
        elif sum + a[i] >= 0 and flag == True:
            flag = False
            count += abs(-1 - sum - a[i])
            sum = -1
        elif sum + a[i] >= 0 and flag == False:
            sum += a[i]
            flag = True
            continue

    return count

def first_negative(n,a):
    count = 0
    sum = a[0]
    #正ならTrue
    flag = True

    if a[0] >= 0:
        sum = -1
        count += abs(-1 - a[0])

    for i in range(1,n):
        if sum + a[i] < 0 and flag == True:
            sum += a[i]
            flag = False
            continue
        elif sum + a[i] < 0 and flag == False:
            flag = True
            count += abs(1 - sum - a[i])
            sum = 1
        elif sum + a[i] >= 0 and flag == True:
            flag = False
            count += abs(-1 - sum - a[i])
            sum = -1
        elif sum + a[i] >= 0 and flag == False:
            sum += a[i]
            flag = True
            continue

    return count

if __name__ == '__main__':
    n = int(input())
    a = [int(i) for i in input().split()]

    p = first_positive(n,a)
    n = first_negative(n,a)

    if p < n:
        print(p)
    else:
        print(n)

Submission status: Wrong Answer

Solution code:
def first_positive(n,a):
    count = 0
    sum = a[0]
    #正ならTrue
    flag = True

    if a[0] < 0:
        sum = 1
        count += abs(1 - a[0])

    for i in range(1,n):
        if sum + a[i] < 0 and flag == True:
            sum += a[i]
            flag = False
            continue
        elif sum + a[i] < 0 and flag == False:
            flag = True
            count += abs(1 - sum - a[i])
            sum = 1
        elif sum + a[i] >= 0 and flag == True:
            flag = False
            count += abs(-1 - sum - a[i])
            sum = -1
        elif sum + a[i] >= 0 and flag == False:
            sum += a[i]
            flag = True
            continue

    return count

def first_negative(n,a):
    count = 0
    sum = a[0]
    #正ならTrue
    flag = True

    if a[0] > 0:
        sum = -1
        count += abs(-1 - a[0])

    for i in range(1,n):
        if sum + a[i] < 0 and flag == True:
            sum += a[i]
            flag = False
            continue
        elif sum + a[i] < 0 and flag == False:
            flag = True
            count += abs(1 - sum - a[i])
            sum = 1
        elif sum + a[i] >= 0 and flag == True:
            flag = False
            count += abs(-1 - sum - a[i])
            sum = -1
        elif sum + a[i] >= 0 and flag == False:
            sum += a[i]
            flag = True
            continue

    return count

if __name__ == '__main__':
    n = int(input())
    a = [int(i) for i in input().split()]

    p = first_positive(n,a)
    n = first_negative(n,a)

    if p < n:
        print(p)
    else:
        print(n)

Submission status: Wrong Answer

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

count1 = 0
count2 = 0
sum = 0

for i in range(0,n-1):
    if i % 2 == 0:
        if sum + a[i] > 0:
            continue
        elif sum + a[i] <= 0:
            count1 += abs(1 - sum -a[i])
            sum = 1
    if i % 2 == 1:
        if sum + a[i] < 0:
            continue
        else:
            count1 += abs(-1 - sum - a[i])
            sum = -1

sum = 0
for i in range(0,n-1):
    if i % 2 == 1:
        if sum + a[i] > 0:
            continue
        elif sum + a[i] <= 0:
            count2 += abs(1 - sum -a[i])
            sum = 1
    if i % 2 == 0:
        if sum + a[i] < 0:
            continue
        else:
            count2 += abs(-1 - sum - a[i])
            sum = -1

if count1 >= count2:
    print(count1)
else:
    print(count2)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
L = [0 for _ in range(n)]
L[0] = a[0]
for i in range(1, n):
  L[i] = L[i-1] + a[i]
delay = 0
all_over = 0
anti_delay = 0
anti_all_over = 0
if a[0] != 0:
  antiL = L[:]
  sign = (a[0] > 0) - (a[0] < 0)
  for i in range(1, n):
    L[i] += delay
    if L[i] <= 0 and sign == -1:
      delay += 1 - L[i]
      all_over += 1 - L[i]
      L[i] = 1
    elif L[i] >= 0 and sign == 1:
      delay -= L[i] + 1
      all_over += L[i] + 1
      L[i] = -1
    sign *= -1
  if antiL[0] < 0:
    sign = 1
    anti_delay = 1 - antiL[0]
    anti_all_over = 1 - antiL[0]
  else:
    sign = -1
    anti_delay = antiL[0] + 1
    anti_all_over = antiL[0] + 1
  for i in range(1, n):
    antiL[i] += anti_delay
    if antiL[i] <= 0 and sign == -1:
      anti_delay += 1 - antiL[i]
      anti_all_over += 1 - antiL[i]
      antiL[i] = 1
    elif antiL[i] >= 0 and sign == 1:
      anti_delay -= antiL[i] + 1
      anti_all_over += antiL[i] + 1
      antiL[i] = -1
    sign *= -1
  print(min(anti_all_over, all_over))
else:
  posL = L[:]
  negL = L[:]
  pos_delay, neg_delay = 1, -1
  pos_all_over, neg_all_over = 1, 1
  sign = 1
  for i in range(1, n):
    posL[i] += pos_delay
    if posL[i] <= 0 and sign == -1:
      pos_delay += 1 - posL[i]
      pos_all_over += 1 - posL[i]
      posL[i] = 1
    elif posL[i] >= 0 and sign == 1:
      pos_delay -= posL[i] + 1
      pos_all_over += posL[i] + 1
      posL[i] = -1
    sign *= -1
  sign = -1
  for i in range(1, n):
    negL[i] += neg_delay
    if negL[i] <= 0 and sign == -1:
      neg_delay += 1 - negL[i]
      neg_all_over += 1 - negL[i]
      negL[i] = 1
    elif negL[i] >= 0 and sign == 1:
      neg_delay -= negL[i] + 1
      neg_all_over += negL[i] + 1
      negL[i] = -1
    sign *= -1
  print(min(pos_all_over, neg_all_over))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
L = [0 for _ in range(n)]
L[0] = a[0]
for i in range(1, n):
  L[i] = L[i-1] + a[i]
delay = 0
all_over = 0
anti_delay = 0
anti_all_over = 0
if a[0] != 0:
  antiL = L[:]
  sign = (a[0] > 0) - (a[0] < 0)
  for i in range(1, n):
    L[i] += delay
    if L[i] <= 0 and sign == -1:
      delay += 1 - L[i]
      all_over += 1 - L[i]
      L[i] = 1
    elif L[i] >= 0 and sign == 1:
      delay -= L[i] + 1
      all_over += L[i] + 1
      L[i] = -1
    sign *= -1
  if antiL[0] < 0:
    sign = 1
    anti_delay = 1 - antiL[0]
    anti_all_over = 1 - antiL[0]
  else:
    sign = -1
    anti_delay = antiL[0] - 1
    anti_all_over = antiL[0] - 1
  for i in range(1, n):
    antiL[i] += anti_delay
    if antiL[i] <= 0 and sign == -1:
      anti_delay += 1 - antiL[i]
      anti_all_over += 1 - antiL[i]
      antiL[i] = 1
    elif antiL[i] >= 0 and sign == 1:
      anti_delay -= antiL[i] + 1
      anti_all_over += antiL[i] + 1
      antiL[i] = -1
    sign *= -1
  print(min(anti_all_over, all_over))
else:
  posL = L[:]
  negL = L[:]
  pos_delay, neg_delay = 1, -1
  pos_all_over, neg_all_over = 1, 1
  sign = 1
  for i in range(1, n):
    posL[i] += pos_delay
    if posL[i] <= 0 and sign == -1:
      pos_delay += 1 - posL[i]
      pos_all_over += 1 - posL[i]
      posL[i] = 1
    elif posL[i] >= 0 and sign == 1:
      pos_delay -= posL[i] + 1
      pos_all_over += posL[i] + 1
      posL[i] = -1
    sign *= -1
  sign = -1
  for i in range(1, n):
    negL[i] += neg_delay
    if negL[i] <= 0 and sign == -1:
      neg_delay += 1 - negL[i]
      neg_all_over += 1 - negL[i]
      negL[i] = 1
    elif negL[i] >= 0 and sign == 1:
      neg_delay -= negL[i] + 1
      neg_all_over += negL[i] + 1
      negL[i] = -1
    sign *= -1
  print(min(pos_all_over, neg_all_over))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
L = [0 for _ in range(n)]
L[0] = a[0]
for i in range(1, n):
  L[i] = L[i-1] + a[i]
delay = 0
all_over = 0
anti_delay = 0
anti_all_over = 0
if a[0] != 0:
  antiL = L[:]
  sign = (a[0] > 0) - (a[0] < 0)
  for i in range(1, n):
    L[i] += delay
    if L[i] <= 0 and sign == -1:
      delay += 1 - L[i]
      all_over += 1 - L[i]
      L[i] = 1
    elif L[i] >= 0 and sign == 1:
      delay -= L[i] + 1
      all_over += L[i] + 1
      L[i] = -1
    sign *= -1
  if antiL[0] < 0:
    sign = 1
    anti_delay = 1 - antiL[0]
    anti_all_over = 1 - antiL[0]
  else:
    sign = 1
    anti_delay = antiL[0] - 1
    anti_all_over = antiL[0] - 1
  for i in range(1, n):
    antiL[i] += anti_delay
    if antiL[i] <= 0 and sign == -1:
      anti_delay += 1 - antiL[i]
      anti_all_over += 1 - antiL[i]
      antiL[i] = 1
    elif antiL[i] >= 0 and sign == 1:
      anti_delay -= antiL[i] + 1
      anti_all_over += antiL[i] + 1
      antiL[i] = -1
    sign *= -1
  print(min(anti_all_over, all_over))
else:
  posL = L[:]
  negL = L[:]
  pos_delay, neg_delay = 1, -1
  pos_all_over, neg_all_over = 1, 1
  sign = 1
  for i in range(1, n):
    posL[i] += pos_delay
    if posL[i] <= 0 and sign == -1:
      pos_delay += 1 - posL[i]
      pos_all_over += 1 - posL[i]
      posL[i] = 1
    elif posL[i] >= 0 and sign == 1:
      pos_delay -= posL[i] + 1
      pos_all_over += posL[i] + 1
      posL[i] = -1
    sign *= -1
  sign = -1
  for i in range(1, n):
    negL[i] += neg_delay
    if negL[i] <= 0 and sign == -1:
      neg_delay += 1 - negL[i]
      neg_all_over += 1 - negL[i]
      negL[i] = 1
    elif negL[i] >= 0 and sign == 1:
      neg_delay -= negL[i] + 1
      neg_all_over += negL[i] + 1
      negL[i] = -1
    sign *= -1
  print(min(pos_all_over, neg_all_over))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
L = [0 for _ in range(n)]
L[0] = a[0]
for i in range(1, n):
  L[i] = L[i-1] + a[i]
delay = 0
all_over = 0
if a[0] != 0:
  sign = (a[0] > 0) - (a[0] < 0)
  for i in range(1, n):
    L[i] += delay
    if L[i] <= 0 and sign == -1:
      delay += 1 - L[i]
      all_over += 1 - L[i]
      L[i] = 1
    elif L[i] >= 0 and sign == 1:
      delay -= L[i] + 1
      all_over += L[i] + 1
      L[i] = -1
    sign *= -1
  print(all_over)
else:
  posL = L[:]
  negL = L[:]
  pos_delay, neg_delay = 1, -1
  pos_all_over, neg_all_over = 1, 1
  sign = 1
  for i in range(1, n):
    posL[i] += pos_delay
    if posL[i] <= 0 and sign == -1:
      pos_delay += 1 - posL[i]
      pos_all_over += 1 - posL[i]
      posL[i] = 1
    elif posL[i] >= 0 and sign == 1:
      pos_delay -= posL[i] + 1
      pos_all_over += posL[i] + 1
      posL[i] = -1
    sign *= -1
  sign = -1
  for i in range(1, n):
    negL[i] += neg_delay
    if negL[i] <= 0 and sign == -1:
      neg_delay += 1 - negL[i]
      neg_all_over += 1 - negL[i]
      negL[i] = 1
    elif negL[i] >= 0 and sign == 1:
      neg_delay -= negL[i] + 1
      neg_all_over += negL[i] + 1
      negL[i] = -1
    sign *= -1
  print(min(pos_all_over, neg_all_over))