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):

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

S1=0
cost1=0
for i in range(n):
    if i%2==0:
        if S1+A[i]>0:
            S1+=A[i]
        else:
            cost1+=abs(S1+A[i])+1
            S1=1
    else:
        if S1+A[i]<0:
            S1+=A[i]
        else:
            cost1+=abs(S1+A[i])+1
            S1=-1

S2=0
cost2=0
for i in range(n):
    if i%2==0:
        if S2+A[i]<0:
            S2+=A[i]
        else:
            cost2+=abs(S2+A[i])+1
            S2=-1
    else:
        if S2+A[i]>0:
            S2+=A[i]
        else:
            cost2+=abs(S2+A[i])+1
            S2=1
    
print(min(cost1,cost2))

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

res1 = 0
S = 0

for i in range(n):
    S += a[i]
    if i % 2 == 0:
        if S <= 0:
            res1 += abs(1 - S)
            S = 1
    else:
        if S >= 0:
            res1 += abs(-1 - S)
            S = -1

res2 = 0
S = 0
for i in range(n):
    S += a[i]
    if i % 2 == 1:
        if S <= 0:
            res2 += abs(1 - S)
            S = 1
    else:
        if S >= 0:
            res2 += abs(-1 - S)
            S = -1

print(min(res1, res2))

Solution (Python):
from collections import defaultdict, Counter, namedtuple, deque
import itertools
import functools
import bisect
import heapq
import math
from fractions import gcd

NN = 202020
MOD = 10**9+7
INF = float("inf")

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

init_pos = 0
psum = 0
for i in range(n):
    psum += A[i]
    if i%2 == 0 and psum <= 0:
        init_pos += 1-psum
        psum = 1
    elif i%2 == 1 and psum >= 0:
        init_pos += psum + 1
        psum = -1

init_neg = 0
psum = 0
for i in range(n):
    psum += A[i]
    if i%2 == 1 and psum <= 0:
        init_neg += 1-psum
        psum = 1
    elif i%2 == 0 and psum >= 0:
        init_neg += psum + 1
        psum = -1

print(min(init_pos, init_neg))

Solution (Python):
n=int(input())

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

kp=0
asum=0
flag=-1
for i in range(n):
    asum=asum+a[i]
    if flag==-1:
        if asum>=0:
            kp=kp+asum+1
            asum=-1
    else:
        if asum<=0:
            kp=kp+1-asum
            asum=1
    flag=-flag


km=0
asum=0
flag=1
for i in range(n):
    asum=asum+a[i]
    if flag==-1:
        if asum>=0:
            km=km+asum+1
            asum=-1
    else:
        if asum<=0:
            km=km+1-asum
            asum=1
    flag=-flag



print(min(kp,km))

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

x=0
y=0
sum1=0
sum2=0

for i in range(n):
  sum1+=a[i]
  sum2+=b[i]
  if(i%2==0):
    if(sum1<=0):
      x+=1-sum1
      a[i]+=1-sum1
      sum1+=1-sum1
    if(sum2>=0):
      y+=sum2+1
      b[i]-=sum2+1
      sum2-=sum2+1
  else:
    if(sum1>=0):
      x+=sum1+1
      a[i]-=1+sum1
      sum1-=1+sum1
    if(sum2<=0):
      y+=1-sum2
      b[i]+=1-sum2
      sum2+=1-sum2
print(min(x,y))

Solution (Python):
import sys
from collections import *
import heapq
import math
import bisect
from itertools import permutations,accumulate,combinations,product
from fractions import gcd
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]

n=int(input())
a=list(map(int,input().split()))
cnt1=0
now=0
for i in range(n):
    now+=a[i]
    if i%2==0:
        if now<=0:
            cnt1+=1-now
            now=1
    else:
        if now>=0:
            cnt1+=now+1
            now=-1
cnt2=0
now=0
for i in range(n):
    now+=a[i]
    if i%2==1:
        if now<=0:
            cnt2+=1-now
            now=1
    else:
        if now>=0:
            cnt2+=now+1
            now=-1

print(min(cnt1,cnt2))

Solution (Python):
import copy
n = int(input())
*A, = map(int, input().split())


