Problem:
 Score : 300 points    Problem Statement You are given an integer sequence of length N . The i -th term in the sequence is a_i .
In one operation, you can select a term and either increment or decrement it by one.  At least how many operations are necessary to satisfy the following conditions?   For every i  (1≤i≤n) , the sum of the terms from the 1 -st through i -th term is not zero.  For every i  (1≤i≤n-1) , the sign of the sum of the terms from the 1 -st through i -th term, is different from the sign of the sum of the terms from the 1 -st through (i+1) -th term.       Constraints  2 ≤ n ≤ 10^5  |a_i| ≤ 10^9  Each a_i is an integer.         Input Input is given from Standard Input in the following format:  n  a_1  a_2  ...  a_n       Output Print the minimum necessary count of operations.        Sample Input 1 4
1 -3 1 0      Sample Output 1 4  For example, the given sequence can be transformed into 1, -2, 2, -2 by four operations. The sums of the first one, two, three and four terms are 1, -1, 1 and -1 , respectively, which satisfy the conditions.       Sample Input 2 5
3 -6 4 -5 7      Sample Output 2 0  The given sequence already satisfies the conditions.       Sample Input 3 6
-1 4 3 2 -5 4      Sample Output 3 8

Solution (Python):

import sys
input = sys.stdin.readline
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7



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

def get_count(summary):
    count = 0
    for i in range(1, N):
    

        
        if summary > 0:
            
            if (summary + A[i]) < 0:
                summary += A[i]
            else:
                
                summary += A[i]
                count += (summary+1)
                summary = -1

        
        else:
            if (summary + A[i]) > 0:
                summary += A[i]
            else:
                
                summary += A[i]
                count += abs(summary)+1
                summary = 1

    return count

if A[0] > 0:
    plus = get_count(A[0])
    minus = get_count(-1)
    minus += (A[0]+1)
elif A[0] == 0:
    plus = get_count(1)+1
    minus = get_count(-1)+1
else:
    plus = get_count(1)
    plus += (abs(A[0])+1)
    minus = get_count(A[0])

print(min(plus, minus))

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


def calc(ls, sign):
    count = 0
    acc = 0
    for x in ls:
        y = acc + x
        if y == 0 or y // abs(y) != sign:
            z = sign - y
            count += abs(z)
            acc = sign
        else:
            acc = y
        sign *= -1
    return count


count = min(calc(ls, 1), calc(ls, -1))
print(count)

Solution (Python):

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

SUM = 0
ans = 0
flag = 0 
for i in range(N):
    if i == 0:
        SUM = A[i]
        if SUM <= 0:
            ans+=1-SUM
            SUM = 1
    else:
        SUM += A[i]
        if flag == 0:
            if SUM >= 0:
                ans += SUM+1
                SUM = -1
            flag = 1
        else:
            if SUM <= 0:
                ans += 1-SUM
                SUM = 1
            flag = 0
    

MIN = ans
ans = 0
flag = 1 
for i in range(N):
    if i == 0:
        SUM = A[i]
        if SUM >= 0:
            ans+=1+SUM
            SUM = -1
    else:
        SUM += A[i]
        if flag == 0:
            if SUM >= 0:
                ans += SUM+1
                SUM = -1
            flag = 1
        else:
            if SUM <= 0:
                ans += 1-SUM
                SUM = 1
            flag = 0
    
                
ans = min(MIN,ans)
print(ans)

Solution (Python):
import numpy as np

N = int(input())
a_s = input().split()
for i in range(N):
    a_s[i] = int(a_s[i])
a_s = np.array(a_s)
    

ans1 = 0
sum0 = 0
for i ,a in enumerate(a_s):
    sum1 = sum0 + a
    if i==0:
        if sum1>0:
            pass
        else:
            ans1 += abs(1 - sum1)
            sum1 = 1
            
    elif i%2==0:
        if sum1*sum0<0:
            pass
        else:
            ans1 += abs(1 - sum1)
            sum1 = 1
            
    else:
        if sum1*sum0<0:
            pass
        else:
            ans1 += abs(-1 - sum1)
            sum1 = -1
    sum0 = sum1
    

ans2 = 0
sum0 = 0
for i ,a in enumerate(a_s):
    sum2 = sum0 + a
    if i==0:
        if sum2<0:
            pass
        else:
            ans2 += abs(-1 - sum2)
            sum2 = -1
            
    elif i%2==0:
        if sum2*sum0<0:
            pass
        else:
            ans2 += abs(-1 - sum2)
            sum2 = -1
            
    else:
        if sum2*sum0<0:
            pass
        else:
            ans2 += abs(1 - sum2)
            sum2 = 1
    sum0 = sum2
    
