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

total = 0
count1 = 0
for i in range(n):
    total += a[i]
    if i % 2 == 0:
        if total <= 0:
            tmp = abs(total) + 1
            total += tmp
            count1 += tmp
    else:
        if total >= 0:
            tmp = abs(total) + 1
            total -= tmp
            count1 += tmp
count2 = 0
total = 0
for i in range(n):
    total += a[i]
    if i % 2 != 0:
        if total <= 0:
            tmp = abs(total) + 1
            total += tmp
            count2 += tmp
    else:
        if total >= 0:
            tmp = abs(total) + 1
            total -= tmp
            count2 += tmp
print(min(count1, count2))

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


ans1 = 0
subtotal = 0
for i in range(n):
	subtotal += N[i]
	if i % 2 == 0 and subtotal <= 0:
		diff = abs(subtotal) + 1
		subtotal += diff
		ans1 += diff
	elif i % 2 != 0 and subtotal >= 0:
		diff = abs(subtotal) + 1
		subtotal -= diff
		ans1 += diff


ans2 = 0
subtotal = 0
for i in range(n):
	subtotal += N[i]
	if i % 2 != 0 and subtotal <= 0:
		diff = abs(subtotal) + 1
		subtotal += diff
		ans2 += diff
	elif i % 2 == 0 and subtotal >= 0:
		diff = abs(subtotal) + 1
		subtotal -= diff
		ans2 += diff

print(min(ans1, ans2))

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

def dfs(sign):
  cnt = 0
  tmp = 0
  for a in A:
    tmp += a
    if(tmp*sign >= 0):
      cnt += abs(tmp) + 1
      tmp = -sign
    sign *= -1
  return cnt

print(min(dfs(1), dfs(-1)))

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

res = 0
tmp = 0

for i in range(N):
    n = L[i] + tmp
    if i%2 == 0:
        if n >= 0:
            res += n + 1
            tmp = -1
        else:
            tmp = n
    else:
        if n <= 0:
            res += abs(n) + 1
            tmp = 1
        else:
            tmp = n
            
res1 = res

res = 0
tmp = 0
for i in range(N):
    n = L[i] + tmp
    if i%2 == 1:
        if n >= 0:
            res += n + 1
            tmp = -1
        else:
            tmp = n
    else:
        if n <= 0:
            res += abs(n) + 1
            tmp = 1
        else:
            tmp = n
print(min(res1, res))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
a_t = [0]
for i in range(n):
    a_t.append(a[i] + a_t[i])
ans = 0
ans_ = 0
count = 0
count_ = 0
for i in range(1, n + 1):
    if i == 1:
        if a_t[i] >= 0:
            ans += a_t[i] + 1
            count += -(a_t[i]) - 1
    else:
        if a_t[i - 1] + count > 0:
            if a_t[i] + count > 0:
                ans += a_t[i] + count + 1
                count += -(a_t[i] + count) - 1
            if a_t[i] + count == 0:
                ans += 1
                count -= 1
        elif a_t[i - 1] + count < 0:
            if a_t[i] + count < 0:
                ans += -(a_t[i] + count) + 1
                count += -(a_t[i] + count) + 1
            if a_t[i] + count == 0:
                ans += 1
                count += 1
for i in range(1, n + 1):
    if i == 1:
        if a_t[i] <= 0:
            ans_ += -a_t[i] + 1
            count_ += -(a_t[i]) + 1
    else:
        if a_t[i - 1] + count_ > 0:
            if a_t[i] + count_ > 0:
                ans_ += a_t[i] + count_ + 1
                count_ += -(a_t[i] + count_) - 1
            if a_t[i] + count_ == 0:
                ans_ += 1
                count_ -= 1
        elif a_t[i - 1] + count_ < 0:
            if a_t[i] + count_ < 0:
                ans_ += -(a_t[i] + count_) + 1
                count_ += -(a_t[i] + count_) + 1
            if a_t[i] + count_ == 0:
                ans_ += 1
                count_ += 1
print(min(ans, ans_))

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

def calcOperation(base):
  sum = 0
  ans = 0
  sign = base
  for i in range(n):
    sum += a[i]
    if sum * sign >= 0:
      ans += abs(sum) + 1
      sum = -sign
    sign *= -1
  return ans

posi = calcOperation(1)
nega = calcOperation(-1)
print(min(posi, nega))

