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()))
sun = 0
ans1 = 0
for i in range(n):
  if i == 0:
    SUN = 1
  else:
    SUN = sun
  now = a[i]
  if SUN * (sun+now) >= 0:
    ans1 += abs(sun+now) + 1
    if SUN > 0:
      sun = -1
    else:
      sun = 1
  else:
    sun += now
sun = 0
ans2 = 0
for i in range(n):
  now = a[i]
  if i == 0:
    SUN = -1
  else:
    SUN = sun
  if SUN * (sun+now) >= 0:
    ans2 += abs(sun+now) + 1
    if SUN > 0:
      sun = -1
    else:
      sun = 1
  else:
    sun += now

print(min(ans1,ans2))

Solution (Python):
n=int(input())
aaa=tuple(map(int, input().split()))

ans_e=0
ans_o=0
even=0
odd=0

for i, a in enumerate(aaa):
    even+=a
    if i%2==0:
        if even>0:
            continue
        else:
            tmp=1-even
            even=1
            ans_e+=tmp
    else:
        if even<0:
            continue
        else:
            tmp=even+1
            even=-1
            ans_e+=tmp

for i, a in enumerate(aaa):
    odd+=a
    if i%2==0:
        if odd<0:
            continue
        else:
            tmp=odd+1
            odd=-1
            ans_o+=tmp
    else:
        if odd>0:
            continue
        else:
            tmp=1-odd
            odd=1
            ans_o+=tmp
print(min(ans_e, ans_o))

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

total = 0
count = 0
pos_or_neg = True  

for a in a_list:
    total += a
    if pos_or_neg:
        if total <= 0:
            count += -total + 1
            total += -total + 1
        pos_or_neg = False

    else:
        if total >= 0:
            count += total + 1
            total -= total + 1
        pos_or_neg = True

first_count = count
count = 0
total = 0
pos_or_neg = False

for a in a_list:
    total += a
    if pos_or_neg:
        if total <= 0:
            count += -total + 1
            total += -total + 1
        pos_or_neg = False

    else:
        if total >= 0:
            count += total + 1
            total -= total + 1
        pos_or_neg = True

print(min(count, first_count))

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

def solve(isPositive):
    sum_a = 0
    ans = 0
    for a in A:
        sum_a += a
        
        
        if isPositive:
            
            
            if sum_a>=0:
                ans += 1 + sum_a
                
                sum_a = -1
            isPositive = False
        else:
            if sum_a<=0:
                ans += 1 - sum_a
                sum_a = 1
            isPositive = True
            
    return ans

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

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

def calc(sign) :
    pre_sign = sign
    s = 0
    ret = 0
    for ai in a :
        if ai + s > 0 :
            cur_sign = 1
        elif ai + s < 0 :
            cur_sign = -1
        else :
            cur_sign = 0
        
        if pre_sign * cur_sign < 0 :
            s += ai
        else :
            ret += abs((pre_sign * -1) - (ai + s))
            s = pre_sign * -1
        pre_sign *= -1
        
    return ret
    
print(min(calc(1), calc(-1)))

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

cnt1 = 0
cnt2 = 0
acs = 0

for i in range(n):
    acs += lst[i]
    if acs == 0:
        if i%2==0:
            acs = 1
        else:
            acs = -1
        cnt1 += 1
    
    if i%2 == 0 and acs < 0: 
        cnt1 += 1 - acs
        acs = 1
        
    elif i%2 != 0 and acs > 0:
        cnt1 += abs(-1 - acs)
        acs = -1

acs = 0

for i in range(n):
    acs += lst[i]
    if acs == 0:
        if i%2==0:
            acs = -1
        else:
            acs = 1
        cnt2 += 1
    if i%2 != 0 and acs < 0: 
        cnt2 += 1 - acs
        acs = 1
        
    elif i%2 == 0 and acs > 0:
        cnt2 += abs(-1 - acs)
        acs = -1
        