def solve(p):
    r = 0
    t = 0
    for a in A:
        t += a
        if t * p <= 0:
            r += abs(p - t)
            t = p
        p *= -1
    return r


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

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
ans1 = 0
num = 0
f1 = 1
for i in range(n):
    num += a[i]
    if f1:
        if num <=0:
            ans1 += 1-num
            num = 1
        f1 = 0
    else:
        if num >= 0:
            ans1 += num+1
            num = -1
        f1 = 1
ans2 = 0
num = 0
f1 = 0
for i in range(n):
    num += a[i]
    if f1:
        if num <=0:
            ans2 += 1-num
            num = 1
        f1 = 0
    else:
        if num >= 0:
            ans2 += num+1
            num = -1
        f1 = 1
print(min(ans1,ans2))

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
def solve(A, p):
  act = 0
  total = 0
  for a in A:
    total += a
    if total * p < 1:
      act += abs(p - total)
      total = p
    p *= -1
  return act
ans = min(solve(A, 1), solve(A, -1))
print(ans)

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

temp = 0
cand1 = 0
for i in range(n):
    temp += s[i]
    if i%2 == 0:
        if temp <= 0:
            cand1 += abs(temp-1)
            temp = 1
    else:
        if temp >= 0:
            cand1 += abs(temp+1)
            temp = -1

temp = 0
cand2 = 0
for i in range(n):
    temp += s[i]
    if i%2 == 0:
        if temp >= 0:
            cand2 += abs(temp+1)
            temp = -1
    else:
        if temp <= 0:
            cand2 += abs(temp-1)
            temp = 1

print(min(cand2,cand1))

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


def f(pm):
    ans, s = 0, 0
    for a in A:
        s += a
        if s * pm <= 0:
            d = abs(s - pm)
            ans += d
            s += d * pm
        pm *= -1
    return ans


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

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

ans = float('inf')

cA = A[:]
cs = 0
cnt = 0
for i, a in enumerate(cA):
    if i%2 == 0: 
        if cs + a > 0:
            cs += a
        else:
            cA[i] += 1-(cs+a)
            cnt += 1-(cs+a)
            cs = 1
    else:
        if cs + a < 0:
            cs += a
        else:
            cA[i] += -1-(cs+a)
            cnt += abs(-1-(cs+a))
            cs = -1
ans = min(ans,cnt)

cA = A[:]
cs = 0
cnt = 0
for i, a in enumerate(cA):
    if i%2 == 1: 
        if cs + a > 0:
            cs += a
        else:
            cA[i] += 1-(cs+a)
            cnt += 1-(cs+a)
            cs = 1
    else:
        if cs + a < 0:
            cs += a
        else:
            cA[i] += -1-(cs+a)
            cnt += abs(-1-(cs+a))
            cs = -1
ans = min(ans,cnt)
print(ans)

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

def count(sign):
    
    count = 0
    a_sum = 0
    for a in a_list:
        a_sum +=a
        if a_sum*sign <= 0: 
            ope = sign - a_sum
            a_sum += ope
            count += abs(ope)
        sign *= -1
    return count
print(min(count(1),count(-1)))

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


def BIT_query(idx):
    res_sum = 0
    while idx > 0:
        res_sum += BIT[idx]
        idx -= idx&(-idx)
    return res_sum


def BIT_update(idx,x):
    while idx <= n:
        BIT[idx] += x
        idx += (idx&(-idx))
    return


BIT = [0]*(n+1)
for i,e in enumerate(a):
   BIT_update(i+1,e)
ans1=0
for i in range(1,n+1):
    SUM=BIT_query(i)
    if i%2==1:
        if SUM<=0:
            ans1+=1-SUM
            BIT_update(i,1-SUM)
    else:
        if SUM>=0:
            ans1+=1+SUM
            BIT_update(i,-1-SUM)


BIT = [0]*(n+1)
for i,e in enumerate(a):
    BIT_update(i+1,e)
ans2=0
for i in range(1,n+1):
    SUM=BIT_query(i)
    if i%2==0:
        if SUM<=0:
            ans2+=1-SUM
            BIT_update(i,1-SUM)
    else:
        if SUM>=0:
            ans2+=1+SUM
            BIT_update(i,-1-SUM)

print(min(ans1,ans2))

Solution (Python):

import math
import heapq
import bisect
import numpy as np
from collections import Counter, deque


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

