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())
arr=list(map(int,input().split()))
totalodd=0
totaleven=0
ansodd=0
anseven=0
for i in range(len(arr)):
    if i%2==0:      
        if totalodd+arr[i]>0:
            totalodd+=arr[i]
        else:
            ansodd+=1-(totalodd+arr[i])
            totalodd=1
        if totaleven+arr[i]<0:
            totaleven+=arr[i]
        else:
            anseven+=(totaleven+arr[i])+1
            totaleven=-1
    else:
        if totalodd+arr[i]<0:
            totalodd+=arr[i]
        else:
            ansodd+=(totalodd+arr[i])+1
            totalodd=-1
        if totaleven+arr[i]>0:
            totaleven+=arr[i]
        else:
            anseven+=1-(totaleven+arr[i])
            totaleven=1

print(min(ansodd,anseven))

Solution (Python):
n = int(input())
a = [int(i) for i in input().split(' ')]
_a = [-1*i for i in a]

def func(l):
    _ans = 0
    product = 0
    for i in range(n):
        product += l[i]
        if i%2 == 0:
            if product<=0:
                _ans += abs(product) + 1
                product = 1
        elif i%2 == 1:
            if product>=0:
                _ans += abs(product) + 1
                product = -1
    return _ans

ans = min(func(a), func(_a))
print(ans)

Solution (Python):
n = int(raw_input())
data = map(int, raw_input().split())
result = []
for i in range(2):
    sum = 0
    count = 0
    for j in range(n):
        sum += data[j]
        if (i+j) % 2 == 0:
            if sum <= 0:
                count += abs(1-sum)
                sum = 1
        else:
            if sum >= 0:
                count += abs(-1-sum)
                sum = -1
    result.append(count)
print min(result)

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

lastPos, lastNeg = l[0], l[0]
countPos, countNeg = 0, 0

if(lastPos<=0):
    countPos = 1 - lastPos
    lastPos = 1
if(lastNeg>=0):
    countNeg = lastNeg+1
    lastNeg = -1

for i in xrange(1,n):
    if(lastPos>0):
        
        if(lastPos+l[i] >= 0):
            diff = lastPos+l[i] + 1
            countPos+=diff
            lastPos = -1
        else:
            lastPos = lastPos + l[i]
    else:
        
        if(lastPos+l[i] <= 0):
            diff = 1 - (lastPos+l[i])
            countPos+=diff
            lastPos = 1
        else:
            lastPos = lastPos+l[i]
    
    if(lastNeg<0):
        
        if(lastNeg+l[i] <= 0):
            diff = 1 - (lastNeg+l[i])
            countNeg+= diff
            lastNeg = 1
        else:
            lastNeg = lastNeg + l[i]
    else:
        
        if(lastNeg+l[i] >= 0):
            diff = lastNeg+l[i]+1
            countNeg+= diff
            lastNeg = -1
        else:
            lastNeg = lastNeg + l[i]

print min(countPos, countNeg)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
_a = list(map(lambda x: x * (-1), a))


def func(l: list):
    _ans = 0
    product = 0
    for i in range(n):
        product += l[i]

        if i % 2 == 0:
            if product <= 0:
                _ans += abs(product) + 1
                product = 1

        elif i % 2 == 1:
            if product >= 0:
                _ans += abs(product) + 1
                product = -1
    return _ans

ans = min(func(a), func(_a))
print(ans)

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


def calcSteps(sign):
    res = 0
    s = 0
    for i in range(n):
        if (s+a[i])*sign < 0:
            s += a[i]
        else:
            step = -s -sign -a[i]
            res +=  abs(step)
            s = -sign
        sign *= -1
    return res



print(min(calcSteps(-1),calcSteps(1)))

Solution (Python):
n = int(raw_input())
a = map(int, raw_input().split())
ans = []
for k in range(2):
    s, cur = 0, 0
    for j in range(n):
        s += a[j]
        if (k+j) % 2 == 0:
            if s <= 0:
                cur += abs(1 - s)
                s = 1
        else:
            if s >= 0:
                cur += abs(-1 - s)
                s = -1
    ans.append(cur)
