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

def test(total, res):
    for i in a[1:]:
        hugou = total > 0
        total += i
        if hugou:
            if total>=0:
                res += abs(total)+1
                total = -1
        else:
            if total<=0:
                res += abs(total)+1
                total = 1
    return res

    
res = 0
total = a[0]
if a[0]<=0:
    total = 1
    res += abs(a[0])+1
res1 = test(total, res)

res = 0
total = a[0]
if a[0]>=0:
    total = -1
    res += abs(a[0])+1
res2 = test(total, res)

print(min(res1, res2))

Solution (Python):

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

def search(x):
    tmp=0
    cnt=0
    for i in range(n):
        tmp+=a[i]
        if i%2==x:
            if tmp<=0:
                cnt+=1-tmp
                tmp=1              
        else:
            if tmp>=0:
                cnt+=tmp+1
                tmp=-1
    return cnt

print(min(search(0),search(1)))

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
mans = 0
msum = 0
for i in range(N):
    if i%2:
        if msum + A[i] <= 0:
            mans += abs(msum + A[i])+1
            msum = 1
        else:
            msum += A[i]
    else:
        if msum + A[i] >= 0:
            mans += msum + A[i]+1
            msum = -1
        else:
            msum += A[i]
pans = 0
psum = 0
for i in range(N):
    if i%2:
        if psum + A[i] >= 0:
            pans += psum + A[i]+1
            psum = -1
        else:
            psum += A[i]
    else:
        if psum + A[i] <= 0:
            pans += abs(psum + A[i])+1
            psum = 1
        else:
            psum += A[i]

print(min(pans,mans))

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

if l[0] <= 0:
    x = 1
    ans1 = 1-l[0]
else:
    x = l[0]
    ans1 = 0
def check(l,x):
    ans=0
    for i in range(1,n):
        if x < 0:
            if x + l[i] <= 0:
                ans += 1-(x+l[i])
                x = 1
            else:
                x = x + l[i]
        else:
            if x + l[i] >= 0:
                ans += x + l[i] + 1
                x = -1
            else:
                x = x + l[i]
    return ans
ans1 += check(l,x)
if l[0] >= 0:
    x = -1
    ans2 = l[0]+1
else:
    ans2 = 0
    x = l[0]
ans2 += check(l,x)
print(min(ans1,ans2))

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

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


def calc(init):
    ans = 0
    sum = 0
    prev = init
    for i in range(n):
        sum += a[i]
        if sum == 0:
            ans += 1
            if prev < 0:
                sum = 1
            else:
                sum = -1
        else:
            if sum > 0 and prev > 0:
                ans += sum + 1
                sum = -1
            elif sum < 0 and prev < 0:
                ans += 1 - sum
                sum = 1
        prev = sum
    return ans


print(min(calc(1), calc(-1)))

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

def F(A, sgn):
  cnt = 0
  cum = 0
  for a in A:
    sgn *= -1
    cum += a
    if cum * sgn > 0:
      continue
    if sgn > 0:
      cnt += 1 - cum
      cum = 1
    else:
      cnt += cum + 1
      cum = -1
  return cnt

answer = min(F(A,1), F(A,-1))
print(answer)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
 
r_even = 0  
a_even = a[:]
sum_even = 0
for i in range(len(a)):
    sum_even += a_even[i]
    if i % 2 == 0:
        if sum_even <= 0:
            r_even += 1 - sum_even
            a_even[i] += 1 - sum_even
            sum_even += 1 - sum_even
    else:
        if sum_even >= 0:
            r_even += sum_even + 1
            a_even[i] -= sum_even + 1
            sum_even -= sum_even + 1

r_odd = 0   
a_odd = a[:]
sum_odd = 0
for i in range(len(a)):
    sum_odd += a_odd[i]
    if i % 2 != 0:
        if sum_odd <= 0:
            r_odd += 1 - sum_odd
            a_odd[i] += 1 - sum_odd
            sum_odd += 1 - sum_odd
    else:
         if sum_odd >= 0:
            r_odd += sum_odd + 1
            a_odd[i] -= sum_odd + 1
            sum_odd -= sum_odd + 1