ans=10**15
for t in [1,-1]:
    r=0
    cnt=0
    T=t
    for i in range(N):
        r+=A[i]
        if(T>0 and r<=0):
            cnt+=1-r
            r+=1-r
        if(T<0 and r>=0):
            cnt+=r+1
            r-=r+1
        T*=-1
    ans=min(cnt,ans)
print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
t = 1
cand1 = 0
tmp = 0
for ai in a:
    if (tmp + ai) * t <= 0:
        cand1 += abs(tmp + ai - t)
        tmp = t
    else:
        tmp += ai
    t *= -1
t = -1
cand2 = 0
tmp = 0
for ai in a:
    if (tmp + ai) * t <= 0:
        cand2 += abs(tmp + ai - t)
        tmp = t
    else:
        tmp += ai
    t *= -1

print(min(cand1, cand2))

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

su=0
op1=0
for i in range(n):

    if i%2==0:
        
        if su+a[i]<=0:
            diff=1-(su+a[i])
            op1+=diff
            su+=diff
 
    if i%2==1:
        
        if su+a[i]>=0:
            diff=(su+a[i])+1
            op1+=diff
            su-=diff
  
    su+=a[i]
su=0
op2=0
for i in range(n):
   
    if i%2==1:
        
        if su+a[i]<=0:
            diff=1-(su+a[i])
            op2+=diff
            su+=diff
    
    if i%2==0:
        
        if su+a[i]>=0:
            diff=(su+a[i])+1
            op2+=diff
            su-=diff
     
    su+=a[i]
print(min(op1,op2))

Solution (Python):
import sys,copy
from itertools import accumulate

input=sys.stdin.readline

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

A=list(accumulate(A))
B=copy.copy(A)
test1=0
s=0
for i in range(0,N):
    A[i]+=s
    if i%2==0:
        if 0>=A[i]:
            test1+=1-A[i]
            s+=1-A[i]
    else:
        if A[i]>=0:
            test1+=A[i]+1
            s-=A[i]+1


test2=0
s=0
for i in range(0,N):
    B[i]+=s
    if i%2==0:
        if B[i]>=0:
            test2+=B[i]+1
            s-=B[i]+1
    else:
        if 0>=B[i]:
            test2+=1-B[i]
            s+=1-B[i]

print(min(test1,test2))

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
sum = 0
res = [0,0]
for change in range(2):
  sum = 0
  for i in range(n):
    if i % 2 == change:
      if sum + a[i] < 0: 
        
        temp = 1 - sum - a[i]
        res[change] += temp
        sum += temp
      elif sum + a[i] == 0:
        sum += 1
        res[change] += 1
    else:
      if sum + a[i] > 0:
        
        temp = -1 - sum - a[i]
        res[change] -= temp
        sum += temp
      elif sum + a[i] == 0:
        sum -= 1
        res[change] += 1
    sum += a[i]
print(min(res[0],res[1]))

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
cnt = [0,0]
s = [0,0]
for i in range(n):
    s[0] += a[i]
    s[1] += a[i]
    if i%2 == 0:
        if s[0] <= 0:
            cnt[0] += -s[0]+1
            s[0] = 1
        if s[1] >= 0:
            cnt[1] += s[1]+1
            s[1] = -1
    else:
        if s[1] <= 0:
            cnt[1] += -s[1]+1
            s[1] = 1
        if s[0] >= 0:
            cnt[0] += s[0]+1
            s[0] = -1
ans = min(cnt)
print(ans)

Solution (Python):
N = int(input())
a = list(map(int,input().split()))
S = [0]*N
S[0] = a[0]

for i in range(1,N):
    S[i] = S[i-1] + a[i]

count = [0]*N
num = [0]*2
for i in range(2):
    value = 0
    for j in range(N):
        if (S[j]+value)*((-1)**(i+j)) <= 0:
            num[i] += abs(S[j] + value - (-1)**(i+j))
            value += (-1)**(i+j) - (S[j]+value)
        


print(min(num))

Solution (Python):
_,*l=map(int,open(0).read().split())
def f(s):
  c=t=0
  for i in l:
    t+=i
    if s*t<=0: c+=abs(t-s); t=s
    s*=-1
  return c
print(min(f(1),f(-1)))

Solution (Python):
input()
l=list(map(int,input().split()))
def f(s):
  c=t=0
  for i in l:
    t+=i
    if s*t<=0: c+=abs(t-s); t=s
    s*=-1
  return c
