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

wa=0
ans1=0
for i in range(n):
    wa+=a[i]
    if i%2==0:
        if wa==0:
            wa=1
            ans1+=1
        elif wa>0:
            pass
        else:
            ans1+=1-wa
            wa=1
    else:
        if wa==0:
            wa=-1
            ans1+=1
        elif wa<0:
            pass
        else:
            ans1+=wa+1
            wa=-1
wa=0
ans2=0
for i in range(n):
    wa+=a[i]
    if i%2:
        if wa==0:
            wa=1
            ans2+=1
        elif wa>0:
            pass
        else:
            ans2+=1-wa
            wa=1
    else:
        if wa==0:
            wa=-1
            ans2+=1
        elif wa<0:
            pass
        else:
            ans2+=wa+1
            wa=-1

print(min(ans1,ans2))

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 f(n, a, p, r):
    for i in range(1, n):
        t = p + a[i]
        if p < 0 and t <= 0:
            r += 1 - t
            t = 1
        elif p > 0 and t >= 0:
            r += t + 1
            t = -1
        p = t
    return r


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


p = a[0]
r = 0
if p <= 0:
    r += 1 - p
    p = 1
result1 = f(n, a, p, r)


p = a[0]
r = 0
if p >= 0:
    r += p + 1
    p = -1
result2 = f(n, a, p, r)

print(min(result1, result2))

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

def f(times, prefix_sum):
    for i in range(1, n):
        if prefix_sum < 0:
            if prefix_sum + a[i] > 0:
                prefix_sum += a[i]
            else:
                times += 1 - (prefix_sum + a[i])
                prefix_sum = 1      
        elif prefix_sum > 0:
            if prefix_sum + a[i] < 0:
                prefix_sum += a[i]
            else:
                times += abs(-1 - (prefix_sum + a[i]))
                prefix_sum = -1
    
    return times

if a[0] > 0:
    t1 = 0
    p1 = a[0]
else:
    t1 = 1 - a[0]
    p1 = 1


if a[0] < 0:
    t2 = 0
    p2 = a[0]
else:
    t2 = abs(-1 - a[0])
    p2 = -1
    

print(min(f(t1, p1), f(t2, p2)))

Solution (Python):
input()
seq = list(map(int, input().rstrip("\n").split()))

iter_ = iter(seq)
ans1 = 0
for i, num in enumerate(iter_):
    if i == 0:
        sum_ = num
        if sum_ > 0:
            pass
        else:
            ans1 += abs(sum_ - 1)
            sum_ = 1
    else:
        sum_ += num
        if i % 2:
            if sum_ < 0:
                pass
            else:
                ans1 += abs(sum_ + 1)
                sum_ = -1
        else:
            if sum_ > 0:
                pass
            else:
                ans1 += abs(sum_ - 1)
                sum_ = 1

iter_ = iter(seq)
ans2 = 0
for i, num in enumerate(iter_):
    if i == 0:
        sum_ = num
        if sum_ < 0:
            pass
        else:
            ans2 += abs(sum_ + 1)
            sum_ = -1
    else:
        sum_ += num
        if i % 2:
            if sum_ > 0:
                pass
            else:
                ans2 += abs(sum_ - 1)
                sum_ = 1
        else:
            if sum_ < 0:
                pass
            else:
                ans2 += abs(sum_ + 1)
                sum_ = -1

print(min(ans1, ans2))

Solution (Python):
def z(s,l):
	for i in range(n-1):
		r=l+a[i+1]
		if r*l>=0:
			if l<=0:
				s+=1-r
				r=1
			else:
				s+=1+r
				r=-1
		l=r
	return s
n=int(input())
a=list(map(int,input().split()))
s1=0
l1=a[0]
if a[0]<=0:
	s1=1-a[0]
	l1=1
s2=0
l2=a[0]
if a[0]>=0:
	s2=a[0]+1
	l2=-1
print(min(z(s1,l1),z(s2,l2)))

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


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


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

print(min(count, count2))

Solution (Python):

