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 = s2 =  0

ans1 = ans2 = 0

for i in range(len(A)):
    s1 += A[i]
    if (i+1)%2==0:
        if s1>0:
            continue
        else:
            ans1+= abs(s1) + 1
            s1 = 1
    else:
        if s1<0:
            continue
        else:
            ans1 += abs(s1) +1
            s1 = -1

for i in range(len(A)):
    s2 += A[i]
    if (i+1)%2 == 0:
        if s2 <0:
            continue
        else:
            ans2 += abs(s2) + 1
            s2 = -1
    else:
        if s2 > 0:
            continue
        else:
            ans2 += abs(s2) + 1
            s2 = 1
print(min(ans1,ans2))

Solution (Python):
n = int(input())
a = [int(x) for x in input().split()]
key = 0
ans = 0
for i in range(n):
  sub = key + a[i]
  if i == 0 and sub == 0:
    ans += 1
    sub = 1
  if i != 0 and sub*key >= 0:
    if key > 0:
      ans += sub+1
      sub = -1
    else:
      ans += 1-sub
      sub = 1
  key = sub

ans2 = 0
key2 = 0
for i in range(n):
  sub = key2 + a[i]
  if i == 0:
    if sub == 0:
      ans2 += 1
      sub = -1
    elif sub > 0:
      ans2 += sub+1
      sub = -1
    else:
      ans2 += 1-sub
      sub = 1
  if i != 0 and sub*key2 >= 0:
    if key2 > 0:
      ans2 += sub+1
      sub = -1
    else:
      ans2 += 1-sub
      sub = 1
  key2 = sub
      
print(min([ans, ans2]))

Solution (Python):
n = int(input())
ns = list(map(int, input().split()))
sum_i = 0
cnt = 0
for i in range(n):
    sum_i += ns[i]
    if i % 2 == 0:
        if sum_i > 0:
            continue
        elif sum_i < 0:
            cnt += abs(sum_i) + 1
            sum_i = 1
        else:
            cnt += 1
            sum_i = 1
    else:
        if sum_i > 0:
            cnt += sum_i + 1
            sum_i = -1
        elif sum_i < 0:
            continue
        else:
            cnt += 1
            sum_i = -1
min_cnt = cnt
cnt = 0
sum_i = 0
for i in range(n):
    sum_i += ns[i]
    if (i+1) % 2 == 0:
        if sum_i > 0:
            continue
        elif sum_i < 0:
            cnt += abs(sum_i) + 1
            sum_i = 1
        else:
            cnt += 1
            sum_i = 1
    else:
        if sum_i > 0:
            cnt += sum_i + 1
            sum_i = -1
        elif sum_i < 0:
            continue
        else:
            cnt += 1
            sum_i = -1
print(min(min_cnt, cnt))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
ans1, ans2 = 0, 0
s = 0
plus = True
for i in range(n):
    if plus:
        if s + a[i] > 0:
            s += a[i]
        else:
            ans1 += abs(1 - (s + a[i]))
            s = 1
    if not plus:
        if s + a[i] < 0:
            s += a[i]
        else:
            ans1 += abs(-1 - (s + a[i]))
            s = -1
    plus = not plus

s = 0
plus = False
for i in range(n):
    if plus:
        if s + a[i] > 0:
            s += a[i]
        else:
            ans2 += abs(1 - (s + a[i]))
            s = 1
    if not plus:
        if s + a[i] < 0:
            s += a[i]
        else:
            ans2 += abs(-1 - (s + a[i]))
            s = -1
    plus = not plus
print(min(ans1, ans2))

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