print(min(f(1),f(-1)))

Solution (Python):
input()
l=list(map(int,input().split()))
def f(b):
  c=s=0
  for i in l:
    if b:
      if s+i>0: s+=i
      else: c-=s+i-1; s=1
    else:
      if s+i<0: s+=i
      else: c+=s+i+1; s=-1
    b=1-b
  return c
print(min(f(0),f(1)))

Solution (Python):
import sys

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

min_a = 0
sum_a = 0
for i in range(n):
    sum_a += a[i]
    if i % 2 == 0:
        if sum_a <= 0:
            min_a += abs(sum_a) + 1
            sum_a += abs(sum_a) + 1
    else:
        if sum_a >= 0:
            min_a += abs(sum_a) + 1
            sum_a -= abs(sum_a) + 1

min_b = 0
sum_b = 0
for i in range(n):
    sum_b += a[i]
    if i % 2 == 0:
        if sum_b >= 0:
            min_b += abs(sum_b) + 1
            sum_b -= abs(sum_b) + 1
    else:
        if sum_b <= 0:
            min_b += abs(sum_b) + 1
            sum_b += abs(sum_b) + 1

print(min(min_a, min_b))

Solution (Python):
def solve():
    n = int(input())
    a = list(map(int, input().split()))
    
    odd, even = 0, 0
    cnt1 = 0
    for i in range(n):
        if i % 2 == 0:
            odd += a[i]
            if odd + even <= 0:
                tmp = 1 - (odd + even)
                odd += tmp
                cnt1 += tmp
        else:
            even += a[i]
            if 0 <= odd + even:
                tmp = -1 - (odd + even)
                even += tmp
                cnt1 += abs(tmp)
    
    odd, even = 0, 0
    cnt2 = 0
    for i in range(n):
        if i % 2 == 0:
            odd += a[i]
            if 0 <= odd + even:
                tmp = -1 - (odd + even)
                odd += tmp
                cnt2 += abs(tmp)
        else:
            even += a[i]
            if odd + even <= 0:
                tmp = 1 - (odd + even)
                even += tmp
                cnt2 += tmp
    
    ans = min(cnt1, cnt2)
    print(ans)


if __name__ == '__main__':
    solve()

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

ans=float('inf')
s=0
cnt=0

for i in range(n):
    s+=a[i]
    if i%2==0:
        if s>=0:
            cnt+=1+s
            s=-1
    else:
        if s<=0:
            cnt+=1-s
            s=1
ans=min(ans,cnt)
s=0
cnt=0
for i in range(n):
    s+=a[i]
    if i%2==1:
        if s>=0:
            cnt+=1+s
            s=-1
    else:
        if s<=0:
            cnt+=1-s
            s=1
ans=min(ans,cnt)

print(ans)

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

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

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

print(min(count1,count2))

Solution (Python):
from sys import maxsize

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

res = maxsize
for i in (0, 1):
    cumsum = cnt = 0
    for k in range(n):
        if k % 2 == i:
            if cumsum + a[k] <= 0:
                cnt += abs(1 - (cumsum + a[k]))
                cumsum = 1
            else:
                cumsum += a[k]
        else:
            if cumsum + a[k] >= 0:
                cnt += abs(-1 - (cumsum + a[k]))
                cumsum = -1
            else:
                cumsum += a[k]
    if cnt < res:
        res = cnt

print(res)

Solution (Python):
def f(a):
    mode=1
    s=0
    ans=0
    for i in range(n):
        s+=a[i]
        if mode*s<=0: ans+=abs(s)+1;s=mode
        mode*=-1
    return ans


n=int(input())
a=list(map(int,input().split()))
b=[-a[i] for i in range(n)]

mode=1
s=0
print(min(f(a),f(b)))

Solution (Python):




import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


class cumsum1d:  
    def __init__(self, ls: list):
        '''
        1次元リストを受け取る
        '''
        from itertools import accumulate
        self.ls_accum = [0] + list(accumulate(ls))

    def total(self, i, j):
        
        return self.ls_accum[j] - self.ls_accum[i]

    def __call__(self, i):
        
        return self.ls_accum[i + 1]


N = read_a_int()
A = read_ints()

A = cumsum1d(A)


