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

Solution (Python):
import time
ts=time.time()
from heapq import heappush, heappop
from collections import deque,defaultdict,Counter
from itertools import permutations,groupby
import sys
import bisect
import string
import random
alp=string.ascii_lowercase
sys.setrecursionlimit(10**6)
def SI():
    return input().split()
def MI():
    return map(int,input().split())
def I():
    return int(input())
def LI():
    return [int(i) for i in input().split()]

n=I()
a=LI()
def solve(a,x):
    if a[0]*x>0:
        p=a[0]
        m=0
    else:
        p=x
        m=abs(a[0]-x)
    for i in range(1,n):
        t=-1 if p>0 else 1
        if (a[i]+p)*t>0:
            p+=a[i]
        else:
            m+=abs(a[i]+p-t)
            p+=a[i]+t*abs(a[i]+p-t)
    return m

print(min(solve(a,1),solve(a,-1)))

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

sa = [0, 0]
count = [0, 0]

for i in range(n):
    sa[0] += a[i]
    if sa[0] <= 0 and i%2 == 0:
        count[0] += 1 - sa[0]
        sa[0] = 1
    elif sa[0] >= 0 and i%2 == 1:
        count[0] += 1 + sa[0]
        sa[0] = -1

    sa[1] += a[i]
    if sa[1] <= 0 and i%2 == 1:
        count[1] += 1 - sa[1]
        sa[1] = 1
    elif sa[1] >= 0 and i%2 == 0:
        count[1] += 1 + sa[1]
        sa[1] = -1

print(min(count))

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

sa = 0
count = [0, 0]

for i in range(n):
    now = a[i]
    sa += now
    if sa <= 0 and i%2 == 0:
        count[0] += 1 - sa
        sa = 1
    elif sa >= 0 and i%2 == 1:
        count[0] += 1 + sa
        sa = -1

sa = 0

for i in range(n):
    now = a[i]
    sa += now
    if sa <= 0 and i%2 == 1:
        count[1] += 1 - sa
        sa = 1
    elif sa >= 0 and i%2 == 0:
        count[1] += 1 + sa
        sa = -1

print(min(count))

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


def check(a, t):
    ans = 0
    x = 0
    for i in a:
        x += i
        if t == True and x < 1:
            ans += 1 - x
            x = 1
        elif t == False and x > -1:
            ans += x + 1
            x = -1
        t = not t
    return ans


print(min(check(a, True), check(a, False)))

Solution (Python):
import sys
input=sys.stdin.readline


def main():
    N = int(input())
    A = list(map(int, input().split()))
    tmp = A.copy()
    mi = -1
    for j in range(2):
        A = tmp.copy()
        s = 0
        n = 0
        if j == 0: 
            if A[0] >= 0:
                n += abs(A[0] +1)
                A[0] = -1
        else:   
            if A[0] <= 0:
                n += abs(A[0] -1)
                A[0] = 1
        s = A[0]

        for i in range(1,N):
            if s * (s+A[i]) >= 0:
                if s < 0:
                    n += abs(-s+1 -A[i])
                    A[i] = -s+1
                else:
                    n += abs(-s-1 -A[i])
                    A[i] = -s-1
            s += A[i]
        if mi == -1:
            mi = n
        else:
            mi = min(mi, n)
    print(mi)

if __name__ == '__main__':
    main()

Solution (Python):


from collections import Counter,deque


from sys import stdin,setrecursionlimit


setrecursionlimit(10**6)
input=stdin.readline
n=int(input().rstrip())
a=[int(i) for i in input().split()]

def rep(l,b):
    cnt,s=0,0
    for i in l:
        s+=i
        if b and s<1:
           
            cnt+=1-s
            s=1
        elif not b and s>-1:
            
            cnt+=s+1
            s=-1
        
        b=not b
    return cnt

print(min(rep(a,1),rep(a,0)))

Solution (Python):
N = int(input())
A = [int(s) for s in input().split()]
ans1 = 0
s = 0
for i, a in enumerate(A):
    s += a
    if i % 2 is 0:
        d = max(0, 1 - s)
        s += d
        ans1 += d
    else:
        d = max(0, 1 + s)
        s -= d
        ans1 += d
