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



Sample input:
4
1 -3 1 0


Sample output:
4

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Sep  8 15:51:53 2018

@author: maezawa
"""
def f(n, a0, cnt, sa, sign):
    a = a0[:]
    if sign == -1:
        a[0] = -a[0]
        cnt += 2*a[0]
    for i in range(n-1):
        sa += a[i]
        na = -sa//abs(sa)*(abs(sa)+1)
        if abs(a[i+1]) > abs(na) and a[i+1]*na > 0:
            continue
        else:
            cnt += abs(na-a[i+1])
            a[i+1] = na
    return cnt

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

cnt0 = f(n, a, cnt, sa, -1)

cnt1 = f(n, a, cnt, sa, 1)
cnt = min([cnt0,cnt1])
print(cnt)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Sep  8 15:51:53 2018

@author: maezawa
"""

n = int(input())
a = list(map(int, input().split()))
sa = 0
cnt = 0
for i in range(0,n-1):
    sa += a[i]
    na = -sa//abs(sa)*(abs(sa)+1)
    if abs(a[i+1]) > abs(na) and a[i+1]*na > 0:
        continue
    else:
        cnt += abs(na-a[i+1])
        a[i+1] = na

print(cnt)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Sep  8 15:51:53 2018

@author: maezawa
"""

n = int(input())
a = list(map(int, input().split()))
sa = 0
cnt = 0
for i in range(0,n-1):
    sa += a[i]
    na = -sa//abs(sa)*(abs(sa)+1)
    if abs(a[i+1]) > abs(na) and a[i+1]*na > 0:
        continue
    cnt += abs(na-a[i+1])
    a[i+1] = na

print(cnt)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Sep  8 15:51:53 2018

@author: maezawa
"""

n = int(input())
a = list(map(int, input().split()))
sa = 0
cnt = 0
for i in range(0,n-1):
    sa += a[i]
    na = -sa//abs(sa)*(abs(sa)+1)
    if abs(a[i+1]) > abs(na) and a[i+1]*na >= 0:
        continue
    cnt += abs(na-a[i+1])
    a[i+1] = na

print(cnt)

Submission status: Runtime Error

Solution code:
import numpy as np

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

def calc(a):
    value = a[0]
    sign = value >= 0
    result = 0
    for i in a[1:]:
        value += i
        diff = 0
        if sign and value >= 0:
            diff = value + 1
        elif not sign and value < 0:
            diff = value - 1
        result += abs(diff)
        sign = not sign
        value -= diff
        if value == 0:
            diff += sign
            value += diff
            result += diff
    return result

r1 = calc(a[:])
diff = -a[0] - a[0] // abs(a[0])
a[0] += diff
r2 = calc(a[:]) + abs(diff)
print(min(r1, r2))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-

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

sum1 = aN[0]
cnt = 0
if aN[0] > 0:
    sei = True
elif aN[0] < 0:
    sei = False
else:
    print("和が0なので、これはないはず")

for i in range(1,N):
    sum1 += aN[i]
    if sei:
        if sum1 >= 0:
            tmp = abs(sum1) +1
            cnt += tmp
            sum1 -= tmp
        sei = False
    else:
        if sum1 <= 0:
            tmp = abs(sum1) +1
            cnt += tmp
            sum1 += tmp
        sei = True
print(cnt)

Submission status: Runtime Error

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

now=a[0]
flag=abs(a[0])//a[0]
c=0
#print(c,flag)

for i in range(1,n):
    tmp=now+a[i]
    if not tmp*flag<0:
        c+=abs(flag*-1-tmp)
        now=flag*-1
    else:
        now=tmp
    flag*=-1
    #print(c,flag)

print(c)

Submission status: Runtime Error

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

# 偶数番目が＋
A_even = A
S_even = 0
ans_even = 0
for i in range(n):
    if (S + A_even[i]) * ((-1) ** (i - 1)) > 0:
        continue
    ans_even += S + A_even[i] + 1
    A_even[i] = (-1) ** (i - 1) - S
    S = (-1) ** (i - 1)