def main():
    N = int(readline())
    A = list(map(int, readline().rstrip().split()))
    for i in range(1, N):
        A[i] += A[i-1]
    cnt1, cnt2 = 0, 0
    ref1, ref2 = 0, 0
    for i, a in enumerate(A):
        if i % 2 == 0:
            if a <= ref1:
                cnt1 += ref1 - a + 1
                ref1 -= ref1 - a + 1
            if a >= ref2:
                cnt2 += a - ref2 + 1
                ref2 += a - ref2 + 1
        if i % 2 == 1:
            if a >= ref1:
                cnt1 += a - ref1 + 1
                ref1 += a - ref1 + 1
            if a <= ref2:
                cnt2 += ref2 - a + 1
                ref2 -= ref2 - a + 1

    print(min(cnt1, cnt2))

if __name__ == '__main__':
    main()

Solution (Python):
def sgn(n):
    if n>0:
        return 1
    elif n==0:
        return 0
    else:
        return -1
    
class SegmentTree:
    
    
    
    def __init__(self, size, f=lambda x,y : x+y, default=0):
        self.size = 2**(size-1).bit_length() 
        self.default = default
        self.dat = [default]*(self.size*2-1) 
        self.f = f

    def update(self, i, x):
        i += self.size-1
        self.dat[i] = x
        while i > 0:
            i = (i-1) >> 1
            self.dat[i] = self.f(self.dat[i*2+1], self.dat[i*2+2])

    def query(self, l, r, k=0, L=0, R=None):
        if R is None:
            R = self.size
        if R <= l or r <= L:
            return self.default
        if l <= L and R <= r:
            return self.dat[k]
        else:
            lres = self.query(l, r, k*2+1, L, (L+R) >> 1)
            rres = self.query(l, r, k*2+2, (L+R) >> 1, R)
            return self.f(lres, rres)
        
n=int(input())
b=[int(i) for i in input().split()]
l = SegmentTree(size=n)
p0=0
for i in range(n):
    l.update(i,b[i])
for i in range(n):
    sg=(-1)**(i)
    v=l.query(0,i+1)
    
    if sgn(v) != sg:
        tmp= abs(v)+1
        
        p0 += tmp
        l.update(i,b[i]+tmp*sg)
        
l = SegmentTree(size=n)
for i in range(n):
    l.update(i,b[i])
p1=0

for i in range(n):
    sg=(-1)**(i+1)
    v=l.query(0,i+1)
    
    if sgn(v) != sg:
        tmp= abs(v)+1
        
        p1 += tmp
        l.update(i,b[i]+tmp*sg)
    
print(min(p0,p1))

Solution (Python):
n  = int(input())
A = list(map(int,input().split()))
pal = [-1,1]
cnt = 10**18
for p in pal:
    s = 0
    tmp = 0
    prev = p
    for a in A:
        s+=a
        if s==0:
            tmp+=1
            s = prev
            prev*=-1
            continue
        if a==0:
            tmp +=abs(s)+1
            s = prev
            prev*=-1
            continue
        if s//abs(s) != prev:
            tmp+=abs(s)+1
            s = prev
        else:
            pass
        prev*=-1
    cnt = min(cnt,tmp)
print(cnt)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
ans1,ans2 = 0,0
sum1,sum2 = 0,0
for i in range(n):
    sum1 += a[i]
    if i%2 == 1 and sum1 >= 0:
        ans1 += sum1 + 1
        sum1 = -1
    elif i%2 == 0 and sum1 <= 0:
        ans1 += -sum1 + 1
        sum1 = 1
for i in range(n):
    sum2 += a[i]
    if i%2 == 0 and sum2 >= 0:
        ans2 += sum2 + 1
        sum2 = -1
    elif i%2 == 1 and sum2 <= 0:
        ans2 += -sum2 + 1
        sum2 = 1
print(min(ans1,ans2))

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

b = [0]*n
b[0]=a[0]
pt = 0
m = 0
ans1 = 0
ans2 = 0
for i in range(1,n):
    b[i]=b[i-1]+a[i]

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

Solution (Python):
import copy

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

s2 = copy.copy(s)
ans = []


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

s = s2

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