ans = min(ans1,ans2)
print(ans)

Solution (Python):
from collections import deque

N = int(input())
A = [int(n) for n in input().split()]

def calc(a, positive):
    curr = 0
    ans = 0
    while a:
        curr += a.popleft()
        if (positive and curr <= 0) or\
           ((not positive) and curr >= 0):
            ans += abs(curr) + 1
            curr = 1 if positive else -1
        positive = not positive
    return ans

def main():
    ans = min(calc(deque(A), True), calc(deque(A), False))
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
nums = list(map(int, input().split()))
ans = 10**14
for start in [-1, 1]:
  before = start
  cnt = 0
  sum_n = 0
  for num in nums:
    sum_n += num
    if before*sum_n >= 0:
      if before < 0:
        cnt += abs(1-sum_n)
        sum_n = 1
      else:
        cnt += abs(-1-sum_n)
        sum_n = -1
    before = sum_n
  ans = min(ans, cnt)
print(ans)

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

counter_plus = 0
is_plus = True
sum = 0
for i in range(n):
  sum += A[i]
  if is_plus == True:
    if sum < 1:
      div = 1 - sum
      sum += div
      counter_plus += div
  else:
    if sum > -1:
      div = -1 -sum
      sum += div
      counter_plus -= div
  is_plus = not(is_plus)

counter_minus = 0
is_plus = False
sum = 0
for i in range(n):
  sum += A[i]
  if is_plus == True:
    if sum < 1:
      div = 1 - sum
      sum += div
      counter_minus += div
  else:
    if sum > -1:
      div = -1 -sum
      sum += div
      counter_minus -= div
  is_plus = not(is_plus)  
print(min(counter_plus, counter_minus))

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

def solve(a,t):
  ans,x=0,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(solve(A,True),solve(A,False)))

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

ans = []
for first in (_arr[0], -1 * _arr[1] + 1, -1 * _arr[1] - 1, 1, -1):
    arr = _arr[:]
    c = 0
    prev = 0
    for i in range(n):
        t = prev + arr[i]
        if i == 0:
            c += abs(arr[i] - first)
            arr[i] = first
        elif prev > 0 and t >= 0:
            diff = t + 1
            c += diff
            arr[i] -= diff
        elif prev < 0 and t <= 0:
            diff = -1 * t + 1
            c += diff
            arr[i] += diff
        prev += arr[i]
    ans.append(c)
print(min(ans))

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

ans = []
for first in (_arr[0], -1 * _arr[1] + 1, -1 * _arr[1] - 1, 1, -1):
    arr = _arr[:]
    c = 0
    prev = 0
    for i in range(n):
        t = prev + arr[i]
        if i == 0:
            c += abs(arr[i] - first)
            arr[i] = first
        elif prev > 0 and t >= 0:
            diff = t + 1
            c += diff
            arr[i] -= diff
        elif prev < 0 and t <= 0:
            diff = -1 * t + 1
            c += diff
            arr[i] += diff
        prev += arr[i]
    ans.append(c)
print(min(ans))

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

ans = float("inf")

for ii in [1, -1]:
    i = ii
    s = 0
    ans0 = 0
    for j in range(n):
        s += A[j]
        if s * i <= 0:
            ans0 += abs(s - i)
            s = i
        i = -i

    ans = min(ans, ans0)

print(ans)

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

cnt_1 = 0
base = 0

for i in range(n):
    if ((base + a[i] >= 0) and (i % 2 == 1)) or ((base + a[i] <= 0) and (i % 2 == 0)):
        cnt_1 += (1 + abs(base + a[i]))
        base += a[i] + (1 + abs(base + a[i])) * (-1) ** i 
    else:
      base += a[i] 


cnt_2 = 0
base = 0
for i in range(n):
    if ((base + a[i] <= 0) and (i % 2 == 1)) or ((base + a[i] >= 0) and (i % 2 == 0)):
        
        cnt_2 += (1 + abs(base + a[i]))
        base += a[i] + (1 + abs(base + a[i])) * (-1) ** (i + 1)  
    else:
      base += a[i] 

print(min(cnt_1, cnt_2))

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

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