import sys
import itertools

def solve(n: int, a: "List[int]"):
    a_sum = list(itertools.accumulate(a))
    
    dif1 = 0
    answer1 = 0
    for i in range(0,n):
        if i%2==0:
            if a_sum[i]+dif1>-1:
                answer1 += abs(-1-(a_sum[i]+dif1))
                dif1 += -1-(a_sum[i]+dif1)
        else:
            if a_sum[i]+dif1 <1:
                answer1 += abs(1-(a_sum[i]+dif1))
                dif1 += 1-(a_sum[i]+dif1)

    
    dif2 = 0
    answer2 = 0
    for i in range(0,n):
        if i%2==0:
            if a_sum[i]+dif2<1:
                answer2 += abs(1-(a_sum[i]+dif2))
                dif2 += 1-(a_sum[i]+dif2)
        else:
            if a_sum[i]+dif2>-1:
                answer2 += abs(-1-(a_sum[i]+dif2))
                dif2 += -1-(a_sum[i]+dif2)
        
    print(min(answer1,answer2))
    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 = input()
a = list(map(int, input().split()))


def chk(a, positive):
    ans = tmp = 0
    for x in a:
        tmp += x
        if positive and tmp < 1:
            ans += 1-tmp
            tmp = 1
        elif not positive and tmp > -1:
            ans += abs(-1-tmp)
            tmp = -1

        positive = not positive
    return ans