# 奇数番目が＋
A_odd = A
S_odd = 0
ans_odd = 0
for i in range(n):
    if (S + A_odd[i]) * ((-1) ** i) > 0:
        continue
    ans_odd += S + A_odd[i] + 1
    A_odd[i] = (-1) ** i - S
    S = (-1) ** i

ans = min(ans_even, ans_odd)

print(ans)

Submission status: Runtime Error

Solution code:
import copy
n = int(input())
a = [int(ai) for ai in input().split()]

def search(a, flip=False):
    if flip:
        count = 0
        a_sum = a[0]
    else:
        count = abs(a[0]) + 1
        a_sum = - a[0]
    for ai in a[1:]:
        next_sum = a_sum + ai
        if next_sum * a_sum < 0:
            continue
        if a_sum < 0:
            a_sum = 1
        elif a_sum > 0:
            a_sum = -1
        else:
            c = 0
        count += abs(next_sum) + 1
        a_sum = next_sum + c
    return count

print(min(search(a, False), search(a, True))

Submission status: Runtime Error

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

def find(a):
	f = "+" if a[0] < 0 else "-"
	s = a[0]
	c = 0
	#print((s,f,c))
	for i in range(1,n):
		if f == "+" and s + a[i] > 0:
			f = "-"
			s = s + a[i]
	#		print((s,f,c))
		elif f == "+":
			c = c + abs(s + a[i])+1
			f = "-"
			s = 1
	#		print((s,f,c))
		elif f == "-" and s + a[i] < 0:
			f = "+"
			s = s + a[i]
	#		print((s,f,c))
		else:
			c = c + abs(s+a[i])+1
			f = "+"
			s = -1
	return c

if a[0] = 0:
	a[0] = -1
	cmin = find(a)
	a[0] = 1
	cmax = find(a)
	c = min(cmin,cmax)
else:
	c = find(a)
print(c)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = a
#1 = plus
cp = 0
cm = 0
sumscp = 0
sumscm = 0

for i in range(len(a)):
    
    if i % 2 == 0:
        while sumscp + a[i] < 1:
            a[i] = a[i] + 1
            cp = cp + 1
        sumscp = sumscp + a[i]
    else:
        while sums + a[i] > -1:
            a[i] = a[i] - 1
            cp = cp + 1
        sumscp = sumscp + a[i]
        
for i in range(len(b)):
    
    if i % 2 == 0:
        while sumscm + b[i] > -1:
            b[i] = b[i] - 1
            cm = cm + 1
        sumscm = sumscm + b[i]
    else:
        while sumscm + a[i] < 1:
            b[i] = b[i] + 1
            cm = cm + 1
        sumscm = sumscm + b[i]
    
print(min(cp,cm))

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = a
#1 = plus
cp = 0
cm = 0
sumscp = 0
sumscm = 0

for i in range(len(a)):
    
    if i % 2 == 0:
        while sumscp + a[i] < 1:
            a[i] = a[i] + 1
            cp = cp + 1
        sumscp = sumscp + a[i]
    else:
        while sums + a[i] > -1:
            a[i] = a[i] - 1
            cp = cp + 1
        sumscp = sumscp + a[i]
        
for i in range(len(b)):
    
    if i % 2 == 0:
        while sumscp + b[i] > -1:
            b[i] = b[i] - 1
            cm = cm + 1
        sumscm = sumscm + b[i]
    else:
        while sums + a[i] < 1:
            b[i] = b[i] + 1
            cm = cm + 1
        sumscm = sumscm + b[i]
    
print(min(cp,cm))

Submission status: Runtime Error

Solution code:
N=int(input())
s=list(map(int,input().split()))
if s[0]<0:
    t=1
elif s[0]>0:
    t=-1
ss=s[0]   
w=0
for i in range(N-1):
    if t==1:
        if ss+s[i+1]>=t:
            ss=ss+s[i+1]
            pass
        else:
            w+=t-ss-s[i+1]
            ss=1
        t=-1
    elif t==-1:
        if ss+s[i+1]<=t:
            ss=ss+s[i+1]
            pass
        else:
            w+=ss+s[i+1]-t
            ss=-1
        t=1
print(w)

Submission status: Runtime Error

Solution code:
n = int(input())
a_p = list(map(int,input().split()))
a_n = a_p

#positive
if a[0] <= 0:
    counter_p = 0
    for i in range(n):
        print(a_p,counter_p)
        sum_ai = sum(a_p[:i+1])
        if i % 2 == 0:
            if sum_ai <= 0:
                a_p[i] += -sum_ai + 1
                counter_p += -sum_ai + 1
        else:
            if sum_ai >= 0:
                a_p[i] += -sum_ai - 1
                counter_p += sum_ai + 1
    print(counter_p)
else:
    #negative
    counter_n = 0
    for i in range(n):
        print(a_n,counter_n)
        sum_ai = sum(a_n[:i+1])
        if i % 2 == 1:
            if sum_ai <= 0:
                a_n[i] += -sum_ai + 1
                counter_n += -sum_ai + 1
        else:
            if sum_ai >= 0:
                a_n[i] += -sum_ai - 1
                counter_n += sum_ai + 1
    print(counter_n)

print(min(counter_n,counter_p))

Submission status: Runtime Error

Solution code:
n, x = map(int, input().split())
l = list(map(int, input().split()))
ans = 0
for i in range(1, n):
  if l[i] + l[i-1] > x:
    ans += (l[i] + l[i-1]) - x
    l[i] -= (l[i] + l[i-1]) - x
print(ans)

Submission status: Runtime Error

Solution code:
def ops_needed(x, target):
    if abs(target - x) < abs(x):
        return 0
    else:
        return target - x

def count_ops(a):
    total_ops = 0
    accum = a[0]
    for i in range(1, len(a)):
        new_accum = accum + a[i]
        if new_accum == 0:
            ops =  -1 * (accum / abs(accum))
        else:
            ops = ops_needed(new_accum, -1 * (accum / abs(accum)))
        total_ops += abs(ops)
        accum = new_accum + ops
    return int(total_ops)
        

if __name__ == '__main__':
    _ = int(input())
    a = list(map(int, input().split()))
    print(count_ops(a))

Submission status: Runtime Error

Solution code:
def count_ops(a):
    if a[0] == 0:
        total_ops = 1
        if a[1] < 0:
            accum = 1
        else:
            accum = -1
    else:
        total_ops = 0
        accum = a[0]
    for i in range(1, len(a)):
        new_accum = accum + a[i]
        if (accum > 0 and new_accum > 0):
            ops = ops_needed(new_accum, -1)
        elif (accum > 0 and new_accum == 0):
            ops = -1
        elif (accum < 0 and new_accum < 0):
            ops = ops_needed(new_accum, 1)
        elif (accum < 0 and new_accum == 0):
            ops = 1
        else:
            ops = 0
        total_ops += abs(ops)
        accum = new_accum + ops
    return total_ops
        

if __name__ == '__main__':
    _ = int(input())
    a = list(map(int, input().split()))
    print(count_ops(a))

Submission status: Runtime Error

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
num,answer = a[0],0
for i in range(1,n):
	if num*(num+a[i]) >= 0:
		x = (-num)//abs(num)
		answer += abs((x-num)-a[i])
		num = x
	else:
		num += a[i]
print(answer)

Submission status: Runtime Error

Solution code:
#include<bits/stdc++.h>
using namespace std;

int main(void){
    int n;
    cin >> n;
    vector<int> a(n);
    for(auto& it : a) cin >> it;
    int total = a[0];
    int ans = 0;
    for(int i=1;i<n;i++){
        if(total > 0){
            if(total + a[i] >= 0) {
                ans += abs(-total-1 - a[i]);
                a[i] = -total-1;
            }
        } else {
            if(total + a[i] <= 0) {
                ans += abs(-total+1  - a[i]);
                a[i] = -total+1;
            }
        }
        total += a[i];
    }


    cout << ans << endl;

    return 0;
}

Submission status: Runtime Error

Solution code:
n = int(input())
as = list(map(int, input().split(" ")))


ope = 0
for i in range(0, n-1):
    sum_be=sum(as[:i])
    sum_af=sum(as[:i+1])
    if sum_be<0<sum_af or sum_af<0<sum_be:
        continue
    elif sum_be > 0:
        ope += as[i+1] + (sum_be+1)
        as[i+1] = sum_be + 1
    elif sum_be < 0:
        ope += 1 - sum_be - sum_af
        as[i+1] = 1 - sum_be

print(ope)

Submission status: Runtime Error

Solution code:
n = int(input())
a_list = [int(i) for i in input().split(' ')]
total = [a_list[0]]
counter = 0

for a in a_list[1:]:
    total.append(total[-1] + a)
    
if total[0]==0:
    total = map(lambda n: n-int(total[1]/abs(total[1])), total)
for i in range(1,n):    
    if total[i-1]<0 and total[i]<=0:
        counter += abs(total[i])+1
        total[i:] = list(map(lambda n: n-(total[i])+1, total[i:]))
    elif total[i-1]>0 and total[i]>=0:
        counter += abs(total[i])+1
        total[i:] = list(map(lambda n: n-(total[i])-1, total[i:]))
    else:
        continue

print(counter)

Submission status: Runtime Error

Solution code:
import sys
# -*- coding: utf-8 -*-
# 整数の入力
n=int(input())
a=list(map(int, input().split()))
b=a[:]
c=a[:]
# 無変更チェック
if a[0]!=0:
  S=int(a[0])
  for i in range(1,n):
    if S<0 and S+a[i]<=0:
      break
    elif S>0 and S+a[i]>=0:
      break
    S+=a[i]
    if i==n-1:
      print(0)
      sys.exit()
# a[0]を1に変えた場合の計算
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]
# a[0]を-1に変えた場合の計算
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))