ans2 = 0
s = 0
for i, a in enumerate(A):
    s += a
    if i % 2 is 1:
        d = max(0, 1 - s)
        s += d
        ans2 += d
    else:
        d = max(0, 1 + s)
        s -= d
        ans2 += d
print(min(ans1, ans2))

Solution (Python):
import numpy as np
import bisect
from operator import itemgetter
import math
import functools
import itertools
import numpy as np
import sys
MAX_INT = int(10e10)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N = I()
a = IL()

l = []
tmp = 0
for i in range(N):
  tmp += a[i]
  l.append(tmp)

ans1 = 0
cnt = 0
tmp = 0
for i in range(N):
  if i%2 == 0:
    if l[i]+tmp > 0:
      continue
    else:
      cnt = -(l[i]+tmp) +1
      tmp += cnt
      ans1 += abs(cnt)
  else:
    if l[i]+tmp < 0:
      continue
    else:
      cnt = -(l[i]+tmp) -1
      tmp += cnt
      ans1 += abs(cnt)

ans2 = 0
cnt = 0
tmp = 0
for i in range(N):
  if i%2 == 0:
    if l[i]+tmp < 0:
      continue
    else:
      cnt = -(l[i]+tmp) -1
      tmp += cnt
      ans2 += abs(cnt)
  else:
    if l[i]+tmp > 0:
      continue
    else:
      cnt = -(l[i]+tmp) +1
      tmp += cnt
      ans2 += abs(cnt)


print(min(ans1,ans2))

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
A = a[:]
count_plus=0
count_minus=0
x = 0
y = 0
for i in range(n):
  if i%2==0:
    x+=a[i]
    if x<=0:
      count_plus+=-1*x+1
      a[i]+=-1*x+1
      x=1
  else:
    x+=a[i]
    if x>0:
      count_plus+=x+1
      a[i]+=-1*x-1
      x=-1
for i in range(n):
  if i%2==0:
    y+=A[i]
    if y>=0:
      count_minus+=y+1
      A[i]+=-1*y-1
      y=-1
  else:
    y+=A[i]
    if y<0:
      count_minus+=-1*y+1
      A[i]+=-1*y+1
      y=1
if x==0:
  count_plus+=1
if y==0:
  count_minus+=1
print(min(count_plus,count_minus))

Solution (Python):
n=int(input())
A=list(map(int,input().split()))
if A[0]>0:
  S=A[0]
  ans_1=0
else:
  S=1
  ans_1=1-A[0]
for i in A[1:]:
  if S*(S+i)<0:
    S+=i
  else:
    ans_1+=abs(S+i)+1
    if S<0:
      S=1
    else:
      S=-1
if A[0]<0:
  S=A[0]
  ans_2=0
else:
  S=-1
  ans_2=A[0]+1
for i in A[1:]:
  if S*(S+i)<0:
    S+=i
  else:
    ans_2+=abs(S+i)+1
    if S<0:
      S=1
    else:
      S=-1
print(min(ans_1,ans_2))

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

def chk(a,first):
    count = 0
    x = 0 
    t = first 
    for item in a:
        x += item
        if t == True and x<1:
            count += 1-x
            x = 1
        elif t == False and x>-1:
            count += x+1
            x = -1
        t = not t
    return count

print(min(chk(a,True),chk(a,False)))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
import numpy as np
na1 = np.array(a).cumsum()
na2 = np.array(a).cumsum()
cnt1 = 0
hoge1 = 0
cnt2 = 0
hoge2 = 0
delta1 = 0
delta2 = 0
for i in range(n):
  na1[i] += hoge1
  na2[i] += hoge2
  if(i % 2 == 0 and na1[i] <= 0):
    delta1 = abs(na1[i]) + 1
    cnt1 = cnt1 + delta1
    hoge1 += delta1
    na1[i] += delta1
  elif(i % 2 == 1 and na1[i] >= 0):
    delta1 = abs(na1[i]) + 1 
    cnt1 = cnt1 + delta1
    hoge1 -= delta1
    na1[i] -= delta1    
  if(i % 2 == 1 and na2[i] <= 0):
    delta2 = abs(na2[i]) + 1
    cnt2 = cnt2 + delta2
    hoge2 += delta2
    na2[i] += delta2  
  elif(i % 2 == 0 and na2[i] >= 0):
    delta2 = abs(na2[i]) + 1
    cnt2 = cnt2 + delta2
    hoge2 -= delta2
    na2[i] -= delta2
  else:
    na1[i]