print(min((chk(a, True), chk(a, 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)))

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

ans_plus=0
tmp=0
if a[0]<=0:
    ans_plus=1-a[0]
    tmp=1
else:
    tmp=a[0]
plus_flag=False
for i in a[1:]:
    tmp+=i
    if plus_flag==True and tmp>0:
        pass
    elif plus_flag==True and tmp<=0:
        ans_plus+=1-tmp
        tmp=1
    elif plus_flag==False and tmp<0:
        pass
    elif plus_flag==False and tmp>=0:
        ans_plus+=1+tmp
        tmp=-1
    plus_flag=not plus_flag


ans_minus=0
tmp=0
if a[0]>=0:
    ans_minus=1+a[0]
    tmp=-1
else:
    tmp=a[0]
plus_flag=True
for i in a[1:]:
    tmp+=i
    if plus_flag==True and tmp>0:
        pass
    elif plus_flag==True and tmp<=0:
        ans_minus+=1-tmp
        tmp=1
    elif plus_flag==False and tmp<0:
        pass
    elif plus_flag==False and tmp>=0:
        ans_minus+=1+tmp
        tmp=-1
    plus_flag=not plus_flag

print(min(ans_plus, ans_minus))

Solution (Python):
def f(flg, an):
    accum = 0
    ret = 0
    for ai in an:
        accum += ai
        if flg:
            flg = False
            if accum <= 0:
                ret += -accum + 1
                accum = 1
        else:
            flg = True
            if 0 <= accum:
                ret += accum + 1
                accum = -1
    return ret


def main():
    an = list(map(int, open(0).read().split()[1:]))
    print(min(f(False, an), f(True, an)))
    return


main()

Solution (Python):
an = list(map(int, open(0).read().split()[1:]))


def f(flg):
    accum = 0
    ret = 0
    for ai in an:
        accum += ai
        if flg:
            flg = False
            if accum <= 0:
                ret += -accum + 1
                accum = 1
        else:
            flg = True
            if 0 <= accum:
                ret += accum + 1
                accum = -1
    return ret


def main():
    print(min(f(False), f(True)))
    return


main()

Solution (Python):
an = list(map(int, open(0).read().split()[1:]))


def f(flg):
    accum = 0
    ret = 0
    for i, ai in enumerate(an):
        accum += ai
        if flg:
            flg = False
            if accum <= 0:
                ret += -accum + 1
                accum = 1
        else:
            flg = True
            if 0 <= accum:
                ret += accum + 1
                accum = -1
    return ret


def main():
    print(min(f(False), f(True)))
    return


main()

Solution (Python):
def f(flg, an):
    accum = 0
    ret = 0
    for i, ai in enumerate(an):
        accum += ai
        if flg:
            flg = False
            if accum <= 0:
                ret += -accum + 1
                accum = 1
        else:
            flg = True
            if 0 <= accum:
                ret += accum + 1
                accum = -1
    return ret


def main():
    an = list(map(int, open(0).read().split()[1:]))
    print(min(f(False, an), f(True, an)))
    return


main()

Solution (Python):
def f(flg, an):
    accum = 0
    ret = 0
    for i, ai in enumerate(an):
        accum += ai
        if flg:
            flg = False
            if accum <= 0:
                ret += -accum + 1
                accum = 1
        else:
            flg = True
            if 0 <= accum:
                ret += accum + 1
                accum = -1
    return ret


def main():
    an = [int(ai) for ai in open(0).read().split()[1:]]
    print(min(f(False, an), f(True, an)))
    return


main()

Solution (Python):
def f(flg, an):
    accum = 0
    ret = 0
    for i, ai in enumerate(an):
        accum += ai
        if flg:
            flg = False
            if accum <= 0:
                ret += -accum + 1
                accum = 1
        else:
            flg = True
            if 0 <= accum:
                ret += accum + 1
                accum = -1
    return ret


def main():
    n, *an = map(int, open(0).read().split())
    print(min(f(False, an), f(True, an)))
    return


main()

Solution (Python):
def f(flg, an):
    accum = 0
    ret = 0
    for i, ai in enumerate(an):
        accum += ai
        if i % 2:
            if flg and 0 <= accum:
                ret += accum + 1
                accum = -1
            elif not flg and accum <= 0:
                ret += -accum + 1
                accum = 1
        else:
            if flg and accum <= 0:
                ret += -accum + 1
                accum = 1
            elif not flg and 0 <= accum:
                ret += accum + 1
                accum = -1

    return ret


def main():
    n, *an = map(int, open(0).read().split())
    print(min(f(0, an), f(1, an)))
    return


main()

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

sums = 0
ans = 0

for itr, val in enumerate(a):
  sums += val
  if itr % 2 == 0:
    if sums <= 0:
      ans += 1 - sums
      sums = 1
  else:
    if sums >= 0:
      ans += sums + 1
      sums = - 1
gans = ans

sums = 0
ans = 0

for itr, val in enumerate(a):
  sums += val
  if itr % 2 != 0:
    if sums <= 0:
      ans += 1 - sums
      sums = 1
  else:
    if sums >= 0:
      ans += sums + 1
      sums = - 1
kans = ans
print(min(gans, kans))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
ans_even = 0
ans_odd = 0
sum = 0

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

sum = 0
for i in range(n):
    
    if i % 2 == 0:
        sum += a[i]
        if sum > 0:
            pass
        else:
            ans_odd += 1 - sum
            sum = 1
    
    else:
        sum += a[i]
        if sum < 0:
            pass
        else:
            ans_odd += abs(-1 - sum)
            sum = -1
print(min(ans_even, ans_odd))

Solution (Python):
n = int(input())
A = list(map(int,input().split()))
B = A[:]
for i in range(1,n):
    B[i] += B[i-1]
C = B[:]
cnt1 = 0
cnt2 = 0
if B[0]<=0:
    cnt1 += abs(1-B[0])
    cnt2 += 1-B[0]
    B[0] += cnt2
for i in range(1,n):
    if i%2==0:
        if B[i]+cnt2<=0:
            cnt1 += abs(1-B[i]-cnt2)
            cnt2 += 1-B[i]-cnt2
            B[i] += cnt2
    else:
        if B[i]+cnt2>=0:
            cnt1 += abs(-1-B[i]-cnt2)
            cnt2 += -1-B[i]-cnt2
            B[i] += cnt2
cnt3 = 0
cnt4 = 0
if C[0]>=0:
    cnt3 += abs(-1-C[0])
    cnt4 += -1-C[0]
    C[0] += cnt4
for i in range(1,n):
    if i%2==0:
        if C[i]+cnt4>=0:
            cnt3 += abs(-1-C[i]-cnt4)
            cnt4 += -1-C[i]-cnt4
            C[i] += cnt4
    else:
        if C[i]+cnt4<=0:
            cnt3 += abs(1-C[i]-cnt4)
            cnt4 += 1-C[i]-cnt4
            C[i] += cnt4
print(min(cnt1,cnt3))

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

def wh(cst,ttl,flg):
    for i in range(n):
        ttl += a[i]

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

        
    return cst




ttl = 0
cst = 0



cst = wh(cst,ttl,1)
ttl = 0
cst2 = 0
cst2 = wh(cst2,ttl,-1)
   

print(min(cst,cst2))

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

count1 = 0
accum1 = 0
for i in range(n):
    if i % 2 == 0:
        if accum1 + A[i] <= 0:
            count1 += 1 - accum1 - A[i]
            accum1 = 1
        else:
            accum1 += A[i]
    else:
        if accum1 + A[i] >= 0:
            count1 += 1 + accum1 + A[i]
            accum1 = -1
        else:
            accum1 += A[i]
            
count2 = 0
accum2 = 0
for i in range(n):
    if i % 2 != 0:
        if accum2 + A[i] <= 0:
            count2 += 1 - accum2 - A[i]
            accum2 = 1
        else:
            accum2 += A[i]
    else:
        if accum2 + A[i] >= 0:
            count2 += 1 + accum2 + A[i]
            accum2 = -1
        else:
            accum2 += A[i]
            
print(min(count1,count2))

Solution (Python):
n=int(input())
l=[int(i) for i in input().split()]
sm=l[0]
req=0 
pr=1 
if sm>0:
    pass 
else:
    req+=-sm+1 
    sm=1 
for i in range(1,n):
    if pr==1:
        n1=sm+l[i]
        if n1<0:
            sm+=l[i]
            pass 
        else: 
            sm+=l[i]
            req+=sm+1 
            sm=-1 
    else: 
        n1=sm+l[i]
        if n1>0:
            sm+=l[i]
            pass 
        else: 
            sm+=l[i]
            req+=(-sm+1) 
            sm=1 
    pr=1-pr  
req1=0 
pr=0
sm=l[0]
if sm<0:
    pass 
else:
    req1+=sm+1 
    sm=-1 
for i in range(1,n):
    if pr==1:
        n1=sm+l[i]
        if n1<0:
            sm+=l[i]
            pass 
        else: 
            sm+=l[i]
            req1+=sm+1 
            sm=-1 
    else: 
        n1=sm+l[i]
        if n1>0:
            sm+=l[i]
            pass 
        else: 
            sm+=l[i]
            req1+=(-sm+1) 
            sm=1 
    pr=1-pr  
print(min(req,req1))

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
A2=[0]*N
for i in range(N):
    A2[i]=A2[i-1]+A[i]
r,f,s=0,1,0
for a in A2:
    if (a+s)*-f>=0:
        x=a+s-f
        s-=x
        r+=abs(x)
    f*=-1
r2,f,s=0,-1,0
for a in A2:
    if (a+s)*-f>=0:
        x=a+s-f
        s-=x
        r2+=abs(x)
    f*=-1
print(min(r,r2))

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
A = 10 ** 15

for i in [1,-1]:
    B = 0
    C = 0
    for j in a:
        B += j
        if B * i <= 0:
            C += abs(B - i)
            B = i
        i *= -1
    A = min(A,C)
print(A)

Solution (Python):
import copy

n = int(input())
A = list(map(int,input().split()))
sums = [0 for _ in range(n)]

sums[0] = A[0]

for i in range(1,n):
    sums[i] = A[i] + sums[i-1]
sums2 = copy.copy(sums)
tyousei = 0
ans1 = 0
for i in range(n):
    if i%2 ==0:
        if sums[i]+tyousei <=0:
            ans1 += abs(sums[i]+tyousei) +1
            tyousei += abs(sums[i]+tyousei) +1
    if i%2 ==1:
        if sums[i]+tyousei >=0:
            ans1 += abs(sums[i]+tyousei) +1
            tyousei -= sums[i]+tyousei +1

ans2 = 0
tyousei =0
for i in range(n):
    if i%2 ==1:
        if sums[i]+tyousei <=0:
            ans2 += abs(sums[i]+tyousei) +1
            tyousei += abs(sums[i]+tyousei) +1
    if i%2 ==0:
        if sums[i]+tyousei >=0:
            ans2 += sums[i]+tyousei +1
            tyousei -= sums[i]+tyousei +1

print(min(ans1,ans2))

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

ans1 = 0
s = 0
flg = 1
for ai in a:
    s += ai
    if s * flg <= 0:
        ans1 += abs(s) + 1
        s = flg
    flg *= -1

ans2 = 0
s = 0
flg = -1
for ai in a:
    s += ai
    if s * flg <= 0:
        ans2 += abs(s) + 1
        s = flg
    flg *= -1

print(min(ans1, ans2))

Solution (Python):


import numpy as np

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

def ope_sequence(a, plus_flg):
    a = a.copy()
    output = 0
    
    if plus_flg == 1: 
        c = 0 
        for idx in range(n):
            c += a[idx]
            if idx % 2 == 0 and c <= 0:
                a[idx] += 1-c 
                output += abs(1-c)
                c = 1
            elif idx % 2 == 1 and c >= 0:
                a[idx] += -1-c 
                output += abs(-1-c)
                c = -1
                
    else: 
        c = 0
        for idx in range(n):
            c += a[idx]
            if idx % 2 == 0 and c >= 0:
                a[idx] += -1-c 
                output += abs(-1-c)
                c = -1
            elif idx % 2 == 1 and c <= 0:
                a[idx] += 1-c 
                output += abs(1-c)
                c = 1
    
    return output

print(min(ope_sequence(original_a,1), ope_sequence(original_a,-1)))

Solution (Python):
def re(k1,l,s):
    r=0
    for i in range(0,n):
        if s==1:
            s=0
            k1+=l[i]
            if k1>=0:
                r+=k1+1
                k1=-1
            continue
        if s==0:
            s=1
            k1+=l[i]
            if k1<=0:
                r+=abs(k1)+1
                k1=1
    return r

n=int(input())
l=list(map(int,input().split()))
ans=0

k1=0

ans=min(re(k1,l,1),re(k1,l,0))



print(ans)

Solution (Python):
inf = float('inf')

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

def accmulate(arr):
    N = len(arr)
    cs = [0]*(N+1)
    for i in range(N):
        cs[i+1] = cs[i] + arr[i]
    return cs


def query(cs, l, r):
    return cs[r] - cs[l]

cs = accmulate(A)[1:]
ans = inf

res = 0
tmp = 0
for i in range(len(cs)):
    if i % 2 == 0:
        
        d = -1 - (cs[i] + tmp)
        d = min(d, 0)
        res += -d
        tmp += d
    else:
        
        d = 1 - (cs[i] + tmp)
        d = max(d, 0)
        res += d
        tmp += d

ans = min(ans, res)

res = 0
tmp = 0
for i in range(len(cs)):
    if i % 2 == 1:
        
        d = -1 - (cs[i] + tmp)
        d = min(d, 0)
        res += -d
        tmp += d
    else:
        
        d = 1 - (cs[i] + tmp)
        d = max(d, 0)
        res += d
        tmp += d

ans = min(ans, res)
print(ans)

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

check=a[0]
ans=0
if check<=0:
    ans=abs(check)+1
    check=1

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

            
check=a[0]
ans2=0
if check>=0:
    ans2=abs(check)+1
    check=-1

for i in range(1,n):
    check2=check+a[i]
    if i%2!=0:
        if check2<=0:
            ans2+=(abs(check2)+1)
            check=1
        else:
            check=check2
            
    else:
        if check2>=0:
            ans2+=(abs(check2)+1)
            check=-1
        else:
            check=check2
print(min(ans,ans2))

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

def calc(flag_sign):
  temp_flag = flag_sign
  sum_a = 0
  count = 0
  for one_a in list_a:
    if temp_flag:
      if sum_a + one_a > 0:
        sum_a += one_a
      else:
        count += abs(sum_a) - one_a + 1
        sum_a = 1
      temp_flag = False
    else:
      if sum_a + one_a < 0:
        sum_a += one_a
      else:
        count += abs(sum_a) + one_a + 1
        sum_a = -1
      temp_flag = True
  return count
    
print(min(calc(True), calc(False)))

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
a1,a2,s=0,0,0
def cal(a,t):
    ans,x=0,0
    for i in a:
        x+=i
        if t 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(cal(a,True),cal(a,False)))