print min(ans)

Solution (Python):


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

counter_1=0
S=int(a[0])
for i in range(1,n):
  if S<0 and S+a[i]<=0:
    counter_1+=-S-a[i]+1
    a[i]=-S+1
  elif S>0 and S+a[i]>=0:
    counter_1+=S+a[i]+1
    a[i]=-S-1
  S+=a[i]

counter_2=abs(b[0]-1)
b[0]=1
S=b[0]
for i in range(1,n):
  if S<0 and S+b[i]<=0:
    counter_2+=-S-b[i]+1
    b[i]=-S+1
  elif S>0 and S+b[i]>=0:
    counter_2+=S+b[i]+1
    b[i]=-S-1
  S+=b[i]

counter_3=abs(c[0]+1)
c[0]=-1
S=c[0]
for i in range(1,n):
  if S<0 and S+c[i]<=0:
    counter_3+=-S-c[i]+1
    c[i]=-S+1
  elif S>0 and S+c[i]>=0:
    counter_3+=S+c[i]+1
    c[i]=-S-1
  S+=c[i]
print(min(counter_1,counter_2,counter_3))

Solution (Python):


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

counter_1=0
S=int(a[0])
for i in range(1,n):
  if S<0 and S+int(a[i])<=0:
    counter_1+=-S-int(a[i])+1
    a[i]=-S+1
  elif S>0 and S+int(a[i])>=0:
    counter_1+=S+int(a[i])+1
    a[i]=-S-1
  S+=int(a[i])

counter_2=abs(int(b[0])-1)
b[0]=1
S=b[0]
for i in range(1,n):
  if S<0 and S+int(b[i])<=0:
    counter_2+=-S-int(b[i])+1
    b[i]=-S+1
  elif S>0 and S+int(b[i])>=0:
    counter_2+=S+int(b[i])+1
    b[i]=-S-1
  S+=int(b[i])

counter_3=abs(int(c[0])+1)
c[0]=-1
S=c[0]
for i in range(1,n):
  if S<0 and S+int(c[i])<=0:
    counter_3+=-S-int(c[i])+1
    c[i]=-S+1
  elif S>0 and S+int(c[i])>=0:
    counter_3+=S+int(c[i])+1
    c[i]=-S-1
  S+=int(c[i])
print(min(counter_1,counter_2,counter_3))

Solution (Python):


n=int(input())
a=input().split()
b=a[:]
c=a[:]

counter_1=0
S=int(a[0])
for i in range(1,n):
  if S<0 and S+int(a[i])<=0:
    counter_1+=-S-int(a[i])+1
    a[i]=-S+1
  elif S>0 and S+int(a[i])>=0:
    counter_1+=S+int(a[i])+1
    a[i]=-S-1
  S+=int(a[i])

counter_2=abs(int(b[0])-1)
b[0]=1
S=b[0]
for i in range(1,n):
  if S<0 and S+int(b[i])<=0:
    counter_2+=-S-int(b[i])+1
    b[i]=-S+1
  elif S>0 and S+int(b[i])>=0:
    counter_2+=S+int(b[i])+1
    b[i]=-S-1
  S+=int(b[i])

counter_3=abs(int(c[0])+1)
c[0]=-1
S=c[0]
for i in range(1,n):
  if S<0 and S+int(c[i])<=0:
    counter_3+=-S-int(c[i])+1
    c[i]=-S+1
  elif S>0 and S+int(c[i])>=0:
    counter_3+=S+int(c[i])+1
    c[i]=-S-1
  S+=int(c[i])
print(min(counter_1,counter_2,counter_3))

Solution (Python):

def check1(a):
  sum = 0
  ans = 0
  for i in range(len(a)):
    if(i % 2 == 0):
      sum += a[i]
      if(sum >= 0):
        a[i] -= abs(sum)+1
        ans += abs(sum)+1
        sum -= abs(sum)+1
    else:
      sum += a[i]
      if(sum <= 0):
        a[i] += abs(sum)+1
        ans += abs(sum)+1
        sum += abs(sum)+1
  return ans

