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

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

a = list(accumulate(a))

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

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

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

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

ans1 = 0
ans2 = 0

sum1 = 0
sum2 = 0

for i in range(n):
    sum1 += a[i]
    sum2 += a[i]
    if sum1*(-1)**i>=0:
        ans1 += abs(sum1)+1
        sum1 = -(-1)**i
    if sum2*(-1)**i<=0:
        ans2 += abs(sum2)+1
        sum2 = (-1)**i

print(min(ans1,ans2))

Solution (Python):
n,*a=map(int,open(0).read().split())
pre1=0
pre2=0
ans1=0
ans2=0
for i in range(n):
    x=(-1)**(i%2)
    y=-x
    pre1+=a[i]
    pre2+=a[i]
    if x*pre1<=0:
        ans1+=abs(pre1-x)
        pre1=x
    if y*pre2<=0:
        ans2+=abs(pre2-y)
        pre2=y
print(min(ans1,ans2))

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

ms, ps = 0, 0
mc, pc = 0, 0
if alist[0] < 0:
  ms = alist[0]
  ps = 1
  pc = abs(ms) + 1
elif alist[0] > 0:
  ps = alist[0]
  ms = -1
  mc = ps + 1
else:
  ps = 1
  pc = 1
  ms = -1
  mc = 1

for a in alist[1:]:
  if a < 0:
    if ms < 0:
      mc += abs(a + ms) + 1
      ms = 1
    else:
      if a + ms < 0:
        ms += a
      else:
        mc += abs(a + ms) + 1
        ms = -1
    if ps < 0:
      pc += abs(a + ps) + 1
      ps = 1
    else:
      if a + ps < 0:
        ps += a
      else:
        pc += abs(a + ps) + 1
        ps = -1
  elif a > 0:
    if ms > 0:
      mc += abs(a + ms) + 1
      ms = -1
    else:
      if a + ms > 0:
        ms += a
      else:
        mc += abs(a + ms) + 1
        ms = 1
    if ps > 0:
      pc += abs(a + ps) + 1
      ps = -1
    else:
      if a + ps > 0:
        ps += a
      else:
        pc += abs(a + ps) + 1
        ps = 1
  else:
    if ms > 0:
      mc += abs(ms) + 1
      ms = -1
    else:
      mc += abs(ms) + 1
      ms = 1
    if ps > 0:
      pc += abs(ps) + 1
      ps = -1
    else:
      pc += abs(ps) + 1
      ps = 1

print(min(pc, mc))

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

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


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

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



print(min(ncount, pcount))

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

if A[0]>0:
    s = A[0]
    ans1 = 0
else:
    s = 1
    ans1 = -A[0]+1

for i in range(1,n):
    if s*(s+A[i])>=0:
        ans1 += abs(s+A[i])+1
        if s>0:     
            s=-1
        else:
            s=1
    else:   
        s += A[i]
                  
if A[0]<0:        
    s = A[0]
    ans2 = 0
else:
    s = -1
    ans2 = A[0]+1
                    
for i in range(1,n):
    if s*(s+A[i])>=0:
        ans2 += abs(s+A[i])+1
        if s>0:
            s=-1
        else:
            s=1
    else:   
        s += A[i]
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:
			ans1 += 1 - s1
			s1 = 1
	else:
		if s1 >= 0:
			ans1 += s1 + 1
			s1 = -1

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

print(min(ans1, ans2))

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

ans1, ans2 = 0, 0
sum_v = 0


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

sum_v = 0


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

print (min(ans1, ans2))

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


now = 0
even_count = 0
for i, a in enumerate(A):
    now += a
    if i % 2 == 0 and now <= 0:
        even_count += 1 - now
        now = 1
    elif i % 2 == 1 and now >= 0:
        even_count += abs(-1 - now)
        now = -1


now = 0
odd_count = 0
for i, a in enumerate(A):
    now += a
    if i % 2 == 0 and now >= 0:
        odd_count += abs(-1 - now)
        now = -1
    elif i % 2 == 1 and now <= 0:
        odd_count += 1 - now
        now = 1