ans1 = 0
bias = 0
pre_sign = 1  
for i in range(N):
    now = A(i) + bias
    if pre_sign * now < 0:  
        pass
    elif pre_sign * now >= 0:  
        ans1 += abs(now) + 1
        bias += -pre_sign * (abs(now) + 1)
    pre_sign = 1 if pre_sign == -1 else -1

ans2 = 0
bias = 0
pre_sign = -1  
for i in range(N):
    now = A(i) + bias
    if pre_sign * now < 0:  
        pass
    elif pre_sign * now >= 0:  
        ans2 += abs(now) + 1
        bias += -pre_sign * (abs(now) + 1)
    pre_sign = 1 if pre_sign == -1 else -1
print(min(ans1, ans2))

Solution (Python):



import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


class cumsum1d:  
    def __init__(self, ls: list):
        '''
        1次元リストを受け取る
        '''
        from itertools import accumulate
        self.ls_accum = [0] + list(accumulate(ls))

    def total(self, i, j):
        
        return self.ls_accum[j] - self.ls_accum[i]

    def __call__(self, i):
        
        return self.ls_accum[i + 1]


N = read_a_int()
A = read_ints()

A = cumsum1d(A)


ans1 = 0
bias = 0
pre_sign = 1  
for i in range(N):
    now = A(i) + bias
    if (-1, 1)[pre_sign] * now < 0:  
        pass
    elif (-1, 1)[pre_sign] * now >= 0:  
        ans1 += abs(now) + 1
        bias += (1, -1)[pre_sign] * (abs(now) + 1)
    pre_sign = 1 - pre_sign

ans2 = 0
bias = 0
pre_sign = 0  
for i in range(N):
    now = A(i) + bias
    if (-1, 1)[pre_sign] * now < 0:  
        pass
    elif (-1, 1)[pre_sign] * now >= 0:  
        ans2 += abs(now) + 1
        bias += (1, -1)[pre_sign] * (abs(now) + 1)
    pre_sign = 1 - pre_sign
print(min(ans1, ans2))

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

def is_pos(x,i):
    if not x >= 1:
        cnt[i] += 1-x
        s[i] = 1
    return

def is_neg(x,i):
    if not x <= -1:
        cnt[i] += 1+x
        s[i] = -1
    return
    
for i in range(n):
    s[0] += a[i]
    s[1] += a[i]
    x = i%2
    y = (i+1)%2
    is_pos(s[x],x)
    is_neg(s[y],y)

ans = min(cnt)
print(ans)

Solution (Python):
from itertools import accumulate

N = int(input())
As = list(map(int, input().split()))
acc = list(accumulate(As))


ans = 0
cnt = 0 
parity = [1,-1]
for i,a in enumerate(acc):
    a += cnt
    p = parity[i%2]
    if p * a > 0:
        continue
    else:
        ans += abs(a)+1
        cnt += p*(abs(a)+1)


ans2 = 0
cnt = 0 
parity = [-1,1]
for i,a in enumerate(acc):
    a += cnt
    p = parity[i%2]
    if p * a > 0:
        continue
    else:
        ans2 += abs(a)+1
        cnt += p*(abs(a)+1)

print(min(ans,ans2))

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

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

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

print(min(ans1, ans2))

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
fg=[1,-1]
ret=[0]*2
tot=[0]*2
for i in range(n):
  for j in range(2):
    tot[j]+=a[i]
    if tot[j]*fg[j]<=0:
      ret[j]+=abs(fg[j]-tot[j])
      tot[j]+=fg[j]-tot[j]
    fg[j]*=-1
print(min(ret))

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
ans=10**15
for t in [1,-1]:
    r=0
    cnt=0
    T=t
    for i in range(N):
        r+=A[i]
        if(T>0 and r<=0):
            cnt+=1-r
            r+=1-r
        if(T<0 and r>=0):
            cnt+=r+1
            r-=r+1
        T*=-1
    ans=min(cnt,ans)
print(ans)

Solution (Python):

import sys


def solve(n: int, a: "List[int]"):
    def _solve(op):
        from itertools import cycle
        ab = 0
        for aa in map(lambda a_o: a_o[0]*a_o[1], zip(a, cycle(op))):
            ab -= aa
            if ab >= 0:
                yield ab + 1
                ab = -1
            ab = abs(ab)
    return min(sum(_solve([1, -1])), sum(_solve([-1, 1])))


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)]  
    print(solve(n, a))