print(min(ans1,ans2))

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
ans1,ans2 = 0,0
sum1,sum2 = 0,0
for i in range(n):
    sum1 += a[i]
    if i%2 == 1 and sum1 >= 0:
        ans1 += sum1 + 1
        sum1 = -1
    elif i%2 == 0 and sum1 <= 0:
        ans1 += -sum1 + 1
        sum1 = 1
for i in range(n):
    sum2 += a[i]
    if i%2 == 0 and sum2 >= 0:
        ans2 += sum2 + 1
        sum2 = -1
    elif i%2 == 1 and sum2 <= 0:
        ans2 += -sum2 + 1
        sum2 = 1
print(min(ans1,ans2))

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


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


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

print(min(minA, minB))

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

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

from itertools import accumulate
acc = a
acc = list(accumulate(acc))

def dfs(ind,prev,tmp,ans):
    if ind == n:
        return ans
    now = acc[ind]

    if prev > 0:
        if now + tmp >= 0:
            ans +=abs(now + tmp +1)
            tmp += -(now + tmp +1)
    elif prev < 0:
        if now + tmp <= 0:
            ans +=abs(now + tmp -1)
            tmp += -(now + tmp -1)

    return dfs(ind+1, now+tmp,tmp,ans)

ansM = dfs(0,-1,0,0)
ansP = dfs(0,+1,0,0)

print(min(ansM,ansP))

Solution (Python):


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



accum = [0] * N
accum[0] = A[0]
for i in range(1,N):
    accum[i] = accum[i-1] + A[i]



ans1 = 0
tmp = [0] * N
for i in range(N):
    if i == 0:
        tmp[i] = A[i]
    else:
        tmp[i] = tmp[i-1] + A[i]

    if i % 2 == 0:
        if tmp[i] <= 0:
            ans1 += abs(tmp[i]) + 1
            tmp[i] = 1
    else:
        if tmp[i] >= 0:
            ans1 += abs(tmp[i]) + 1
            tmp[i] = -1


ans2 = 0
tmp = [0] * N
for i in range(N):
    if i == 0:
        tmp[i] = A[i]
    else:
        tmp[i] = tmp[i-1] + A[i]

    if i % 2 == 1:
        if tmp[i] <= 0:
            ans2 += abs(tmp[i]) + 1
            tmp[i] = 1
    else:
        if tmp[i] >= 0:
            ans2 += abs(tmp[i]) + 1
            tmp[i] = -1

print(min(ans1,ans2))

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

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

total = 0
ans_m = 0
ans_p = 0

hugo = 1
for i in range(n):
    total += a[i]
    if hugo == -1 and total <= 0:
        ans_p = ans_p + (1-total)
        total = 1
        hugo = 1
    elif hugo == -1 and total > 0:
        hugo = 1
    elif hugo == 1 and total >= 0:
        ans_p = ans_p + (1+total)
        total = -1
        hugo = -1
    elif hugo == 1 and total < 0:
        hugo = -1

total = 0
hugo = -1

for i in range(n):
    total += a[i]
    if hugo == -1 and total <= 0:
        ans_m = ans_m + (1-total)
        total = 1
        hugo = 1
    elif hugo == -1 and total > 0:
        hugo = 1
    elif hugo == 1 and total >= 0:
        ans_m = ans_m + (1+total)
        total = -1
        hugo = -1
    elif hugo == 1 and total < 0:
        hugo = -1

print(min(ans_m, ans_p))

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

    def solve(positive, A):
        total = 0
        ope = 0
        for i in range(len(A)):
            
            
            
            if positive:
                
                if total + A[i] >= 0:
                    ope += total + A[i] + 1
                    total = total + A[i] - (total + A[i]) - 1
                else:
                    total += A[i]
            else:
                
                if total + A[i] <= 0:
                    ope += abs(total + A[i]) + 1
                    total = total + A[i] + abs(total + A[i]) + 1
                else:
                    total += A[i]
            positive = (not positive)
        return ope
    
    print(min(solve(True, A), solve(False, A)))