Solution (Python):
n = int(input())
l = list(map(int, input().split()))
ans_l = []
for j in range(2):
    last_sum = l[0]
    ans = 0
    if j == 0:
        if last_sum <= 0:
            ans += abs(1 - last_sum)
            last_sum += 1 - last_sum
    else:
        if last_sum >= 0:
            ans += abs(-1 - last_sum)
            last_sum += -1 - last_sum
    for i in range(n - 1):
        
        if last_sum > 0:
            if last_sum + l[i + 1] < 0:
                last_sum += l[i + 1]
            else:
                a = -1 - last_sum - l[i + 1]
                ans += abs(a)
                last_sum += a + l[i + 1]
        else:
            if last_sum + l[i + 1] > 0:
                last_sum += l[i + 1]
            else:
                a = 1 - last_sum - l[i + 1]
                ans += abs(a)
                last_sum += a + l[i + 1]
    ans_l.append(ans)
print(min(ans_l))

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

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

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


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


print(min(check("+"), check("-")))

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


def f(x):
    if x == 0:
        return 0
    else:
        return x // abs(x)


a1 = [0] * n

if a[0] != 0:
    a1[0] = a[0]
    ans = 0
else:
    a1[0] = 1
    ans = 1

b = a1[0]

for i in range(1, n):
    if b * (b + a[i]) >= 0:
        if a[i] != 0:
            a1[i] = -f(a1[i - 1]) - b
        else:
            if b > 0:
                a1[i] = -1
            else:
                a1[i] = 0
        if b + a1[i] == 0:
            a1[i] += f(a1[i])
        ans += abs(a1[i] - a[i])
    else:
        a1[i] = a[i]

    b += a1[i]