Solution (Python):
n = int(input())
A = [int(a) for a in input().split()]
ans1,ans2 = 0,0
total1,total2 = 0,0

for i in range(n):
    total1 += A[i]
    if i % 2== 0 and total1 <= 0:
        diff = abs(total1) + 1
        total1 += diff
        ans1 += diff
    elif i % 2 == 1 and total1 >= 0:
        diff = abs(total1) + 1
        total1 -= diff
        ans1 += diff
        
for i in range(n):
    total2 += A[i]
    if i % 2== 1 and total2 <= 0:
        diff = abs(total2) + 1
        total2 += diff
        ans2 += diff
    elif i % 2 == 0 and total2 >= 0:
        diff = abs(total2) + 1
        total2 -= diff
        ans2 += diff
        
print(min(ans1,ans2))

Solution (Python):


















import collections
def inpl(): return list(map(int, input().split()))


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

ans = 1e16
for s in [1, -1]:
  res, csum = 0, 0
  for a_i in a:
    csum += a_i
    if csum * s <= 0:
      res += abs(csum-s)
      csum = s
    s *= -1
  ans = min(ans, res)

print(ans)

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


ans1 = 0
subtotal = 0
for i in range(n):
    subtotal += A[i]
    if i % 2 == 0 and subtotal <= 0:
        diff = abs(subtotal) + 1
        subtotal += diff
        ans1 += diff
    elif i % 2 == 1 and subtotal >= 0:
        diff = abs(subtotal) + 1
        subtotal -= diff
        ans1 += diff


ans2 = 0
subtotal = 0
for i in range(n):
    subtotal += A[i]
    if i % 2 == 1 and subtotal <= 0:
        diff = abs(subtotal) + 1
        subtotal += diff
        ans2 += diff
    elif i % 2 == 0 and subtotal >= 0:
        diff = abs(subtotal) + 1
        subtotal -= diff
        ans2 += diff

print(min(ans1, ans2))

Solution (Python):
def psolve(positive, As):
    i = 0
    ans = 0
    for a in As:
        if positive:
            if i + a <= 0:
                ans += abs(1 - (i + a))
                a += 1 - (i + a)
        else:
            if i + a >= 0:
                ans += abs(-1 - (i + a))
                a += -1 - (i + a)
        i += a
        positive = not positive
    return ans


def solve(N, As):
    return min(psolve(True, As), psolve(False, As))


if __name__ == "__main__":
    n = int(input())
    As = list(map(int, input().split(" ")))
    print(solve(n, As))

Solution (Python):
def sign(x):
    if x < 0:
        return -1
    elif x > 0:
        return 1
    else:
        return 0


def cumsum(a):
    c= [0] * len(a)
    c[0] = a[0]
    for i in range(1, len(a)):
        c[i] = a[i] + c[i-1]
    return c


def calc(a, signs):
    s = cumsum(a)

    op = 0
    ans = 0
    for i, sig in enumerate(signs):
        s[i] = s[i] + op
        mysign = sign(s[i])

        if mysign != sig:
            delta = sig*(abs(s[i]) + 1)
            op += delta
            s[i] += delta
            ans += abs(delta)
    return ans


def solve():
    N = int(input())
    a = [int(x) for x in input().split()]

    signs1 = [1] * N
    for i in range(N):
        if i%2==0:
            signs1[i] = -1
    signs2 = list(map(lambda x: -x, signs1))

    return min(calc(a, signs1), calc(a, signs2))


def main():
    print(solve())


if __name__ == '__main__':
    main()

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

cnt = 0
sgn = 1
s = 0
for _a in a:
    if (s + _a) * sgn <= 0:
        cnt += abs(_a + s - sgn)
        s = sgn
    else:
        s += _a
    sgn = -sgn

ans = cnt

cnt = 0
sgn = -1
s = 0
for _a in a:
    if (s + _a) * sgn <= 0:
        cnt += abs(_a + s - sgn)
        s = sgn
    else:
        s += _a
    sgn = -sgn

ans = min(ans, cnt)

print(ans)

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

cnt1, cum1 = 0, 0
for i in range(N):
    if i%2 == 0:
        if B[i] + cum1 > 0:
            pass
        else:
            d = 1 - (B[i] + cum1)
            cum1 += d
            cnt1 += abs(d)
    else:
        if B[i] + cum1 < 0:
            pass
        else:
            d = -1 - (B[i] + cum1)
            cum1 += d
            cnt1 += abs(d)

