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: Runtime Error

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

S1 = 0
S2 = 0
#S1が奇数番目が正の場合、S2が偶数番目が負の場合
cnt = 0

for i,num in enumerate(a):
    S1 += num
    if i % 2 == 0 and S1 <= 0:
        cnt1 += 1 - S1
        S1 = 1
    if i % 2 != 0 and S1 >= 0:
        cnt1 += 1 + S1
        S1 = -1

    S2 += num
    if i % 2 == 0 and S2 >= 0:
        cnt2 += 1 + S2
        S2 = -1
    if i % 2 != 0 and S2 <= 0:
        cnt2 += 1 - S2
        S2 = 1

print(cnt1 if cnt1 <= cnt2 else cnt2)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
sum=0
cnt=0
# 奇数+
for i in range(n):
    sum+=a[i]
    if i%2==0:
        if sum>=0:
            cnt+=(sum+1)
            sum=-1
    else:
        if sum<=0:
            cnt+=(1-sum)
            sum=1
cnt_sbst=cnt
# 奇数-
cnt=0
sum=0
for i in range(n):
    z+=a[i]
    if i%2==1:
        if sum>=0:
            cnt+=(sum+1)
            sum=-1
    else:
        if sum<=0:
            cnt+=(1-sum)
            sum=1
cnt_plus=cnt
ans=min(cnt_plus,cnt_sbst)
print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
import sys
sum=0
cnt=0
# 奇数+
for i in range(n):
    z+=a[i]
    if i%2==0:
        if z<0:
            sum=z
        else:
            cnt+=(z+1)
            sum=-1
    else:
        if sum<=0:
            cnt+=(1-sum)
            sum=1
cnt_sbst=cnt
# 奇数-
for i in range(n):
    z+=a[i]
    if i%2==1:
        if sum>=0:
            cnt+=(z+1)
            sum=-1
    else:
        if sum<=0:
            cnt+=(1-z)
            sum=1
cnt_plus=cnt
ans=min(cnt_plus,cnt_sbst)
print(ans)

Submission status: Runtime Error

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

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

# スタート＋
if a[0] <= 0:
    A1 = 1
    ans1 += abs(a[0]) + 1
else:
    A1 = a[0]
    ans1 = 0
for i in range(1, n):
    nextA = A1 + a[i]
    if (A1 > 0 and nextA < 0) or (A1 < 0 and nextA > 0):
        A1 = nextA
    elif nextA == 0 and A1 > 0:
        ans1 += 1
        A1 = -1
    elif nextA == 0 and A1 < 0:
        ans1 += 1
        A1 = 1
    elif A1 > 0:
        ans1 += abs(nextA) + 1
        A1 = -1
    else:
        ans1 += abs(nextA) + 1
        A1 = 1

# スタート-
if a[0] >= 0:
    ans2 = abs(a[0]) + 1
    A2 = -1
else:
    A2 = a[0]
    ans2 = 0
for i in range(1, n):
    nextA = A2 + a[i]
    if (A2 > 0 and nextA < 0) or (A2 < 0 and nextA > 0):
        A2 = nextA
    elif nextA == 0 and A2 > 0:
        ans2 += 1
        A2 = -1
    elif nextA == 0 and A2 < 0:
        ans2 += 1
        A2 = 1
    elif A2 > 0:
        ans2 += abs(nextA) + 1
        A2 = -1
    else:
        ans2 += abs(nextA) + 1
        A2 = 1

print(min(ans1, ans2))

Submission status: Runtime Error

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

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

# スタート＋
if a[0] <= 0:
    A1 = 1
    ans1 += abs(a[0]) + 1
else:
    A1 = a[0]
for i in range(1, n):
    nextA = A1 + a[i]
    if (A1 > 0 and nextA < 0) or (A1 < 0 and nextA > 0):
        A1 = nextA
    elif nextA == 0 and A1 > 0:
        ans1 += 1
        A1 = -1
    elif nextA == 0 and A1 < 0:
        ans1 += 1
        A1 = 1
    elif A1 > 0:
        ans1 += abs(nextA) + 1
        A1 = -1
    else:
        ans1 += abs(nextA) + 1
        A1 = 1

# スタート-
if a[0] >= 0:
    ans2 = abs(a[0]) + 1
    A2 = -1