a2 = [0] * n

if a[0] != 0:
    a2[0] = -f(a[0])
    ans1 = abs(a2[0] - a[0])
else:
    a2[0] = -1
    ans1 = 1

b1 = a2[0]

for i in range(1, n):
    if b1 * (b1 + a[i]) > 0:
        if a[i] != 0:
            a2[i] = -f(a2[i - 1]) - b1
        else:
            if b1 > 0:
                a2[i] = -1
            else:
                a2[i] = 0
        if b1 + a2[i] == 0:
            a2[i] += f(a2[i])
        ans1 += abs(a2[i] - a[i])
    else:
        a2[i] = a[i]

    b1 += a2[i]

print(min(ans1, ans))

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
cs = [A[0]] + [0] * (N - 1)
a = 0
b = 0
for i in range(N - 1):
  cs[i + 1] = cs[i] + A[i + 1]
temp = 0
for i in range(N):
  if i % 2:
    if cs[i] + temp >= 0:
      a += abs(cs[i] + temp) + 1
      temp -= abs(cs[i] + temp) + 1
  else:
    if cs[i] + temp <= 0:
      a += abs(cs[i] + temp) + 1
      temp += abs(cs[i] + temp) + 1
temp = 0
for i in range(N):
  if i % 2:
    if cs[i] + temp <= 0:
      b += abs(cs[i] + temp) + 1
      temp += abs(cs[i] + temp) + 1
  else:
    if cs[i] + temp >= 0:
      b += abs(cs[i] + temp) + 1
      temp -= abs(cs[i] + temp) + 1