cnt2, cum2 = 0, 0
for i in range(N):
    if i%2 == 0:
        if B[i] + cum2 < 0:
            pass
        else:
            d = -1 - (B[i] + cum2)
            cum2 += d
            cnt2 += abs(d)
    else:
        if B[i] + cum2 > 0:
            pass
        else:
            d = 1 - (B[i] + cum2)
            cum2 += d
            cnt2 += abs(d)

print(min(cnt1, cnt2))

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

def f(A, op, acc = 0, cnt = 0):
    for i in range(n):
        acc += A[i]
        
        if i % 2 == 0 and op * acc <= 0:
            cnt += - op * acc + 1
            acc = op
                
        if i % 2 == 1and op * acc >= 0:
            cnt += op * acc + 1
            acc = -op
                
    if acc == 0:
        cnt += 1
        
    return cnt

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

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

def f(A, op, acc = 0, cnt = 0):
    for i in range(n):
        acc += A[i]
        
        if i % 2 == 0 and acc * op >= 0:
            cnt += op * acc + 1
            acc = - op
            
        if i % 2 == 1 and acc * op <= 0:
            cnt += - op * acc + 1
            acc = op
            
    if acc == 0:
        cnt += 1
        
    return cnt
        
print(min(f(A, -1), f(A, 1)))

Solution (Python):
def func(s, li):
    ans = 0
    for a in li:
        if s < 0 and s + a <= 0:
            ans += -(s + a) + 1
            s = 1
        elif s > 0 and s + a >= 0:
            ans += (s + a) + 1
            s = -1
        else:
            s = s + a
    return ans

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

ans0 = 0
s = li[0]
if s >= 0:
    ans0 += s + 1
    s = -1
ans0 += func(s, li[1:])
ans1 = 0
s = li[0]
if s <= 0:
    ans1 += -s + 1
    s = 1
ans1 += func(s, li[1:])

print(min(ans0, ans1))

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

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

ans2 = 0
s = A[0]
if s <= 0:
    ans2 = 1-s
    s = 1
for i in range(1, N):
    s += A[i]
    if i % 2 == 0:
        if s <= 0:
            ans2 += 1-s
            s = 1
    else:
        if s >= 0:
            ans2 -= -1-s
            s = -1
print(min(ans1, ans2))

Solution (Python):
n = int(input())
a = list(int(i) for i in input().split())  
ctplus = 0
ctminu = 0
checksum = 0
for i in range(n):
  checksum += a[i]
  if i%2 ==0:
    if checksum <= 0:
      ctplus += abs(checksum)+1
      checksum += abs(checksum)+1
  else:
    if checksum >= 0:
      ctplus += abs(checksum)+1
      checksum -= (abs(checksum)+1)
checksum = 0
for j in range(n):
  checksum += a[j]
  if j%2 ==1:
    if checksum <= 0:
      ctminu += abs(checksum)+1
      checksum += abs(checksum)+1
  else:
    if checksum >= 0:
      ctminu += abs(checksum)+1
      checksum -= (abs(checksum)+1)      
print(min(ctplus,ctminu))

Solution (Python):
import sys

sys.setrecursionlimit(200000)


def input():
    return sys.stdin.readline()[:-1]


def ii(t: type = int):
    return t(input())


def il(t: type = int):
    return list(map(t, input().split()))


def imi(N: int, t: type = int):
    return [ii(t) for _ in range(N)]


def iml(N: int, t: type = int):

    return [il(t) for _ in range(N)]


N = ii()
A = il()
ans = float("inf")
for sign in (1, -1):
    sum_a = 0
    c = 0
    for a in A:
        sum_a += a
        if sum_a * sign <= 0:
            
            c += abs(sum_a) + 1
            
            sum_a = sign
        sign *= -1
    ans = min(ans, c)
print(ans)

Solution (Python):
import sys

sys.setrecursionlimit(200000)


def input():
    return sys.stdin.readline()[:-1]


def ii(t: type = int):
    return t(input())


def il(t: type = int):
    return list(map(t, input().split()))


def imi(N: int, t: type = int):
    return [ii(t) for _ in range(N)]