def check2(a):
  sum = 0
  ans = 0
  for i in range(len(a)):
    if(i % 2 == 0):
      sum += a[i]
      if(sum <= 0):
        a[i] += abs(sum)+1
        ans += abs(sum)+1
        sum += abs(sum)+1
    else:
      sum += a[i]
      if(sum >= 0):
        a[i] -= abs(sum)+1
        ans += abs(sum)+1
        sum -= abs(sum)+1
  return ans



n = input()
b = input().split()
a = [int(b[i]) for i in range(len(b))]
a2 = list(a)

ans1 = check1(a)
ans2 = check2(a2)


print(ans1) if(ans1<ans2) else print(ans2)

Solution (Python):
n = int(raw_input())
l = map(int, raw_input().split())
sp = 0
p = 0
sm = 0
m = 0
pflg = True
for i in l:
    sp += i
    if pflg and sp <= 0:
        p += abs(sp) + 1
        sp = 1
    elif not pflg and sp >= 0:
        p += abs(sp) + 1
        sp = -1
    sm += i
    if pflg and sm >= 0:
        m += abs(sm) + 1
        sm = -1
    elif not pflg and sm <= 0:
        m += abs(sm) + 1
        sm = 1
    pflg = not pflg
print min(p,m)

Solution (Python):
def count(seq, is_positive):
    operation = 0
    sum = 0
    for i in range(n):
        sum += seq[i]
        if sum == 0:
            operation += 1
            if is_positive:
                sum -= 1
            else:
                sum += 1
        elif sum > 0 and is_positive:
            operation += abs(sum) + 1
            sum = -1
        elif sum < 0 and not is_positive:
            operation += abs(sum) + 1
            sum = 1

        if sum > 0:
            is_positive = True
        else:
            is_positive = False

    return operation

if __name__ == "__main__":
    n = input()
    seq = map(int, raw_input().split(" "))

    print min(count(seq, False), count(seq, True))

Solution (Python):
import sys

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

    ans1 = scan(a, -1)
    ans2 = scan(a, 1)

    ans = min(ans1, ans2)

    print(ans)

def scan(a, sgn):
    if a[0] * sgn > 0:
        res = 0
        v = a[0]
    else:
        res = abs(a[0] - sgn)
        v = sgn

    for ai in a[1:]:
        if v * (v + ai) < 0:
            v += ai
        else:
            if v > 0:
                res += abs(v + ai + 1)
                v = -1
            else:
                res += abs(v + ai - 1)
                v = 1

    return res

if __name__ == '__main__':
    solve()

Solution (Python):
import sys

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

    ans1 = 0 if a[0] > 0 else abs(a[0] - 1)
    v = a[0] if a[0] > 0 else 1

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

    ans2 = 0 if a[0] < 0 else abs(a[0] + 1)
    v = a[0] if a[0] < 0 else -1

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

    ans = min(ans1, ans2)

    print(ans)

if __name__ == '__main__':
    solve()

Solution (Python):


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

ans = 1e16
for s in (1, -1):
    res, cumsum = 0, 0
    for j in range(n):
        cumsum += a[j]
        if cumsum * s <= 0:
            res += abs(cumsum-s)
            cumsum = s
        s *= -1
    ans = min(ans, res)

print(ans)

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

ans = 1e16
for s in (1, -1):
    res, acc = 0, 0
    for a in A:
        acc += a
        if acc * s <= 0:
            res += abs(acc-s)
            acc = s
        s *= -1
    ans = min(ans, res)

print(ans)

Solution (Python):


def count(seq, is_positive):
    operation = 0
    sum = 0
    for i in range(n):
        sum += seq[i]
        if sum == 0:
            operation += 1
            if is_positive:
                sum -= 1
            else:
                sum += 1
        elif sum > 0 and is_positive:
            operation += abs(sum) + 1
            sum = -1
        elif sum < 0 and not is_positive:
            operation += abs(sum) + 1
            sum = 1

        if sum > 0:
            is_positive = True
        else:
            is_positive = False

    return operation