print(min([abs(cnt1),abs(cnt2)]))

Solution (Python):
n=input()
a=[int(i) for i in input().split()]
def chk(a,t):
    ans=0
    x=0
    for i in a:
        x+=i
        if t==True and x<1:
            ans+=1-x
            x=1
        elif t==False and x>-1:
            ans+=x+1
            x=-1
        t=not t
    return ans
print(min(chk(a,True),chk(a,False)))

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

    def solver(sign):
        ret, total = 0, 0
        for a in A:
            total += a
            if sign * total <= 0:
                ret += abs(sign - total)  
                total = sign
            sign *= -1
        return ret
    
    return min(solver(1), solver(-1))

print(c_sequence())

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
ans = {True:0, False:0}
for s in [True, False]:
    sign = s
    total = 0
    for i in range(len(A)):
        if total + A[i] == 0 or sign == (total + A[i] > 0):
            ans[s] += abs(total + A[i]) + 1
            total = -1 if sign else 1
        else:
            total += A[i]
        sign = total > 0
print(min(ans.values()))

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
ans1 = 0
sign = True
total = 0
for i in range(len(A)):
    if sign:
        if total + A[i] >= 0:
            ans1 += total + A[i] + 1
            total = -1
        else:
            total += A[i]
    else:
        if total + A[i] <= 0:
            ans1 += 1 - (total + A[i])
            total = 1
        else:
            total += A[i]
    sign = total > 0
ans2 = 0
sign = False
total = 0
for i in range(len(A)):
    if sign:
        if total + A[i] >= 0:
            ans2 += total + A[i] + 1
            total = -1
        else:
            total += A[i]
    else:
        if total + A[i] <= 0:
            ans2 += 1 - (total + A[i])
            total = 1
        else:
            total += A[i]
    sign = total > 0
print(min(ans1, ans2))

Solution (Python):
import numpy as np

N = int(input())
A = list(map(int,input().split()))
sumA = [max(A[0],1)]
ans1 = abs(sumA[0]-A[0])
for i in range(1,N):
    sumA.append(sumA[-1]+A[i])
    if np.sign(sumA[-2]) == np.sign(sumA[-1]) or np.sign(sumA[-1]) == 0:
        ans1 += abs(sumA[-1] + np.sign(sumA[-2]))
        sumA[-1] = -np.sign(sumA[-2])
sumA = [min(A[0],-1)]
ans2 = abs(sumA[0]-A[0])
for i in range(1,N):
    sumA.append(sumA[-1]+A[i])
    if np.sign(sumA[-2]) == np.sign(sumA[-1]) or np.sign(sumA[-1]) == 0:
        ans2 += abs(sumA[-1] + np.sign(sumA[-2]))
        sumA[-1] = -np.sign(sumA[-2])
print(min(ans1,ans2))

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

if a[0] > 0:
  A = 0
  t = a[0]
else:
  A = -a[0] + 1
  t = 1
for i in range(1, n):
  if i % 2 == 1:
    if t + a[i] >= 0:
      A += t + a[i] + 1
      t = -1
    else:
      t = t + a[i]
  else:
    if t + a[i] <= 0:
      A += -(t + a[i]) + 1
      t = 1
    else:
      t = t + a[i]

if a[0] < 0:
  B = 0
  t = a[0]
else:
  B = a[0] + 1
  t = -1
for i in range(1, n):
  if i % 2 == 0:
    if t + a[i] >= 0:
      B += t + a[i] + 1
      t = -1
    else:
      t = t + a[i]
  else:
    if t + a[i] <= 0:
      B += -(t + a[i]) + 1
      t = 1
    else:
      t = t + a[i]

print(min(A, B))

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