def f(sgn):
    cum = 0
    cnt_operation = 0
    for a in A:
        sgn *= -1
        cum += a
        if cum * sgn > 0:
            continue
        else:
            cnt_operation += abs(cum) + 1
            cum = sgn
    return cnt_operation
print(min(f(-1), f(1)))

Solution (Python):
import numpy as np
def main():
    n=int(input())
    a=list(map(int,input().split(' ')))
    sumcum_odd = 0
    sumcum_even = 0
    ans_odd = 0
    ans_even = 0
    for i in range(n):
        if i%2 ==0:
            if sumcum_odd + a[i] >0:
                sumcum_odd += a[i]
            elif sumcum_odd <=0:
                ans_odd += 1-1*(sumcum_odd+a[i])
                sumcum_odd = 1
            if sumcum_even + a[i] <0:
                sumcum_even += a[i]
            elif sumcum_even >= 0:
                ans_even += sumcum_even + a[i] +1
                sumcum_even = -1
        else:
            if sumcum_odd + a[i] <0:
                sumcum_odd += a[i]
            elif sumcum_odd >=0:
                ans_odd += sumcum_odd + a[i] +1
                sumcum_odd = -1
            if sumcum_even + a[i] >0:
                sumcum_even += a[i]
            elif sumcum_even <= 0:
                ans_even += 1-1*(sumcum_even+a[i])
                sumcum_even = 1
    print(min(ans_odd,ans_even))



if __name__ == '__main__':
    main()

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

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



count1 = 0
if A[0] <= 0:
  sum1 = 1
  count1 = 1 - A[0]
else:
  sum1 = A[0]

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

count2 = 0
if A[0] >= 0:
  sum2 = -1
  count2 = 1 + A[0]
else:
  sum2 = A[0]

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

print(min(count1, count2))

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

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

    
    cost1 = 0
    sum_ = 0
    for i in range(n):
        sum_ += a[i]
        if i&1:
            if sum_ >= 0:
                tmp = -1 - sum_
                cost1 += abs(tmp)
                sum_ = -1
        else:
            if sum_ <= 0:
                tmp = 1 - sum_
                cost1 += abs(tmp)
                sum_ = 1
    
    cost2 = 0
    sum_ = 0
    for i in range(n):
        sum_ += a[i]
        if not i&1:
            if sum_ >= 0:
                tmp = -1 - sum_
                cost2 += abs(tmp)
                sum_ = -1
        else:
            if sum_ <= 0:
                tmp = 1 - sum_
                cost2 += abs(tmp)
                sum_ = 1
    print(min(cost1, cost2))

if __name__ == "__main__":
    main()

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

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

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

Solution (Python):

import sys

input = sys.stdin.readline


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

        even_plus_ans = 0
        before = a[0]
        if a[0] <= 0:
            even_plus_ans -= a[0] - 1
            before = 1

        for i in range(1, n):
            if i % 2 == 0:
                v = a[i] + before
                if v > 0:
                    before = v
                elif v <= 0:
                    before = 1
                    even_plus_ans -= v - 1
            else:
                v = a[i] + before
                if v < 0:
                    before = v
                elif v >= 0:
                    before = -1
                    even_plus_ans += v + 1

        odd_plus_ans = 0
        before = a[0]
        if a[0] >= 0:
            odd_plus_ans += a[0] + 1
            before = -1

        for i in range(1, n):
            if i % 2 != 0:
                v = a[i] + before
                if v > 0:
                    before = v
                elif v <= 0:
                    before = 1
                    odd_plus_ans -= v - 1
            else:
                v = a[i] + before
                if v < 0:
                    before = v
                elif v >= 0:
                    before = -1
                    odd_plus_ans += v + 1

        print(min(even_plus_ans, odd_plus_ans))



if __name__ == '__main__':
    AtCoder().main()

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

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

if A[0] > 0:
  print(min(calc(A),calc([-1]+A[1:])+A[0]+1))
elif A[0] < 0:
  print(min(calc(A),calc([1]+A[1:])-A[0]+1))
else:
  print(min(calc([1]+A[1:]),calc([-1]+A[1:]))+1)

Solution (Python):
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_p = 0
app_p = 0

for i in range(N):
	if i == 0:
		if S[i] <= 0:
			cnt_p += 1 - S[i]
			app_p += 1 - S[i]
		continue

	if i % 2 == 1: 
		if S[i]+app_p >= 0:
			cnt_p += (S[i]+app_p)  - (-1)
			app_p += -1 - (S[i]+app_p)
	elif i % 2 == 0: 
		if S[i] + app_p <= 0:
			cnt_p += 1 - (S[i]+app_p)
			app_p += 1 - (S[i]+app_p)