print(min(r_odd, r_even))

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

ans_even = 0
ans_odd = 0

sum = 0

for index, i in enumerate(A):
  sum += i

  if index % 2 == 0:
    if sum <= 0:
      ans_even += abs(sum - 1)
      sum = 1
  else:
    if sum >= 0:
      ans_even += abs(sum + 1)
      sum = -1

sum = 0
for index, i in enumerate(A):
  sum += i
  
  if index % 2 == 0:
    if sum >= 0:
      ans_odd += abs(sum + 1)
      sum = -1
  else:
    if sum <= 0:
      ans_odd += abs(sum - 1)
      sum = 1
      
print(min(ans_even, ans_odd))

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

x = 0
tmp = 0
for i in range(N):
    tmp += A[i]
    if i & 1 == 0:
        if tmp < 1:
            x += 1-tmp
            tmp = 1
    else:
        if tmp > -1:
            x += 1+tmp
            tmp = -1
y = 0
tmp = 0
for i in range(N):
    tmp += A[i]
    if i & 1:
        if tmp < 1:
            y += 1-tmp
            tmp = 1
    else:
        if tmp > -1:
            y += 1+tmp
            tmp = -1
print(min(x,y))

Solution (Python):
n=int(input())
a=list(map(int, input().split()))
c1, c2 = 0, 0
s = 0
for i in range(len(a)):
    S = s+a[i]
    if i%2 == 0:
        if S > 0:
            s = S
        else:
            c1 += abs(S)+1
            s = 1
    else:
        if S < 0:
            s = S
        else:
            c1 += abs(S)+1
            s = -1
s=0
for i in range(len(a)):
    S = s+a[i]
    if i%2 != 0:
        if S > 0:
            s = S
        else:
            c2 += abs(S)+1
            s = 1
    else:
        if S < 0:
            s = S
        else:
            c2 += abs(S)+1
            s = -1
print(min(c1, c2))

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

sum = 0
ans_even = 0
ans_odd = 0

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

sum = 0

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

print(min(ans_even, ans_odd))

Solution (Python):
N = int(input())
a = list(map(int, input().split()))
num = float('inf')
for k in [0, 1]:
    s = 0
    tmp = 0
    for i in range(N):
        s += a[i]
        if i % 2 == k:
            if s <= 0:
                tmp += abs(s) + 1
                s = 1
        else:
            if s >= 0:
                tmp += abs(s) + 1
                s = -1
    num = min(tmp, num)

print(num)

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

def main():
    n = int(input())
    a_list = list(map(int, input().split()))
    a_sum = 0
    sign = "plus"
    ans1 = 0
    for i in range(0, n):
        if sign == "plus":
            sign = "minus"
            if a_sum + a_list[i] == 0:
                ans1 += 1
                a_sum = -1
            elif a_sum + a_list[i] > 0:
                ans1 += a_sum + 1 + a_list[i]
                a_sum = -1
            else:
                a_sum += a_list[i]
        elif sign == "minus":
            sign = "plus"
            if a_sum + a_list[i] == 0:
                ans1 += 1
                a_sum = 1
            elif a_sum + a_list[i] < 0:
                ans1 += -1 * a_sum + 1 + -1 * a_list[i]
                a_sum = 1
            else:
                a_sum += a_list[i]

    a_sum = 0
    sign = "minus"
    ans2 = 0
    for i in range(0, n):
        if sign == "plus":
            sign = "minus"
            if a_sum + a_list[i] == 0:
                ans2 += 1
                a_sum = -1
            elif a_sum + a_list[i] > 0:
                ans2 += a_sum + 1 + a_list[i]
                a_sum = -1
            else:
                a_sum += a_list[i]
        elif sign == "minus":
            sign = "plus"
            if a_sum + a_list[i] == 0:
                ans2 += 1
                a_sum = 1
            elif a_sum + a_list[i] < 0:
                ans2 += -1 * a_sum + 1 + -1 * a_list[i]
                a_sum = 1
            else:
                a_sum += a_list[i]

    print(min(ans1, ans2))