def iml(N: int, t: type = int):

    return [il(t) for _ in range(N)]


N = ii()
A = il()
ans = float("inf")
for sign in (1, -1):
    sum_a = 0
    c = 0
    for i in range(N):
        sum_a += A[i]
        if sum_a * sign <= 0:
            
            c += abs(sum_a) + 1
            
            sum_a = sign
        sign *= -1
    ans = min(ans, c)
print(ans)

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

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

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

print(min(ans1,ans2))

Solution (Python):
def solve(asum, a, ans):
    for i in range(1, n):
        
        asumplus = asum + a[i]
        if asumplus == 0 and asum > 0:
            asumplus -= 1
            ans += 1
        elif asumplus == 0 and asum < 0:
            asumplus -= 1
            ans += 1
        elif asumplus > 0 and asum > 0:
            ans += asumplus + 1
            asumplus = -1
        elif asumplus < 0 and asum < 0:
            ans += -asumplus + 1
            asumplus = 1
        asum = asumplus
    return ans

n = int(input())
a = list(map(int,input().split()))
asum = a[0]
if asum > 0:
    ans = min(solve(asum,a,0),solve(-1,a,asum+1))
elif asum < 0:
    ans = min(solve(asum,a,0),solve(1,a,1-asum))
else:
    ans = min(solve(1,a,1),solve(-1,a,1))
print(ans)

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

if a[0]>=0:
    f=1
else:
    f=-1

s=0
ans=0
for i in range(n):
    s+=a[i]
    if s*f<0:
        ans+=abs(s)+1
        s=f
    elif s==0:
        ans+=1
        s=f
    f*=-1


if a[0]>=0:
    f=-1
else:
    f=1
s=0
ans2=0
for i in range(n):
    s+=a[i]
    if s*f<0:
        ans2+=abs(s)+1
        s=f
    elif s==0:
        ans2+=1
        s=f
    f*=-1
    
print(min(ans,ans2))

Solution (Python):
import sys

input = sys.stdin.readline
n = int(input())
m = list(map(int, input().split()))

def solve(isPosi):
    ans = 0
    sm = 0
    
    for a in m:
        if isPosi:
            sm += a
            if sm >= 0:
                ans += (1+sm)
                sm = -1
            isPosi = False
        
        else:
            sm += a
            if sm <= 0:
                ans += (1-sm)
                sm = 1
            isPosi = True
    
    return ans

ans_p = solve(True)
ans_n = solve(False)

print(min(ans_p, ans_n))

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

ans1 = 0
ans2 = 0
a1 = a[:]
a2 = a[:]
num1 = 0
num2 = 0

for i in range(n):
    if i % 2 == 0:
        if num1 + a1[i] > 0:
            num1 += a1[i]

        else:
            ans1 += abs(a1[i] - (1 - num1))
            a1[i] = 1 - num1
            num1 = 1

        if num2 + a2[i] < 0:
            num2 += a2[i]

        else:
            ans2 += abs(a2[i] + 1 + num2)
            a2[i] = -1 -num2
            num2 = -1

    else:
        if num1 + a1[i] < 0:
            num1 += a1[i]

        else:
            ans1 += abs(a1[i] + 1 + num1)
            a1[i] = -1 -num1
            num1 = -1

        if num2 + a2[i]  > 0:
            num2 += a2[i]
        else:
            ans2 += abs(a2[i] -1 + num2)
            a2[i] = 1 - num2
            num2 = 1

print(min(ans1, ans2))

Solution (Python):
N=int(input())
A=[int(i) for i in input().split()]
S1,S2=[],[]
if A[0]>0:
  S1.append(A[0])
  S2.append(-1)
  a1,a2=0,A[0]+1
elif A[0]==0:
  S1.append(1)
  S2.append(-1)
  a1,a2=1,1
else:
  S1.append(1)
  S2.append(A[0])
  a1,a2=-A[0]+1,0