print(min(cnt1,cnt2))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
l = len(a)
cnt = 0
a_ = 0
x = -1

for i in range(l):
    a_ += a[i]
    if x == -1:
        if a_ > x:
            cnt += a_ -x
            a_ = -1
    else:
        if a_ < x:
            cnt += x-a_
            
            a_ = 1

    x *= -1

cnt1 = 0
a_ = 0
x1 = 1

for i in range(l):
    a_ += a[i]
    if x1 == -1:
        if a_ > x1:
            cnt1 += a_ -x1
            a_ = -1
    else:
        if a_ < x1:
            cnt1 += x1-a_
            
            a_ = 1

    x1 *= -1

if cnt >= 0 and cnt1 >=0:
    print(min(cnt, cnt1))
elif cnt < 0:
    print(cnt1)
else:
    print(cnt)

Solution (Python):

import sys, math, fractions, itertools


def solve(n: int, a: "List[int]"):
    cum = 0
    mn = 10000000000
    counter1 = 0
    for i in range(n):
        if cum + a[i] == 0:
            counter1 += 1
            if i % 2 == 0:
                cum += a[i] + 1
            else:
                cum += a[i] - 1
        elif cum + a[i] > 0:
            if i % 2 == 0:
                cum += a[i]
            else:
                counter1 += abs(a[i] + abs(cum) + 1)
                cum -= abs(cum)+1
        else:
            if i % 2 == 0:
                counter1 += abs(a[i] - abs(cum) - 1)
                cum += abs(cum)+1
            else:
                cum += a[i]

    cum = 0
    counter2 = 0
    for i in range(n):
        if cum + a[i] == 0:
            counter2 += 1
            if i % 2 != 0:
                cum += a[i] + 1
            else:
                cum += a[i] - 1
        elif cum + a[i] > 0:
            if i % 2 != 0:
                cum += a[i]
            else:
                counter2 += abs(a[i] + abs(cum) + 1)
                cum -= abs(cum) + 1
        else:
            if i % 2 != 0:
                counter2 += abs(a[i] - abs(cum) - 1)
                cum += abs(cum) + 1
            else:
                cum += a[i]
    print(min(counter1, counter2))

    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    n = int(next(tokens))  
    a = [ int(next(tokens)) for _ in range(n) ]  
    solve(n, a)

if __name__ == '__main__':
    main()

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


def solve(A, N):
    S = [0]*N
    answer = 0
    total = 0
    for i in range(N):
        total += A[i]
        S[i] = total
        if total == 0:
            d = 1 if S[i-1] < 0 else -1
            A[i] += d
            answer += 1
            total += d
        else:
            
            
            
            if S[i-1] < 0 and S[i] < 0:
                answer += -S[i]+1
                total += -S[i]+1
            elif S[i-1] > 0 and S[i] > 0:
                answer += S[i]+1
                total += -(S[i]+1)
        S[i] = total
        
    return answer

if A[0] == 0:
    A[0] = -1
    m1 = solve(A, N) + 1
    A[0] = +1
    m2 = solve(A, N) + 1
    print(min(m1, m2))
else:
    m1 = solve(A, N)
    t = abs(A[0])
    A[0] = 1 if A[0] < 0 else -1
    m2 = solve(A, N) + t+1
    print(min(m1, m2))

Solution (Python):
import sys
input=sys.stdin.readline
n=int(input())
a_list=list(map(int,input().split()))
a_c=[]
Sum=0
for i in a_list:
    a_c.append(Sum+i)
    Sum+=i
ans_p=0
count=0
for i in range(n):
    if i%2==0:
        if a_c[i]+count<=0:
            ans_p+=1-a_c[i]-count
            count=1-a_c[i]
    else:
        if a_c[i]+count>=0:
            ans_p+=1+a_c[i]+count
            count=-1-a_c[i]
ans_n=0
count=0
for i in range(n):
    if i%2==1:
        if a_c[i]+count<=0:
            ans_n+=1-a_c[i]-count
            count=1-a_c[i]
    else:
        if a_c[i]+count>=0:
            ans_n+=1+a_c[i]+count
            count=-1-a_c[i]