ans = 10**10


cost1 = 0
s = 0
for i in range(n):
    t = 1 if i % 2 == 0 else -1
    c = 0
    if i % 2 == 0:
        if s + a[i] > 0:
            s += a[i]
        else:
            c = abs(s - t)
            s += c
            cost1 += abs(c - a[i])
    else:
        if s + a[i] < 0:
            s += a[i]
        else:
            c = abs(s - t)
            s -= c
            cost1 += abs(- c - a[i])

cost2 = 0
s = 0
for i in range(n):
    t = -1 if i % 2 == 0 else 1
    c = 0
    if i % 2 == 1:
        if s + a[i] > 0:
            s += a[i]
        else:
            c = abs(s - t)
            s += c
            cost2 += abs(c - a[i])
    else:
        if s + a[i] < 0:
            s += a[i]
        else:
            c = abs(s - t)
            s -= c
            cost2 += abs(- c - a[i])

print(min(cost1, cost2))

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

def sign(n):
  if n > 0:
    return 'pos'
  elif n < 0:
    return 'neg'
  else:
    return 'zero'



result_pos = 0
result_neg = 0
now_sum_pos = 0
now_sum_neg = 0
for i,a in enumerate(A):
  now_sum_pos += a
  now_sum_neg += a
  if i%2 == 0:
    if not sign(now_sum_pos) == 'pos':
      result_pos += 1+abs(now_sum_pos)
      now_sum_pos = 1
    if not sign(now_sum_neg) == 'neg':
      result_neg += 1+abs(now_sum_neg)
      now_sum_neg = -1
  elif i%2 == 1:
    if not sign(now_sum_pos) == 'neg':
      result_pos += 1+abs(now_sum_pos)
      now_sum_pos = -1
    if not sign(now_sum_neg) == 'pos':
      result_neg += 1+abs(now_sum_neg)
      now_sum_neg = 1
print(min(result_pos, result_neg))

Solution (Python):
def main():
    n = int(input())
    A = [int(a) for a in input().split()]
    min_count = float('inf')
    if A[0] != 0:
        tot = A[0]
        count = 0
        for a in A[1:]:
            if tot > 0 and tot + a >= 0:
                count += tot+a+1
                tot = -1
            elif tot < 0 and tot + a <= 0:
                count += -(tot+a)+1
                tot = 1
            else:
                tot += a
        min_count = min(min_count, count)
    for i in [1, -1]:
        count = abs(i - A[0])
        tot = i
        for a in A[1:]:
            if tot > 0 and tot + a >= 0:
                count += tot+a+1
                tot = -1
            elif tot < 0 and tot + a <= 0:
                count += -(tot+a)+1
                tot = 1
            else:
                tot += a
        min_count = min(min_count, count)
    return min_count

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

Solution (Python):
n = int(input())
f,*A,= map(int,input().split())
pcnt = 0 if f>0 else 1-f
ncnt = 0 if f<0 else f+1
pcur = max(1,f)
ncur = min(-1,f)
for i,a in enumerate(A):
	if pcur*(pcur+a)<0:
		pcur += a
	else:
		pcnt += abs((-1)**(i+1)-(pcur+a))
		pcur = (-1)**(i+1)
	if ncur*(ncur+a)<0:
		ncur += a
	else:
		ncnt += abs((-1)**i-(ncur+a))
		ncur = (-1)**i
print(min(pcnt,ncnt))

Solution (Python):

N = int(input())

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



odd = 0
total = 0
for i in range(N):
    total += A[i]
    if i % 2 == 0: 
        if total > -1:
            odd += 1 + total
            total = -1
    else:
        if total < 1:
            odd += 1 - total
            total = 1


even = 0
total = 0
for i in range(N):
    total += A[i]
    if i % 2 == 0: 
        if total < 1:
            even += 1 - total
            total = 1
    else:
        if total > -1:
            even += 1 + total
            total = -1


print(min(odd, even))