else:
    A2 = a[0]

for i in range(1, n):
    nextA = A2 + a[i]
    if (A2 > 0 and nextA < 0) or (A2 < 0 and nextA > 0):
        A2 = nextA
    elif nextA == 0 and A2 > 0:
        ans2 += 1
        A2 = -1
    elif nextA == 0 and A2 < 0:
        ans2 += 1
        A2 = 1
    elif A2 > 0:
        ans2 += abs(nextA) + 1
        A2 = -1
    else:
        ans2 += abs(nextA) + 1
        A2 = 1

print(min(ans1, ans2))

Submission status: Runtime Error

Solution code:
n = input()
a = [int(i) for i in input().split()]
cou = len(a)
counter = 0
X = a[0]
ans = 0
for i in a:
    if X > 0:
        b = -1 - X
        ans += abs(b - a[i])
        X = -1
        counter += 1
        if counter == cou - 1:
            break
    else:
        b = 1 - X
        ans += abs(b - a[i])
        X = 1
        counter += 1
        if counter == cou - 1:
            break
print (ans)

Submission status: Runtime Error

Solution code:
n = input()
a = [int(i) for i in input.split()]
X = 0
ans = 0
for i in a:
    X += i
    if X > 0:
        b = -1 - X
        ans += b - a[i+1]
    else:
        b = 1 - X
        ans += b - a[i+1]
print (ans)

Submission status: Runtime Error

Solution code:
n = input()
a = [int(i) for i in input.split()]
X = 0
ans = 0
for i in a:
	X += i
    if X > 0:
      b = -1 - X
      ans += b - a(i+1)
    else X < 0:
      b = 1 - X
      ans += b - a(i+1)
    return ans
print (ans)

Submission status: Runtime Error

Solution code:
n = input()
a = [int(i) for i in input.solit()]
X = 0
ans = 0
for i in a:
	X += i
    if X > 0:
      b = -1 - X
      ans += b - a(i+1)
    else X < 0:
      b = 1 - X
      ans += b - a(i+1)
    return ans
print (ans)

Submission status: Runtime Error

Solution code:
n=int(input())
l=list(map(int,input().split()))
c,f=0,l[0]/abs(l[0])
s=l[0]
for i in range(1,n):
  t=s+l[i]
  if s*t>=0:
    s=-1*f
    c+=abs(t)+1
  else:
    f=-1*f
    s=s+l[i]
print(int(c))

Submission status: Runtime Error

Solution code:
n=int(input())
l=list(map(int,input().split()))
c,f=0,l[0]/abs(l[0])
s=l[0]
for i in range(1,n,1):
  t=s+l[i]
  if s*t>0:
    s=-1*f
    c+=abs(t-s+1)
  else:
    f=-1*f
    s=s+l[i]
print(int(c))

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
n = int(input())
a = list(map(int,input().split()))
cnt = 0
current_sum = a[0]#現在の操作後の値
# 正負どちらを起点にするかで2通り考える必要がある。
# a[0]に従うパターン
for i in range(n-1):
    if current_sum * (current_sum + a[i+1]) < 0:#符号が違う
        current_sum += a[i+1]
        cnt += 0#そのまま
    elif current_sum < 0:
        cnt += 1 - (current_sum + a[i+1])
        current_sum = 1 
    else:
        cnt += (current_sum + a[i+1]) +1 
        current_sum = -1
case1 = cnt

cnt = abs(a[0])+1
current_sum = -1 * a[0] // abs(a[0])#現在の操作後の値
for i in range(n-1):
    if current_sum * (current_sum + a[i+1]) < 0:#符号が違う
        current_sum += a[i+1]
        cnt += 0#そのまま
    elif current_sum < 0:
        cnt += 1 - (current_sum + a[i+1])
        current_sum = 1 
    else:
        cnt += (current_sum + a[i+1]) +1 
        current_sum = -1
#print(cnt,case1)
print(min(cnt,case1))

Submission status: Runtime Error

Solution code:
# input = sys.stdin.readline
from bisect import *
from collections import *
from heapq import *
# import functools
# import itertools
# import math

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


temp=A[0]
if temp<0:
    flag=0
if temp>0:
    flag=1