Submission status: Runtime Error

Solution code:
# -*- coding:utf-8 -*-

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


n = int(raw_input())
numlist = (raw_input()).split(' ')

sumlist = [int(numlist[0])]
count = 0

for i in range(1, n):
  sumlist.append(sumlist[i-1] + int(numlist[i]))
  while ((sign(sumlist[i-1]) == sign(sumlist[i])) or (sumlist[i] == 0)):
    if (sumlist[i] > 0):  #i-1,i番目までのsumがともに正
      numlist[i] = int(numlist[i]) - 1
      sumlist[i] -= 1
      count += 1
    elif (sumlist[i] < 0):  #i-1,i番目までのsumがともに負
      numlist[i] = int(numlist[i]) + 1
      sumlist[i] += 1
      count += 1
    else:
      if (sumlist[i-1] > 0):
        numlist[i] = int(numlist[i]) - 1
        sumlist[i] -= 1
      if (sumlist[i-1] < 0):
        numlist[i] = int(numlist[i]) + 1
        sumlist[i] += 1
      count += 1
    print numlist
    print sumlist


print count
print numlist
print sumlist

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力
n=int(input())
a=input().split()
counter=0
S=a[0]
# 出力
for i in range(1,n):
  S+=a[i]
  if S<0 and S+int(a[i])<=0:
    counter+=-S-int(a[i])+1
    a[i]=-S+1
  elif S>0 and S+int(a[i])>=0:
    counter+=S+int(a[i])+1
    a[i]=-S-1