if __name__ == '__main__':
    main()

Solution (Python):



def check(a_list :list, flg :int) -> int:
    
    

    count=0
    sum=0
    for num in a_list:
        sum_next = sum + num
        
        sum_next_p_m_sign = (sum_next>0) - (sum_next<0)
        
        sign_chck = flg * sum_next_p_m_sign

        if sign_chck == -1:
            
            diff = (abs(sum_next) + 1) * (-1)*sum_next_p_m_sign
            sum += num + diff
            count += abs(diff)
        
        elif sign_chck == 0:
            
            diff = (1)*flg
            sum += num + diff
            count += abs(diff)
        
        else:
            sum = sum + num
 
        flg *= (-1)
    return count
        

n = int(input().strip())
a_list = list(map(int, input().rstrip().split()))

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

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

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

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

print(min(ans))

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
def test(L, sgn):
  count = 0
  s = 0
  for i in L:
    sgn *= -1
    s += i
    if s*sgn > 0:
      continue
    if sgn > 0:
      count += (1 - s)
      s = 1
    else:
      count += (s + 1)
      s = -1
  return count

ans = min(test(A, 1), test(A, -1))
print(ans)

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(int,input().split()))

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

Solution (Python):
N = int(input())
A = [int(x) for x in input().split()]
ans_plusstart = 0
sum_plusstart = 0
ans_minusstart = 0
sum_minusstart = 0
if A[0] < 0:
    ans_plusstart += 1 - A[0]
    sum_minusstart = A[0]
    sum_plusstart = 1
else:
    ans_minusstart += 1 + A[0]
    sum_minusstart = -1
    sum_plusstart = max(1, A[0])
    if A[0] == 0:
        ans_plusstart = 1
for i in range(1, N):
    
    if i % 2 == 0:  
        if A[i] + sum_plusstart > 0:
            sum_plusstart = A[i] + sum_plusstart
        if A[i] + sum_plusstart <= 0:
            ans_plusstart += 1-(A[i] + sum_plusstart)
            sum_plusstart = 1

        if A[i] + sum_minusstart < 0:
            sum_minusstart = A[i] + sum_minusstart
        if A[i] + sum_minusstart >= 0:
            ans_minusstart += 1 + A[i] + sum_minusstart
            sum_minusstart = -1

    else:  
        if A[i] + sum_plusstart < 0:
            sum_plusstart = A[i] + sum_plusstart
        if A[i] + sum_plusstart >= 0:
            ans_plusstart += 1+A[i] + sum_plusstart
            sum_plusstart = -1

        if A[i] + sum_minusstart > 0:
            sum_minusstart = A[i] + sum_minusstart
        if A[i] + sum_minusstart <= 0:
            ans_minusstart += 1 - (A[i] + sum_minusstart)
            sum_minusstart = 1
print(min(ans_minusstart, ans_plusstart))

Solution (Python):
n=int(input())
a=list(map(int, input().split()))
c1, c2 = 0, 0
s = 0
for i in range(len(a)):
    S = s+a[i]
    if i%2 == 0:
        if S > 0:
            s = S
        else:
            c1 += abs(S)+1
            s = 1
    else:
        if S < 0:
            s = S
        else:
            c1 += abs(S)+1
            s = -1
s=0
for i in range(len(a)):
    S = s+a[i]
    if i%2 != 0:
        if S > 0:
            s = S
        else:
            c2 += abs(S)+1
            s = 1
    else:
        if S < 0:
            s = S
        else:
            c2 += abs(S)+1
            s = -1