print(min(even_count, odd_count))

Solution (Python):
n = input()
import numpy
a = map(int, raw_input().split())
cumsum_a = numpy.cumsum(a)
cumsum_v = 0
res1 = 0
for i, v in enumerate(cumsum_a):
  
  if i % 2 == 0:
    if v + cumsum_v <= 0:
      res1 += -(v + cumsum_v) + 1
      cumsum_v += -(v + cumsum_v) + 1
  
  else:
    if v + cumsum_v >= 0:
      res1 += (v + cumsum_v) + 1
      cumsum_v += -(v + cumsum_v) - 1

cumsum_v = 0
res2 = 0
for i, v in enumerate(cumsum_a):
  
  if i % 2 != 0:
    if v + cumsum_v <= 0:
      res2 += -(v + cumsum_v) + 1
      cumsum_v += -(v + cumsum_v) + 1
  
  else:
    if v + cumsum_v >= 0:
      res2 += (v + cumsum_v) + 1
      cumsum_v += -(v + cumsum_v) - 1
print min(res1, res2)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
p=[1,-1]
j=[-1,1]
c=0
l=0
d=0
e=0
for i in range(n):
    s=i%2
    c+=a[i]
    l+=a[i]
    if p[s]*c<=0:
        d+=abs(c-p[s])
        c=p[s]
    if j[s]*l<=0:
        e+=abs(l-j[s])
        l=j[s] 
print(min(e,d))

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
count1=a[0]
count2=a[0]
answer1=0
answer2=0

if a[0]<=0:
  answer1+=(1-a[0])
  count1=1
if a[0]>=0:
  answer2+=(1+a[0])
  count2=-1
    
for i in a[1:]:
  if (count1>0 and count1+i<0) or (count1<0 and count1+i>0):
    count1+=i
    continue
  else:
    answer1+=abs(count1+i)+1
    if count1>0:
      count1=-1
    else:
      count1=1
for i in a[1:]:
  if (count2>0 and count2+i<0) or (count2<0 and count2+i>0):
    count2+=i
    continue
  else:
    answer2+=abs(count2+i)+1
    if count2>0:
      count2=-1
    else:
      count2=1  
print(min(answer1,answer2))

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

calc1 = 0
calc2 = 0

L = A

if L[0] > 0 :
    su = L[0]
else :
    su = 1
    calc1 += 1 + abs(L[0])

for i in range(1,N) :
    su += L[i]
    if i % 2 :
        if su >= 0 :
            calc1 += 1 + abs(su)
            su = -1
    else :
        if su <= 0 :
            calc1 += 1 + abs(su)
            su = 1

L = A

if L[0] < 0 :
    su = L[0]
else :
    su = -1
    calc2 += 1 + abs(L[0])

for i in range(1,N) :
    su += L[i]
    if not i % 2 :
        if su >= 0 :
            calc2 += 1 + abs(su)
            su = -1
    else :
        if su <= 0 :
            calc2 += 1 + abs(su)
            su = 1
print(min(calc1,calc2))

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

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

Pnum=0
Psum=0
if L[0]>=1:
    Pnum=L[0]
else:
    Psum=1-L[0]
    Pnum=1
for i in range(1,N):
    if i%2==1:
        if Pnum+L[i]<=-1:
            Pnum=Pnum+L[i]
        else:
            Psum+=(Pnum+L[i]+1)
            Pnum=-1
    else:
        if Pnum+L[i]>=1:
            Pnum=Pnum+L[i]
        else:
            Psum+=(1-Pnum-L[i])
            Pnum=1
            
Mnum=0
Msum=0
if L[0]<=-1:
    Mnum=L[0]
else:
    Msum=L[0]+1
    Mnum=-1
for i in range(1,N):
    if i%2==0:
        if Mnum+L[i]<=-1:
            Mnum=Mnum+L[i]
        else:
            Msum+=(Mnum+L[i]+1)
            Mnum=-1
    else:
        if Mnum+L[i]>=1:
            Mnum=Mnum+L[i]
        else:
            Msum+=(1-Mnum-L[i])
            Mnum=1