if '__main__' == __name__:
    resolve()

Solution (Python):
N = int(input())
A = [int(_n) for _n in input().split()]
res0 = 0
s = 0
for i, a in enumerate(A):
    s += a
    if i % 2 == 0 and s >= 0:
        res0 += s + 1
        s = -1
    elif i % 2 == 1 and s <= 0:
        res0 += -s + 1
        s = 1
res1 = 0
s = 0
for i, a in enumerate(A):
    s += a
    if i % 2 == 1 and s >= 0:
        res1 += s + 1
        s = -1
    elif i % 2 == 0 and s <= 0:
        res1 += -s + 1
        s = 1
print(min(res0,res1))

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

def solve(A, mode=0):
  
  
  res = 0
  pre_a = 0
  for i, a in enumerate(A):
    a += pre_a
    if a * (-1)**(i+mode) > 0: 
      pre_a = a
      continue
    
    res += abs(a) + 1
    pre_a = (-1) ** (i + mode)
  return res
  
ans = solve(A, mode=0)
ans = min(ans, solve(A, mode=1))
print(ans)

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





def kf(a,flag,n):
    a0=a[0]*flag
    if a0<=0:
        kevn=1-a0
        sevn=1
    elif a0>0:
        kevn=0
        sevn=a0

    for i in range(1,n):
        ai=a[i]*flag
        if i%2==0:
            if sevn+ai<=0:
                kevn=kevn+1-(ai+sevn)
                sevn=1
            elif sevn+ai>0:
                kevn=kevn
                sevn=sevn+ai
        elif i%2==1:
            if sevn+ai<0:
                kevn=kevn
                sevn=sevn+ai
            elif sevn+ai>=0:
                kevn=kevn+1+(ai+sevn)
                sevn=-1
    return kevn

print(min(kf(a,1,n),kf(a,-1,n)))

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





a0=a[0]
if a0<0:
    kevn=1-a0
    sevn=1
elif a0==0:
    kevn=1
    sevn=1
elif a0>0:
    kevn=0
    sevn=a0

for i in range(1,n):
    ai=a[i]
    if i%2==0:
        if sevn+ai<0:
            kevn=kevn+1-(ai+sevn)
            sevn=1
        elif sevn+ai==0:
            kevn=kevn+1
            sevn=1
        elif sevn+ai>0:
            kevn=kevn
            sevn=sevn+ai
    elif i%2==1:
        if sevn+ai<0:
            kevn=kevn
            sevn=sevn+ai
        elif sevn+ai==0:
            kevn=kevn+1
            sevn=-1
        elif sevn+ai>0:
            kevn=kevn+1+(ai+sevn)
            sevn=-1
kevn1=kevn
            
a0=-a[0]
if a0<0:
    kevn=1-a0
    sevn=1
elif a0==0:
    kevn=1
    sevn=1
elif a0>0:
    kevn=0
    sevn=a0

for i in range(1,n):
    ai=-a[i]
    if i%2==0:
        if sevn+ai<0:
            kevn=kevn+1-(ai+sevn)
            sevn=1
        elif sevn+ai==0:
            kevn=kevn+1
            sevn=1
        elif sevn+ai>0:
            kevn=kevn
            sevn=sevn+ai
    elif i%2==1:
        if sevn+ai<0:
            kevn=kevn
            sevn=sevn+ai
        elif sevn+ai==0:
            kevn=kevn+1
            sevn=-1
        elif sevn+ai>0:
            kevn=kevn+1+(ai+sevn)
            sevn=-1
            
kevn=min(kevn,kevn1)

print(kevn)

Solution (Python):
from sys import stdin
import sys
import math

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






count_pair = 0
current_sum = 0
diff = 0
for i in range(len(a)):
    if i % 2 == 0:
        if current_sum + a[i] < 1:
            diff = 1 - (current_sum + a[i])
            count_pair += diff
        else:
            diff = 0
    elif i % 2 == 1:
        if current_sum + a[i] > -1:
            diff = -1 - (current_sum + a[i])
            count_pair += -1 * diff
        else:
            diff = 0

    current_sum += a[i] + diff
    if current_sum == 0:
        print("Error")