print(counter)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-

n = 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)

Submission status: Runtime Error

Solution code:
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
'''
    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
    '''print('totn', totn)
    print('countn', countn)
    print('totp', totp)
    print('countp', countp) '''   
count = min(countn, countp)
print(count)

Submission status: Runtime Error

Solution code:
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
'''
    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

    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)

Submission status: Runtime Error

Solution code:
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(max(ret,ret2))

Submission status: Runtime Error

Solution code:
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 += 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 += abs(a_sum) + 1
        a_sum = signs[i%2]

print(min(changes_1,changes_2))

#
# for i, sum_i in enumerate(a_sum):
#     if i == 0:
#         signs = [sign(sum_i), -sign(sum_i)]
#     elif sign(sum_i) != signs[i%2]:
#         a_sum[i:] = [num + (abs(sum_i) + 1) * signs[i%2] for num in a_sum[i:]]
#         changes += abs(sum_i) + 1
#     # print(a_sum)
# print(changes)

Submission status: Runtime Error

Solution code:
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()))

a_sum = [a_i[0]]
for i, a in enumerate(a_i[1:]):
    i += 1
    a_sum.append(a_sum[-1]+a)

signs = [1, -1]
for i, sum_i in enumerate(a_sum):
    if sum_i != 0 && i%2 == 0:
        signs = [sign(sum_i), -sign(sum_i)]
        break
    elif sum_i != 0 && i%2 == 1:
        signs = [-sign(sum_i), sign(sum_i)]
        break

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