cnt_n = 0
app_n = 0

for i in range(N):
	if i == 0:
		if S[i] >= 0:
			cnt_n += S[i] - (-1)
			app_n += -1 - S[i]
		continue

	if i % 2 == 1: 
		if S[i]+app_n <= 0:
			cnt_n += 1 - (S[i]+app_n)
			app_n += 1 - (S[i]+app_n)
	elif i % 2 == 0: 
		if S[i] + app_n >= 0:
			cnt_n += (S[i]+app_n) - (-1)
			app_n += -1 - (S[i]+app_n)

print(min(cnt_n, cnt_p))

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

for i in [-1,1]:
    ansi,sum=0,0
    for a in A:
        sum+=a
        if sum*i<=0:
          ansi+=abs(sum-i)
          sum=i
        i*=-1
    ans=min(ans,ansi)
print(ans)

Solution (Python):
from collections import Counter,defaultdict
import sys,heapq,bisect,math,itertools,string,queue,fractions
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))
def inpl_str(): return list(sys.stdin.readline().split())
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)

n = inp()
a = inpl()
acc = list(itertools.accumulate(a))

cnt = 0
tmp = 0
for i in range(n):
    if i%2==0:
        tt = acc[i] + tmp
        if tt <= 0:
            cnt += 1 - tt
            tmp += 1 - tt
    else:
        tt = acc[i] + tmp
        if tt >= 0:
            cnt += tt + 1
            tmp -= tt + 1
ans = cnt

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

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

odd_ans = 0
odd_sum = 0

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

even_ans = 0
even_sum = 0

for i in range(n):
    even_sum += A[i]
    
    if i % 2 == 0:
        if even_sum <= 0:
            even_ans += 1 - even_sum
            even_sum = 1
    else:
        if even_sum >= 0:
            even_ans += even_sum + 1
            even_sum = -1
                        
print(min(odd_ans, even_ans))

Solution (Python):
if __name__ == '__main__':
    n = int(input())
    a = list(map(int, input().split()))
    
    total = 0
    ans_a0_plus = 0
    for i in range(n):
        if i % 2 == 0:
            if total + a[i] > 0:
                total = total + a[i]
            else:
                ans_a0_plus += 1 - (total + a[i])
                total = 1
        else:
            if total + a[i] < 0:
                total = total + a[i]
            else:
                ans_a0_plus += 1 + (total + a[i])
                total = -1
    
    total = 0
    ans_a0_minus = 0
    for i in range(n):
        if i % 2 == 0:
            if total + a[i] < 0:
                total = total + a[i]
            else:
                ans_a0_minus += 1 + (total + a[i])
                total = -1
        else:
            if total + a[i] > 0:
                total = total + a[i]
            else:
                ans_a0_minus += 1 - (total + a[i])
                total = 1
    
    print(min(ans_a0_plus, ans_a0_minus))

Solution (Python):
N = int(input())
A = [int(x) for x in input().split()]
c = [0, 0]

for f in [0, 1]:
  sumA = 0
  for i, a in enumerate(A):
    sumA += a
    if i % 2 == f:
      if sumA < 1:
        d = 1 - sumA
        c[f] += d
        sumA += d
    else:
      if sumA > -1:
        d = 1 + sumA
        c[f] += d
        sumA -= d
      
print(min(c))

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

ans = 0

oans, eans = 0, 0
oacum, eacum = 0, 0
acum = 0

for i in range(0, N):
    oacum += A[i]
    eacum += A[i]

    if i & 1:
        
        if oacum >= 0:
            oans += abs(oacum) + 1
            oacum = -1
        

        
        if eacum <= 0:
            eans += abs(eacum) + 1
            eacum = 1

    else:
        
        if oacum <= 0:
            oans += abs(oacum) + 1
            oacum = 1
        

        
        if eacum >= 0:
            eans += abs(eacum) + 1
            eacum = -1

print(min(oans, eans))

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

ans = 0

"""
acum = A[0]
for i in range(1, N):
    if (acum >= 0 and acum + A[i] >= 0) or (acum < 0 and acum + A[i] < 0) or (acum + A[i] == 0):
        
        ans += abs(acum + A[i]) + 1
        acum = 1 if acum < 0 else -1

    else:
        
        acum += A[i]
"""
oans, eans = 0, 0
oacum, eacum = 0, 0
acum = 0