Solution (Python):
def main():
    n = int(input())
    a = [int(i) for i in input().split()]
    i = 0
    
    s = 0
    tmp1 = 0
    if a[i] > 0:
        s = a[i]
    else:
        tmp1 = abs(a[i])+1
        s = 1
    i = 1
    while i < n:
        if not s * (s+a[i]) < 0:
            tmp1 += abs(s+a[i])+1
            if i % 2 == 0:
                s = 1
            else:
                s = -1
        else:
            s += a[i]
        i += 1
    
    s = 0
    tmp2 = 0
    i = 0
    if a[i] < 0:
        s = a[i]
    else:
        tmp2 = abs(a[i])+1
        s = -1
    i = 1
    while i < n:
        if not s * (s+a[i]) < 0:
            tmp2 += abs(s+a[i])+1
            if i % 2 == 1:
                s = 1
            else:
                s = -1
        else:
            s += a[i]
        i += 1
    print(min(tmp1, tmp2))

if __name__ == '__main__':
    main()

Solution (Python):
nums = int(input())
ary = list(map(int,input().split()))


count = 0
sum = ary[0]
if ary[0] < 0 :
    count += abs(ary[0])+1
    sum = 1
elif ary[0] == 0:
    sum = 1
    count += 1

for i in range (1,nums):
    sum += ary[i]

    if i % 2 == 1:
        if sum > 0 :
            count += abs(sum)+1
            sum = -1
        elif sum == 0:
            sum = -1
            count += 1
    elif i % 2 == 0:
        if sum < 0:
            count += abs(sum)+1
            sum  = 1
        elif sum == 0:
            sum = 1
            count += 1

result_plus = count


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

for i in range(1,nums):
    sum += ary[i]
    if i % 2 == 1:
        if sum < 0:
            count += abs(sum)+1
            sum = 1
        elif sum == 0:
            count += 1
            sum = 1
    if i % 2 == 0:
        if sum > 0:
            count += abs(sum)+1
            sum = -1
        elif sum == 0:
            count += 1
            sum = -1

result_minus = count

if result_plus > result_minus:
    print(result_minus)
else:
    print(result_plus)

Solution (Python):
from copy import copy
n = int(input())
a = [int(x) for x in input().split()]
ans1=[(-1)**i for i in range(n)]

b=copy(a)
res_b=0
sb=0
c=copy(a)
res_c=0
sc=0
for i in range(n):
  sb+=a[i]
  if ans1[i]*sb>0:
    pass
  else:
    b[i]=ans1[i]-(sb-b[i])
    sb=sb-a[i]+b[i]
    res_b+=abs(b[i]-a[i])

  sc+=a[i]
  if -1*ans1[i]*sc>0:
    pass
  else:
    c[i]=-1*ans1[i]-(sc-c[i])
    sc=sc-a[i]+c[i]
    res_c+=abs(c[i]-a[i])
  
print(min(res_b,res_c))

Solution (Python):
"""


 = sys.stdin.readline().rstrip()
 = list(sys.stdin.readline().rstrip())


 = int(sys.stdin.readline())
 = map(int, sys.stdin.readline().split())
 = list(map(int, sys.stdin.readline().split()))
 = [list(map(int,list(sys.stdin.readline().split()))) for i in range(h)]  


a = 100
b = 0.987654321
print("{0:06d}-{1:6f}".format(a,b))
000100-0.987654


char_list = list("abcd") 
"""

import math
import sys
import itertools
import queue
from fractions import gcd

def lcm(a, b):
    return a * b // gcd(a, b)

mod = 1000000007

if __name__ == "__main__":
    n = int(sys.stdin.readline())
    a = list(map(int, sys.stdin.readline().split()))
    def sequence(a,t):
        ans = 0
        x = 0
        for e in a:
            x += e
            if t == True and x<1:
                ans += 1-x
                x=1
            elif t== False and x>-1:
                ans += x+1
                x=-1
            t = not t
        return ans
    print(min(sequence(a,True),sequence(a,False)))

Solution (Python):
n = int(input())
As = list(map(int, input().split()))
rs = []
for m in {0, 1}:
  r = 0
  s = 0
  for i in range(n):
    A = As[i]
    if i%2 == m:
      if s+A >= 0:
        r += s+A+1
        s = -1
      else:
        s += A
    else:
      if s+A <= 0:
        r += -(s+A)+1
        s = +1
      else:
        s += A
  
  rs.append(r)