ans=min(ans_p,ans_n)
print(ans)

Solution (Python):

import itertools
from collections import Counter
from collections import defaultdict
import bisect
from heapq import heappush, heappop

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

    cumulative = 0
    count1 = 0
    for i in range(len(a)):
        cumulative += a[i]
        if i % 2 == 0: 
            if cumulative <= 0:
                count1 += abs(cumulative) + 1
                cumulative += (abs(cumulative) + 1)
        else:  
            if cumulative >= 0:
                count1 += abs(cumulative) + 1
                cumulative -= (abs(cumulative) + 1)
        

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

    print(min(count1, count2))


if __name__ == '__main__':
    main()

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

def count(s):
    ans = 0
    for i, a in enumerate(A):
        if i == 0:
            continue
        if s < 0:
            if 1-s-a >= 0:
                ans += 1-s-a
                s = 1
            else:
                s += a
        elif s > 0:
            if s+a+1 >= 0:
                ans += s+a+1
                s = -1
            else:
                s += a
    return ans

b = A[0]
ans1 = count(b)
if b > 0:
    ans2 = count(-1)+b+1
elif b < 0:
    ans2 = count(1)+1-b
else:
    ans1 = count(1)+1
    ans2 = count(-1)+1
print(min(ans1, ans2))

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
tmp1 = 0
tmp2 = 0
S = [0]*n

if a[0] > 0:
  S[0] = a[0]
else:
  S[0] = 1
  tmp1 += abs(1-a[0])
  
for i in range(1,n):
  tmpS = a[i] + S[i-1]
  if tmpS*S[i-1] < 0:
    S[i] = tmpS
    continue
  else:
    if i%2 == 0:
      S[i] = 1
      tmp1 += abs(1 - tmpS)
    else:
      S[i] = -1
      tmp1 += abs(-1-tmpS)
if a[0] < 0:
  S[0] = a[0]
else:
  S[0] = -1
  tmp2 += abs(-1-a[0])
for i in range(1,n):
  tmpS = a[i] + S[i-1]
  if tmpS*S[i-1] < 0:
    S[i] = tmpS
    continue
  else:
    if i%2 == 0:
      S[i] = -1
      tmp2 += abs(-1 - tmpS)
    else:
      S[i] = 1
      tmp2 += abs(1-tmpS)
print(min(tmp1,tmp2))

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


def f(p):
    sa = 0
    ans = 0
    for ai in a:
        if p:
            sa += ai
            if sa >= 0:
                ans += sa + 1
                sa = -1
            p = False
        else:
            sa += ai
            if 0 >= sa:
                ans += 1 - sa
                sa = 1
            p = True
    return ans


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


main()

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

s1=c1=s2=c2=0
for i in range(n):
    if i%2:
        c1+=max(0,a[i]+s1+1); s1+=min(-s1-1,a[i])
        c2+=max(0,-s2+1-a[i]); s2+=max(-s2+1,a[i])
    else:
        c1+=max(0,-s1+1-a[i]); s1+=max(-s1+1,a[i])
        c2+=max(0,a[i]+s2+1); s2+=min(-s2-1,a[i])
print(min(c1,c2))

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

if A[0] < 0:
	sumP = 1
	sumM = A[0]
	countP = -A[0] + 1
	countM = 0
elif A[0] == 0:
	sumP = 1
	sumM = -1
	countP = 1
	countM = 1
else:
	sumP = A[0]
	sumM = -1
	countP = 0
	countM = A[0] + 1

for i in range(1, N):
	if sumP > 0:
		if A[i] + sumP >= 0:
			countP += A[i] + sumP + 1
			sumP = -1
		else:
			sumP += A[i]
	else:
		if A[i] + sumP <= 0:
			countP += -(A[i] + sumP) + 1
			sumP = 1
		else:
			sumP += A[i]
	
	if sumM > 0:
		if A[i] + sumM >= 0:
			countM += A[i] + sumM + 1
			sumM = -1
		else:
			sumM += A[i]
	else:
		if A[i] + sumM <= 0:
			countM += -(A[i] + sumM) + 1
			sumM = 1
		else:
			sumM += A[i]