if __name__ == "__main__":
    n = int(raw_input())
    seq = map(int, raw_input().split(" "))

    print min(count(seq, False), count(seq, True))

Solution (Python):
def read(): return list(map(int, input().split()))


n = int(input())
a = read()

ans = 1145141919810893
for j in range(2):
    sum, cur = 0, 0
    for i, v in enumerate(a):
        plus = (i % 2 == j % 2)
        sum += v
        if plus and sum > 0: continue
        if not plus and sum < 0: continue

        if plus:
            cur += 1 - sum
            sum = 1
        else:
            cur += sum + 1
            sum = -1

    ans = min(ans, cur)

print(ans)

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

m_count = 0
p_count = 0

s = [0]*n
s[0] = a[0]


if a[0] >= 0:
    tmp = -a[0] -1
    s[0] += tmp
    m_count += abs(tmp)

for i in range(1,n):
    s[i] = s[i-1] + a[i]

    if s[i] == 0:
        tmp = -s[i-1]//abs(s[i-1])
        s[i] += tmp
        m_count += abs(tmp)
    elif s[i] * s[i-1] > 0:
        tmp = (-s[i]) + (-s[i]//abs(s[i]))
        s[i] += tmp
        m_count += abs(tmp)


s[0] = a[0]

if a[0] <= 0:
    tmp = -a[0] +1
    s[0] += tmp
    p_count += abs(tmp)

for i in range(1,n):
    s[i] = s[i-1] + a[i]

    if s[i] == 0:
        tmp = -s[i-1]//abs(s[i-1])
        s[i] += tmp
        p_count += abs(tmp)
    elif s[i] * s[i-1] > 0:
        tmp = (-s[i]) + (-s[i]//abs(s[i]))
        s[i] += tmp
        p_count += abs(tmp)

print(min(p_count,m_count))

Solution (Python):
n = int(input())
b = [int(x) for x in input().split()]
a = list()
temp = 0
count1 = 0
count2 = 0

a = b.copy()

if a[0] == 0:
	a[0] = 1
	count1 = 1

sum = a[0]
for i in range(1, n):
	if abs(a[i]) <= abs(sum) or a[i] * sum >= 0:
		if sum > 0:
			temp = -1 * abs(sum) - 1
		else:
			temp = abs(sum) + 1
		count1 += abs(temp - a[i])
		a[i] = temp
	sum += a[i]

a = b.copy()

count2 = abs(a[0]) + 1
if a[0] >= 0:
	a[0] = -1
else:
	a[0] = 1
	
sum = a[0]
for i in range(1, n):
	if abs(a[i]) <= abs(sum) or a[i] * sum >= 0:
		if sum > 0:
			temp = -1 * abs(sum) - 1
		else:
			temp = abs(sum) + 1
		count2 += abs(temp - a[i])
		a[i] = temp
	sum += a[i]

print(min(count1, count2))

Solution (Python):


n = input()
a = map(int, raw_input().split())
b = a
asum = [0]*len(a)
bsum = [0]*len(a)

asum[0] = a[0]
bsum[0] = b[0]
cnt_a = 0
cnt_b = 0
if(a[0]==0):
    asum[0] = 1
    cnt_a = 1
    bsum[0] = -1
    cnt_b = 1

if(a[0]<0):
    cnt_a += (-1)*a[0]+1
    asum[0] = 1
for i in range(len(a)-1):
    asum[i+1] = a[i+1] + asum[i]
    if(i%2==0 and asum[i+1]>=0):
        cnt_a += asum[i+1]+1
        asum[i+1] = -1
    elif(i%2==1 and asum[i+1]<=0):
        cnt_a += (-1)*asum[i+1]+1
        asum[i+1] = 1
if(b[0]>0):
    cnt_b += b[0]+1
    bsum[0] = -1
for i in range(len(b)-1):
    bsum[i+1] = b[i+1] + bsum[i]
    if(i%2==0 and bsum[i+1]<=0):
        cnt_b += (-1)*bsum[i+1]+1
        bsum[i+1] = 1
    elif(i%2==1 and bsum[i+1]>=0):
        cnt_b += bsum[i+1]+1
        bsum[i+1] = -1
print(min(cnt_a,cnt_b))

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

def judge_pm(a,b):
    if a*b<0:
        return True
    else:
        return False

a1 = copy.deepcopy(a)
operate_num1 = 0
if a1[0]==0:
    a1[0] = 1
    operate_num1 += 1
tmp_sum = a1[0]
for i in range(1, n):
    if judge_pm(tmp_sum, tmp_sum+a1[i]):
        pass 
    elif tmp_sum<0:
        tmp_operate_num =  - tmp_sum + 1 - a1[i]
        operate_num1 += tmp_operate_num
        a1[i] += tmp_operate_num
    else:
        tmp_operate_num = tmp_sum + 1 + a1[i]
        operate_num1 += tmp_operate_num
        a1[i] -= tmp_operate_num
    tmp_sum += a1[i]
        
a2 = copy.deepcopy(a)
operate_num2 = 0
if a2[0]<0:
    operate_num2 += 1 - a2[0]
    a2[0] += operate_num2
else:
    operate_num2 += a2[0] + 1
    a2[0] -= operate_num2

tmp_sum = a2[0]

for i in range(1, n):
    if judge_pm(tmp_sum, tmp_sum+a2[i]):
        pass 
    elif tmp_sum<0:
        tmp_operate_num =  - tmp_sum + 1 - a2[i]
        operate_num2 += tmp_operate_num
        a2[i] += tmp_operate_num
    else:
        tmp_operate_num = tmp_sum + 1 + a2[i]
        operate_num2 += tmp_operate_num
        a2[i] -= tmp_operate_num
    tmp_sum += a2[i]

print(min(operate_num1, operate_num2))

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

i = i.split()
for item in range(len(i)):
    i[item] = int(i[item])

totn = 0
totp = 0

countp = 0
countn = 0

for x in range(len(i)):
    totp += i[x]
    totn += i[x]

    if x %2 == 1:
        if totn == 0:
            countn += 1
            totn = 1
        elif totn < 0:
            countn += abs(totn) + 1
            totn = 1

        if totp == 0:
            countp += 1
            totp = -1
        elif totp > 0:
            countp += abs(totp) + 1
            totp = -1

    if x %2 == 0:
        if totn == 0:
            countn += 1
            totn = -1
        elif totn > 0:
            countn += abs(totn) + 1
            totn = -1

        if totp == 0:
            countp += 1
            totp = 1
        elif totp < 0:
            countp += abs(totp) + 1
            totp = 1

count = min(countn, countp)
print(count)

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

i = i.split()
for item in range(len(i)):
    i[item] = int(i[item])

totn = 0
totp = 0

countp = 0
countn = 0

for x in range(len(i)):
    totp += i[x]
    totn += i[x]

    if x %2 == 1:
        if totn == 0:
            countn += 1
            totn = 1
        elif totn < 0:
            countn += abs(totn) + 1
            totn = 1

        if totp == 0:
            countp += 1
            totp = -1
        elif totp > 0:
            countp += abs(totp) + 1
            totp = -1

    if x %2 == 0:
        if totn == 0:
            countn += 1
            totn = -1
        elif totn > 0:
            countn += abs(totn) + 1
            totn = -1

        if totp == 0:
            countp += 1
            totp = 1
        elif totp < 0:
            countp += abs(totp) + 1
            totp = 1

count = min(countn, countp)
print(count)

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

i = i.split()
for item in range(len(i)):
    i[item] = int(i[item])

totn = 0
totp = 0

countp = 0
countn = 0

for x in range(len(i)):
    totp += i[x]
    totn += i[x]
    
    if x == 0:
        if totp == 0:   
            totn = -1
            countn = 1

            totp = 1
            countp = 1
            
        elif totp < 0:
            countp = abs(totp) + 1
            totp = 1

        elif totp > 0:
            countn = abs(totn) + 1
            totn = -1

    elif x %2 == 1:
        if totn == 0:
            countn += 1
            totn = 1
        elif totn < 0:
            countn += abs(totn) + 1
            totn = 1

        if totp == 0:
            countp += 1
            totp = -1
        elif totp > 0:
            countp += abs(totp) + 1
            totp = -1

    elif x %2 == 0:
        if totn == 0:
            countn += 1
            totn = -1
        elif totn > 0:
            countn += abs(totn) + 1
            totn = -1

        if totp == 0:
            countp += 1
            totp = 1
        elif totp < 0:
            countp += abs(totp) + 1
            totp = 1
    '''print('totn', totn)
    print('countn', countn)
    print('totp', totp)
    print('countp', countp) '''   
count = min(countn, countp)
print(count)

Solution (Python):

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


total = 0
sign = -1
nega_cost = 0
for tmp in range(len(a)):
    total += a[tmp]
    if total * sign <= 0:
        nega_cost += abs(total*sign)+1
        total = sign
    sign *= -1


total = 0
sign = 1
posi_cost = 0
for tmp in range(len(a)):
    total += a[tmp]
    if total * sign <= 0:
        posi_cost += abs(total*sign)+1
        total = sign
    sign *= -1

print(min(nega_cost, posi_cost))

Solution (Python):
if __name__ == '__main__':
    n = int(input())
    a0 = input().split()
    a = [int(i) for i in a0]
    s1 = 0
    s2 = 0
    m1 = 0
    m2 = 0
    o = 1
    e = -1
    for i in a:
        s1 += i
        if s1 == 0:
            m1 += 1
            s1 = o
        elif o * s1 < 0:
            m1 += abs(s1) + 1
            s1 = o
        o *= -1

        s2 += i
        if s2 == 0:
            m2 += 1
            s2 = e
        elif e * s2 < 0:
            m2 += abs(s2) + 1
            s2 = e
        e *= -1
    print(min(m1, m2))

Solution (Python):
n = int(input())
sa = input().split()

a = [0 for i in range(n)]
sumeven = 0
sumodd = 0
for i,sai in enumerate(sa):
    a[i] = int(sai)

    if i % 2 == 0:
        sumeven += a[i]
    else:
        sumodd += a[i]

ret = 0
sm = 0
for i,ai in enumerate(a):
    if i % 2 == 1:
        ret += max(-sm+1-ai,0)
        sm += max(ai, -sm+1)
    else:
        ret += max(sm+1 + ai, 0)
        sm += min(ai, -(sm+1))
ret2 = 0
sm = 0
for i,ai in enumerate(a):
    if i % 2 == 0:
        ret2 += max(-sm+1-ai,0)
        sm += max(ai, -sm+1)
    else:
        ret2 += max(sm+1 + ai, 0)
        sm += min(ai, -(sm+1))
        
print(min(ret,ret2))

Solution (Python):
import itertools

def sign(num):
    if num < 0:
        return -1
    elif num > 0:
        return 1
    else:
        return 0

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

signs = [-1,1]
a_sum = 0
changes_1 = 0
for i, a in enumerate(a_i):
    a_sum += a
    if sign(a_sum) != signs[i%2]:
        changes_1 += abs(a_sum) + 1
        a_sum = signs[i%2]

signs = [1,-1]
a_sum = 0
changes_2 = 0
for i, a in enumerate(a_i):
    a_sum += a
    if sign(a_sum) != signs[i%2]:
        changes_2 += abs(a_sum) + 1
        a_sum = signs[i%2]

print(min(changes_1,changes_2))

Solution (Python):
def c(ints):
        
    sig = -1
    sig_ = +1
    total = 0
    total_ = 0
    mov = 0
    mov_ = 0
    
    for i_ in ints:
        tmp = total + i_
        tmp_ = total_ + i_
        if tmp == 0:
            mov +=1
            tmp = -sig
        elif sig * tmp > 0:
            mov += abs(tmp) + 1
            tmp = -sig
            
        if tmp_ == 0:
            mov_ +=1
            tmp_ = -sig_
        elif sig_ * tmp_ > 0:
            mov_ += abs(tmp_) + 1
            tmp_ = -sig_
            
        sig *= -1
        total = tmp
        sig_ *= -1
        total_ = tmp_

    return min(mov, mov_)

_ = input()
inp = input()
inp = inp.split(' ')
inp = [int(i_) for i_ in inp]
print(c(inp))

Solution (Python):
import sys
import math

INF = 10**9+7

def k(i):
    if(i == 1):
        return 1
    else:
        return(i * k(i-1))

def comb(n, r):
    if(n == r or r == 1):
        return 1
    else:
        return k(n) / (k(n-r) * k(r))

stdin = sys.stdin
def na(): return map(int, stdin.readline().split())
def ns(): return stdin.readline().strip()
def nsl(): return list(stdin.readline().strip())
def ni(): return int(stdin.readline())
def nil(): return list(map(int, stdin.readline().split()))

n = ni()
a = nil()

b = []

for i in range(n):
    b.append(a[i])


sum = 0

c1 = 0
c2 = 0

if a[0] == 0:
    a[0] = 1
    c1 += 1;

for i in range(0, n-1):
    sum += a[i]

    sum2 = sum + a[i+1]

    if sum2 == 0:
        if sum >0:
            a[i+1] -= 1
            sum2 -= 1
            c1 +=1

    if(sum * sum2 >= 0):
        k = abs(sum2) + 1

        h = k - (abs(sum) - 1)
        l = k - h

        if sum > 0 :
            a[i] -= l
            sum -= l
            a[i + 1] -= h
        else:
            a[i] += l
            sum += l
            a[i + 1] += h

        c1 += h+l


sum = 0

a = b

if a[0] == 0:
    a[0] = -1
    c2 += 1;
else:
    c2 = abs(a[0]) + 1
    if a[0] > 0:
        a[0] = -1
    else:
        a[0] = 1

for i in range(0, n-1):
    sum += a[i]

    sum2 = sum + a[i+1]

    if sum2 == 0:
        if sum >0:
            a[i+1] -= 1
            sum2 -= 1
            c2 +=1

    if(sum * sum2 >= 0):
        k = abs(sum2) + 1

        h = k - (abs(sum) - 1)
        l = k - h

        if sum > 0 :
            a[i] -= l
            sum -= l
            a[i + 1] -= h
        else:
            a[i] += l
            sum += l
            a[i + 1] += h

        c2 += k

print(min(c1, c2))

Solution (Python):

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

count_a = 0 
count_b = 0 

nowsum = a[0]
if nowsum != 0:
    for n in range(1, N):
        if nowsum * (nowsum + a[n]) >= 0:
            count_a += abs(nowsum + a[n]) + 1
            if nowsum < 0:
                nowsum = 1
            else:
                nowsum = -1
        else:
            nowsum += a[n]

    count_b += abs(a[0]) + 1
    nowsum = int(a[0] / abs(a[0]) * -1)

    for n in range(1, N):
        if nowsum * (nowsum + a[n]) >= 0:
            count_b += abs(nowsum + a[n]) + 1
            if nowsum < 0:
                nowsum = 1
            else:
                nowsum = -1
        else:
            nowsum += a[n] 

    print(min(count_a, count_b))

else:
    a[0] = 1
    count_a += 1
    nowsum = 1
    for n in range(1, N):
        if nowsum * (nowsum + a[n]) >= 0:
            count_a += abs(nowsum + a[n]) + 1
            if nowsum < 0:
                nowsum = 1
            else:
                nowsum = -1
        else:
            nowsum += a[n]
    
    a[0] = -1
    count_b += 1
    nowsum = -1
    for n in range(1, N):
        if nowsum * (nowsum + a[n]) >= 0:
            count_b += abs(nowsum + a[n]) + 1
            if nowsum < 0:
                nowsum = 1
            else:
                nowsum = -1
        else:
            nowsum += a[n]   
    print(min(count_a, count_b))

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

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

    
    sign = 1
    S = 0
    cost = 0
    for a in A:
        S += a
        if S * sign > 0:
            pass
        else:
            cost += abs(S) + 1
            S = sign
        sign *= -1
    p = cost

    
    sign = -1
    S = 0
    cost = 0
    for a in A:
        S += a
        if S * sign > 0:
            pass
        else:
            cost += abs(S) + 1
            S = sign
        sign *= -1
    q = cost

    print(min(p,q))


if __name__ == "__main__":
    main()

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

ans1 = ans2 = 0
s = s2 = a[0]
if a[0] <= 0:
    s = 1
    ans1 = -a[0] + 1
if a[0] >= 0:
    s2 = -1
    ans2 = a[0] + 1

for i in range(1, len(a)):
    s += a[i]
    if i % 2 == 1 and s >= 0:
        ans1 += s + 1
        s = -1
    elif i % 2 == 0 and s <= 0:
        ans1 += -s + 1
        s = 1
    s2 += a[i]
    if i % 2 == 0 and s2 >= 0:
        ans2 += s2 + 1
        s2 = -1
    elif i % 2 == 1 and s2 <= 0:
        ans2 += -s2 + 1
        s2 = 1

print(min(ans1, ans2))

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

count = 0
sum_i = a[0]

if a[0] < 0:
    sum_i = 1
    pos_count = 1 - a[0]
    for i in range(1, n):
        if sum_i < 0:
            sum_i += a[i]
            if sum_i <= 0:
                pos_count += 1 - sum_i
                sum_i = 1
        elif sum_i > 0:
            sum_i += a[i]
            if sum_i >= 0:
                pos_count += sum_i + 1
                sum_i = -1
    sum_i = a[0]
    neg_count = 0
    for i in range(1, n):
        if sum_i < 0:
            sum_i += a[i]
            if sum_i <= 0:
                neg_count += 1 - sum_i
                sum_i = 1
        elif sum_i > 0:
            sum_i += a[i]
            if sum_i >= 0:
                neg_count += sum_i + 1
                sum_i = -1
    if neg_count < pos_count:
        count = neg_count
    else:
        count = pos_count
elif a[0] > 0:
    sum_i = a[0]
    pos_count = 0
    for i in range(1, n):
        if sum_i < 0:
            sum_i += a[i]
            if sum_i <= 0:
                pos_count += 1 - sum_i
                sum_i = 1
        elif sum_i > 0:
            sum_i += a[i]
            if sum_i >= 0:
                pos_count += sum_i + 1
                sum_i = -1
    sum_i = -1
    neg_count = 1 + a[0]
    for i in range(1, n):
        if sum_i < 0:
            sum_i += a[i]
            if sum_i <= 0:
                neg_count += 1 - sum_i
                sum_i = 1
        elif sum_i > 0:
            sum_i += a[i]
            if sum_i >= 0:
                neg_count += sum_i + 1
                sum_i = -1
    if neg_count < pos_count:
        count = neg_count
    else:
        count = pos_count
else:
    sum_i = 1
    pos_count = 1
    for i in range(1, n):
        if sum_i < 0:
            sum_i += a[i]
            if sum_i <= 0:
                pos_count += 1 - sum_i
                sum_i = 1
        elif sum_i > 0:
            sum_i += a[i]
            if sum_i >= 0:
                pos_count += sum_i + 1
                sum_i = -1
    sum_i = -1
    neg_count = 1
    for i in range(1, n):
        if sum_i < 0:
            sum_i += a[i]
            if sum_i <= 0:
                neg_count += 1 - sum_i
                sum_i = 1
        elif sum_i > 0:
            sum_i += a[i]
            if sum_i >= 0:
                neg_count += sum_i + 1
                sum_i = -1
    if neg_count < pos_count:
        count = neg_count
    else:
        count = pos_count

print(count)

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

values = []
for s in (1, -1):
    xs = a[::]
    signs = [s * (-1) ** i for i in range(N)]
    total = 0
    for i in range(N):
        if (total + xs[i]) * signs[i] <= 0:
            xs[i] = signs[i] - total
        total += xs[i]
    values.append(sum(abs(a[i] - xs[i]) for i in range(N)))
print(min(values))

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

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