print(min(Psum,Msum))

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
p=[1,-1]
j=[-1,1]
c=0
l=0
d=0
e=0
for i in range(n):
    s=i%2
    c+=a[i]
    l+=a[i]
    if p[s]*c<=0:
        d+=abs(c-p[s])
        c=p[s]
    if j[s]*l<=0:
        e+=abs(l-j[s])
        l=j[s] 
print(min(e,d))

Solution (Python):


"""
Created on Sat Feb  9 14:32:30 2019

@author: shinjisu
"""



def getInt(): return int(input())


def getIntList(): return [int(x) for x in input().split()]


def db(x):
    global debug
    if debug:
        print(x)


def count(N, A, initSum):
    
    sm = initSum
    cnt = abs(sm-A[0])
    db(cnt)
    for i in range(N-1):
        nextSum = sm + A[i+1]
        db((sm, nextSum))
        if sm > 0 and nextSum >= 0:
            cnt += abs(nextSum)+1
            sm = -1
        elif sm < 0 and nextSum <= 0:
            cnt += abs(nextSum)+1
            sm = 1
        else:
            sm = nextSum
        db(cnt)
    return cnt


def probC():
    N = getInt()
    A = getIntList()
    db((N, A))
    if A[0] != 0:
        initSum = A[0]
    else:
        initSum = 1
    cnt1 = count(N, A, initSum)
    db(('a[0] or +1: ', cnt1))
    if initSum > 0:
        cnt2 = count(N, A, -1)
    else:
        cnt2 = count(N, A, 1)
    db(('反対符号 or -1: ', cnt2))
    return min(cnt1, cnt2)


debug = False
print(probC())

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

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

ans = 0


def func(A, flag):
    ans = 0
    acc = 0
    for i in range(0, N):
        acc = acc+A[i]
        if i % 2 == flag:
            if acc >= 0:
                ans += 1+acc
                acc = -1
        else:
            if acc <= 0:
                ans += 1-acc
                acc = 1
    return ans


print(min(func(A, 0), func(A, 1)))

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

ans = 0


def func(A, flag):
    l = [0]*N
    ans = 0
    acc = 0
    for i in range(N):
        l[i] = A[i]
    for i in range(0, N):
        acc = acc+l[i]
        if i % 2 == flag:
            if acc >= 0:
                l[i] = l[i] - (1 + acc)
                ans += 1+acc
                acc = -1
        else:
            if acc <= 0:
                l[i] = l[i]+(1-acc)
                ans += 1-acc
                acc = 1
    return ans


print(min(func(A, 0), func(A, 1)))

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
ans1 = abs(a[0]-1)
ans2 = abs(a[0]+1)
ans3 = 0
def f(x,y):
  for i in range(1,n):
    if y > 0:
      y += a[i]
      if y < 0:
        continue
      else:
        x += abs(y+1)
        y = -1
    else:
      y += a[i]
      if y > 0:
        continue
      else:
        x += abs(y-1)
        y = 1
  return x
if a[0] == 0:
  ans3 = float("inf")
else:
  ans3 = f(ans3,a[0])
print(min(ans3,f(ans1,1),f(ans2,-1)))

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


def f(isPlus):
    sum = 0
    ret = 0
    for a in A:
        sum += a
        if isPlus:
            if sum <= 0:
                ret += abs(sum) + 1
                sum = 1
        else:
            if sum >= 0:
                ret += abs(sum) + 1
                sum = -1

        isPlus = not isPlus
    return ret


a1 = f(True)
a2 = f(False)
print(min(a1, a2))

Solution (Python):
N = int(input())
A = list(map(int, input().split(' ')))
ans1 = 0
ans2 = 0
sum1 = 0
sum2 = 0
flag = True
for i in range(len(A)):
    sum1 = sum1 + A[i]
    sum2 = sum2 + A[i]
    flag = not(flag)
    if flag:
        if sum1 > 0:
            pass
        else:
            ans1 += abs(1-sum1)
            sum1 = 1
        if sum2 < 0:
            pass
        else:
            ans2 += abs(-1-sum2)
            sum2 = -1
    else:
        if sum2 > 0:
            pass
        else:
            ans2 += abs(1-sum2)
            sum2 = 1
        if sum1 < 0:
            pass
        else:
            ans1 += abs(-1-sum1)
            sum1 = -1