print(min(c1, c2))

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

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

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

Solution (Python):
import copy
n=int(input())
A=list(map(int,input().split()))
def solve(a,f):
  
  ans=0
  for i in range(n):
    if i!=0: a[i]+=a[i-1]
    if not f:
      if a[i]>=0:
        ans+=a[i]+1
        a[i]=-1
    else:
      if a[i]<=0:
        ans+=a[i]*-1+1
        a[i]=1
    
    f^=True
  return ans
x=A.copy()
print(min(solve(A,True),solve(x,False)))

Solution (Python):
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))



n = I()
a = LI()

def calc(flag):
    ans = 0
    now = 0
    for i in range(n):
        now += a[i]
        if flag == '+':
            if now <= 0:
                ans += 1-now
                now = 1
            flag = '-'
        else:
            if now >= 0:
                ans += now+1
                now = -1
            flag = '+'
    return ans

print(min(calc('+'),calc('-')))

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








total = 0
ans = 0
total2 = 0
ans2 = 0

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

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

print(min(ans,ans2))

Solution (Python):
import sys
def input():
    return sys.stdin.readline()[:-1]
n=int(input())
a=list(map(int,input().split()))
s=0
A=0
for i in range(n):
    s+=a[i]
    if i%2==0:
        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+=a[i]
    if i%2==1:
        if s<=0:
            B+=1-s
            s=1
    else:
        if s>=0:
            B+=s+1
            s=-1
print(A if A<B else B)

Solution (Python):
N = int(input())
a_lst = [int(x) for x in input().split()]
b_lst = []
for i in a_lst:
    b_lst.append(i)

def my_sign(num):
    return (num > 0) - (num < 0)

cnt_p = 0
cnt_n = 0
sum_lst = []
sum2_lst = []

for i in range(N): 
    sum_lst.append(a_lst[i]) if i == 0 else sum_lst.append(a_lst[i] + sum_lst[i - 1])
    if i%2 == 0 and sum_lst[i] >= 0:
        cnt_p += 1 + sum_lst[i]
        a_lst[i] -= 1 + sum_lst[i]
        sum_lst[i] = -1
    elif i%2 == 1 and sum_lst[i] <= 0:
        cnt_p += 1 + abs(sum_lst[i])
        a_lst[i] += 1 + abs(sum_lst[i])
        sum_lst[i] = 1
for i in range(N): 
    sum2_lst.append(b_lst[i]) if i == 0 else sum2_lst.append(b_lst[i] + sum2_lst[i - 1])
    if i%2 == 0 and sum2_lst[i] <= 0:
        cnt_n += 1 + abs(sum2_lst[i])
        b_lst[i] += 1 + abs(sum2_lst[i])
        sum2_lst[i] = 1
    elif i%2 == 1 and sum2_lst[i] >= 0:
        cnt_n += 1 + sum2_lst[i]
        b_lst[i] -= 1 + sum2_lst[i]
        sum2_lst[i] = -1
print(min(cnt_p, cnt_n))

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


ans1 = 0
sum = 0
flg = 1
for i in range(n):
    sum += a[i]
    if flg == 1:
        if sum < 0:
            flg = -1
        else:
            ans1 += sum + 1
            sum = -1
            flg = -1
    else:
        if sum > 0:
            flg = 1
        else:
            ans1 += (-sum) + 1
            sum = 1
            flg = 1


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

print(min(ans1, ans2))

Solution (Python):
from sys import stdout
printn = lambda x: stdout.write(str(x))
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True 
BIG = 999999999999999
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

n = inn()
a = inl()
ne = sum(a[::2])
no = sum(a[1::2])
p0 = (ne>no)
mn = BIG
for p0 in [True,False]:
 cnt = 0
 acc = 0
 
 for i in range(n):
    if (p0 if i%2==0 else not p0):
        x = max(0, 1-a[i]-acc)
        cnt += x
        acc += a[i]+x
    else:
        x = max(0, a[i]+acc+1)
        cnt += x
        acc += a[i]-x
    
 mn = min(mn,cnt)