count=0
for i in range(1,n):
    temp+=A[i]


    if flag:
        if temp>=0:
            count+=temp-(-1)
            temp=-1
        flag=0
    else:
        if temp<=0:
            count+=1-temp
            temp=1
        flag=1
    #print(temp,flag)

print(count)

Submission status: Runtime Error

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

cnt=0
for i in range(1,n):
    
    # 条件満たすまでループ
    for _ in range(3):
        print(a)
    
        now_tmp = sum(a[:i])
        next_tmp = sum(a[:i+1])
        print(i, now_tmp, next_tmp)
        
        # 符号が逆転していればOK かつ 現在までの総和が0でない
        #  異なる符号を掛けるとマイナスになる
        if now_tmp * next_tmp <0 and now_tmp !=0:
            break
        else:
            # 現在の合計がマイナスの場合
            if now_tmp < 0:
                a[i] += -next_tmp+1
                cnt +=abs(next_tmp+1)

            # 現在の合計がプラスの場合
            elif now_tmp > 0 :
                a[i] += -next_tmp-1
                cnt +=abs(next_tmp+1)
            # 現在の合計が0の場合
            elif now_tmp == 0 :
                # 1個前がプラスの場合、
                if sum(a[:i-1]) > 0:
                    a[i] += -next_tmp+1
                    cnt +=abs(next_tmp+1)
                # 1個前がマイナスの場合
                else:
                    a[i] += -next_tmp+1
                    cnt +=abs(next_tmp+1)
                
print(cnt)

Submission status: Runtime Error

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

cnt=0
for i in range(1,n):
    
    # 条件満たすまでループ
    for _ in range(3):
        print(a)
    
        now_tmp = sum(a[:i])
        next_tmp = sum(a[:i+1])
        print(i, now_tmp, next_tmp)
        
        # 符号が逆転していればOK かつ 現在までの総和が0でない
        #  異なる符号を掛けるとマイナスになる
        if now_tmp * next_tmp <0 and now_tmp !=0:
            break
        else:
            # 現在の合計がマイナスの場合
            if now_tmp < 0:
                a[i] += -next_tmp+1
                cnt +=abs(next_tmp+1)

            # 現在の合計がプラスの場合
            elif now_tmp > 0 :
                a[i] += next_tmp-1
                cnt +=abs(next_tmp+1)
            # 現在の合計が0の場合
            elif now_tmp == 0 :
                # 1個前がプラスの場合、
                if sum(a[:i-1]) > 0:
                    a[i] += -next_tmp+1
                    cnt +=abs(next_tmp+1)
                # 1個前がマイナスの場合
                else:
                    a[i] += next_tmp+1
                    cnt +=abs(next_tmp+1)
                
print(cnt)

Submission status: Runtime Error

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

cnt=0
for i in range(1,n):
    
    # 条件満たすまでループ
    for _ in range(3):
        print(a)
    
        now_tmp = sum(a[:i])
        next_tmp = sum(a[:i+1])
        print(i, now_tmp, next_tmp)
        
        # 符号が逆転していればOK かつ 現在までの総和が0でない
        #  異なる符号を掛けるとマイナスになる
        if now_tmp * next_tmp <0 and now_tmp !=0:
            break
        else:
            # 現在の合計がマイナスの場合
            if now_tmp < 0:
                a[i] += next_tmp+1
                cnt +=abs(next_tmp+1)

            # 現在の合計がプラスの場合
            elif now_tmp > 0 :
                a[i] += -next_tmp-1
                cnt +=abs(next_tmp+1)
            # 現在の合計が0の場合
            elif now_tmp == 0 :
                # 1個前がプラスの場合、
                if sum(a[:i-1]) > 0:
                    a[i] += -next_tmp+1
                    cnt +=abs(next_tmp+1)
                # 1個前がマイナスの場合
                else:
                    a[i] += next_tmp+1
                    cnt +=abs(next_tmp+1)
                
print(cnt)

Submission status: Runtime Error

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