for i in range(1,N):
  if S1[i-1]>0:
    if S1[i-1]+A[i]<0:
      S1.append(S1[i-1]+A[i])
    elif S1[i-1]+A[i]>=0:
      S1.append(-1)
      a1+=S1[i-1]+A[i]+1
  elif S1[i-1]<0:
    if S1[i-1]+A[i]>0:
      S1.append(S1[i-1]+A[i])
    elif S1[i-1]+A[i]<=0:
      S1.append(1)
      a1+=-S1[i-1]-A[i]+1
  if S2[i-1]>0:
    if S2[i-1]+A[i]<0:
      S2.append(S2[i-1]+A[i])
    elif S2[i-1]+A[i]>=0:
      S2.append(-1)
      a2+=S2[i-1]+A[i]+1
  elif S2[i-1]<0:
    if S2[i-1]+A[i]>0:
      S2.append(S2[i-1]+A[i])
    elif S2[i-1]+A[i]<=0:
      S2.append(1)
      a2+=-S2[i-1]-A[i]+1
print(min(a1,a2))

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
b=[]
for i in range(n):
  b.append(a[i])

ct1=0
if a[0]<=0:
  ct1+=1-a[0]
  a[0]=1
x=a[0]
for i in range(1,n):
  y=x+a[i]
  if i%2==1:
    if y>=0:
      ct1+=y+1
      a[i]=-x-1
  else:
    if y<=0:
      ct1+=1-y
      a[i]=-x+1
  x+=a[i]

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

print(min(ct1,ct2))

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

sum = a[0]
ans1 = 0
if sum <= 0:
	sum = 1
	ans1 += abs(a[0]-1)

for i in range(1, n):
	nextsum = sum + a[i]
	if (sum>0 and nextsum<0) or (sum<0 and nextsum>0):
		sum = nextsum
	elif sum>0:
		ans1 += abs(nextsum+1)
		sum = -1
	elif sum<0:
		ans1 += abs(nextsum-1)
		sum = 1


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

for i in range(1, n):
	nextsum = sum + a[i]
	if (sum>0 and nextsum<0) or (sum<0 and nextsum>0):
		sum = nextsum
	elif sum>0:
		ans2 += abs(nextsum+1)
		sum = -1
	elif sum<0:
		ans2 += abs(nextsum-1)
		sum = 1

print(min(ans1, ans2))

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

ans1=0
ans2=0


sign=1
total=0
for i in range(n):
  total+= A[i]
  if total*sign<=0:   
    k=abs(total-sign)
    ans1+=k           
    total=sign        
  sign*=-1            


sign=-1
total=0
for i in range(n):
  total+=A[i]
  if total*sign<=0:
    k=abs(total-sign)
    ans2+=k
    total=sign
  sign*=-1
print(min(ans1,ans2))

Solution (Python):
def main():
    n = int(input())
    aa = list(map(int, input().split()))

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

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

    print(min(res1, res2))


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
A = list(map(int,input().split()))
def func(list1,op,acc = 0,count = 0):
    for i in range(n):
        acc += list1[i]
        if i % 2 == 1 and acc * op >= 0:
            count += op * acc + 1
            acc = -op
        elif i % 2 == 0 and acc * op <= 0:
            count += -op * acc + 1
            acc = op
    if acc == 0:
        count += 1
    return count
ans = min(func(A,1),func(A,-1))
print(ans)

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




_sum = 0
count = 0
out = 0

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

out = count
_sum = 0
count = 0

for i in range(len(a)):
    if i % 2 == 0:  
        if _sum + a[i] >= 0:
            count += abs(_sum + a[i] - (-1))
            _sum = -1
        else:
            _sum += a[i]
    else:  
        if _sum + a[i] <= 0:
            count += abs(1 - (_sum + a[i]))
            _sum = 1
        else:
            _sum += a[i]
            
print(min(out, count))

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

m = 0
c = 0

for i in range(n):
    m += a[i]

    if i % 2 == 0:
        if m <= 0:
            c += abs(m) + 1
            m += abs(m) + 1
    else:
        if m >= 0:
            c += m + 1
            m -= m + 1

m = 0
d = 0

for i in range(n):
    m += a[i]

    if i % 2 == 1:
        if m <= 0:
            d += abs(m) + 1
            m += abs(m) + 1
    else:
        if m >= 0:
            d += m + 1
            m -= m + 1

print(min(c, d))

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

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


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

print(min(c, d))

Solution (Python):

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