print(mn)

Solution (Python):
def countave(n , a):
    ans = 100000000000000

    for i in range(2 , 4):
        kei = 0
        forans = 0
        
        if i % 2 == 0:
            fast = 1
        else:
            fast = -1
        for w in range(n):
            if fast < 0 and kei + int(a[w]) >= 0:
                forans += abs(kei + int(a[w])) + 1
                kei = -1
                fast = 1
            elif fast > 0 and kei + int(a[w]) <= 0:
                forans += abs(kei + int(a[w])) + 1
                kei = 1
                fast = -1
            else:
                kei += int(a[w])
                fast *= -1
        ans = min(ans , forans)

    return ans

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

if __name__ == '__main__':
    main()

Solution (Python):
def countave(n , a):
    ans = 100000000000000

    for i in range(2):
        kei = 0
        forans = 0
        for w in range(n):
            if (w + i) % 2:
                if kei + int(a[w]) > -1:
                    forans += abs(kei + int(a[w])) + 1
                    kei = -1
                else:
                    kei += int(a[w])
            else:
                if kei + int(a[w]) < 1:
                    forans += abs(kei + int(a[w])) + 1
                    kei = 1
                else:
                    kei += int(a[w])

        ans = min(ans , forans)

    return ans

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

if __name__ == '__main__':
    main()

Solution (Python):


N=int(input())

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

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

print(min(ans,ans2))

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

s_pos = 0
s_neg = 0


cum = 0
for i in range(n):
    cum += a[i]
    if i % 2 == 0 and cum >= 0:
        s_neg += abs(cum) + 1
        cum = -1
    if i % 2 != 0 and cum <= 0:
        s_neg += abs(cum) + 1
        cum = 1


cum = 0
for i in range(n):
    cum += a[i]
    if i % 2 == 0 and cum <= 0:
        s_pos += abs(cum) + 1
        cum = 1
    if i % 2 != 0 and cum >= 0:
        s_pos += abs(cum) + 1
        cum = -1

ans = min(s_pos, s_neg)
print(ans)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from bisect import bisect
from heapq import heappush, heappop

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

n = INT()
A = LIST()

def solve(lis):
	lis = lis.copy()
	tmp_sum = lis[0]
	lis = lis[1:]
	cnt = 0
	for x in lis:
		if (tmp_sum + x) * tmp_sum < 0:  
			tmp_sum += x
		else:
			cnt += abs((-1)**(tmp_sum>0)*(abs(tmp_sum)+1)-x)
			tmp_sum = (-1)**(tmp_sum>0)
	return cnt

if A[0] > 0:
	ans = solve(A)
	B = A.copy()
	B[0] = -1
	ans = min(ans, solve(B)+abs(A[0]+1))
elif A[0] < 0:
	ans = solve(A)
	B = A.copy()
	B[0] = 1
	ans = min(ans, solve(B)+abs(A[0]-1))
else:
	A[0] = 1
	ans = solve(A)+1
	A[0] = -1
	ans = min(ans, solve(A)+1)
print(ans)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from bisect import bisect
from heapq import heappush, heappop

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

n = INT()
A = LIST()

def solve(lis):
	lis = lis.copy()
	tmp_sum = lis[0]
	lis = lis[1:]
	cnt = 0
	for x in lis:
		if (tmp_sum + x) * tmp_sum < 0:  
			tmp_sum += x
		else:
			 if tmp_sum > 0:
			 	cnt += abs(-tmp_sum-1-x)
			 	tmp_sum = -1
			 else:
			 	cnt += abs(-tmp_sum+1-x)
			 	tmp_sum = 1
	return cnt