#
# for i, sum_i in enumerate(a_sum):
#     if i == 0:
#         signs = [sign(sum_i), -sign(sum_i)]
#     elif sign(sum_i) != signs[i%2]:
#         a_sum[i:] = [num + (abs(sum_i) + 1) * signs[i%2] for num in a_sum[i:]]
#         changes += abs(sum_i) + 1
#     # print(a_sum)
# print(changes)

Submission status: Runtime Error

Solution code:
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()))

a_sum = [a_i[0]]
for i, a in enumerate(a_i[1:]):
    i += 1
    a_sum.append(a_sum[-1]+a)

signs = [1, -1]
for i, sum_i in enumerate(a_sum):
    if sum_i != 0:
        signs[i%2] = sign(sum_i)
        signs[i%2+1] = -sign(sum_i)
        break

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

#
# for i, sum_i in enumerate(a_sum):
#     if i == 0:
#         signs = [sign(sum_i), -sign(sum_i)]
#     elif sign(sum_i) != signs[i%2]:
#         a_sum[i:] = [num + (abs(sum_i) + 1) * signs[i%2] for num in a_sum[i:]]
#         changes += abs(sum_i) + 1
#     # print(a_sum)
# print(changes)

Submission status: Runtime Error

Solution code:


n = int(input())
l = map(int, input().split())
ll = []
for q in range(len(l)):
    ll.append(l[q])
print(ll)

sums1 = [0] * n
count1 = 0
sums1[0] = ll[0]
for i in range(1, len(ll)):
        sums1[i] = sums1[i-1] + ll[i]



if l[0] == 0:
    for v in range(len(sums1)):
        sums1 += [1] * len(sums1)
    count1 += 1

for k in range(1, len(sums1)):
    while sums1[k] == 0 or sums1[k] * sums1[k-1] > 0:
        if sums1[k] == 0:
            for p in range(k, len(sums1)):
                sums1[p] += -(sums1[k-1]/abs(sums1[k-1]))
            count1 += 1
        if sums1[k] * sums1[k-1] > 0:
            for p in range(k, len(sums1)):
                sums1[p] += (abs(sums1[k])+1) * (-(sums1[k])/abs(sums1[k]))
            count1 += abs(sums1[k])+1



sums2 = [0] * n
count2 = 0
sums2[0] = ll[0]
for i in range(1, len(ll)):
        sums2[i] = sums2[i-1] + ll[i]



if l[0] == 0:
    for v in range(len(sums2)):
        sums2 -= [1] * len(sums2)
    count2 += 1

for k in range(1, len(sums2)):
    while sums2[k] == 0 or sums2[k] * sums2[k-1] > 0:
        if sums2[k] == 0:
            for p in range(k, len(sums2)):
                sums2[p] += -sums2[k-1]/abs(sums2[k-1])
            count2 += 1
        if sums2[k] * sums2[k-1] > 0:
            for p in range(k, len(sums1)):
                sums2[p] += -(abs(sums2[k])+1) * ((sums2[k])/abs(sums2[k]))
            count2 += abs(sums1[k])+1