print(min(countP, countM))

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

def solve(n):
    ans = 0
    cusm = []
    if n == -1 and a[0] > 0:
        cusm.append(-1)
        ans += abs(-1 - a[0])
    elif n == -1 and a[0] < 0:
        cusm.append(1)
        ans += abs(1 - a[0])
    elif a[0] == 0:
        cusm.append(n)
        ans += abs(n - a[0])
    else:
        cusm.append(a[0])

    for i, ai in enumerate(a):
        if i == 0: continue
        if (cusm[-1] + ai) * cusm[-1] < 0: 
            cusm.append(cusm[-1] + ai)
        elif cusm[-1] > 0: 
            ans += abs(-1 - (cusm[-1] + ai))
            cusm.append(-1)
        else:  
            ans += abs(1 - (cusm[-1] + ai))
            cusm.append(1)
    return ans

print(min(solve(1),solve(-1)))

Solution (Python):
n = int(raw_input())
A = map(int, raw_input().split())
if A[0] > 0:
    psep = A[0]
    psop = -1
    opop = abs(-1 - A[0])
    opep = 0
elif A[0] < 0:
    psep = 1
    psop = A[0]
    opep = abs(1-A[0])
    opop = 0
else:
    psep = 1
    psop = -1
    opep = 1
    opop = 1
for i in range(1, n):
    if i % 2 == 0:
        if psep + A[i] <= 0:
            opep += abs(1 - (psep + A[i]))
            psep = 1
        else:
            psep = psep + A[i]
        if psop + A[i] >= 0:
            opop += abs(-1 - (psop + A[i]))
            psop = -1
        else:
            psop += A[i]
    else:
        if psep + A[i] >= 0:
            opep += abs(-1 - (psep+A[i]))
            psep = -1
        else:
            psep += A[i]
        if psop + A[i] <= 0:
            opop += abs(1-(psop+A[i]))
            psop = 1
        else:
            psop = psop + A[i]
print min(opop, opep)

Solution (Python):

from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')


def A():
    a = input().split()
    a = list(map(lambda x: x.capitalize(), a))
    a,b,c = a
    print(a[0]+b[0]+c[0])
    return


def B():
    a = II()
    b = II()
    if a > b:
        print("GREATER")
    if a < b:
        print("LESS")
    if a == b:
        print("EQUAL")
    return


def C():
    II()
    a = LI()
    def f(suma, b):
        for i in a[1:]:
            if suma * (suma + i) < 0:
                suma += i
                continue
            b += (abs(suma + i) + 1)
            suma = (-1 * (suma > 0)) or 1
        return b

    if a[0] == 0:
        ans = min(f(1, 1), f(-1, 1))
    else:
        ans = min(f(a[0], 0), f(1 * (a[0] < 0) or - 1, abs(a[0]) + 1))
    print(ans)
    return


def D():
    s = S()
    for i in range(len(s) - 1):
        if s[i] == s[i+1]:
            print(i + 1, i + 2)
            return
    for i in range(len(s) - 2):
        if s[i] == s[i + 2]:
            print(i + 1, i + 3)
            return
    print(-1, -1)
    return



if __name__ == '__main__':
    C()

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

res = 0
former = 0
s = 0


for i in range(N):
    if i == 0:
        if A[0] <= 0:
            res += (-A[0]+1)
            s = 1
        else:
            s = A[0]
    else:
        former = s
        s += A[i]
        if former*s >= 0:
            if s == 0:
                if former > 0:
                    res += 1
                    s = -1
                else:
                    res += 1
                    s = 1
            elif s > 0:
                res += s+1
                s = -1
            elif s < 0:
                res += (-s+1)
                s = 1
        else:
            pass
    