if A[0] > 0:
	ans = solve(A)
	B = A.copy()
	B[0] = -1
	ans = min(ans, solve(B)+abs(A[0]+1))
elif A[0] < 0:
	ans = solve(A)
	B = A.copy()
	B[0] = 1
	ans = min(ans, solve(B)+abs(A[0]-1))
else:
	A[0] = 1
	ans = solve(A)+1
	A[0] = -1
	ans = min(ans, solve(A)+1)
print(ans)

Solution (Python):
import  copy
n = int(input())
a = list(map(int, input().split()))
check = copy.deepcopy(a)

def segfunc(x,y):
    return x+y

def init(init_val):
    
    for i in range(len(init_val)):
        seg[i+num-1]=init_val[i]   
    
    for i in range(num-2,-1,-1) :
        seg[i]=segfunc(seg[2*i+1],seg[2*i+2]) 
    
def update(k,x):
    k += num-1
    seg[k] = x
    while k:
        k = (k-1)//2
        seg[k] = segfunc(seg[k*2+1],seg[k*2+2])
    
def query(p,q):
    if q<=p:
        return ide_ele
    p += num-1
    q += num-2
    res=ide_ele
    while q-p>1:
        if p&1 == 0:
            res = segfunc(res,seg[p])
        if q&1 == 1:
            res = segfunc(res,seg[q])
            q -= 1
        p = p//2
        q = (q-1)//2
    if p == q:
        res = segfunc(res,seg[p])
    else:
        res = segfunc(segfunc(res,seg[p]),seg[q])
    return res


ide_ele = 0
num =2**(n-1).bit_length()
seg=[ide_ele]*(2*num - 1)
init(a)
ans_1 = 0
pre_sum = (-1) * a[0]
for i in range(n):
    q_sum = query(0,i+1)
    if q_sum * pre_sum >= 0:
        if pre_sum < 0:
            update(i, abs(pre_sum) + 1)
        else:
            update(i, (-1) * (pre_sum+1))
    pre_sum = query(0,i+1)
for i in range(n):
    ans_1 += abs(check[i] - seg[i + num - 1])
if a[0] == 0:
    a[0] = 1
else:
    if a[0] < 0:
        a[0] = 1
    else:
        a[0] = -1
ans_2 = 0
pre_sum = (-1) * a[0]
init(a)
for i in range(n):
    q_sum = query(0,i+1)
    if q_sum * pre_sum >= 0:
        if pre_sum < 0:
            update(i, abs(pre_sum) + 1)
        else:
            update(i, (-1) * (pre_sum+1))
    pre_sum = query(0,i+1)
for i in range(n):
    ans_2 += abs(check[i] - seg[i + num - 1])
print(min(ans_1,ans_2))

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(int,input().split()))
def solve(cnt,current_sum):
    for i in range(n-1):
        if current_sum * (current_sum + a[i+1]) < 0:
            current_sum += a[i+1]
            cnt += 0
        elif current_sum < 0:
            cnt += 1 - (current_sum + a[i+1])
            current_sum = 1 
        else:
            cnt += (current_sum + a[i+1]) +1 
            current_sum = -1
    return cnt
if a[0] > 0:
    print(min(solve(0,a[0]),solve(1+abs(a[0]),-1)))
elif a[0] < 0:
    print(min(solve(0,a[0]),solve(1+abs(a[0]),1)))
else:
    print(min(solve(1,1),solve(1,-1)))

Solution (Python):
import sys
import itertools

input = sys.stdin.readline