count_odd = 0
current_sum = 0
diff = 0
i=0
for i in range(len(a)):
    if i % 2 == 1:
        if current_sum + a[i] < 1:
            diff = 1 - (current_sum + a[i])
            count_odd += diff
        else:
            diff = 0
    elif i % 2 == 0:
        if current_sum + a[i] > -1:
            diff = -1 - (current_sum + a[i])
            count_odd += -1 * diff
        else:
            diff = 0


    current_sum += a[i] + diff
    if current_sum == 0:
        print("Error")


print(min(count_odd, count_pair))

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

def calc_sum(a, flag):
    ret = 0
    sum = 0
    for i in range(n):
        if (flag == 1 and sum + a[i] >= 0) or (flag == -1 and sum + a[i] <= 0):
            ret += abs(sum) + a[i] * flag + 1
            sum = -flag
        else:
            sum += a[i]
        flag = -flag
    return ret

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

Solution (Python):
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**6)
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n = ni()
a = na()

x = 0 
y = 0 
xx,yy = 0,0

for i in range(n):
    xx += a[i]
    yy += a[i]
    if not i%2:
        p = 0 if xx > 0 else 1-xx
        q = 0 if yy < 0 else 1+yy
        x += p
        y += q
        xx += p
        yy -= q
    else:
        p = 0 if yy > 0 else 1-yy
        q = 0 if xx < 0 else 1+xx
        y += p
        x += q
        yy += p
        xx -= q

print(min(x,y))

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

    if i%2:
        if s<=0:
            a+=1-s
            s=1
    else:
        if s>=0:
            a+=s+1
            s=-1

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

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

s = [0,0]
ans = [0,0]


for i in range(n):
    s[0] += a[i]
    s[1] += a[i]
    if s[0]*pow(-1,i) <= 0:
        ans[0] += 1 - s[0]*pow(-1,i)
        s[0] = pow(-1,i)
    if s[1]*pow(-1,i) >= 0:
        ans[1] += s[1]*pow(-1,i) + 1
        s[1] = -pow(-1,i)
print(min(ans))

Solution (Python):
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:
        cnt += max(0, 1 - SUM)
        SUM = max(1, SUM)
    else:
        cnt += max(0, 1 + SUM)
        SUM = min(-1, SUM)
ans = cnt

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

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

seq_sum = 0
sign = 1
count_1 = 0
for i in range(n):
    if A[i] * sign * (-1) > seq_sum * sign:
        seq_sum += A[i]
    else:
        count_1 += abs(sign * (-1) - (seq_sum + A[i]))
        seq_sum += A[i] + (sign * (-1) - (seq_sum + A[i]))
    sign = sign * (-1)

seq_sum = 0
sign = -1
count_2 = 0
for i in range(n):
    if A[i] * sign * (-1) > seq_sum * sign:
        seq_sum += A[i]
    else:
        count_2 += abs(sign * (-1) - (seq_sum + A[i]))
        seq_sum += A[i] + (sign * (-1) - (seq_sum + A[i]))
    sign = sign * (-1)

print(min(count_1, count_2))

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

ans = 0
f = 1
num = 0
for i in range(n):
    num += a[i]
    if f:
        if num <= 0:
            ans += 1 - num
            num = 1
        f = 0
    else:
        if num >= 0:
            ans += abs(-1 - num)
            num = -1
        f = 1

ans_2 = 0
f = 1
num = 0
for i in range(n):
    num += a[i]
    if f:
        if num >= 0:
            ans_2 += abs(-1 - num)
            num = -1
        f = 0
    else:
        if num <= 0:
            ans_2 += 1 - num
            num = 1
        f = 1