if __name__ == '__main__':
    main()

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
a1=0
a2=0
s1=0
s2=0
for i,j in enumerate(l):
    s1+=j
    s2+=j
    if i%2:
        if s1>=0:
            a1+=s1+1
            s1=-1
        if s2<=0:
            a2+=-s2+1
            s2=1
    else:
        if s2>=0:
            a2+=s2+1
            s2=-1
        if s1<=0:
            a1+=-s1+1
            s1=1
print(min(a1,a2))

Solution (Python):
import sys
input = sys.stdin.readline
INF = 10**10
MOD = 10**9 + 7
sys.setrecursionlimit(100000000)
from functools import lru_cache
from copy import deepcopy 

def main():
    n = int(input())
    a = list(map(int,input().split()))
    b = deepcopy(a)

    ans1 = 0
    if a[0] <= 0:
        ans1 += 1 - a[0]
        a[0] = 1
    
    for i in range(1,n):
        a[i] += a[i - 1]
        if a[i - 1] < 0 and a[i] <= 0:
            ans1 += 1 - a[i]
            a[i] = 1
        elif a[i - 1] > 0 and a[i] >= 0:
            ans1 += a[i] + 1
            a[i] = -1
    
    ans2 = 0
    if b[0] >= 0:
        ans2 += b[0] + 1
        b[0] = -1

    for i in range(1,n):
        b[i] += b[i - 1]
        if b[i - 1] < 0 and b[i] <= 0:
            ans2 += 1 - b[i]
            b[i] = 1
        elif b[i - 1] > 0 and b[i] >= 0:
            ans2 += b[i] + 1
            b[i] = -1
    
    ans = min(ans1,ans2)
    print(ans)

if __name__=='__main__':
    main()

Solution (Python):
def resolve():
    n = int(input())
    C = [0, 0]
    S = list(map(int, input().split()))
    for i in range(2):
        tmp = 0
        for j in range(n):
            tmp += S[j]
            if (i+j)%2==0:
                if tmp <= 0:
                    C[i] += abs(tmp) + 1
                    tmp = 1
            else:
                if tmp >= 0:
                    C[i] += abs(tmp) + 1
                    tmp = -1

    print(min(C[0], C[1]))

if __name__ == '__main__':
    resolve()

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

ans = 10 ** 18
for z in range(2):
    if z == 0:
        sign = 1
    else:
        sign = -1
    c = 0
    cost = 0
    for x in a:
        c += x
        if c * sign <= 0:
            cost += 1 - c * sign
            c = sign
        sign *= -1
    ans = min(ans, cost)
print(ans)

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

ans1 = c = 0
for i,a in enumerate(A):
    c += a
    if i%2:
        if c < 1:
            ans1 += 1-c
            c = 1
    else:
        if c > -1:
            ans1 += c+1
            c = -1

ans2 = c = 0
for i,a in enumerate(A):
    c += a
    if i%2==0:
        if c < 1:
            ans2 += 1-c
            c = 1
    else:
        if c > -1:
            ans2 += c+1
            c = -1

print(min(ans1,ans2))

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

res1 = 0
cum1 = 0

for i in range(N):
    cum1 += A[i]
    if i % 2 == 0 and cum1 <= 0:
        res1 += - cum1 + 1
        cum1 = 1
    if i % 2 != 0 and cum1 >= 0:
        res1 += cum1 + 1
        cum1 = -1

res2 = 0
cum2 = 0

for i in range(N):
    cum2 += A[i]
    if i % 2 != 0 and cum2 <= 0:
        res2 += - cum2 + 1
        cum2 = 1
    if i % 2 == 0 and cum2 >= 0:
        res2 += cum2 + 1
        cum2 = -1

print(min(res1, res2))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
cnt1 = 0
s = 0
swc = 1
for i in a:
	s += i
	if swc == 1:
		swc = 0
		if s > 0:
			continue
		else:
			cnt1 += 1-s
			s = 1
	else:
		swc = 1
		if s < 0:
			continue
		else:
			cnt1 += s+1
			s = -1
swc = 0
cnt2 = 0
s= 0
for i in a:
	s += i
	if swc == 1:
		swc = 0
		if s > 0:
			continue
		else:
			cnt2 += 1-s
			s = 1
	else:
		swc = 1
		if s < 0:
			continue
		else:
			cnt2 += s+1
			s = -1