anss = [0] * 2
for init in range(2):
    ans = 0
    state = init
    sum_tmp = 0
    for i, ai in enumerate(a):
        ans_tmp = 0
        
        if i == 0:
            if state == 1 and ai <= 0: ans_tmp += (-ai + 1)
            elif state == 0 and ai >= 0: ans_tmp += (ai + 1)
            sum_tmp = ai + ans_tmp if state == 1 else ai - ans_tmp
            ans += ans_tmp
            continue
        
        state = 1 - state
        ans_tmp = 0
        if state == 1:
            sum_tmp += ai
            if sum_tmp <= 0: ans_tmp += (-sum_tmp + 1) 
            sum_tmp += ans_tmp
        else:
            sum_tmp += ai
            if sum_tmp >= 0: ans_tmp += (sum_tmp + 1) 
            sum_tmp -= ans_tmp
        ans += ans_tmp
    anss[init] = ans

print(min(anss))

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

s = 0 
ans1 = 0

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

s = 0
ans2 = 0

for i in range(n):
    s += a[i]
    if i%2==1:
        if s>0: continue
        else: 
            ans2 += abs(s-1)
            s = 1
    else: 
        if s<0: continue
        else: 
            ans2 += abs(s-(-1))
            s = -1
            
print(min(ans1, ans2))

Solution (Python):
def op(sign, n, a):
    if sign * a[0] <= 0:
        count = abs(a[0]) + 1
        S = sign
    else:
        count = 0
        S = a[0]

    for i in a[1:]:
        if S * (S + i) > 0:
            count += abs(S + i) + 1
            S = -1 if S > 0 else 1
        elif S + i == 0:
            count += 1
            S = -1 if S > 0 else 1
        else:
            S += i

    return count


def main():
    n = int(input())
    a = list(map(int, input().split()))
    c1 = op(1, n, a)
    c2 = op(-1, n, a)
    print(min(c1, c2))


if __name__ == "__main__":
    main()

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

ans1 = 0
ans2 = 0

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

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

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

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
b=a
ans1=ans2=0
sign=1
total=0
for i in range(n):
    total+=a[i]
    if total*sign<=0:
        k=abs(total-sign)
        ans1+=k
        total=sign
    sign*=-1
sign=-1
total=0
for i in range(n):
    total+=b[i]
    if total*sign<=0:
        k=abs(total-sign)
        ans2+=k
        total=sign
    sign*=-1
print(min(ans1,ans2))

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


pos_count = 0
sign = 1
cumsum = 0
for a in A:
    cumsum += a
    if cumsum * sign <= 0:
        pos_count += abs(sign - cumsum)
        cumsum = sign
    sign *= -1


neg_count = 0
sign = -1
cumsum = 0
for a in A:
    cumsum += a
    if cumsum * sign <= 0:
        neg_count += abs(sign - cumsum)
        cumsum = sign
    sign *= -1

print(min(pos_count, neg_count))

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

ansl = [0, 0]
s = [0, 0]

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

print(min(ansl))

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

result = []

for i in range(2):
    num = 0
    r = 0
    for j in range(len(a)):
        num += a[j]
        if (j + i) % 2 == 0:
            if num <= 0:
                r -= num - 1
                num = 1

        else:
            if num >= 0:
                r += num + 1
                num = -1

    result.append(r)

print(min(result))

Solution (Python):
n = int(input())
a = input().split()
a = [int(m) for m in a]
q = []
k = 0
kco = 0

for i in range(n):
    if i == 0:
        if a[i] > 0:
            q.append(a[i])
        else:
            q.append(1)
            kco += 1 - a[i]
        k += q[i]
    else:
        k += a[i]
        if i % 2 != 0:
            if k < 0:
                q.append(a[i])
            else:
                q.append(a[i]-k-1)
                kco += k + 1
            k += q[i] - a[i]
        if i % 2 == 0:
            if k > 0:
                q.append(a[i])
            else:
                q.append(a[i]-k+1)
                kco += -k + 1
            k += q[i] - a[i]
xco = kco
q = []
k = 0
kco = 0

for i in range(n):
    if i == 0:
        if a[i] < 0:
            q.append(a[i])
        else:
            q.append(-1)
            kco += a[i] + 1
        k += q[i]
    else:
        k += a[i]
        if i % 2 == 0:
            if k < 0:
                q.append(a[i])
            else:
                q.append(a[i]-k-1)
                kco += k + 1
            k += q[i] - a[i]
        if i % 2 != 0:
            if k > 0:
                q.append(a[i])
            else:
                q.append(a[i]-k+1)
                kco += -k + 1
            k += q[i] - a[i]