print(min(ans,ans_2))

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
mulsum=[0]*n
cnt=0
cnt2=0
buf=0
cur=0
mulsum[0]=a[0]
for i in range(n-1):
    mulsum[i+1]=mulsum[i]+a[i+1]

i=0
pm=[]
mp=[]
while i<n:
    if (mulsum[i]+buf)<=0:
        cur=mulsum[i]+buf
        buf=1-mulsum[i]
        cnt=cnt+(1-cur)
        
    else:
        i=i
        
    if i==n-1:
        break
    if (mulsum[i+1]+buf)>=0:
        cur=mulsum[i+1]+buf
        buf=-1-mulsum[i+1]
        cnt=cnt+(1+cur)
        
    else:
        i=i
        
    i=i+2

buf=0
i=0
while i<n:
    if (mulsum[i]+buf)>=0:
        cur=mulsum[i]+buf
        buf=-1-mulsum[i]
        cnt2=cnt2+(1+cur)
        
    else:
        i=i
        
    
    if i==n-1:
        break
    if (mulsum[i+1]+buf)<=0:
        cur=mulsum[i+1]+buf
        buf=1-mulsum[i+1]
        cnt2=cnt2+(1-cur)
        
    else:
        i=i
        
    i=i+2
    

print(min(cnt,cnt2))

Solution (Python):
n=int(input())
A=list(map(int,input().split()))
S=[A[0]]
for i in range(n-1):
  S.append(S[-1]+A[i+1])
ans1=0
s1=0
a=-1
for i in range(n):
  a=a*(-1)
  if a==1:
    ans1=ans1+max(0,1-(S[i]+s1))
    s1=s1+max(0,1-(S[i]+s1))
  else:
    ans1=ans1+max(0,S[i]+s1+1)
    s1=s1-max(0,S[i]+s1+1)
ans2=0
a=1
s2=0
for i in range(n):
  a=a*(-1)
  if a==1:
    ans2=ans2+max(0,1-(S[i]+s2))
    s2=s2+max(0,1-(S[i]+s2))
  else:
    ans2=ans2+max(0,S[i]+s2+1)
    s2=s2-max(0,S[i]+s2+1)
print(min(ans1,ans2))

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

S1 = 0
S2 = 0

cnt1 = 0
cnt2 = 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)

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

X = [0]
for i in range(n):
    if i % 2 == 0:
        X.append(1)
    else:
        X.append(-1)

Y = [-x for x in X]

x = 0
AX = [0] + A
for i in range(1, n + 1):
    temporary = AX[i - 1] + AX[i]
    if temporary * X[i] > 0:
        AX[i] = temporary
    else:
        x += abs(temporary - X[i])
        AX[i] = X[i]

y = 0
AY = [0] + A
for i in range(1, n + 1):
    temporary = AY[i - 1] + AY[i]
    if temporary * Y[i] > 0:
        AY[i] = temporary
    else:
        y += abs(temporary - Y[i])
        AY[i] = Y[i]

print(min(x, y))

Solution (Python):

import math
import io

data=int(input())
qwe = (list(map(int,input().split())))
score1=score2=0
cnt1=cnt2=0
for i ,num in enumerate(qwe):
	score1+=num
	if score1<=0 and i%2==0:
		cnt1+=1-score1
		score1=1
	elif score1>=0 and i%2!=0:
		cnt1+=1+score1
		score1=-1
		
	score2+=num
	if score2<=0 and i%2!=0:
		cnt2+=1-score2
		score2=1
	elif score2>=0 and i%2==0:
		cnt2+=1+score2
		score2=-1
print(min(cnt1,cnt2))

Solution (Python):
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): 
    return list(map(int,input().split()))
def m(): 
    return map(int,input().split())
def onem(): 
    return int(input())
def s(x): 
    a = []
    if len(x) == 0:
        return []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): 
    return " ".join(map(str,x))
def max2(x): 
    return max(map(max,x))
def In(x,a): 
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False