print(min(count2, count2) * 2)

Submission status: Runtime Error

Solution code:


n = int(input())
l = map(int, input().split())
ll = []
for q in range(len(l)):
    ll.append(l[q])
print(ll)

sums1 = [0] * n
count1 = 0
sums1[0] = ll[0]
for i in range(1, len(ll)):
        sums1[i] = sums1[i-1] + ll[i]



if l[0] == 0:
    for v in range(len(sums1)):
        sums1 += [1] * len(sums1)
    count1 += 1

for k in range(1, len(sums1)):
    while sums1[k] == 0 or sums1[k] * sums1[k-1] > 0:
        if sums1[k] == 0:
            for p in range(k, len(sums1)):
                sums1[p] += -(sums1[k-1]/abs(sums1[k-1]))
            count1 += 1
        if sums1[k] * sums1[k-1] > 0:
            for p in range(k, len(sums1)):
                sums1[p] += (abs(sums1[k])+1) * (-(sums1[k])/abs(sums1[k]))
            count1 += abs(sums1[k])+1



sums2 = [0] * n
count2 = 0
sums2[0] = ll[0]
for i in range(1, len(ll)):
        sums2[i] = sums2[i-1] + ll[i]



if l[0] == 0:
    for v in range(len(sums2)):
        sums2 -= [1] * len(sums2)
    count2 += 1

for k in range(1, len(sums2)):
    while sums2[k] == 0 or sums2[k] * sums2[k-1] > 0:
        if sums2[k] == 0:
            for p in range(k, len(sums2)):
                sums2[p] += -sums2[k-1]/abs(sums2[k-1])
            count2 += 1
        if sums2[k] * sums2[k-1] > 0:
            for p in range(k, len(sums1)):
                sums2[p] += -(abs(sums2[k])+1) * ((sums2[k])/abs(sums2[k]))
            count2 += abs(sums1[k])+1


print(min(count2, count2) * 2)

Submission status: Runtime Error

Solution code:
N = int(input())
prog = list(map(int,input().split()))
def seq(N,prog):
    result = [0]*N
    result[0] = prog[0]
    for i in range(N-1):
        result[i+1] = result[i] + prog[i+1]
    if result[0] == 0:
        if sum(result[0]+result[1]) >= 0:
            result[0] -= 1
            x = [-1]*(N-1)
            result[1:] = [a+b for (a,b) in zip(x,result[1:])]
        else:
            result[0] += 1
            x = [1]*(N-1)
            result[1:] = [a+b for (a,b) in zip(x,result[1:])]
    answer = 0
    for i in range(N-1):
        y=0
        if result[i] >= 0:
            if result[i+1] >0:
                y = (-1)*(result[i+1]+1)
                result[i+1] = -1
                answer += abs(y)
        else:
            if result[i+1] <= 0:
                y = abs(result[i+1])+1
                result[i+1] = 1
                answer += abs(y)
        z = [y]*(N-i-1)
        result[(i+1):] = [a+b for (a,b) in zip(z,result[(i+1):])]
    print(answer)
seq(N,prog)

Submission status: Runtime Error

Solution code:


n = int(input())
l = map(int, input().split())
ll = []
for q in range(len(l)):
    ll.append(l[q])
print(ll)

sums1 = [0] * n
count1 = 0
sums1[0] = ll[0]
for i in range(1, len(ll)):
        sums1[i] = sums1[i-1] + ll[i]



if l[0] == 0:
    for v in range(len(sums1)):
        sums1 += [1] * len(sums1)
    count1 += 1