cnt=0
for i in range(1,n):
    
    # 条件満たすまでループ
    while True:
        print(a)
    
        now_tmp = sum(a[:i])
        next_tmp = sum(a[:i+1])
        print(i, now_tmp, next_tmp)
        
        # 符号が逆転していればOK かつ 現在までの総和が0でない
        #  異なる符号を掛けるとマイナスになる
        if now_tmp * next_tmp <0 and now_tmp !=0:
            break
        else:
            # 現在の合計がマイナスの場合
            if now_tmp < 0:
                a[i] += next_tmp+1
                cnt +=abs(next_tmp+1)

            # 現在の合計がプラスの場合
            elif now_tmp > 0 :
                a[i] += -next_tmp-1
                cnt +=abs(next_tmp+1)
            # 現在の合計が0の場合
            elif now_tmp == 0 :
                # 1個前がプラスの場合、
                if sum(a[:i]) > 0:
                    a[i] += -next_tmp+1
                    cnt +=abs(next_tmp+1)
                # 1個前がマイナスの場合
                else:
                    a[i] += next_tmp+1
                    cnt +=abs(next_tmp+1)
                
print(cnt)

Submission status: Runtime Error

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

totals = [0] * N
totals[0] = A[0]
con = 0

if totals[0] == 0:
  for i in range(1,N):
    if A[i] != 0:
      f = A[i]
      if f > 0:
        totals[0] = -1
      else:
        totals[0] = 1
    break
  else:
    totals[0] = 1
  con += 1

for i in range(1,N):
  totals[i] = totals[i - 1] + A[i]
  if totals[i - 1] * totals[i] >= 0:
    if totals[i - 1] < 0:
      con += abs(1 - totals[i])
      totals[i] += abs(1 - totals[i])
    elif totals[i - 1] > 0:
      con += abs(-1 - totals[i])
      totals[i] -= abs(-1 - totals[i])
  print(totals[:i + 1])
  
print(con)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n = int(input())

a = list(int(i) for i in input().split())
b = []
for i in range(0,len(a)):
    b.append(a[i])


def solve(cnt,A,N):
    for i in xrange(1, N):
        if sum(A[0:i])>0:
            if sum(A[0:i+1])>=0:
                r = A[i]
                A[i]=-sum(A[0:i])-1
                cnt+=abs(r-A[i])
        else:
            if sum(A[0:i+1])<=0:
                r = A[i]
                A[i]=-sum(A[0:i])+1
                cnt+=abs(r-A[i])
    return cnt

cnt1=0
if b[0]<=0:
    ini=b[0]
    b[0]=1
    cnt1=abs(1-ini)


ans1=solve(cnt1,b,n)
cnt2=0
if a[0]>=0:
    ini=a[0]
    a[0]=-1
    cnt2=abs(-1-ini)


ans2=solve(cnt2,a,n)
print(min(ans1,ans2))

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
n=int(input())
a=list(map(int,input().split()))

v=[]
for s in (1,-1):
    x=a[::]
    hugou=[s*(-1)**i sor i in range(n)]
    total=0
    for i in range(n):
        if (total+x[i])*hugou[i]<=0:
            x[i]=hugou[i]-total
        total+=xs[i]
    v.append(sum(absa[i]-x[i])for i in range(n))
print(min(v))

Submission status: Runtime Error

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