r = min(rs)
print(r)

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

def f(a,t):
  ans=0
  cur=0
  for i in a:
    cur+=i
    if t==True and cur<1:
      ans+=(1-cur)
      cur=1
    elif t==False and cur>-1:
      ans+=(1+cur)
      cur=-1
    t=not t
  return ans
print(min(f(a,True),f(a,False)))

Solution (Python):
n=int(input())
A=list(map(int,input().split()))
def chk(A,t):
  ans=0
  s=0
  for i in A:
    s+=i
    if t==True and s<1:
      ans+=1-s
      s=1
    elif t==False and s>-1:
      ans+=1+s
      s=-1
    t=not t
  return ans
print(min(chk(A,True),chk(A,False)))

Solution (Python):

import bisect
import collections
import itertools

def getint(): return int(input())
def getints(): return list(map(int, input().split()))
def getint2d(rows): return [getints() for _ in range(rows)]
def getgrid(rows): return [input() for _ in range(rows)]
def array1d(n, value): return [value for _ in range(n)]
def array2d(n, m, value): return [array1d(m, value) for _ in range(n)]

n = getint()
values = getints()

def solve(values, prev):
    res = 0
    for v in values:
        s = prev + v
        add = 0
        if prev > 0 and s >= 0:
            add = -s - 1
        if prev < 0 and s <= 0:
            add = -s + 1
        res += abs(add)
        prev = s + add
    return res

res1, res2 = 0, 0
if values[0] <= 0:
    add = abs(values[0]) + 1
    res1 = solve(values[1:], values[0] + add) + abs(add)
    add = -1 if values[0] == 0 else 0
    res2 = solve(values[1:], values[0] + add) + abs(add)
else:
    add = 1 if values[0] == 0 else 0
    res1 = solve(values[1:], values[0] + add) + abs(add)
    add = -(values[0] + 1)
    res2 = solve(values[1:], values[0] + add) + abs(add)

print(min(res1, res2))

Solution (Python):



n = int(input())
a = list(map(int, input().split()))
count_p = 0  
count_m = 0  
s = 0  


def counter(a, s):
    if s * (s + a) < 0:
        return 0
    else:
        if s < 0:
            return 1 - (s + a)
        else:
            return -1 - (s + a)



if a[0] <= 0:
    count_p = 1 - a[0]
    s = 1
else:
    s = a[0]

for i in range(n-1):
    tmp = counter(a[i + 1], s)
    s = s + a[i+1]+tmp
    count_p = count_p + abs(tmp)


if a[0] >= 0:
    count_m = 1 + a[0]
    s = -1
else:
    s = a[0]


for i in range(n-1):
    tmp = counter(a[i + 1], s)
    s = s + a[i+1]+tmp
    count_m = count_m + abs(tmp)

print(str(min(count_p, count_m)))

Solution (Python):
import numpy as np
n = int(input())
A = [int(i) for i in input().split()]

def j(B,sign):
    ans,cur = 0,[0]
    for i in range(n):
        cur.append(cur[i]+B[i])
        cs = np.sign(cur[i+1])
        if cs != sign:
            if sign == -1:
                ans += -(-cur[i]-1)+B[i]
                cur[i+1] = -1
            else:
                ans += -cur[i]+1-B[i]
                cur[i+1] = 1
        sign *= -1
    return ans

print(min(j(A,1),j(A,-1)))

Solution (Python):

import sys
INF = float("inf")


def solve(n: int, a: "List[int]"):

    
    tot = 0
    countA = 0
    for i, x in enumerate(a):
        tot += x
        if i % 2 == 0:
            if tot <= 0:
                countA += -tot+1
                tot = 1
        else:
            if tot >= 0:
                countA += tot+1
                tot = -1

    tot = 0
    countB = 0
    for i, x in enumerate(a):
        tot += x
        if i % 2 == 1:
            if tot <= 0:
                countB += -tot+1
                tot = 1
        else:
            if tot >= 0:
                countB += tot+1
                tot = -1
    print(min(countA, countB))

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    n = int(next(tokens))  
    a = [int(next(tokens)) for _ in range(n)]  
    solve(n, a)