for i in range(0, N):
    oacum += A[i]
    eacum += A[i]

    if i & 1:
        
        if oacum >= 0:
            oans += abs(oacum) + 1
            oacum = -1
        
        
        if eacum <= 0:
            eans += abs(eacum) + 1
            eacum = 1

    else:
        
        if oacum <= 0:
            oans += abs(oacum) + 1
            oacum = 1
        
        
        if eacum >= 0:
            eans += abs(eacum) + 1
            eacum = -1


print(min(oans, eans))

Solution (Python):
n=int(input())
a= (list(map(int,input().split())))
sm1=sm2=0
cnt1=cnt2=0
for i ,num in enumerate(a):
	sm1+=num
	if sm1<=0 and i%2==0:
		cnt1+=1-sm1
		sm1=1
	elif sm1>=0 and i%2!=0:
		cnt1+=1+sm1
		sm1=-1
		
	sm2+=num
	if sm2<=0 and i%2!=0:
		cnt2+=1-sm2
		sm2=1
	elif sm2>=0 and i%2==0:
		cnt2+=1+sm2
		sm2=-1
print(min(cnt1,cnt2))

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

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

			
even_ans = 0
even_sum = 0

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

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

Solution (Python):
import sys
import copy
import  math
from _bisect import *
from collections import *
from operator import itemgetter
from math import factorial
"""
from fractions import gcd
def lcm(x, y):
    return (x * y) // gcd(x, y)
"""
stdin = sys.stdin
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()

n = ni()
li = na()
ans = [0, 0]
for j in range(2):
    tmp = j
    s = 0
    for i in range(n):
        tmp = 1 - tmp
        s += li[i]
        if tmp:
            if s <= 0:
                d = abs(s - 1)
                ans[j] += d
                s = 1
        else:
            if s >= 0:
                d = abs(s - (-1))
                ans[j] += d
                s = -1
print(min(ans))

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

def solve(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(solve(a,True), solve(a,False)))

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

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

ans = 0

tmp = a[0]

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




ans2 = 0

if a[0] > 0:
    ans2 += a[0]+1
    tmp = -1
elif a[0] < 0:
    ans2 += -a[0]+1
    tmp = 1
else:
    tmp = -1
    ans2 += 1

    
for i in range(1,n):
    
    if tmp > 0:
        if tmp + a[i] >= 0:
            ans2 += tmp + a[i] + 1
            tmp = -1
        else:
            tmp += a[i]
    else:
        if tmp + a[i] <= 0:
            ans2 += abs(tmp + a[i]) + 1
            tmp = 1
        else:
            tmp += a[i]

print(min(ans,ans2))
exit()

ans2 = 0

tmp = -a[0]

ans2 += abs(a[0])*2
    
for i in range(1,n):
    
    if tmp > 0:
        if tmp + a[i] >= 0:
            ans2 += tmp + a[i] + 1
            tmp = -1
        else:
            tmp += a[i]
    else:
        if tmp + a[i] <= 0:
            ans2 += abs(tmp + a[i]) + 1
            tmp = 1
        else:
            tmp += a[i]

print(min(ans,ans2))

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

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


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


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

print(min(p,m))

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

ans0 = ans1 = 0
cs = 0
for i, ai in enumerate(a):
    cs += ai

    if i % 2 == 0:
        if cs < 1:
            ans0 += 1 - cs
            cs = 1
    else:
        if cs > -1:
            ans0 += cs + 1
            cs = -1

cs = 0
for i, ai in enumerate(a):
    cs += ai

    if i % 2 == 0:
        if cs > -1:
            ans1 += cs + 1
            cs = -1
    else:
        if cs < 1:
            ans1 += 1 - cs
            cs = 1

ans = min(ans0, ans1)
print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
s1 = 0
s2 = 0
ans1 = 0
ans2 = 0

for i in range(n):
    s1 += a[i]
    s2 += a[i]
    if i%2 == 0 and s1 >= 0:
        ans1 += s1
        ans1 += 1
        s1 = -1
    elif i%2 != 0 and s1 <= 0:
        ans1 -= s1
        ans1 += 1
        s1 = 1
    if i%2 == 0 and s2 <= 0:
        ans2 -= s2
        ans2 += 1
        s2 = 1
    elif i%2 != 0 and s2 >= 0:
        ans2 += s2
        ans2 += 1
        s2 = -1

print(min(ans1, ans2))

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

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

Solution (Python):
import numpy as np
import copy

n = int(input())