print(min(a, b))

Solution (Python):


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


def solve(bl):
    if bl:
        tmp = -1
    else:
        tmp = 0
    sm = 0
    cnt = 0
    for i, ele in enumerate(a):
        sm += ele
        if i % 2 + tmp:
            if sm < 1:
                cnt += 1 - sm
                sm = 1
        else:
            if sm > -1:
                cnt += sm + 1
                sm = -1

    return cnt


ans = min(solve(True), solve(False))
print(ans)

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


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


T=0
countb=0
for i in range(n):
  if i%2 == 1:
    if T+a[i] < 1:
      countb += (1-T-a[i])
      T = 1
    else:
      T+= a[i]
  else:
    if T+a[i] > -1:
      countb += (T+a[i]+1)
      T = -1
    else:
      T+=a[i]
      
print(min(counta,countb))

Solution (Python):
from sys import stdin
n = int(stdin.readline().rstrip())
a = [int(x) for x in stdin.readline().rstrip().split()]
ans1 = 0
sum_sec = 0
for ind,i in enumerate(a):
    sum_sec += i
    if ind % 2 == 0:
        if sum_sec <= 0:
            ans1 += (-sum_sec+1)
            sum_sec = 1
    else:
        if sum_sec >= 0:
            ans1 += (sum_sec+1)
            sum_sec = -1