if __name__ == '__main__':
    main()

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

tmp1 = 0
sum1 = 0
tmp2 = 0
sum2 = 0

for i in range(n):
    sum1 += a[i]
    if i % 2 == 0:
        if sum1  > 0 :
            pass
        else:
            tmp1 += 1 - sum1
            sum1 =  1
    else:
        if sum1  < 0 :
            pass
        else:
            tmp1 += sum1 - (-1)
            sum1 =  -1

for i in range(n):
    sum2 += a[i]
    if i % 2 == 1:
        if sum2  > 0 :
            pass
        else:
            tmp2 += 1 - sum2
            sum2 =  1
    else:
        if sum2  < 0 :
            pass
        else:
            tmp2 += sum2 - (-1)
            sum2 =  -1

ans = min(tmp1, tmp2)
print(ans)

Solution (Python):
n=input()
a=list(map(int,input().split()))
ans=mans=0
if a[0]==0:
    ans+=1
    mans+=1
    sim=1
    msim=-1
elif a[0]>0:
    ans=0
    sim=a[0]
    mans+=a[0]+1
    msim=-1
else:
    ans=-a[0]+1
    sim=1
    mans=0
    msim=a[0]
for i in a[1:]:
    if sim<0:
        sim+=i
        if sim<=0:
            ans+=1-sim
            sim=1
    else:
        sim+=i
        if sim>=0:
            ans+=sim+1
            sim=-1
    if msim<0:
        msim+=i
        if msim<=0:
            mans+=1-msim
            msim=1
    else:
        msim+=i
        if msim>=0:
            mans+=msim+1
            msim=-1
print(min(ans,mans))

Solution (Python):
n = int(input())
*l, = map(int,input().split())
def calc(l,even):
  ans = 0
  l1 = l[:]
  s = 0
  for i in range(n):
    s += l1[i] 
    if i%2 == even and s<=0: 
      ans += -s+1 
      s = 1
    elif i%2 != even and s>=0: 
      ans += s+1
      s = -1
  return ans
print(min([calc(l,0),calc(l,1)]))

Solution (Python):
n = int(input())
an = list(map(int,input().split()))

def f(x):
    ans = 0
    count = 0
    for a in an:
        count += a
        if count * x >= 0:
            ans += abs(count)+1
            count = -x
        x *= -1
    return ans


print(min(f(1),f(-1)))

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


def chk(a, t):
    ans = 0
    x = 0
    for i in a:
        x += i
        if t == True and x < 1:
            ans += 1 - x
            x = 1
        if t == False and x > -1:
            ans += x + 1
            x = -1
        t = not t
    return ans


print(min(chk(a, True), chk(a, False)))

Solution (Python):
n=int(input())
lista=list(map(int,input().split()))

goukei=0
count=0

for i in range(n):
    number=lista[i]+goukei
    if i%2==0:
        if number>=0:
            count=count+number+1
            goukei=-1
        else:
            goukei=number
    else:
        if number<=0:
            count=count+abs(number)+1
            goukei=1
        else:
            goukei=number

count1=count

goukei=0
count=0

for i in range(n):
    number=lista[i]+goukei
    if i%2==1:
        if number>=0:
            count=count+number+1
            goukei=-1
        else:
            goukei=number
    else:
        if number<=0:
            count=count+abs(number)+1
            goukei=1
        else:
            goukei=number
print(min(count,count1))

Solution (Python):
n=int(input())
lista=list(map(int,input().split()))

goukei=0
count=0

for i in range(n):
    number=lista[i]+goukei
    if i%2==0:
        if number>=0:
            count=count+number+1
            goukei=-1
        else:
            goukei=number
    else:
        if number<=0:
            count=count+abs(number)+1
            goukei=1
        else:
            goukei=number

count1=count

goukei=0
count=0

for i in range(n):
    number=lista[i]+goukei
    if i%2==1:
        if number>=0:
            count=count+number+1
            goukei=-1
        else:
            goukei=number
    else:
        if number<=0:
            count=count+abs(number)+1
            goukei=1
        else:
            goukei=number