a = list(map(int,input().split()))
b=copy.deepcopy(a)

cntm=0
cntp=0
sum_a=0
sum_b=0

if a[0]<=0:
    cntp+=-a[0]+1
    a[0]=1
if b[0]>=0:
    cntm+=b[0]+1
    b[0]=-1
for i in range(n-1):
    sum_a += a[i]
    sum_b += b[i]
    if abs(sum_a) >= abs(a[i+1]) or sum_a*a[i+1]>=0:
            cntp += abs(-sum_a-np.sign(sum_a) -a[i+1])
            a[i+1]=-sum_a-np.sign(sum_a)
    if abs(sum_b) >= abs(b[i+1]) or sum_b*b[i+1]>=0:
            cntm += abs(-sum_b-np.sign(sum_b) -b[i+1])
            b[i+1]=-sum_b-np.sign(sum_b)
print(min(cntm, cntp))

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




def count_operation(one_is_pos):
    cnt = 0
    s = 0
    for i, a in enumerate(a_list):
        s += a
        if one_is_pos:
            if i % 2 == 0:
                if s <= 0:
                    cnt += -s + 1
                    s = 1
            else:
                if s >= 0:
                    cnt += s + 1
                    s = -1
        else:
            if i % 2 == 1:
                if s <= 0:
                    cnt += -s + 1
                    s = 1
            else:
                if s >= 0:
                    cnt += s + 1
                    s = -1
    return cnt

print(min(count_operation(True),count_operation(False)))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
def tat(t,a,n):
  ans = 0
  ssu = 0
  for x in a:
    ssu += x
    if ssu*t <= 0:
      ans += abs(ssu-t)
      ssu = t
    t *= -1
  return ans
print(min(tat(1,a,n),tat(-1,a,n)))

Solution (Python):


import copy
N = int(input().split()[0])
a_list = list(map(int, input().split()))

w_list_1 = copy.copy(a_list)
w_list_2 = copy.copy(a_list)
c_1, c_2 = 0, 0
s_1, s_2 = 0, 0
c_list = []

for i in range(N):  
    
    before = w_list_1[i]
    s_1 += before

    if i % 2 == 0 and s_1 < 0:
        w_list_1[i] += abs(s_1)
        c_1 += abs(s_1)
    elif i % 2 != 0 and s_1 >= 0:
        w_list_1[i] -= abs(s_1+1)
        c_1 += abs(s_1+1)

    s_1 = s_1 - before + w_list_1[i]

    if s_1 == 0:
        a = w_list_1[i]
        w_list_1[i] = a + 1 if a >= 0 else a - 1
        s_1 = 1 if a >= 0 else -1
        c_1 += 1

    
    before = w_list_2[i]
    s_2 += before

    if i % 2 == 1 and s_2 < 0:
        w_list_2[i] += abs(s_2)
        c_2 += abs(s_2)
    elif i % 2 != 1 and s_2 >= 0:
        w_list_2[i] -= abs(s_2+1)
        c_2 += abs(s_2+1)

    s_2 = s_2 - before + w_list_2[i]

    if s_2 == 0:
        a = w_list_2[i]
        w_list_2[i] = a + 1 if a >= 0 else a - 1
        s_2 = 1 if a >= 0 else -1
        c_2 += 1

ans = min([c_1, c_2])
print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split(' ')))
result = 0
counter = 0
if a[0] == 0:
    counter += 1
    result += 1
else:
    counter += a[0]

for i in range(1, n):
    if counter < 0:
        counter += a[i]
        if counter == 0:
            counter += 1
            result +=1
        elif counter > 0:
            continue
        else:
            result += 1-counter
            counter = 1
    else:
        counter += a[i]
        if counter == 0:
            counter -= 1
            result += 1
        elif counter < 0:
            continue
        else:
            result += counter+1
            counter = -1
out = []
out.append(result)
result = 0
counter = 0
if a[0] == 0:
    counter -= 1
    result +=1
elif a[0]< 0:
    counter += 1
    result += 1-a[0]
else:
    counter -= 1
    result += 1+a[0]

for i in range(1, n):
    if counter < 0:
        counter += a[i]
        if counter == 0:
            counter += 1
            result += 1
        elif counter > 0:
            continue
        else:
            result += 1-counter
            counter = 1
    else:
        counter += a[i]
        if counter == 0:
            counter -= 1
            result +=1
        elif counter < 0:
            continue
        else:
            result += counter+1
            counter = -1
out.append(result)
print(min(out))

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

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

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