def main():
    N = int(input())
    A = [int(x) for x in input().split()]


    accu = list(itertools.accumulate(A))

    ans1 = 0
    ruiseki = 0
    for i, x in enumerate(accu):
        if i % 2 == 0:
            if ruiseki + x <= 0:
                ans1 += 1 + abs(ruiseki + x)
                ruiseki += 1 + abs(ruiseki + x)
        else:
            if ruiseki + x >= 0:
                ans1 += 1 + abs(ruiseki + x)
                ruiseki += -1 - abs(ruiseki + x)

    ans2 = 0
    ruiseki = 0
    for i, x in enumerate(accu):
        if i % 2 != 0:
            if ruiseki + x <= 0:
                ans2 += 1 + abs(ruiseki + x)
                ruiseki += 1 + abs(ruiseki + x)
        else:
            if ruiseki + x >= 0:
                ans2 += 1 + abs(ruiseki + x)
                ruiseki += -1 - abs(ruiseki + x)

    print(min(ans1, ans2))

if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
A = list(map(int,input().split()))
s = max(A[0],1)
cnt1 = abs(A[0]-s) 
for a in A[1:]:
    if s>0:
        b=min(a, -s-1) 
    if s<0:
        b=max(a,-s+1)
    s += b 
    cnt1 += abs(a-b)

s = min(A[0],-1)
cnt2 = abs(A[0]-s)
for a in A[1:]:
    if s>0:
         b=min(a, -s-1) 
    if s<0:
        b=max(a,-s+1)
    s += b 
    cnt2 += abs(a-b)
print(min(cnt1,cnt2))

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
ans1 = 0
acc = 0
d = 1
for a in A:
    if (acc + a) * d > 0:
        acc += a
    else:
        ans1 += abs(1 - (acc + a) * d)
        acc = d
    d *= -1
ans2 = 0
acc = 0
d = -1
for a in A:
    if (acc + a) * d > 0:
        acc += a
    else:
        ans2 += abs(1 - (acc + a) * d)
        acc = d
    d *= -1
print(min(ans1, ans2))

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





if a[0]<0:
    for i in range(n):
        a[i]*=-1


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


ans1 = 0
if not a[0]:
    ans1 += 1
    a[0] = 1
sm = a[0]

for i in range(1,n):
    sm2 = sm + a[i]
    
    if sm2*sm>=0:
        if sm<0:
            ans1 += abs((1-sm)-a[i])
            a[i]=1-sm
            sm = 1
            
        else:
            ans1 += abs(-1-sm-a[i])
            a[i]=-1-sm
            sm = -1
    else:
        sm=sm2

ans2 = abs(-1-a2[0])
a2[0]=-1
sm = -1

for i in range(1,n):
    sm2 = sm + a2[i]
    if sm2*sm>=0:
        if sm<0:
            ans2 += abs((1-sm)-a2[i])
            a2[i] = 1-sm
            sm = 1
        else:
            ans2 += abs(-1-sm-a2[i])
            a2[i]=-1-sm
            sm = -1
    else:
        sm =sm2
   

print(min(ans1,ans2))

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

s = max(A[0], +1)
ans_p = abs(A[0]-s)
for a in A[1:]:
    if s>0: b=min(a, -s-1)
    if s<0: b=max(a, -s+1)
    s += b
    ans_p += abs(a-b)

s = min(A[0], -1)
ans_n = abs(A[0]-s)
for a in A[1:]:
    if s>0: b=min(a, -s-1)
    if s<0: b=max(a, -s+1)
    s += b
    ans_n += abs(a-b)

print(min(ans_p, ans_n))

Solution (Python):

from bisect import *
from collections import *
from heapq import *




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

def main(c,temp):

    if temp>0:
        flag=1
    elif temp<0:
        flag=0
    for i in range(1,n):
        temp+=A[i]
        if flag:
            if temp>=0:
                c+=temp-(-1)
                temp=-1
            flag=0
        else:
            if temp<=0:
                c+=1-temp
                temp=1
            flag=1
    return(c)

temp=A[0]
if temp:
    a=main(0,temp)
    if temp>0:
        b=main(temp - (-1),-1)
    else:
        b=main(1-temp,1)
    print(min(a,b))
else:
    temp=1
    a=main(1,temp)
    temp=-1
    b=main(1,temp)
    print(min(a,b))