print(min(count,count1))

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

pos = 0
left = A[0]
if left <= 0:
    pos += 1 - left
    left = 1
for a in A[1:]:
    if left * (left + a) < 0:
        left += a
    else:
        if left + a == 0:
            if left < 0:
                left += a + 1
            else:
                left += a - 1
            pos += 1
        else:
            pos += abs(left + a) + 1
            if left < 0:
                left = 1
            else:
                left = -1

neg = 0
left = A[0]
if left >= 0:
    neg += left + 1
    left = -1
for a in A[1:]:
    if left * (left + a) < 0:
        left += a
    else:
        if left + a == 0:
            if left < 0:
                left += a + 1
            else:
                left += a - 1
            neg += 1
        else:
            neg += abs(left + a) + 1
            if left < 0:
                left = 1
            else:
                left = -1

print(min(neg, pos))

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

a1 = list(a)
s1 = 0
for i in range(n):
    if i % 2 == 0:
        if s1 + a1[i] >= 0:
            a1[i] = -1 - s1
    else:
        if s1 + a1[i] <= 0:
            a1[i] =  1 - s1
    s1 += a1[i]


a2 = list(a)
s2 = 0
for i in range(n):
    if i % 2 == 0:
        if s2 + a2[i] <= 0:
            a2[i] =  1 - s2
    else:
        if s2 + a2[i] >= 0:
            a2[i] = -1 - s2
    s2 += a2[i]

t1 = sum(abs(x - y) for x, y in zip(a, a1))
t2 = sum(abs(x - y) for x, y in zip(a, a2))
t = min(t1, t2)
print(t)

Solution (Python):

import sys
import copy
import collections
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import math
import itertools
import random
 




 
INF = float('inf')
def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
 
def main():
    n = inputInt()
    a = inputList()
    aa = []
    for i,val in enumerate(a):
        if i == 0:
            aa.append(val)
        else:
            tmp = val + aa[i-1]
            aa.append(tmp)
            
    barans = 0
    ans = 0
    flg = True
    for i,val in enumerate(aa):
        if i == 0:
            if val <= 0:
                barans = 1 - val
                ans += barans
        else:
            val_sin = val + barans
            if flg == True:
                flg = False
                if val_sin >= 0:
                    tmp = 1 + val_sin
                    ans += tmp
                    barans -= tmp
            else:
                flg = True
                if val_sin <= 0:
                    tmp = 1 - val_sin
                    ans += tmp
                    barans += tmp
        
    barans = 0
    ansans = 0
    flg = False
    for i,val in enumerate(aa):
        if i == 0:
            if val >= 0:
                barans = 1 + val
                ansans += barans
                barans = -1 * barans
        else:
            val_sin = val + barans
            if flg == True:
                flg = False
                if val_sin >= 0:
                    tmp = 1 + val_sin
                    ansans += tmp
                    barans -= tmp
            else:
                flg = True
                if val_sin <= 0:
                    tmp = 1 - val_sin
                    ansans += tmp
                    barans += tmp
        
    
    print(min(ans,ansans))
        
    
if __name__ == "__main__":
	main()

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
s = []
for i in range(n):
    if i == 0:
        s.append(a[0])
    else:
        s.append(a[i]+s[-1])

count = 0
ans1 = 0
for i in range(n):
    if i%2 == 0:
        if s[i]+count > 0:
            continue
        else:
            ans1 += 1-(s[i]+count)
            count += 1-(s[i]+count)
    else:
        if s[i]+count < 0:
            continue
        else:
            ans1 += (s[i]+count) + 1
            count -= (s[i]+count) + 1
            
count1 = 0
ans2 = 0
for i in range(n):
    if i%2 == 1:
        if s[i]+count1 > 0:
            continue
        else:
            ans2 += 1-(s[i]+count1)
            count1 += 1-(s[i]+count1)
    else:
        if s[i]+count1 < 0:
            continue
        else:
            ans2 += (s[i]+count1) + 1
            count1 -= (s[i]+count1) + 1
            
print(min(ans1,ans2))