yco = kco
print(min(xco, yco))

Solution (Python):
N = int(input())
a = [int(i) for i in input().split()]
 
def pura(a):
    A = a[:]
    ans = 0
    check = 0
    if A[0] <= 0:
        ans += 1 + abs(A[0])
        A[0] = 1
        
    check += A[0]
    
    for i in range(1, N):
        if i % 2 != 0:
            if check + A[i] >= 0:
                ans += abs(A[i] + 1 + check)
                A[i] = -1 - check
        else:
            if check + A[i] <= 0:
                ans += abs(A[i] - (1 - check))
                A[i] = 1 - check
        check += A[i]
    return ans
 
def mai(a):
    A = a[:]
    ans = 0
    check = 0
    if A[0] >= 0:
        ans +=1 + abs(A[0])
        A[0] = -1
        
    check += A[0]
    
    for i in range(1, N):
        if i % 2 != 0:
            if check + A[i] <= 0:
                ans += abs(A[i] - (1 - check))
                A[i] = 1 - check
        else:
            if check + A[i] >= 0:
                ans += abs(A[i] +( 1 + check))
                A[i] = -1 - check
        check += A[i]
    return ans
print(min(pura(a), mai(a)))

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
s=[l[0]]
for i in range(1,n):
    s.append(s[i-1]+l[i])
x,y=0,0
for i in range(n):
    if s[i]*((-1)**i)<=0:
        x+=abs(s[i]-(-1)**i)
        s[i]=(-1)**i
    if i+1<=n-1:
        s[i+1]=s[i]+l[i+1]
s=[l[0]]
for i in range(1,n):
    s.append(s[i-1]+l[i])
for i in range(n):
    if s[i]*((-1)**(i+1))<=0:
        y+=abs(s[i]-(-1)**(i+1))
        s[i]=(-1)**(i+1)
    if i+1<=n-1:
        s[i+1]=s[i]+l[i+1]
print(min(x,y))

Solution (Python):
n = int(input())
num_list = list(map(int, input().split()))
count = 0
sum_ = num_list[0]
if sum_ > 0:
    for i in range(1, n):
        sum_ += num_list[i]
        if i%2 == 0:
            if sum_ <= 0:
                count += abs(sum_) + 1
                sum_ = 1
        else:
            if sum_ >= 0:
                count += abs(sum_) + 1
                sum_ = -1
    count1 = count
    
    count = 0
    sum_ = -1
    for i in range(1, n):
        sum_ += num_list[i]
        if i%2 == 1:
            if sum_ <= 0:
                count += abs(sum_) + 1
                sum_ = 1
        else:
            if sum_ >= 0:
                count += abs(sum_) + 1
                sum_ = -1
    count2 = count + num_list[0] + 1
    
    print(min(count1, count2))
elif sum_ < 0:
    for i in range(1, n):
        sum_ += num_list[i]
        if i%2 == 1:
            if sum_ <= 0:
                count += abs(sum_) + 1
                sum_ = 1
        else:
            if sum_ >= 0:
                count += abs(sum_) + 1
                sum_ = -1
    count1 = count
    
    count = 0
    sum_ = 1
    for i in range(1, n):
        sum_ += num_list[i]
        if i%2 == 0:
            if sum_ <= 0:
                count += abs(sum_) + 1
                sum_ = 1
        else:
            if sum_ >= 0:
                count += abs(sum_) + 1
                sum_ = -1
    count2 = count + abs(num_list[0]) + 1
    
    print(min(count1, count2))
    
else:
    sum_ = -1
    for i in range(1, n):
        sum_ += num_list[i]
        if i%2 == 1:
            if sum_ <= 0:
                count += abs(sum_) + 1
                sum_ = 1
        else:
            if sum_ >= 0:
                count += abs(sum_) + 1
                sum_ = -1
    count1 = count + 1
    
    count = 0
    sum_ = 1
    for i in range(1, n):
        sum_ += num_list[i]
        if i%2 == 0:
            if sum_ <= 0:
                count += abs(sum_) + 1
                sum_ = 1
        else:
            if sum_ >= 0:
                count += abs(sum_) + 1
                sum_ = -1
    count2 = count + 1    

    print(min(count1, count2))