ans2 = 0
sum_sec = 0
for ind,i in enumerate(a):
    sum_sec += i
    
    if ind % 2 == 0:
        if sum_sec >= 0:
            ans2 += (sum_sec+1)
            sum_sec = -1
    else:
        if sum_sec <= 0:
            ans2 += (-sum_sec+1)
            sum_sec = 1
    
print(min(ans1,ans2))

Solution (Python):


n = int(input())
l = list(map(int, input().split()))
cas1 = [0, 0]
cas2 = [0, 0]
def plus(index, cas):
    cost = cas[0]
    sum = cas[1]
    if (sum + l[index]) <= 0:
        return [cost + abs(l[index]+sum-1), 1]
    else:
        return [cost, sum + l[index]]
def minus(index, cas):
    cost = cas[0]
    sum = cas[1]
    if (sum + l[index]) >= 0:
        return [cost + abs(l[index]+sum+1), -1]
    else:
        return [cost, sum + l[index]]

for i in range(n):
    if i%2 == 0:
        cas1 = plus(i, cas1)
        cas2 = minus(i, cas2)
    else:
        cas1 = minus(i, cas1)
        cas2 = plus(i, cas2)

print(min(cas1[0], cas2[0]))

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
u=0
s=1
x=0
y=0
for i in a:
    u+=i
    if s*u<=0:
      x+=1-s*u
      u=s
    s=s*(-1)
s=-1
u=0
for i in a:
    u+=i
    if s*u<=0:
      y+=1-s*u
      u=s
    s=-1*s
print(min(x,y))

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007



flag = 1
n = I()
A = LI()
ret = 0
cumsum = 0
for i in range(n):
    cumsum += A[i]
    if flag and cumsum <= 0:
        ret += 1 - cumsum
        cumsum = 1
    elif not flag and cumsum >= 0:
        ret += cumsum + 1
        cumsum = -1
    flag ^= 1


ans = ret
ret = 0
flag = 0
cumsum = 0
for i in range(n):
    cumsum += A[i]
    if flag and cumsum <= 0:
        ret += 1 - cumsum
        cumsum = 1
    elif not flag and cumsum >= 0:
        ret += cumsum + 1
        cumsum = -1
    flag ^= 1


print(min(ans, ret))

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

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

count = 0
ans2 = 0
for i in range(N):
  if i%2==0:
    if count+a[i]<=0:
      ans2 += -a[i]+abs(count)+1
      count = 1
    else:
      count += a[i]
  else:
    if count+a[i]>=0:
      ans2 += count+1+a[i]
      count = -1
    else:
      count += a[i]
  

print(min(ans1, ans2))

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