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 copy
n = int(input())

numbers = list(map(int, input().split()))
numbers1 = copy.copy(numbers)
counter = 0
counter2 = 0
sum_in = 0
sum_in1 = 0

for i in range(len(numbers)):
    sum_in += numbers[i]
    if i % 2 == 0:
        if sum_in <= 0:
            sub = abs(sum_in) + 1
            sum_in += sub
            numbers[i] += sub
            counter += sub
    else:
        if sum_in >= 0:
            sub = abs(sum_in) + 1
            sum_in -= sub
            numbers[i] -= sub
            counter += sub

for i in range(len(numbers1)):
    sum_in1 += numbers1[i]

    if i % 2 != 0:
        if sum_in1 <= 0:
            sub = abs(sum_in1) + 1
            sum_in1 += sub
            numbers1[i] += sub
            counter2 += sub
    else:
        if sum_in1 >= 0:
            sub = abs(sum_in1) + 1
            sum_in1 -= sub
            numbers1[i] -= sub
            counter2 += sub

print(min([counter2, counter]))

Solution (Python):
n = int(input())
a = list(map(int,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 = list(input().split())

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


def chk(A, flg):
    
    
    x=0
    ans =0
    for a in A:
        
        x += a
        
        
        if flg==True and x < 1:
            ans +=1-x
            
            x=1
        elif flg == False and x> -1:
            ans += x+1
            x=-1
        
        flg = not flg

    return(ans)
print(min(chk(A, True),chk(A, False)))

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    n = II()
    aa = LI()
    
    cs = 0
    ans1 = 0
    for i, a in enumerate(aa):
        cs += a
        if i % 2:
            if cs >= 0:
                ans1 += abs(-1 - cs)
                cs = -1
        else:
            if cs <= 0:
                ans1 += abs(1 - cs)
                cs = 1
    
    cs = 0
    ans2 = 0
    for i, a in enumerate(aa):
        cs += a
        if i % 2:
            if cs <= 0:
                ans2 += abs(1 - cs)
                cs = 1
        else:
            if cs >= 0:
                ans2 += abs(-1 - cs)
                cs = -1
    print(min(ans1,ans2))

main()

Solution (Python):
import numpy as np
N = int(input())
A = list(map(int, input().split()))

res_plus = np.cumsum(A)
res_minus = np.cumsum(A)
ans_plus = 0
ans_minus = 0

tmp_tmp = 0

for i in range(N):
    res_plus[i] += tmp_tmp
    if i % 2 == 0:         
        if res_plus[i] > 0:
            pass
        else:
            tmp = 1 - res_plus[i]
            ans_plus += tmp
            tmp_tmp += tmp
    else:                  
        if res_plus[i] < 0:
            pass
        else:
            tmp = res_plus[i] + 1
            ans_plus += tmp
            tmp_tmp -= tmp


tmp_tmp = 0

for i in range(N):
    res_minus[i] += tmp_tmp
    if i % 2 == 0:      
        if res_minus[i] < 0:
            pass
        else:
            tmp = res_minus[i] + 1
            ans_minus += tmp
            tmp_tmp -= tmp
    else:               
        if res_minus[i] > 0:
            pass
        else:
            tmp = 1 - res_minus[i]
            ans_minus += tmp
            tmp_tmp += tmp


print(min(ans_plus, ans_minus))

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
m1 = 0
cnt1 = 0
f = 1
for i in range(n):
    f *= -1
    m1 += a[i]
    if f == 1:
        if m1 <= 0:
            cnt1 += f-m1
            m1 += f-m1
    else:
        if m1 >= 0:
            cnt1 += m1-f
            m1 -= m1-f
m2 = 0
cnt2 = 0
f = -1
for i in range(n):
    f *= -1
    m2 += a[i]
    if f == 1:
        if m2 <= 0:
            cnt2 += f-m2
            m2 += f-m2
    else:
        if m2 >= 0:
            cnt2 += m2-f
            m2 -= m2-f

print(min(cnt1, cnt2))

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()

def solve(cumsum, prev_plus):
    cnt = 0
    total_sum = 0
    for i, c in enumerate(cumsum):
        c = c + total_sum
        if prev_plus:
            
            if c < 0:
                pass
            else:
                cnt += (c - (-1))
                total_sum -= (c - (-1))
        else:
            if c > 0:
                pass
            else:
                cnt += (1 - c)
                total_sum += (1 - c)
        prev_plus = not prev_plus
    return cnt


def main():
    N = I()
    A = LI()
    cumsum = [0]
    for i in range(N):
        cumsum.append(A[i] + cumsum[i])
    cumsum = cumsum[1:]
    prev_plus = cumsum[0] > 0
    a1 = solve(cumsum, True)
    a2 = solve(cumsum, False)
    print(min(a1, a2))

main()

Solution (Python):
def k(n, a, p):
    x = 0
    c = 0
    for i in range(n):
        s = x + a[i]
        if p and s > 0 or not p and s < 0: x = s
        else:
            c += abs(s) + 1
            if p: x = 1
            else: x = -1
        p = not p
    return c

n = int(input())
a = [int(x) for x in input().split()]
print(min(k(n, a, True), k(n, a, False)))

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

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


def solve(cnt,A,N):
    sumA=A[0]
    for i in range(1, N):
        if sumA>0:
            if sumA+A[i]>=0:
                r = A[i]
                A[i]=-sumA-1
                cnt+=abs(r-A[i])
        else:
            if sumA+A[i]<=0:
                r = A[i]
                A[i]=-sumA+1
                cnt+=abs(r-A[i])

        sumA+=A[i]
    return cnt

cnt1=0
if b[0]<=0:
    ini=b[0]
    b[0]=1
    cnt1=abs(1-ini)


ans1=solve(cnt1,b,n)
cnt2=0
if a[0]>=0:
    ini=a[0]
    a[0]=-1
    cnt2=abs(-1-ini)


ans2=solve(cnt2,a,n)

print(min(ans1,ans2))

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

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

b = a[:]
acc = 0

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

c = a[:]
acc = 0

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

ans = min(sum(abs(ai-bi) for ai, bi in zip(a, b)), sum(abs(ai-ci) for ai, ci in zip(a, c)))

print(ans)

Solution (Python):

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

for i in range(n):
    sum1+=lists[i]
    if i%2==0:
        if sum1>=0:
            a+=(sum1+1)
            sum1=-1
        elif sum1<0:
            pass
    elif  i%2!=0:
        if sum1<=0:
            a+=(1-sum1)
            sum1=1
        elif sum1>0:
            pass

b=0

sum2=0
for i in range(n):
    sum2+=lists[i]
    if i%2!=0:
        if sum2>=0:
            b+=(sum2+1)
            sum2=-1
        elif sum2<0:
            pass
    elif  i%2==0:
        if sum2<=0:
            b+=(1-sum2)
            sum2=1
        elif sum2>0:
            pass
print(min(a,b))

Solution (Python):
import sys
input = sys.stdin.readline
n=int(input())
a=list(map(int,input().split()))

v=[]
for s in (1,-1):
    x=a[::]
    hugou=[s*(-1)**i for i in range(n)]
    total=0
    for i in range(n):
        if (total+x[i])*hugou[i]<=0:
            x[i]=hugou[i]-total
        total+=x[i]
    v.append(sum(abs(a[i]-x[i]) for i in range(n)))
print(min(v))

Solution (Python):
N=int(input())
A=list(map(int,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())
arr = [0] + list(map(int, input().split()))
cnt = 0
ans = float('inf')
for sign in [-1, 1]:
    res = 0
    partial_sum = 0
    A = [x for x in arr]
    for i in range(n):
        partial_sum += A[i]
        diff = sign - (partial_sum + A[i+1]                )
        if sign < 0:
            diff = min(diff, 0)
        else:
            diff = max(diff, 0)
        res += abs(diff)
        A[i+1] += diff
        sign *= -1
    ans = min(ans, res)
print(ans)

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

def exec(sign):
    a = [x for x in arr]
    res = 0

    x = 0
    for i in range(n):
        x += a[i]
        tmp = sign - (x + a[i+1])
        if sign < 0:
            tmp = min(tmp, 0)
        else:
            tmp = max(tmp, 0)
        res += abs(tmp)
        a[i+1] += tmp
        sign *= (-1)
    return res

print(min(exec(1), exec(-1)))

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

    n = int(input())
    A = list(map(int, input().split()))
    def Chk(a, pos):
        cnt = 0
        tmp = 0

        for a in A:
            tmp += a

            if pos and tmp < 1:
                cnt += 1 - tmp
                tmp = 1
            elif not pos and tmp > -1:
                cnt += 1 + tmp
                tmp = -1

            pos = not pos
        return cnt

    print(min(Chk(A, True), Chk(A, False)))
    
if __name__ == '__main__':
    main()

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

def Chk(a, pos):
    cnt = 0
    tmp = 0

    for a in A:
        tmp += a

        if pos and tmp < 1:
            cnt += 1 - tmp
            tmp = 1
        elif not pos and tmp > -1:
            cnt += 1 + tmp
            tmp = -1

        pos = not pos
    return cnt

print(min(Chk(A, True), Chk(A, False)))

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

def Chk(a, pos):
    cnt = 0
    tmp = 0

    for a in A:
        tmp += a

        if pos and tmp < 1:
            cnt += 1 - tmp
            tmp = 1
        elif not pos and tmp > -1:
            cnt += 1 + tmp
            tmp = -1

        pos = not pos
    return cnt

print(min(Chk(A, True), Chk(A, False)))

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

kouho=[]


ans=0
cumsum=0
for i in range(n):
  cumsum+=a[i]
  if i%2:
    if cumsum<=0:
      ans+=abs(0-cumsum)+1
      cumsum=1
  elif not i%2:
    if cumsum>=0:
      ans+=(cumsum+1)
      cumsum=-1
kouho.append(ans)

ans=0
cumsum=0


for i in range(n):
  cumsum+=a[i]
  if not i%2:
    if cumsum<=0:
      ans+=abs(0-cumsum)+1
      cumsum=1
  elif i%2:
    if cumsum>=0:
      ans+=(cumsum+1)
      cumsum=-1
kouho.append(ans)
print(min(kouho))

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


res, sum = 0, 0
if 0 < A[0]: sum = A[0]
else: res, sum = -A[0] + 1, 1

for i in range(1, N):
    if 0 < sum:
        if -1 < sum + A[i]: 
            res += abs(A[i] + sum + 1)
            sum = -1
        else: 
            sum += A[i]
    else:
        if sum + A[i] < 1:
            res += abs(A[i] - (-sum + 1))
            sum = 1
        else:
            sum += A[i]

ans = res


res, sum = 0, 0
if A[0] < 0: sum = A[0]
else: res, sum = A[0] + 1, -1

for i in range(1, N):
    if 0 < sum:
        if -1 < sum + A[i]: 
            res += abs(A[i] + sum + 1)
            sum = -1
        else: 
            sum += A[i]
    else:
        if sum + A[i] < 1:
            res += abs(A[i] - (-sum + 1))
            sum = 1
        else:
            sum += A[i]

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

Solution (Python):

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

even_ans, odd_ans = 0, 0

s = 0
for i, a in enumerate(A):
    s += a

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



s = 0
for i, a in enumerate(A):
    s += a

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


print(min(even_ans, odd_ans))

Solution (Python):
import copy
n=int(input())
a1=[int(i) for i in input().split()]
a2=copy.deepcopy(a1)
for i in range(1,n):
    a1[i]+=a1[i-1]
for i in range(1,n):
    a2[i]+=a2[i-1]

pm1,c1=0,0
pm2,c2=0,0

for i in range(n):
    a1[i]+=pm1
    if i%2==1:
        if a1[i]>=0:
            pm1-=(a1[i]+1)
            c1+=(a1[i]+1)
    else:
        if a1[i]<=0:
            pm1+=(-a1[i]+1)
            c1+=(-a1[i]+1)

for i in range(n):
    a2[i]+=pm2
    if i%2==0:
        if a2[i]>=0:
            pm2-=(a2[i]+1)
            c2+=(a2[i]+1)
    else:
        if a2[i]<=0:
            pm2+=(-a2[i]+1)
            c2+=(-a2[i]+1)



print(min(c1,c2))

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
B = [A[0]]
for i in range(1,N):
    B.append(B[-1]+A[i])

plus_ans = 0
c = 0
for i in range(N):
    current_value = B[i]+c
    if i%2 == 0:
        if current_value <= 0:
            plus_ans += abs(current_value-1)
            c -= current_value-1
    else:
        if current_value >= 0:
            plus_ans += abs(current_value+1)
            c -= current_value+1

minus_ans = 0
c = 0
for i in range(N):
    current_value = B[i]+c
    if i%2 == 0:
        if current_value >= 0:
            minus_ans += abs(current_value+1)
            c -= current_value+1
    else:
        if current_value <= 0:
            minus_ans += abs(current_value-1)
            c -= current_value-1
print(min(plus_ans,minus_ans))

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

sum_a = 0
ans_1 = 0
for i in range(N):
    sum_a += a[i]
    if sum_a >= 0 and i%2 == 0:
        ans_1 += sum_a + 1
        sum_a = -1
    elif sum_a <= 0 and i%2 != 0:
        ans_1 += -sum_a + 1
        sum_a = 1
        
sum_a = 0
ans_2 = 0
for i in range(N):
    sum_a += a[i]
    if sum_a >= 0 and i%2 != 0:
        ans_2 += sum_a + 1
        sum_a = -1
    elif sum_a <= 0 and i%2 == 0:
        ans_2 += -sum_a + 1
        sum_a = 1

print(min(ans_1, ans_2))

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
ans=[0]*2
for pat in range(2):
  cur=0
  isplus=pat
  for i in range(N):
    cur+=A[i]
    if isplus:
      if cur<=0:
        ans[pat]+=abs(cur-1)
        cur=1
    else:
      if cur>=0:
        ans[pat]+=abs(cur-(-1))
        cur=-1
    isplus^=1

print(min(ans))

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
res=10**15
for isplus in (True,False):
  cur=0
  ans=0
  for i in range(N):
    cur+=A[i]
    if isplus:
      if cur<=0:
        ans+=abs(cur-1)
        cur=1
      isplus=False
    else:
      if cur>=0:
        ans+=abs(cur-(-1))
        cur=-1
      isplus=True
  if res>ans:
    res=ans

print(res)

Solution (Python):


import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N = INT()
A = LIST()

def check(A, mod2):
    cur = res = 0
    for i in range(N):
        cur += A[i]
        if i % 2 == mod2:
            
            if cur <= 0:
                
                A[i] += 1 - cur
                res += 1 - cur
                cur += 1 - cur
        else:
            
            if cur >= 0:
                
                A[i] -= 1 + cur
                res += 1 + cur
                cur -= 1 + cur
    return res
    
ans = min(check(A[:], 0), check(A[:], 1))
print(ans)

Solution (Python):

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

total=a[0]
ans=0
if total<=0:
    ans=1-total
    total=1

for aa in a[1:]:
    if total>0:
        y=total+aa
        if y<0:
            total=y
        else:
            ans+=y+1
            total=-1
    else:
        y=total+aa
        if y>0:
            total=y
        else:
            ans+=1-y
            total=1


total=a[0]
ans2=0
if total>=0:
    ans2+=total+1
    total=-1

for aa in a[1:]:
    if total>0:
        y=total+aa
        if y<0:
            total=y
        else:
            ans2+=y+1
            total=-1
    else:
        y=total+aa
        if y>0:
            total=y
        else:
            ans2+=1-y
            total=1

print(min(ans,ans2))

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

def check(flag):
    acc = 0
    cnt = 0

    flag = flag
    for i in a:
        acc += i
        if (flag == 1 and flag > acc) or (flag == -1 and flag < acc) or i == 0:
            cnt += abs(flag - acc)
            acc = flag
        flag *= -1
    return cnt

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

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

def f(presum, x, fugo):
  if (presum+x)*fugo<=0:
    sa=fugo-presum-x
    prosum=fugo
  else:
    sa=0
    prosum=presum+x
  return sa, prosum

out1=0
y=0
for i in range(n):
  x, y=f(y, a[i], (-1)**i)
  out1+=abs(x)
  
out2=0
y=0
for i in range(n):
  x, y=f(y, a[i], -(-1)**i)
  out2+=abs(x)
  
print(min(out1, out2))

Solution (Python):
def F(list_A, sign):
  total = 0
  cnt = 0
  for a in list_A:
    total += a
    if sign==True and total < 1:
      cnt += 1- total
      total = 1
    elif sign==False and total > -1:
      cnt += total + 1
      total = -1
    
    sign = not sign
  return cnt

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

ans = min(F(A, True), F(A, False))
print(ans)

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

ans = 10**15
for i in range(2):
    c = 0
    S = 0
    for j in range(n):
        S += A[j]
        if i % 2 == j % 2 and S <= 0:
            c += abs(S)+1
            S = 1
        elif i % 2 != j % 2 and S >= 0:
            c += S+1
            S = -1
    ans = min(ans, c)
print(ans)

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

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


def f(sign):
    ans = 0
    s = 0
    for i in range(N):
        a = A[i]
        s += a
        if s == 0:
            ans += 1
            s = sign
        else:
            a_sign = s // abs(s)
            if a_sign != sign:
                ans += abs(s) + 1
                s = sign
        sign *= -1
    return ans


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

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
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)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n = I()
    a = LI()
    ans = 0
    s = a[0]
    if s <= 0:
        ans += 1-a[0]
        s = 1
    f = 1
    for i in a[1:]:
        s += i
        if f and (s >= 0):
            ans += s+1
            s = -1
        if not f and (s <= 0):
            ans += 1-s
            s = 1
        f ^= 1
    m = ans
    ans = 0
    s = a[0]
    if s >= 0:
        ans += a[0]+1
        s = -1
    f = 0
    for i in a[1:]:
        s += i
        if f and (s >= 0):
            ans += s+1
            s = -1
        if not f and (s <= 0):
            ans += 1-s
            s = 1
        f ^= 1
    print(min(m,ans))
    return


if __name__ == "__main__":
    solve()

Solution (Python):
def pm(input_list):
    calc_result = [0]
    cnt = 0
    
    for i in range(n):
        if i % 2 == 0: plus = True
        else: plus = False

        x = input_list[i] + calc_result[i]
        if plus:
            if x > 0:
                calc_result.append(x)
            
            else:
                cnt += 1 - x
                calc_result.append(1)

        else:
            if x < 0:
                calc_result.append(x)
            
            else:
                cnt += abs(-1 - x)
                calc_result.append(-1)

    return cnt

def mp(input_list):
    calc_result = [0]
    cnt = 0
    
    for i in range(n):
        if i % 2 == 0: minus = True
        else: minus = False

        x = input_list[i] + calc_result[i]
        if minus:
            if x < 0:
                calc_result.append(x)
            
            else:
                cnt += abs(-1 - x)
                calc_result.append(-1)

        else:
            if x > 0:
                calc_result.append(x)
            
            else:
                cnt += 1 - x
                calc_result.append(1)

    return cnt


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

print(min(pm(input_list), mp(input_list)))

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

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


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


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

print(min(res1,res2))

Solution (Python):

N = int(input())

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


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

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

print(min(ans_plus, ans_minus))

Solution (Python):
import numpy as np
n = int(input())
L = np.array([int(i) for i in input().split()])

count = 0
s = 0
  

  
loopnum = n//2



for i in range(loopnum):
  s = s + L[2*i]
  if s <= 0:
    subt = -s + 1
    count += subt
    s = s + subt
  
  s = s + L[2*i+1]
  if s >= 0:
    subt = s + 1 
    count += subt
    s = s - subt
  
    
if n%2 == 1:
  s = s + L[-1]
  if s <= 0:
    subt = -s + 1
    count += subt

cand1 = count


count = 0
s = 0



for i in range(loopnum):
  s = s + L[2*i]
  if s >= 0:
    subt = s + 1
    count += subt
    s = s - subt
  
  s = s + L[2*i+1]
  if s <= 0:
    subt = -s + 1 
    count += subt
    s = s + subt
  
    
if n%2 == 1:
  s = s + L[-1]
  if s >= 0:
    subt = s + 1
    count += subt

cand2 = count


print(min(cand1,cand2))

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

import itertools
al_ac = list(itertools.accumulate(al))

ans = 0
init = 1

fix = 0
for a in al_ac:
    a += fix
    if init == 1:
        if a >= 1:
            init *= -1
            continue
        else:
            ans += abs(init - a)
            fix += init - a
            init *= -1
    else:
        if a <= -1:
            init *= -1
            continue
        else:
            ans += abs(init - a)
            fix += init - a
            init *= -1
ans2 = 0
init = -1

fix = 0
for a in al_ac:
    a += fix
    if init == 1:
        if a >= 1:
            init *= -1
            continue
        else:
            ans2 += abs(init - a)
            fix += init - a
            init *= -1
    else:
        if a <= -1:
            init *= -1
            continue
        else:
            ans2 += abs(init - a)
            fix += init - a
            init *= -1

print(min(ans, ans2))

Solution (Python):



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

result_1 = 0
before_sum = a[0]

if a[0] <= 0:
    before_sum = 1
    result_1 += 1 + abs(a[0])

after_sum = before_sum

for i in range(1,N):
    before_sum = after_sum
    after_sum = before_sum + a[i]
    if i % 2 == 0:
        if after_sum <= 0:
            result_1 += 1 + abs(after_sum)
            after_sum = 1
    else:
        if after_sum >= 0:
            result_1 += 1 + abs(after_sum)
            after_sum = -1

result_2 = 0
before_sum = a[0]

if a[0] >= 0:
    before_sum = -1
    result_2 += 1 + abs(a[0])

after_sum = before_sum

for i in range(1,N):
    before_sum = after_sum
    after_sum = before_sum + a[i]
    if i % 2 == 1:
        if after_sum <= 0:
            result_2 += 1 + abs(after_sum)
            after_sum = 1
    else:
        if after_sum >= 0:
            result_2 += 1 + abs(after_sum)
            after_sum = -1

print(min(result_1,result_2))

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

def sig(v):
    if v >0:
        return "+"
    elif v<0:
        return "-"
    else:
        return "0"

c_total_sig="+"
c_total=0
cost1=0

for i in range(n):
    temp_total=c_total+a[i]
    temp_total_sig=sig(temp_total)
    
    plus=a[i]
    if c_total_sig==temp_total_sig:
        
        dif=abs(temp_total)+1
        cost1+=dif
        if temp_total_sig=="-":
            plus=a[i]+dif
        elif temp_total_sig=="+":
            plus=a[i]-dif
    if temp_total_sig=="0" and c_total_sig=="-":
        dif=abs(temp_total)+1
        cost1+=dif
        plus=a[i]+dif
    elif temp_total_sig=="0" and c_total_sig=="+":
        dif=abs(temp_total)+1
        cost1+=dif
        plus=a[i]-dif
    c_total+=plus
    c_total_sig=sig(c_total)

c_total_sig="-"
c_total=0
cost2=0

for i in range(n):
    temp_total=c_total+a[i]
    temp_total_sig=sig(temp_total)
    
    plus=a[i]
    if c_total_sig==temp_total_sig:
        
        dif=abs(temp_total)+1
        cost2+=dif
        if temp_total_sig=="-":
            plus=a[i]+dif
        elif temp_total_sig=="+":
            plus=a[i]-dif
    if temp_total_sig=="0" and c_total_sig=="-":
        dif=abs(temp_total)+1
        cost2+=dif
        plus=a[i]+dif
    elif temp_total_sig=="0" and c_total_sig=="+":
        dif=abs(temp_total)+1
        cost2+=dif
        plus=a[i]-dif
    c_total+=plus
    c_total_sig=sig(c_total)
print(min(cost1,cost2))

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

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

sum=0
count=0

sum1=0
count1=0

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

    else:
        if b[i]+sum<0:
            continue
        else:
            count+=b[i]+sum+1
            sum-=b[i]+sum+1

for i in range(n):
    if i%2==0:
        if b[i]+sum1<0:
            continue
        else:
            count1+=b[i]+sum1+1
            sum1-=b[i]+sum1+1


    else:
        if b[i]+sum1>0:
            continue
        else:
            count1+=1-(b[i]+sum1)
            sum1+=1-(b[i]+sum1)

print(min(count,count1))

Solution (Python):
import sys

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

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

            if s2 <= 0:
                c2 += 1 - s2
                s2 = 1
        else:
            if s1 <= 0:
                c1 += 1 - s1
                s1 = 1

            if s2 >= 0:
                c2 += abs(-1 - s2)
                s2 = -1
    
    return min(c1, c2)

if __name__ == '__main__':
    ans = main()
    print(ans)

Solution (Python):


n = int(input())
a = list(map(int,input().split()))
def operate(ls,x):
    sm = 0
    ans = 0
    ans += abs(ls[0]-x)
    k = x
    for i in range(1,len(ls)):
        sm += k
        if sm > 0:
            if abs(ls[i]) <= sm or sm < ls[i]:
                ans += abs(ls[i]+sm+1)
                k = -sm-1
            else:
                k = ls[i]
        if sm < 0:
            if abs(ls[i]) <= abs(sm) or sm > ls[i]:
                ans += abs(-sm+1-ls[i])
                k = -sm+1
            else:
                k = ls[i]
    return ans

anstot = []
if a[0] > 0:
    inv = -1
if a[0] < 0:
    inv = 1
if a[0] != 0:
    anstot.append(operate(a,a[0]))
    anstot.append(operate(a,inv))
if a[0] == 0:
    a[0] = 1
    anstot.append(1+operate(a,1))
    a[0] = -1
    anstot.append(1+operate(a,-1))    
print(min(anstot))