res = 0
x = 0
for i in range(n-1):
    x += a[i]
    sign = (x // abs(x)) * (-1)
    tmp = sign - (x + a[i+1])
    if sign < 0:
        tmp = min(tmp, 0)
    else:
        tmp = max(tmp, 0)
    res += abs(tmp)
    a[i+1] += tmp

print(res)

Submission status: Runtime Error

Solution code:
def main():
    import sys
    input = sys.stdin.readline

    n = int(input())
    a = list(map(int, input().split()))
    def Chk(a, pos):
        cnt = 0
        tmp = 0

        for a in A:
            tmp += a

            if pos and tmp < 1:
                cnt += 1 - tmp
                tmp = 1
            elif not pos and tmp > -1:
                cnt += 1 + tmp
                tmp = -1

            pos = not pos
        return cnt

    print(min(Chk(A, True), Chk(A, False)))
    
if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

def Chk(a, pos):
    cnt = 0
    tmp = 0
    
    for a in A:
        tmp += a
        
        if pos and tmp < 1:
            cnt += 1 - tmp
            tmp = 1
        elif not pos and tmp > -1:
            cnt += 1 + tmp
            tmp = -1
        
        pos = not pos
    return cnt

print(min(Chk(A, True), Chk(a, False)))

Submission status: Runtime Error

Solution code:
def main():
    import sys
    input = sys.stdin.readline

    n = int(input())
    a = list(map(int, input().split()))
    A = ()
    A_append = A.append
    cnt = 0

    for i in range(n-1):
        A_append(a[i])
        x = sum(A) + a[i+1]
        if sum(A) > 0 and x > 0:
            y = abs(x)+1
            cnt += y
            a[i+1] -= y

        elif sum(A) < 0 and x < 0:
            y = abs(sum(A) - a[i+1])-1
            cnt += y
            a[i+1] += y

    if sum(a) == 0:
        cnt += 1

    print(cnt)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

c = 10**15
for i in range(2):
    A = L[i]
    if A[0] != 0:
        ans = 0
        S = A[0]
        f = A[0]//abs(A[0])
    else:
        ans = 1
        S = 1
        f = 1

    for a in A[1:]:
        S += a
        if S == 0:
            ans += 1
            S = -f
        else:
            if S/abs(S) != f*(-1):
                ans += abs(S)+1
                S = -f
        f *= -1
    c = min(ans, c)
print(c)

Submission status: Runtime Error

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

ff = -1
for i in range(n):
    if A[i] != 0:
        ff = i
        break

if A[0] != 0:
    ans = 0
    S = A[0]
    f = A[0]//abs(A[0])
else:
    if ff == -1:
        ans = 1
        S[0] = 1
        f = 1
    else:
        if ff % 2 == 0:
            ans = 1
            S[0] = 1
            f = 1
        else:
            ans = 1
            S[0] = -1
            f = -1

for a in A[1:]:
    S += a
    if S == 0:
        ans += 1
        S = -f
    else:
        if S/abs(S) != f*(-1):
            ans += abs(S)+1
            S = -f
    f *= -1
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())

A = [int(_) for _ in input().split()]
ans = 0
s = A[0]