def pow_k(x, n):
    ans = 1
    while n:
        if n % 2:
            ans *= x
        x *= x
        n >>= 1
    return ans

"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""

n = onem()

a = l()

po = [a[i] for i in range(n)]

for i in range(1,n):
    po[i] += po[i-1]

co = 0

ans = 0

for i in range(n):
    po[i] += co
    if i % 2 == 0:
        if po[i] > 0:
            continue
        else:
            co += abs(po[i]) + 1
            ans += abs(po[i]) + 1

    else:
        if po[i] < 0:
            continue
        else:
            co -= abs(po[i]) + 1
            ans += abs(po[i]) + 1
anss = 0
co = 0
po = [a[i] for i in range(n)]

for i in range(1,n):
    po[i] += po[i-1]

for i in range(n):
    po[i] += co
    if i % 2 != 0:
        if po[i] > 0:
            continue
        else:
            co += abs(po[i]) + 1
            anss += abs(po[i]) + 1

    else:
        if po[i] < 0:
            continue
        else:
            co -= abs(po[i]) + 1
            anss += abs(po[i]) + 1
print(min(ans,anss))

Solution (Python):
def solve(n, arr):
  a = [x for x in arr]
  res = 0
  s = [0 for i in range(n)]
  if a[0] <= 0:
    res += a[0] * (-1) + 1
    a[0] += a[0] * (-1) + 1
  s[0] = a[0]
  for i in range(1, n):
    s[i] = s[i - 1] + a[i]
    if s[i - 1] > 0 and s[i] >= 0:
      res += s[i - 1] + a[i] + 1
      a[i] -= s[i - 1] + a[i] + 1
    elif s[i - 1] < 0 and s[i] <= 0:
      res += 1 - s[i - 1] - a[i]
      a[i] += 1 - s[i - 1] - a[i]
    s[i] = s[i - 1] + a[i]
  return res

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

  print(min(solve(n, a), solve(n, list(map(lambda x: x * (-1), a)))))

if __name__ == "__main__":
  main()

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
s = [a[0] for _ in range(n)]
t = [a[0] for _ in range(n)]
for i in range(1, n):
    s[i] = s[i - 1] + a[i]
    t[i] = t[i - 1] + a[i]
x, y = 0, 0
ans1, ans2 = 0, 0
for i in range(n):
    ss = s[i] + x
    tt = t[i] + y
    if i % 2 == 0:
        if ss <= 0:
            x += abs(ss) + 1
            ans1 += abs(ss) + 1
        if tt >= 0:
            y -= abs(tt) + 1
            ans2 += abs(tt) + 1
    else:
        if ss >= 0:
            x -= abs(ss) + 1
            ans1 += abs(ss) + 1
        if tt <= 0:
            y += abs(tt) + 1
            ans2 += abs(tt) + 1
print(min(ans1, ans2))

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

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

def check(sign):
    a_cnt=0
    a_sum=0
    for a in an:
        a_sum+=a
        if a_sum*sign<=0:
            a_cnt+=abs(a_sum-sign)
            a_sum=sign
        sign*=-1

        

    return a_cnt

print(min(check(1),check(-1)))

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

a0 = A[0]
if a0 > 0:
    S1, cnt1 = a0, 0
    S2, cnt2 = -1, abs(a0) + 1
elif a0 < 0:
    S1, cnt1 = 1, abs(a0) + 1
    S2, cnt2 = a0, 0
else:
    S1, cnt1 = 1, 1
    S2, cnt2 = -1, 1

S = S1
cnt = cnt1
for i in range(1, N):
    nS = S + A[i]
    if S > 0:
        if nS > 0:
            cnt += abs(nS) + 1
            nS = -1
    elif S < 0:
        if nS < 0:
            cnt += abs(nS) + 1
            nS = 1
    S = nS
ans = cnt