print(min(ans1, ans2))

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
s=0
t=0
a=0
b=0
for i in range(N):
    s+=A[i]
    if i%2==0:
        if s>=0:
            a+=s+1
            s=-1
    if i%2!=0:
        if s<=0:
            a+=-s+1
            s=1
for j in range(N):
    t+=A[j]
    if j%2!=0:
        if t>=0:
            b+=t+1
            t=-1
    if j%2==0:
        if t<=0:
            b+=-t+1
            t=1
print(min(a,b))

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 6)
_ = int(input())
A = list(map(int, input().split()))


def solve(i, s, k, sign):
    if i >= len(A):
        return k

    if sign * (s + A[i]) > 0:
        return solve(i + 1, s + A[i], k, sign * -1)
    else:
        return solve(i + 1, sign, k + abs(s + A[i]) + 1, sign * -1)


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

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

ret = float('inf')

sign = 1
s = 0
k = 0
for a in A:
    if sign * (s + a) > 0:
        s += a
    else:
        k += abs(s + a) + 1
        s = sign
    sign *= -1
ret = min(ret, k)

sign = -1
s = 0
k = 0
for a in A:
    if sign * (s + a) > 0:
        s += a
    else:
        k += abs(s + a) + 1
        s = sign
    sign *= -1

ret = min(ret, k)


print(ret)

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):
def inpl(): return [int(i) for i in input().split()]
def minich(A, plus):
    ans = 0
    back = 0
    for i in A:
        if plus:
            if back + i < 0:
                back = back + i
            else:
                ans += back + i + 1
                back = -1
        else:
            if back + i > 0:
                back = back + i
            else:
                ans += 1 - back - i
                back = 1
        plus = not plus
    return ans
N = int(input())
A = inpl()
print(min(minich(A, True), minich(A, False)))

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

def calc(tmp, cnt=0):
    for i in a[1:]:
        if tmp < 0:
            tmp = tmp + i
            if tmp <= 0:
                cnt += -tmp + 1
                tmp = 1
        elif tmp > 0:
            tmp = tmp + i
            if tmp >= 0:
                cnt += tmp + 1
                tmp = -1
    return cnt

if a[0]:
    hugou = 1 if a[0] > 0 else -1
    print(min(calc(a[0]), calc(-hugou, abs(a[0]) + 1)))
else:
    print(min(calc(1, 1), calc(-1, 1)))

Solution (Python):
n=int(input().strip())
a=list(map(int, input().split()))
su=0
ans1=0
ans2=0
for i in range(n):
    if i%2==0:
        su+=a[i]
        if su<=0:
            ans1+=abs(su-1)
            su=1
    if i%2==1:
        su+=a[i]
        if su>=0:
            ans1+=abs(su+1)
            su=-1
su=0
for i in range(n):
    if i%2==0:
        su+=a[i]
        if su>=0:
            ans2+=abs(su+1)
            su=-1
    if i%2==1:
        su+=a[i]
        if su<=0:
            ans2+=abs(su-1)
            su=1

print(min(ans1,ans2))

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

Solution (Python):
n = int(input())
r = [int(i) for i in input().split()]
for i in range(1,n):
    r[i] += r[i-1]

a = r.copy()
ans_a = 0; change_v = 0
for i in range(n):
    a[i] += change_v
    if i%2 == 1:
        ans_a += max(0,1-a[i])
        change_v += max(0,1-a[i])
    else:
        ans_a += max(0,a[i]+1)
        change_v -= max(0,a[i]+1)

a = r.copy()
ans_b = 0; change_v = 0
for i in range(n):
    a[i] += change_v
    if i%2 == 0:
        ans_b += max(0,1-a[i])
        change_v += max(0,1-a[i])
    else:
        ans_b += max(0,a[i]+1)
        change_v -= max(0,a[i]+1)

print(min(ans_a,ans_b))