i = 1
while i < N:
    a = A[i]
    # print(i, s, ans)

    sign = (s // abs(s))
    abs_min = abs(s) + 1
    if a == 0:
        ans += abs_min
        s = -sign

        i += 1
        continue

    abs_a = abs(a)
    a_sign = (a // abs_a)
    if a_sign == sign:
        A[i] = 0
        ans += abs_a
    else:
        if abs_min <= abs_a:
            s = (abs_a - abs(s)) * a_sign
        else:
            ans += abs_min - abs_a
            s = -sign
        i += 1
print(ans)

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Here your code



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

result_1 = 0
before_sum =a[0]
after_sum =a[0]

for i in range(1,N):
    before_sum = after_sum
    after_sum = before_sum + a[i]
    if before_sum * after_sum > 0:
        if after_sum < 0:
            result_1 += 1 - after_sum
            after_sum = 1
        elif after_sum > 0:
            result_1 += 1 + after_sum
            after_sum = -1
    elif before_sum * after_sum == 0:
        result_1 += 1
        if before_sum < 0:
            after_sum = 1
        else:
            after_sum = -1

before_sum =int(-a[0]/abs(a[0]))
after_sum =before_sum
result_2 = 1 + abs(before_sum)


for i in range(1,N):
    before_sum = after_sum
    after_sum = before_sum + a[i]
    if before_sum * after_sum > 0:
        if after_sum < 0:
            result_2 += 1 - after_sum
            after_sum = 1
        elif after_sum > 0:
            result_2 += 1 + after_sum
            after_sum = -1
    elif before_sum * after_sum == 0:
        result_2 += 1
        if before_sum < 0:
            after_sum = 1
        else:
            after_sum = -1

print(min(result_1,result_2-1))

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Here your code



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

result_1 = 0
before_sum =a[0]
after_sum =a[0]

for i in range(1,N):
    before_sum = after_sum
    after_sum = before_sum + a[i]
    if before_sum * after_sum > 0:
        if after_sum < 0:
            result_1 += 1 - after_sum
            after_sum = 1
        elif after_sum > 0:
            result_1 += 1 + after_sum
            after_sum = -1
    elif before_sum * after_sum == 0:
        result_1 += 1
        if before_sum < 0:
            after_sum = 1
        else:
            after_sum = -1

before_sum =int(-a[0]/abs(a[0]))
after_sum =before_sum
result_2 = 1 + abs(before_sum)


for i in range(1,N):
    before_sum = after_sum
    after_sum = before_sum + a[i]
    if before_sum * after_sum > 0:
        if after_sum < 0:
            result_2 += 1 - after_sum
            after_sum = 1
        elif after_sum > 0:
            result_2 += 1 + after_sum
            after_sum = -1
    elif before_sum * after_sum == 0:
        result_2 += 1
        if before_sum < 0:
            after_sum = 1
        else:
            after_sum = -1


print(min(result_1,result_2))

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
n = int(input())
a = list(map(int,input().split()))
def operate(ls,x):
    sm = 0
    ans = 0
    ans += abs(ls[0]-x)
    k = x
    for i in range(1,len(ls)):
        sm += k
        if sm > 0:
            if abs(ls[i]) <= sm or sm < ls[i]:
                ans += abs(ls[i]+sm+1)
                k = -sm-1
            else:
                k = ls[i]
        if sm < 0:
            if abs(ls[i]) <= abs(sm) or sm > ls[i]:
                ans += abs(-sm+1-ls[i])
                k = -sm+1
            else:
                k = ls[i]
    return ans

anstot = []
if a[0] > 0:
    inv = -1
if a[0] < 0:
    inv = 1
if a[0] != 0:
    anstot.append(operate(a,a[0]))
    anstot.append(operate(a,inv))
if a[0] == 0:
    for i in range(n):
        if a[i] == 0:
            a[i] = -1**(i%2)
        if a[i] != 0:
            r = i
            break
    anstot.append(operate(r+a,a[0]))
    for i in range(r):
        a[i] = -1**((1+i)%2)
    anstot.append(operate(r+a,a[0]))    
print(min(anstot))

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
currentSum = 0
count3 = 0
count4 = 0

currentSum = 0
for i in range(N):
    restSum = currentSum
    currentSum += A[i]
    if currentSum <= 0 and restSum < 0:
        count3 += abs(currentSum) + 1
        currentSum = 1
    elif currentSum >= 0 and restSum > 0:
        count3 += abs(currentSum) + 1
        currentSum = -1
    elif A[i] <= 0 and restSum == 0:
        count3 += abs(currentSum) + 1
        currentSum = 1
        
currentSum = 0
for i in range(N):
    restSum = currentSum
    currentSum += A[i]
    if currentSum <= 0 and restSum < 0:
        count4 += abs(currentSum) + 1
        currentSum = 1
    elif currentSum >= 0 and restSum > 0:
        count4 += abs(currentSum) + 1
        currentSum = -1
    elif A[i] >= 0 and restSum == 0:
        count4 += abs(currentSum) + 1
        currentSum = -1
        
print(count1, count2, count3, count4)

Submission status: Runtime Error

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

tmp=0
tmp2=0
for i in range(N):
  tmp+=a[i]
  if i %2==0:
    if tmp>=0:
      tmp2+=abs(tmp)+1
      tmp=-1
  else:
    if tmp<=0:
      tmp2+=abs(tmp)+1
      tmp=1
print(min(ans,tmp))

Submission status: Runtime Error

Solution code:
n = int(input())
a_list = list(map(int,input().split()))
def greeed(sum_a,now_a,next_a,count):
  if now_a >= 0 and next_a >= 0:
    next_a = -1
    count += next_a + 1
  elif now_a < 0 and next_a <0:
    next_a = 0
    count += next_a
  if sum_a + next_a ==0:
    if next_a >=0:
      next_a += 1
      count += 1
    else :
      next_a = next_a -1
      count+= 1
  return count

count = 0
if a_list[0] ==0:
    if a_list[1]>=0:
        a_list = -1
        count+= 1
    else :
        a_list = 1
        count +=1
sum_a = a_list[0]
for i in range(0,n-1):
    now_a = a_list[i]
    next_a = a_list[i + 1]
    count = greeed(sum_a,now_a,next_a,count)
print(count)

Submission status: Runtime Error

Solution code:
n=int(input())
l=list(map(int,input().split()))
l.append(0)
a1=0
b1=0
a2=0
b2=0
for i in range(n):
    b1+=l[i]
    b2+=l[i]
    if i%2==0:
        if b1>=0:
            a1+=abs(-1-b1)
            b1=-1-l[i+1]
        if b2=<0:
            a2+=abs(1-b2)
            b2=1-l[i+1]
    else:
        if b1<=0:
            a1+=abs(1-b1)
            b1=1-l[i+1]
        if b2>=0:
            a2+=abs(-1-b2)
            b2=-1-l[i+1]
print(min(a1,a2))

Submission status: Runtime Error

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

i=0
while a[i]!=0and i!=n-1:i++
if a[i]>0:
  s0+=1
  count+=1
else:
  s0-=1
  count+=1
  

  for i in range(1,n):
  s1 = s0+a[i]
  if s0*s1>=0:
    if s1>0:
      a[i]-=(abs(s1)+1)
      count+=(abs(s1)+1)
    elif s1<0:
      a[i]+=(abs(s1)+1)
      count+=(abs(s1)+1)
    elif s1==0:
      if s0>0:
        a[i]-=1
        count+=1
      elif s0<0:
        a[i]+=1
        count+=1
  s0 += a[i]
print(count)

Submission status: Runtime Error

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

i=0
while a[i]!=0:i++
if a[i]>0:
  s0+=1
  count+=1
else:
  s0-=1
  count+=1
  

  for i in range(1,n):
  s1 = s0+a[i]
  if s0*s1>=0:
    if s1>0:
      a[i]-=(abs(s1)+1)
      count+=(abs(s1)+1)
    elif s1<0:
      a[i]+=(abs(s1)+1)
      count+=(abs(s1)+1)
    elif s1==0:
      if s0>0:
        a[i]-=1
        count+=1
      elif s0<0:
        a[i]+=1
        count+=1
  s0 += a[i]
print(count)

Submission status: Runtime Error

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

ttl = a[0]
cst = 0

if a[0]>0:
    flg = 1

elif a[0]<0:
    flg = -1

for i in range(1,n):
    ttl += a[i]
    

    if ttl*flg < 0:
        flg *= -1
    else:
        if flg > 0:
            memo = abs(ttl)+1
            ttl -= memo
            cst += memo
        elif flg < 0:
            memo = abs(ttl)+1
            ttl += memo
            cst += memo
        flg *= -1
   


ttl = a[0]
cst2 = 0


   

print(min(cst,cst2))

Submission status: Runtime Error

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

ttl = a[0]
cst = 0

if a[0]>0:
    flg = 1

elif a[0]<0:
    flg = -1

for i in range(1,n):
    ttl += a[i]
    

    if ttl*flg < 0:
        flg *= -1
    else:
        if flg > 0:
            memo = abs(ttl)+1
            ttl -= memo
            cst += memo
        elif flg < 0:
            memo = abs(ttl)+1
            ttl += memo
            cst += memo
        flg *= -1
   


ttl = a[0]
cst2 = 0

if a[0]>0:
    flg = -1
    cst2 += abs(ttl)+1
    ttl += 0-ttl-1 

elif a[0]<0:
    flg = 1
    cst2 += abs(ttl)+1
    ttl += 0-ttl+1 

for i in range(1,n):
    ttl += a[i]
    

    if ttl*flg < 0:
        flg *= -1
    else:
        if flg > 0:
            memo = abs(ttl)+1
            ttl -= memo
            cst2 += memo
        elif flg < 0:
            memo = abs(ttl)+1
            ttl += memo
            cst2 += memo
        flg *= -1
   

print(min(cst,cst2))

Submission status: Runtime Error

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

ttl = a[0]
cst = 0

if a[0]>0:
    flg = 1

elif a[0]<0:
    flg = -1

for i in range(1,n):
    ttl += a[i]
    

    if ttl*flg < 0:
        flg *= -1
    else:
        if flg > 0:
            memo = abs(ttl)+1
            ttl -= memo
            cst += memo
        elif flg < 0:
            memo = abs(ttl)+1
            ttl += memo
            cst += memo
        flg *= -1
    


ttl = a[0]
cst2 = 0

if a[0]>0:
    flg = -1
    cst2 += abs(ttl)+1
    ttl += 0-ttl-1 

elif a[0]<0:
    flg = 1
    cst2 += abs(ttl)+1
    ttl += 0-ttl+1 

for i in range(1,n):
    ttl += a[i]
    

    if ttl*flg < 0:
        flg *= -1
    else:
        if flg > 0:
            memo = abs(ttl)+1
            ttl -= memo
            cst2 += memo
        elif flg < 0:
            memo = abs(ttl)+1
            ttl += memo
            cst2 += memo
        flg *= -1
    print(cst2," ",ttl)

Submission status: Runtime Error

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

ans = 0
if a[0] > 0:
    op = "+"
elif a[0] < 0:
    op = "-"
elif a[0] == 0:
    ans += 1
    if a[1] > 0:
        a[0] = -1
    else:
        a[0] = 1

total = a[0]
for i in range(1, n):
    if (total+a[i]) >= 0 and op == "+":
        ans += abs((-1 - total) - a[i])
        total = -1
        op = "-"
    elif (total+a[i]) < 0 and op == "-":
        ans += abs((1 - total) - a[i])
        total = 1
        op = "+"
    elif total+a[i] == 0:
        ans += 1
        if a[1] > 0:
            a[0] = -1
        else:
            a[0] = 1
    else:
        total += a[i]
        if op == "+":
            op = "-"
        else:
            op = "+"
print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
N=list(map(int,input().split()))
emptylists=[]
number=0
for i in range(n):
	number+=N[i]
    emptylists.append(number)
ans1=0
use=0
for i in range(n):
    #偶数番が正、奇数番が負の時
    if i%2==0:
        #奇数番め
        if emptylists[i]+use<0:
            continue
        if emptylists[i]+use>=0:
            ans1+=emptylists[i]+1  
            use=use-emptylists[i]-1
        
    if i%2!=0:
        #偶数番め
        if emptylists[i]+use>0:
            continue
        if emptylists[i]+use<=0:
            ans1+=1-emptylists[i]-use
            use=use+1-emptylists[i]
ans2=0
uses=0
for i in range(n):
    #偶数番が負、奇数番が正の時
    if i%2!=0:
        #ぐう数番め
        if emptylists[i]<0:
            continue
        if emptylists[i]>=0:
            ans2+=emptylists[i]+1  
            uses=uses-emptylists[i]-1       
    if i%2==0:
        #き数番め
        if emptylists[i]>0:
            continue
        if emptylists[i]<=0:
            ans2+=1-emptylists[i]-uses
            uses=uses+1-emptylists[i]
print(min(ans1,ans2))

Submission status: Runtime Error

Solution code:
n = int(input())
a = [] * n
a = list(map(int, input().split()))
if a[0] > 0:
  symbol = "plus"
elif a[0] < 0:
  symbol = "minus"
sum1 = a[0]
count = 0
for i in range(1, n):
  sum1 += a[i]
  if symbol == "plus" and sum1 >= 0:
    operation = sum1 + 1
    count += operation
    sum1 = -1
  elif symbol == "minus" and sum1 <= 0:
    operation = abs(sum1) + 1
    count += operation
    sum1 = 1
  if symbol == "plus":
    symbol = "minus"
  else:
    symbol = "plus"
print(count)

Submission status: Runtime Error

Solution code:
n = int(input())
a = [] * n
a = list(map(int, input().split()))
if a[0] > 0:
  symbol = "plus"
elif a[0] < 0:
  symbol = "minus"
sum1 = a[0]
count = 0
for i in range(1, n):
  sum1 += a[i]
  if symbol == "plus" and sum1 >= 0:
    operation = sum1 + 1
    count += operation
    sum1 -= operation
  elif symbol == "minus" and sum1 <= 0:
    operation = abs(sum1) + 1
    count += operation
    sum1 += operation
  if symbol == "plus":
    symbol = "minus"
  else:
    symbol = "plus"
print(count)

Submission status: Runtime Error

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

ans = 0
s = A[0]
if s > 0:
  flag = 1
elif s < 0:
  flag = -1
elif if A[1] < 0:
  flag = 1
  ans += 1
else:
  flag = -1
  ans += 1
for i in range(1,N):
  s += A[i]
  if flag == 1 and s >= 0:
    ans += s + 1
    s = -1
  elif flag == -1 and s <= 0:
    ans += 1 - s
    s = 1
  flag *= -1
print(ans)