t = 0
for i in range(N):
    if i == 0:
        if A[0] >= 0:
            t += (A[0]+1)
            s = -1
        else:
            s = A[0]
    else:
        former = s
        s += A[i]
        if former*s >= 0:
            if s == 0:
                if former > 0:
                    t += 1
                    s = -1
                else:
                    t += 1
                    s = 1
            elif s > 0:
                t += s+1
                s = -1
            elif s < 0:
                t += (-s+1)
                s = 1
        else:
            pass
    
if res > t:
    res = t
print(res)

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

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

sum_a = 0

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

print(min(ans1,ans2))

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

Solution (Python):
import math
import itertools

INF = 10**9+7

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

def solve(x):
	global s
	cost = 0
	debt = 0
	y = [1,-1]
	for i in range(len(s)):
		c = s[i]*x + debt
		if(y[i%2]!=sgn(c)):
			cost += abs(y[i%2]-c)
			debt += y[i%2]-c
	return cost

def sgn(n):
	if(n>0): return 1
	if(n<0): return -1
	return 0

print(min(solve(-1),solve(1)))

Solution (Python):
from subprocess import*
with open('Main.java','w')as f:f.write(r"""
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = Integer.parseInt(sc.next());
		int[] a = new int[n];
		for (int i = 0; i < n; i++)
			a[i] = Integer.parseInt(sc.next());
		int diff = 0;
		long c = 0;
		for (int i = 0; i < n; i++) {
			diff += a[i];
			if (i % 2 == 0) {
				if (diff <= 0) {
					c += -diff + 1;
					diff = 1;
				}
			}
			else {
				if (diff >= 0) {
					c += diff + 1;
					diff = -1;
				}
			}
		}
		long ans = c;
		c = 0;
		diff = 0;
		for (int i = 0; i < n; i++) {
			diff += a[i];
			if (i % 2 != 0) {
				if (diff <= 0) {
					c += -diff + 1;
					diff = 1;
				}
			}
			else {
				if (diff >= 0) {
					c += diff + 1;
					diff = -1;
				}
			}
		}
		ans = Math.min(ans, c);
		System.out.println(ans);
	}
}
""")
call(('javac','Main.java'))
call(('java','Main'))

Solution (Python):
from subprocess import*
with open('Main.java','w')as f:f.write(r"""
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = Integer.parseInt(sc.next());
		int[] a = new int[n];
		for (int i = 0; i < n; i++)
			a[i] = Integer.parseInt(sc.next());
		int diff = 0;
		long c = 0;
		for (int i = 0; i < n; i++) {
			diff += a[i];
			if (i % 2 == 0) {
				if (diff <= 0) {
					c += -diff + 1;
					diff = 1;
				}
			}
			else {
				if (diff >= 0) {
					c += diff + 1;
					diff = -1;
				}
			}
		}
		long ans = c;
		c = 0;
		diff = 0;
		for (int i = 0; i < n; i++) {
			diff += a[i];
			if (i % 2 != 0) {
				if (diff <= 0) {
					c += -diff + 1;
					diff = 1;
				}
			}
			else {
				if (diff >= 0) {
					c += diff + 1;
					diff = -1;
				}
			}
		}
		ans = Math.min(ans, c);
		System.out.println(ans);
	}
}
""")
call(('javac','Main.java'))
call(('java','Main'))

Solution (Python):


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

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

print(min(ans1,ans2))

Solution (Python):


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

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

print(min(ans1,ans2))

Solution (Python):

def main():
    num = int(input())
    data = list(map(int, input().split()))

    now_num = [0, 0]
    now_c = [0, 0]
    for i in range(num):
        now_num[0] += data[i]
        if i % 2 == 0:
            if now_num[0] <= 0:
                now_c[0] += 1 - now_num[0]
                now_num[0] = 1
        else:
            if now_num[0] >= 0:
                now_c[0] += now_num[0] + 1
                now_num[0] = -1

        now_num[1] += data[i]
        if i % 2 == 1:
            if now_num[1] <= 0:
                now_c[1] += 1 - now_num[1]
                now_num[1] = 1
        else:
            if now_num[1] >= 0:
                now_c[1] += now_num[1] + 1
                now_num[1] = -1

    print(min(now_c))