for k in range(1, len(sums1)):
    while sums1[k] == 0 or sums1[k] * sums1[k-1] > 0:
        if sums1[k] == 0:
            for p in range(k, len(sums1)):
                sums1[p] += -(sums1[k-1]/abs(sums1[k-1]))
            count1 += 1
        if sums1[k] * sums1[k-1] > 0:
            for p in range(k, len(sums1)):
                sums1[p] += (abs(sums1[k])+1) * (-(sums1[k])/abs(sums1[k]))
            count1 += abs(sums1[k])+1



sums2 = [0] * n
count2 = 0
sums2[0] = ll[0]
for i in range(1, len(ll)):
        sums2[i] = sums2[i-1] + ll[i]



if l[0] == 0:
    for v in range(len(sums2)):
        sums2 -= [1] * len(sums2)
    count2 += 1

for k in range(1, len(sums2)):
    while sums2[k] == 0 or sums2[k] * sums2[k-1] > 0:
        if sums2[k] == 0:
            for p in range(k, len(sums2)):
                sums2[p] += -sums2[k-1]/abs(sums2[k-1])
            count2 += 1
        if sums2[k] * sums2[k-1] > 0:
            for p in range(k, len(sums1)):
                sums2[p] += -(abs(sums2[k])+1) * ((sums2[k])/abs(sums2[k]))
            count2 += abs(sums1[k])+1


print(min(count2, count2))

Submission status: Runtime Error

Solution code:
    n=int(raw_input())
    a=map(int,raw_input().split(' '))
     
    p_s=a[0]
    c=0
    for i in range(1,n):
        s=p_s+a[i]
        if s==0:
            if i==n-1:
                a[i]+=1
                c+=1
            else:
                if a[i]>=0:
                    a[i]+=1
                    c+=1
                else:
                    a[i]-=1
                    c+=1
        s=p_s+a[i]
        if p_s*s>=0:
            if p_s>0:
                c+=abs(p_s*-1 - 1 - a[i])
                a[i]=p_s*-1 - 1
            else:
                c+=abs(p_s*-1 + 1 - a[i])
                a[i]=p_s*-1 + 1
        p_s=p_s+a[i]
     
    print c

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
c = 0
i = 1
k = 1
sum = a[0]

while a[i] == 0:
		i += 1
	if a[i] > 0:
		if i % 2 == 1:
			a[0] = 1
			c = 1
		else:
			a[0] = -1
			c = 1
	else:
		if i % 2 == 1:
			a[0] = -1
			c = 1
		else:
			a[0] = 1
			c = 1
i = 1
if a[0] > 0:
	while i < n:
		if i == 2*k-1:
			sum = sum + a[2*k-1]
			if sum >= 0:
				c = c + sum + 1
				sum = -1
			else:
				pass
			i += 1
		else:
			sum = sum + a[2*k]
			if sum <= 0:
				c = c - sum + 1
				sum = 1
			else:
				pass
			i += 1
			k += 1
	print(c)
elif a[0] < 0:
	while i < n:
		if i == 2*k-1:
			sum = sum + a[2*k-1]
			if sum <= 0:
				c = c - sum + 1
				sum = 1
			else:
				pass
			i += 1
		else:
			sum = sum + a[2*k]
			while sum >= 0:
				c = c + sum + 1
				sum = -1
			else:
				pass
			i += 1
			k += 1
	print(c)

Submission status: Runtime Error

Solution code:
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()))

a_sum = [a_i[0]]
for i, a in enumerate(a_i[1:]):
    i += 1
    a_sum.append(a_sum[-1]+a)

signs = [0, 0]
changes = 0

for i, sum_i in enumerate(a_sum):
    if sum_i != 0:
        signs[i%2] = sign(sum_i)
        signs[i%2+1] = -sign(sum_i)
        break

if signs == [0, 0]:
    signs = [1, -1]

for i, sum_i in enumerate(a_sum):
    if i == 0:
        signs = [sign(sum_i), -sign(sum_i)]
    elif sign(sum_i) != signs[i%2]:
        a_sum[i:] = [num + (abs(sum_i) + 1) * signs[i%2] for num in a_sum[i:]]
        changes += abs(sum_i) + 1
    # print(a_sum)
print(changes)