S = S2
cnt = cnt2
for i in range(1, N):
    nS = S + A[i]
    if S > 0:
        if nS >= 0:
            cnt += abs(nS) + 1
            nS = -1
    elif S < 0:
        if nS <= 0:
            cnt += abs(nS) + 1
            nS = 1
    S = nS
ans = min(cnt, ans)
print(ans)

Solution (Python):
import sys

n, *a = map(int, sys.stdin.read().split())

def main():
    c1 = c2 = s1 = s2 = 0
    for i in range(n):
        s1 += a[i]
        s2 += a[i]
        if i & 1:
            if s1 >= 0:
                c1 += s1 + 1
                s1 = -1
            if s2 <= 0:
                c2 += 1 - s2
                s2 = 1
        else:
            if s1 <= 0:
                c1 += 1 - s1
                s1 = 1
            if s2 >= 0:
                c2 += s2 + 1
                s2 = -1
    print(min(c1, c2))

if __name__ ==  '__main__':
    main()

Solution (Python):




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

    s = [A[0]]

    for i in range(1, N):
        s.append(s[-1] + A[i])

    cnt_a = 0
    base = 0
    for i in range(N):
        if ((base + s[i] >= 0) and (i % 2 == 1)) \
                or ((base + s[i] <= 0) and (i % 2 == 0)):
            cnt_a += (1 + abs(base + s[i]))
            base += (1 + abs(base + s[i])) * (-1) ** i

    cnt_b = 0
    base = 0
    for i in range(N):
        if ((base + s[i] <= 0) and (i % 2 == 1)) \
                or ((base + s[i] >= 0) and (i % 2 == 0)):
            cnt_b += (1 + abs(base + s[i]))
            base -= (1 + abs(base + s[i])) * (-1) ** i

    print(min(cnt_a, cnt_b))


if __name__ == "__main__":
    main()

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

def cnt(p):
  s=d=0
  for i in range(n):
    c=(s+a[i])*p
    if c<=0:
      d+=(1-c)
      s=p
    else:
      s+=a[i]
    p*=(-1)
  return d

da=cnt(1)
db=cnt(-1)
print(min(da,db))

Solution (Python):










"""def kiri(n,m):
  r_ = n / m
  if (r_ - (n // m)) > 0:
    return (n//m) + 1
  else:
    return (n//m)"""

""" n! mod m 階乗
mod = 1e9 + 7
N = 10000000
fac = [0] * N
def ini():
  fac[0] = 1 % mod
  for i in range(1,N):
    fac[i] = fac[i-1] * i % mod"""

"""mod = 1e9+7
N = 10000000
pw = [0] * N
def ini(c):
  pw[0] = 1 % mod
  for i in range(1,N):
    pw[i] = pw[i-1] * c % mod"""

"""
def YEILD():
  yield 'one'
  yield 'two'
  yield 'three'
generator = YEILD()
print(next(generator))
print(next(generator))
print(next(generator))
"""
"""def gcd_(a,b):
  if b == 0:
    return a
  return gcd_(a,a % b) 
"""def extgcd(a,b,x,y):
  d = a
  if b!=0:
    d = extgcd(b,a%b,y,x)
    y -= (a//b) * x
    print(x,y)
  else:
    x = 1
    y = 0
  return d"""


def readInts():
  return list(map(int,input().split()))
mod = 10**9 + 7
def main():
  n = int(input())
  A = readInts()
  
  cur = 0
  ans1 = 0
  for i in range(n):
    cur += A[i]
    if i % 2:
      if cur <= 0:
        ans1 += -cur+1
        cur = 1
    else:
      if cur >= 0:
        ans1 += cur + 1
        cur = -1
  cur = 0
  ans2 = 0
  for i in range(n):
    cur += A[i]
    if i % 2:
      if cur >= 0:
        ans2 += cur + 1
        cur = -1
    else:
      if cur <= 0:
        ans2 += -cur + 1
        cur = 1
  print(min(ans1,ans2))
if __name__ == '__main__':
  main()