if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
cnt1,cnt2 = 0,0
S1,S2 = A[0],A[0]

if A[0]==0:
    cnt1,cnt2 = 1,1
    S1,S2 = 1,-1
elif A[0]>0:
    cnt2 += 1+A[0]
    S2 = -1
else:
    cnt1 += 1-A[0]
    S1 = 1

for i in range(1,N):
    K = S1+A[i]
    if S1>0 and K>=0:
        cnt1 += 1+K
        S1 = -1
    elif S1<0 and K<=0:
        cnt1 += 1-K
        S1 = 1
    else:
        S1 = K

for i in range(1,N):
    K = S2+A[i]
    if S2>0 and K>=0:
        cnt2 += 1+K
        S2 = -1
    elif S2<0 and K<=0:
        cnt2 += 1-K
        S2 = 1
    else:
        S2 = K
print(min(cnt1,cnt2))

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

pos = 0
P = [0] * n
if A[0] > 0:
    P[0] = A[0]
else:
    P[0] = 1
    pos += 1 - A[0]
for i in range(1, n):
    P[i] = P[i - 1] + A[i]
    if i % 2 == 0 and P[i] <= 0:
        pos += 1 - P[i]
        P[i] = 1
    elif i % 2 == 1 and P[i] >= 0:
        pos += 1 + P[i]
        P[i] = -1

neg = 0
N = [0] * n
if A[0] < 0:
    N[0] = A[0]
else:
    N[0] = -1
    neg += 1 + A[0]
for i in range(1, n):
    N[i] = N[i - 1] + A[i]
    if i % 2 == 0 and N[i] >= 0:
        neg += 1 + N[i]
        N[i] = -1
    elif i % 2 == 1 and N[i] <= 0:
        neg += 1 - N[i]
        N[i] = 1

print(min(pos, neg))

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

def func(x):
	global a

	s = 0
	k = 0

	for i in a:
		s += i
		if s*x <= 0:
			k += abs(x-s)
			s = x
		x *= -1

	return k


print(min(func(1), func(-1)))

Solution (Python):
import copy

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

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

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

print(min(ans_a,ans_b))

Solution (Python):


import sys

sys.setrecursionlimit(10**7)


def read_h(typ=int):
    return list(map(typ, input().split()))


def read_v(n, m=1, typ=int):
    return [read_h() if m > 1 else typ(input()) for _ in range(n)]


def solve(arr, positive):
    num_op = 0
    cumul = 0
    

    for a in arr:
        cumul += a

        if positive and cumul >= 0:
            num_op += 1 + cumul
            cumul = -1
        elif not positive and cumul <= 0:
            num_op += 1 - cumul
            cumul = 1

        positive = not positive

    return num_op


def main():
    read_h()
    arr = read_h()

    op_plus = solve(arr, True)
    
    op_minus = solve(arr, False)
    

    print(min(op_plus, op_minus))


if __name__ == '__main__':
    main()

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

cnt=0
wa=0
condition='plus'
for i in range(n):
  wa+=a[i]
  if condition == 'plus':
    condition='minus'
    if wa<=0:
      cnt+=abs(wa)+1
      a[i]+=abs(wa)+1
      wa+=abs(wa)+1
  elif condition == 'minus':
    condition='plus'
    if wa>=0:
      cnt+=abs(wa)+1
      a[i]-=abs(wa)+1
      wa-=abs(wa)+1