print (min(cnt1, cnt2))

Solution (Python):
n, *A = map(int, open(0).read().split())
def sgn(n):
    return 0 if n==0 else 1 if n>0 else -1

a = A[0]
C = [0 if a>0 else -a+1, 0 if a<0 else a+1]
S = [max(1, a), min(-1, a)]

for a in A[1:]:
    for i, s in enumerate(S):
        sgn_sum = sgn(s)
        if sgn(s+a) == -sgn_sum:
            S[i] += a
        else:
            C[i] += abs(s+a+sgn_sum)
            S[i] = -sgn_sum

print(min(C))

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


def solve(tmp):
    ans = 0
    for i in range(1, n):
        if tmp * (tmp + a[i]) < 0:
            tmp += a[i]
        else:
            ans += abs(tmp + a[i]) + 1
            if tmp < 0:
                tmp = 1
            else:
                tmp = - 1
    return ans

minus = min(a[0], -1)
plus = max(a[0], 1)
ans = min(solve(plus) + abs(a[0] - plus), solve(minus) + abs(a[0] - minus))
print(ans)

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

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


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

print(min(count1,count2))

Solution (Python):
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))

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

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N=I()
    a=LI()
    ans=10**30
    aaa=[-1,1]
    
    for j in range(2):
        pos=aaa[j]
        S=0
        temp=0
        for i in range(N):
            S+=a[i]
            if S>0:
                Sp=1
            elif S<0:
                Sp=-1
            else:
                Sp=0
            
            
            if Sp!=pos*(-1):
                target=-1*pos
                temp+=abs(target-S)
                S=target
            pos*=-1
        ans=min(ans,temp)
        
    print(ans)
        
        

main()

Solution (Python):






n = int(input())
arr = list(map(int, input().split()))
p, m = 0, 0  
pn, mn = 0, 0  
for i in range(n):
    if i % 2 == 0:
        
        if p + arr[i] > 0:
            p += arr[i]
        else:
            
            d = p + arr[i]  
            pn += abs(1 - d)
            p = 1
    else:
        
        if p + arr[i] < 0:
            p += arr[i]
        else:
            
            d = p + arr[i]
            pn += abs(-1 - d)
            p = -1
for i in range(n):
    if i % 2 == 0:
        
        if m + arr[i] < 0:
            m += arr[i]
        else:
            
            d = m + arr[i]
            mn += abs(-1 - d)
            m = -1
    else:
        
        if m + arr[i] > 0:
            m += arr[i]
        else:
            
            d = m + arr[i]
            mn += abs(1 - d)
            m = 1

print(min(pn, mn))

Solution (Python):
import copy

n = int(input())
a = list(map(int, input().split(' ')))
sum_list = [0] * n
past = 0
for i in range(n):
    if i > 0:
        sum_list[i] = sum_list[i-1] + a[i]
    else:
        sum_list[0] = a[i]
sum1 = copy.copy(sum_list)
sum2 = copy.copy(sum_list)

ans = 0
count = 0
for i in range(n):
    
    num = 0
    sum1[i] += past
    if i % 2 == 0 and sum1[i] <= 0:
        num = (abs(sum1[i]) + 1)
        sum1[i] += num
        past += num
    if i % 2 == 1 and sum1[i] >= 0:
        num = (abs(sum1[i]) + 1)
        sum1[i] -= num
        past -= num
    count += num

ans = count
count = 0
past = 0

for i in range(n):
    
    num = 0
    sum2[i] += past
    if i % 2 == 0 and sum2[i] >= 0:
        num = (abs(sum2[i]) + 1)
        sum2[i] -= num
        past -= num
    if i % 2 == 1 and sum2[i] <= 0:
        num = (abs(sum2[i]) + 1)
        sum2[i] += num
        past += num
    count += num

ans = min(ans, count)
print(ans)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
ret = 0
def count(nega):
  ret = 0
  s = 0
  for e in A:
    nega = not nega
    
    if nega and 0 <= s + e:
      ret += abs(s + e) + 1
      s = -1
    elif not nega and s + e <= 0:
      ret += abs(s + e) + 1
      s = 1
    else:
      s += e
  return ret
print(min(count(True),count(False)))

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