cnt1=cnt
a=b[:]
cnt=0
wa=0
condition='minus'
for i in range(n):
  wa+=a[i]
  if condition == 'plus':
    condition='minus'
    if wa<=0:
      cnt+=abs(wa)+1
      a[i]+=abs(wa)+1
      wa+=abs(wa)+1
  elif condition == 'minus':
    condition='plus'
    if wa>=0:
      cnt+=abs(wa)+1
      a[i]-=abs(wa)+1
      wa-=abs(wa)+1

cnt2=cnt
print(min(cnt1,cnt2))

Solution (Python):


def main(N, A):
    ans = float("INF")
    for i in range(2):
        sm = 0
        cnt = 0
        for j in range(N):
            sm += A[j]
            
            if(j%2==i and sm<=0):
                cnt += 1 - sm
                sm = 1
            
            elif(j%2==1-i and sm>=0):
                cnt += 1 + sm
                sm = -1
        ans = min(ans, cnt)
    print(ans)
    
if __name__ == '__main__':
    N = int(input())
    A = [int(i) for i in input().split()]
    
    main(N, A)

Solution (Python):
import numpy as np
import copy
n = int(input())
a = list(map(int, input().split()))
a_copy = copy.copy(a)
count = np.zeros(2)


i = 0
sum_before = 0
sum_after = 0    
    
if a[0] <= 0:
    count[0] += abs(a[0]) + 1
    a[0] = 1
while i < n:
    sum_after = sum_before + a[i]
    if sum_after * sum_before > 0 or sum_after == 0:
        if sum_after < 0:
            a[i] = a[i] - sum_after + 1
        elif sum_after > 0:
            a[i] = a[i] - sum_after - 1
        elif sum_before < 0:
            a[i] += 1
        else:
            a[i] -= 1
        count[0] += abs(sum_after) + 1
        sum_after = sum_before + a[i]
    i += 1
    sum_before = sum_after

i = 0
sum_before = 0
sum_after = 0    

if a_copy[0] >= 0:
    count[1] += abs(a_copy[0]) + 1
    a_copy[0] = -1

while i < n:
    sum_after = sum_before + a_copy[i]
    if sum_after * sum_before > 0 or sum_after == 0:
        if sum_after < 0:
            a_copy[i] = a_copy[i] - sum_after + 1
        elif sum_after > 0:
            a_copy[i] = a_copy[i] - sum_after - 1
        elif sum_before < 0:
            a_copy[i] += 1
        else:
            a_copy[i] -= 1
        count[1] += abs(sum_after) + 1
        sum_after = sum_before + a_copy[i]
    i += 1
    sum_before = sum_after

print(int(min(count[0], count[1])))

Solution (Python):

n = int(input())



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

ans = float("inf")

now = 0
summ = 0
for i in range(n):

    summ += a[i]

    if i % 2 == 0 and summ <= 0:
        now += 1 - summ
        summ = 1

    elif i % 2 == 1 and summ >= 0:
        now += summ + 1
        summ = -1

ans = min(ans,now)

now = 0
summ = 0

for i in range(n):

    summ += a[i]

    if i % 2 == 1 and summ <= 0:
        now += 1 - summ
        summ = 1

    elif i % 2 == 0 and summ >= 0:
        now += summ + 1
        summ = -1

ans = min(ans,now)

print (ans)

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

x = 0
tmp = 0
for i in range(N):
    tmp += A[i]
    if i % 2 == 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 % 2 == 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()))
total_n=0
cnt_n=0
total_p=0
cnt_p=0

mark=-1
for i in range(n):
    total_n+=a[i]
    if mark<0:
        if total_n>=0:
            cnt_n+=abs(-1-total_n) 
            total_n=-1
    else:
        if total_n<=0:
            cnt_n+=abs(1-total_n)
            total_n=1
    mark*=-1

mark=1
for i in range(n):
    total_p+=a[i]
    if mark<0:
        if total_p>=0:
            cnt_p+=abs(-1-total_p)
            total_p=-1
    else:
        if total_p<=0:
            cnt_p+=abs(1-total_p)
            total_p=1
    mark*=-1

print(min(cnt_p,cnt_n))