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:
n = int(input())
a = list(map(int, input().split(' ')))
result = 0
h = 0
if a[0] == 0:
    counter += 1
    result += 1
else:
    counter += a[0]

for i in range(1, n):
    if counter < 0:
        counter += a[i]
        if counter == 0:
            counter += 1
            result +=1
        elif counter > 0:
            continue
        else:
            result += 1-counter
            counter = 1
    else:
        counter += a[i]
        if counter == 0:
            counter -= 1
            result += 1
        elif counter < 0:
            continue
        else:
            result += counter+1
            counter = -1
out = []
out.append(result)
result = 0
counter = 0
if a[0] == 0:
    counter -= 1
    result +=1
elif a[0]< 0:
    counter += 1
    result += 1-a[0]
else:
    counter -= 1
    result += 1+a[0]

for i in range(1, n):
    if counter < 0:
        counter += a[i]
        if counter == 0:
            counter += 1
            result += 1
        elif counter > 0:
            continue
        else:
            result += 1-counter
            counter = 1
    else:
        counter += a[i]
        if counter == 0:
            counter -= 1
            result +=1
        elif counter < 0:
            continue
        else:
            result += counter+1
            counter = -1
out.append(result)
print(min(out))

Submission status: Runtime Error

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


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

Submission status: Runtime Error

Solution code:
import sys
input=sys.stdin.readline
input = open(sys.argv[1], "r").readline

def main():
    N = int(input())
    A = list(map(int, input().split()))
    tmp = A.copy()
    mi = -1
    for j in range(2):
        A = tmp.copy()
        s = 0
        n = 0
        if j == 0: # 偶数番目までの和が正 ⇒ A[0] を負にする
            if A[0] > 0:
                n += abs(A[0] +1)
                A[0] = -1
        else:   # 奇数番目までの和が正 ⇒ A[0] を正にする
            if A[0] < 0:
                n += abs(A[0] -1)
                A[0] = 1
        s = A[0]
#        print(A)
        for i in range(1,N):
            if s * (s+A[i]) >= 0:
                if s < 0:
                    n += abs(-s+1 -A[i])
                    A[i] = -s+1
                else:
                    n += abs(-s-1 -A[i])
                    A[i] = -s-1
            s += A[i]
        if mi == -1:
            mi = n
        else:
            mi = min(mi, n)
    print(mi)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

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

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

print(min(ans1, ans2))

Submission status: Runtime Error

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

count = 0
sum_a = a[0]
for i in range(n - 1):
    sum_a2 = sum_a + a[i + 1]
    if sum_a * sum_a2 >= 0:
        count += abs(sum_a2) + 1
        sum_a2 = - abs(sum_a) // sum_a
    sum_a = sum_a2
print(count)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3

import sys

sys.setrecursionlimit(10**7)


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


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


def calc_num_op(cumul, a):
    assert cumul != 0
    tmp_cumul = cumul + a
    #  print('current cumul:', cumul)
    #  print('current a:', a)
    #  print('bare cumul:', tmp_cumul)
    if (cumul < 0 and tmp_cumul <= 0):
        return abs(-cumul - a + 1), 1
    if (cumul > 0 and tmp_cumul >= 0):
        return abs(-cumul - a - 1), -1
    return 0, tmp_cumul


def solve(arr, sign):
    num_op = 0
    cumul = arr[0]
    for a in arr[1:]:
        delta, cumul = calc_num_op(cumul, a)
        #  print('delta:', delta)
        #  print('modified cumul:', cumul)
        num_op += delta
        sign = '+' if sign == '-' else '-'
    return num_op


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

    op_plus = solve(arr, '+')
    #  print('op plus:', op_plus)
    op_minus = solve(arr, '-')
    #  print('op minus:', op_minus)

    print(min(op_plus, op_minus))


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n=int(input())
b=list(map(int,input().split()))
a=b[:]
condition=''
cnt=0
wa=0
for i in range(n):
  wa+=a[i]
  if i == 0:
    if a[i]>0:
      condition='minus'
    else:
      condition='plus'
  elif condition == 'plus':
    condition='minus'
    if wa<=0:
      cnt+=abs(wa)+1
      a[i]+=abs(wa)+1
      wa+=abs(wa)+1
  elif condition == 'minus':
    condition='plus'
    if wa>=0:
      cnt+=abs(wa)+1
      a[i]-=abs(wa)+1
      wa-=abs(wa)+1

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

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

Submission status: Runtime Error

Solution code:
n=int(input())
b=list(map(int,input().split()))
a=b[:]
condition=''
cnt=0
wa=0
for i in range(n):
  wa+=a[i]
  if i == 0:
    if a[i]>0:
      condition='minus'
    else:
      condition='plus'
  elif condition == 'plus':
    condition='minus'
    if wa<=0:
      cnt+=abs(wa)+1
      a[i]+=abs(wa)+1
      wa+=abs(wa)+1
  elif condition == 'minus':
    condition='plus'
    if wa>=0:
      cnt+=abs(wa)+1
      a[i]-=abs(wa)+1
      wa-=abs(wa)+1

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

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

Submission status: Runtime Error

Solution code:
n=int(input())
b=list(map(int,input().split()))
a=b[:]
condition=''
cnt=0
wa=0
for i in range(n):
  wa+=a[i]
  if i == 0:
    if a[i]>0:
      condition='minus'
    else:
      condition='plus'
  elif condition == 'plus':
    condition='minus'
    if wa<=0:
      cnt+=abs(wa)+1
      a[i]+=abs(wa)+1
      wa+=abs(wa)+1
  elif condition == 'minus':
    condition='plus'
    if wa>=0:
      cnt+=abs(wa)+1
      a[i]-=abs(wa)+1
      wa-=abs(wa)+1

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

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

Submission status: Runtime Error

Solution code:
n=int(input())
b=list(map(int,input().split()))
a=b
condition=''
cnt=0
wa=0
for i in range(n):
  wa+=a[i]
  if i == 0:
    if a[i]>0:
      condition='minus'
    else:
      condition='plus'
  elif condition == 'plus':
    condition='minus'
    if wa<=0:
      cnt+=abs(wa)+1
      a[i]+=abs(wa)+1
      wa+=abs(wa)+1
  elif condition == 'minus':
    condition='plus'
    if wa>=0:
      cnt+=abs(wa)+1
      a[i]-=abs(wa)+1
      wa-=abs(wa)+1

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

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

Submission status: Runtime Error

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

sm=a[0]
ans=[0, 0]
for i in range(n-1):
    sm1=sm+a[i+1]
    if sm*sm1>=0:
        ans[0]+=abs(sm1)+1
        sm//=abs(sm)*(-1)
    else:
        sm=sm1


sm=-1
ans[1]+=abs(a[0])+1
for i in range(n-1):
    sm1=sm+a[i+1]
    if sm*sm1>=0:
        ans[1]+=abs(sm1)+1
        sm//=abs(sm)*(-1)
    else:
        sm=sm1
print(min(ans))

Submission status: Runtime Error

Solution code:
n=int(input())

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

sm=a[0]

ans=0
for i in range(n-1):
    sm1=sm+a[i+1]
    if sm*sm1>=0:
        ans+=abs(sm1)+1
        sm//=abs(sm)*(-1)
    else:
        sm=sm1
print(ans)

Submission status: Runtime Error

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

def f(x):
  cnt = 0
  cur = 0
  for ai in a:
    cur += ai
    cur *= x
    if cur < 0:
      cnt += 1 - ai
    x *= -1
  print(cnt)

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

Submission status: Runtime Error

Solution code:
N = int(input())
a = [int(x) for x in input().split()]

A = a[0]
asum = [A]
if A == 0:
    ansp,ansm,tempp,tempm = 1,1,1,-1
elif A < 0:
    ansp,ansm,tempp,tempm = -A + 1, 0, -A + 1, 0
else:
    ansp,ansm,tempp,tempm = 0, A2 + 1, 0, -A2 - 1

for i in range(1,N):
    A = asum[i-1] + a[i]
    asum += [A]

    A1 = A + tempp
    if A1 == 0:
        ansp += 1
        tempp += ((i%2)*2-1)
    elif i%2==0 and A1 < 0:
        ansp += -A1 + 1
        tempp += -A1 + 1
    elif i%2!=0 and A1 > 0:
        ansp += A1 + 1
        tempp += -A1 - 1

    A2 = A + tempm
    if A2 == 0:
        ansm += 1
        tempm += -((i%2)*2-1)
    elif i%2!=0 and A2 < 0:
        ansm += -A2 + 1
        tempm += -A2 + 1
    elif i%2==0 and A2 > 0:
        ansm += A2 + 1
        tempm += -A2 - 1

print(min(ansp,ansm))

Submission status: Runtime Error

Solution code:
N = int(input())
*A, = map(int,input().split())
ans1 = 0
S = A[0]
if S <= 0:
    S = 1
    ans1 = abs(S)+1
for a in A[1:]:
    S1 = S+a
    if S1*S >= 0:
        ans1 += abs(S1)+1
        S1 = -S//abs(S)
    S = S1
ans2 = 0
S = A[0]
if S >= 0:
    S = -1
    ans2 = abn(S)+1
for a in A[1:]:
    S1 = S+a
    if S1*S >= 0:
        ans2 += abs(S1)+1
        S1 = -S//abs(S)
    S = S1
print(min(ans1,ans2))

Submission status: Runtime Error

Solution code:
N = int(input())
*A, = map(int,input().split())
ans = 0
S = A[0]
for a in A[1:]:
    S1 = S+a
    if S1*S >= 0:
        ans += abs(S1)+1
        S1 = -S//abs(S)
    S = S1
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
A = list(map(int, input().split()))
cnt = 0
w = A[0]
for i in range(n - 1):
    nw = w + A[i + 1]
    if w > 0:
        if nw >= 0:
            cnt += nw + 1
            chg = -(nw  + 1)
    else:
        if nw <= 0:
            cnt += 1 - nw
            chg = 1 - nw
    w = nw + chg
    chg = 0
    
print(cnt)

Submission status: Runtime Error

Solution code:
n = int(input())
A = list(map(int, input().split()))
cnt = 0
w = A[0]
for i in range(n - 1):
    nw = w + A[i + 1]
    if w > 0:
        if nw >= 0:
            cnt += nw + 1
            chg = -(nw  + 1)
    elif w < 0:
        if nw <= 0:
            cnt += 1 - nw
            chg = 1 - nw
    w = nw + chg
    chg = 0
    
print(cnt)

Submission status: Runtime Error

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

def ssa(n):
    arr = A
    memo =['inf'] * (n + 1)
    def _ssa(n):
        if n == 0:
            return arr[0]
        if memo[n] != 'inf':
            return memo[n]
        memo[n] = _ssa(n -1) + arr[n]
        return memo[n]
    return _ssa(n)

cnt = 0
chg = 0
for i in range(n - 1):
    if ssa(i) + chg > 0:
        if ssa(i + 1) + chg >= 0:
            cnt += ssa(i + 1) + chg + 1
            chg += -(ssa(i + 1)+ chg  + 1)
    elif ssa(i) + chg < 0:
        if ssa(i + 1) + chg <= 0:
            cnt += 1 - ssa(i + 1) - chg
            chg += 1 - ssa(i + 1) - chg

if ssa(n - 1) + chg == 0:
    cnt +=1

print(cnt)

Submission status: Runtime Error

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

ans1 = 0
sum = A[0]
for a in A[1:]:
    if (sum + a) * sum < 0:
        sum += a
    else:
        fugo = sum // abs(sum)
        nextsum = - fugo
        #print('fugo={}, nextsum={}'.format(fugo, nextsum))
        a_should_be = nextsum - sum
        dif = abs(a_should_be - a)
        #print('a_shoule_be={}, dif={}'.format(a_should_be, dif))
        sum = nextsum
        ans1 += dif

#print(ans1)

#A[0]を反転したほうがいいパターンの処理
ans2 = abs(A[0]) + 1
sum = - (A[0] // abs(A[0]))

for a in A[1:]:
    if (sum + a) * sum < 0:
        sum += a
    else:
        fugo = sum // abs(sum)
        nextsum = - fugo
        a_should_be = nextsum - sum
        dif = abs(a_should_be - a)
        sum = nextsum
        ans2 += dif
        
#print(ans2) 
print(min(ans1, ans2))

Submission status: Runtime Error

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

ans1 = 0
sum = A[0]
for a in A[1:]:
    if (sum + a) * sum < 0:
        sum += a
    else:
        fugo = sum // abs(sum)
        nextsum = - fugo
        a_should_be = nextsum - sum
        dif = abs(a_should_be - a)
        sum += a_should_be
        ans1 += dif

#print(ans1)

#A[0]を反転したほうがいいパターンの処理
ans2 = A[0] + 1
sum = - (A[0] // abs(A[0]))

for a in A[1:]:
    if (sum + a) * sum < 0:
        sum += a
    else:
        fugo = sum // abs(sum)
        nextsum = - fugo
        a_should_be = nextsum - sum
        dif = abs(a_should_be - a)
        sum += a_should_be
        ans2 += dif
        
#print(ans2) 
print(min(ans1, ans2))

Submission status: Runtime Error

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

ans1 = 0
sum = A[0]
for a in A[1:]:
    if (sum + a) * sum < 0:
        sum += a
    else:
        fugo = sum // abs(sum)
        nextsum = - fugo
        a_should_be = nextsum - sum
        dif = abs(a_should_be - a)
        sum += a_should_be
        ans1 += dif

print(ans1)

ans2 = A[0] + 1
sum = - (A[0] // abs(A[0]))

for a in A[1:]:
    if (sum + a) * sum < 0:
        sum += a
    else:
        fugo = sum // abs(sum)
        nextsum = - fugo
        a_should_be = nextsum - sum
        dif = abs(a_should_be - a)
        sum += a_should_be
        ans2 += dif
        
print(ans2) 
#A[0]を反転したほうがいいパターンの処理

Submission status: Runtime Error

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

ans = 0
sum = A[0]
for a in A[1:]:
    if (sum + a) * sum < 0:
        sum += a
    else:
        fugo = sum // abs(sum)
        nextsum = - fugo
        a_should_be = nextsum - sum
        dif = abs(a_should_be - a)
        sum += a_should_be
        ans += dif

print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
A = [map(int,input().split())]
acm1 = 0
acm2 = 0
ans1 = 0
ans2 = 0
for i, a in enumerate(A):
  acm1 += a
  acm2 += a
  if(i%2==1):
    if(acm1>=0):
      ans1 += acm1 + 1
      acm1 = -1
    if(acm2<=0):
      ans2 += -acm2 + 1
      acm2 = 1
  else:
    if(acm1<=0):
      ans1 += -acm1 + 1
      acm1 = 1
    if(acm2>=0):
      ans2 += acm2 + 1
      acm2 = -1
print(min(ans1, ans2))

Submission status: Runtime Error

Solution code:
n=int(input())
A=list(map(int,input().split()))
ans=10**15
for i in[-1,1]:
    ansi,sum=0,0
    for a in A:
        sum+=a
        if sum*i<=0:;ansi+=abs(sum-i);sum=i
        i*=-1
    ans=min(ans,ansi)
print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())

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

res=X[0]
cnt=0

if X[0]==0:
    cnt=1
    res=1
    for i in range(1,n):
        if X[i]>0:
            tmp=i
            cnt+=2*(i-1)
            res=-1
        elif X[i]<0:
            tmp=i
            cnt+=2*(i-1)
            res=1
            
for i in range(tmp,n):
    res+=X[i]
    if res>=0 and res-X[i]>0:
        cnt+=(1+res)
        res=-1
    elif res<=0 and res-X[i]<0:
        cnt+=(1-res)
        res=1
    

print(cnt)

Submission status: Runtime Error

Solution code:
n=int(input())
p=list(map(int,input().split()))
stack=[0]*(n+1)
ans=0
if p[0]==0:
  p[0]=1
  ans+=1
  stack[1]=1
else:
  stack[1]=p[0]
for i in range(1,n):
  if i%2==1:
    if p[i]+p[i-1]+stack[i-1]>=0:
      p[i]-=(p[i]+p[i-1]+stack[i-1])+1
      ans+=(p[i]+p[i-1]+stack[i-1])+1
  else:
    if p[i]+p[i-1]+stack[i-1]<=0:
      p[i]+=-(p[i]+p[i-1]+stack[i-1])+1
      ans+=-(p[i]+p[i-1]+stack[i-1])+1
  stack=p[i]+p[i-1]+stack[i-1]
print(ans)

Submission status: Runtime Error

Solution code:
import copy
 
N=int(input())
l=list(map(int, input().split()))  #リスト入力
cp = copy.copy(l)
#c=0
 
for k in range(1,N):
 
  if sum(l[:k])==0:
    #c=c+1
    if l[k]>0:
      l[k]=l[k]+1
    else:
      l[k]=l[k]-1
 
  if sum(l[:k])*sum(l[:k+1])>0:
    
    if sum(l[:k+1])>0:
      l[k]=l[k]-(sum(l[:k+1])-(-1))
      #c=c+abs(sum(l[:k+1])-(-1))
    else:
      l[k]=l[k]+(1-sum(l[:k+1]))
      #c=c+abs(1-sum(l[:k+1]))
 
 
if sum(l)==0:
  c=c+1
  l[-1]=l[-1]+1
 
#print(l)
print(sum([abs(l[n]-cp[n]) for n in range(N)]))

Submission status: Runtime Error

Solution code:
import copy

N=int(input())
l=list(map(int, input().split()))  #リスト入力
cp = copy.copy(l)
#c=0

for k in range(N-1):

  if sum(l[:k+1])==0:
    #c=c+1
    if l[k+1]>0:
      l[k+1]=l[k+1]+1
    else:
      l[k+1]=l[k+1]-1

  if sum(l[:k])*sum(l[:k+1])>0:
    
    if sum(l[:k+1])>0:
      l[k]=l[k]-(sum(l[:k+1])-(-1))
      #c=c+abs(sum(l[:k+1])-(-1))
    else:
      l[k]=l[k]+(1-sum(l[:k+1]))
      #c=c+abs(1-sum(l[:k+1]))


if sum(l)==0:
  c=c+1
  l[-1]=l[-1]+1

  
print(sum([abs(l[n]-cp[n]) for n in range(N)]))

Submission status: Runtime Error

Solution code:
import copy

N=int(input())
l=list(map(int, input().split()))  #リスト入力
cp = copy.copy(l)
#c=0

for k in range(N-1):

  if sum(l[:k+1])==0:
    #c=c+1
    if l[k+1]>0:
      l[k+1]=l[k+1]+1
    else:
      l[k+1]=l[k+1]-1

  if sum(l[:k])*sum(l[:k+1])>0:
    print(k,sum(l[:k]),sum(l[:k+1]),l[k+1])
    if sum(l[:k+1])>0:
      l[k]=l[k]-(sum(l[:k+1])-(-1))
      #c=c+abs(sum(l[:k+1])-(-1))
    else:
      l[k]=l[k]+(1-sum(l[:k+1]))
      #c=c+abs(1-sum(l[:k+1]))
    print(l[k])

if sum(l)==0:
  c=c+1
  l[-1]=l[-1]+1
#print(l)
#print(c)
print(sum([abs(l[n]-cp[n]) for n in range(N)]))

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int,input().split()))
s = 0
sign = a[0]//abs(a[0])
ans = 0
for i in range(n):
    s += a[i]
    if s == 0:
        s = sign
        ans += 1
    
    elif s//abs(s) == sign:
        pass
    else:
        ans += abs(sign-s)
        s = sign
    sign *= -1
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())

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

if a[0] != 0:
    b = [a[0]]
else:
    su += 1
    if b[1] > 0:
        b = [-1]
    else:
        b = [1]
su = 0
for i in range(n-1):
    b.append(b[-1]+a[1+i])
    if b[-1] * b[-2] > 0:
        su += abs(b[-1]) + 1
        if b[-2] > 0:
            b[-1] = -1
        else:
            b[-1] = 1
    else:
        if b[-1] == 0:
            su += 1
            if b[-2] > 0:
                b[-1] = -1
            else:
                b[-1] = 1
print(su)

Submission status: Runtime Error

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

if a[0]:
    ans = 0
    e0 = 1 if a[0] > 0 else -1
    S = a[0]

    for i in range(1, n):
        e = e0 * int((i % 2-0.5)*2)
        n = max(e*(S+a[i])+1, 0)
        S += a[i]+(-e*n)
        ans += n
    print(ans)

else:
    ans = [0, 0]
    for e0 in [1, -1]:
        a[0] = e0*1
        S = a[0]

        for i in range(1, n):
            e = e0 * int((i % 2-0.5)*2)
            n = max(e*(S+a[i])+1, 0)
            S += a[i]+(-e*n)
            ans[int(e0/2+0.5)] += n
    print(min(ans))

Submission status: Runtime Error

Solution code:
import sys
n = int(input())
a = [int(n) for n in input().split()]
sum = [0]*n
sum[0] = a[0]
ans = 0
for i in range(1,n):
    sum[i] = sum[i-1]
    while((sum[i]+a[i])*sum[i-1] >= 0):
        if(sum[i-1] > 0):
            ans+=sum[i-1] + a[i]+1
            a[i]-=sum[i-1] + a[i]+1
        else:
            ans+=1 - sum[i-1] - a[i]
            a[i]+=1 - sum[i-1] - a[i]
        print(a)
    sum[i] += a[i]
print(ans)
# print(a)
# print(sum)

Submission status: Runtime Error

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

not_0 = n
for i in range(n):
    if a[i]:
        not_0 = i
        break

if not_0 == n:
    print(1+2*(n-1))
    exit()

ans = 0
if not_0 == 0:
    b = [a[0]]
else:
    if abs(a[not_0]) == 1:
        a[not_0] *= 2
        ans = 2*not_0
    else:
        ans = 1 + 2*(not_0-1)

    if a[not_0] > 0:
        b = [a[not_0] - 1]

    else:
        b = [a[not_0] + 1]

tmp = b[0]
for i in range(not_0+1, n):
    tmp += a[i]
    b.append(tmp)

for i in range(not_0+1, n):
    if b[i-1]*b[i] < 0:
        continue
    else:
        if b[i-1] > 0:
            d = b[i] + 1
            ans += d
            for j in range(i, n):
                b[j] -= d
        else:
            d = -b[i] + 1
            ans += d
            for j in range(i, n):
                b[j] += d
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
A = [item for item in list(map(int, input().split()))]
S = [A[0]]
for i in range(1, n):
    S.append(A[i]+S[i-1])
count = 0
if S[0] == 0:
    A[0] += 1
    for item in S:
        item += 1 
    count += 1
#print(S)
for i in range(1, n):
    #print(S[i-1], S[i])
    if S[i] * S[i-1] >= 0:
        dif = -1 * (S[i-1]//abs(S[i-1])) - S[i] 
        A[i] += dif
        for j in range(i,n):
            S[j] += dif
        count += abs(dif)
        #print(S)


print(count)

Submission status: Runtime Error

Solution code:

def culc(a, sum, plus, ans):
    for i in range(1, n):
        plus = not(plus)
        sum += a[i]
        if plus:
            if sum <= 0:
                ans += abs(sum) + 1
                sum = 1
        else:
            if sum >= 0:
                ans += abs(sum) + 1
                sum = -1
    return ans

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

plus = None
if a[0] > 0:
    plus = True
else:
    plus = False

ans1 = culc(a, a[0], plus, 0)

a[0] = a[0] // abs(a[0]) * -1

ans2 = abs(a[0]) + 1
ans2 = culc(a, a[0], not(plus), abs(a[0]) + 1)

print(min(ans1, ans2))

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
sum = a[0]
plus = None
if a[0] > 0:
    plus = True
else:
    plus = False

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

sum = 0
a[0] = a[0] // abs(a[0]) * -1
if a[0] > 0:
    plus = True
else:
    plus = False

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

print(min(ans1, ans2))

Submission status: Runtime Error

Solution code:
def 解()
    iN = int(input())
    aA = [int(_) for _ in input().split()]
    iL = len(aA)


    iStart = 0
    if sum(aA[0::2]) < sum(aA[2::2]):
        iStart = 1


    iC = 0
    aD = [0]*iL
    if 0 % 2 == iStart :
        if aA[0] < 0:
            aA[0] = 1
            iC += -1 * aA[0] + 1
    else:
        if 0 < aA[0] :
            aA[0] = -1
            iC += aA[0] + 1

    aD[0] = aA[0]
    for i in range(1,iL):
        aD[i] = aD[i-1]+aA[i]
        if i % 2 == iStart:
            if aD[i] <= 0:
                iC += -1*aD[i] +1
                aD[i] = 1
        else:
            if aD[i] >= 0:
                iC += aD[i] +1
                aD[i] = -1
    print(iC)
解()

Submission status: Runtime Error

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

cnt = 0
sum = [0]*n
sum[0] = a[0]

for i in range(1,n):
    sum[i] = sum[i-1]+a[i]
    if sum[i]*sum[i-1]<0:
        continue
    elif sum[i-1]*a[i]<0:
        cnt += abs(sum[i-1])-abs(a[i])+1
        sum[i] = -1*(sum[i-1]//abs(sum[i-1]))
    else:
        cnt += abs(sum[i-1])+abs(a[i])+1
        sum[i] = -1*(sum[i-1]//abs(sum[i-1]))

print(cnt)

Submission status: Runtime Error

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

if n == 1:
    print(0)

cnt = 0
sum = [0]*n
sum[0] = a[0]

for i in range(1,n):
    sum[i] = sum[i-1]+a[i]
    if sum[i]*sum[i-1]<0:
        continue
    elif sum[i-1]*a[i]<0:
        cnt += abs(sum[i-1])-abs(a[i])+1
        sum[i] = -(sum[i-1]//abs(sum[i-1]))
    else:
        cnt += abs(sum[i-1])+abs(a[i])+1
        sum[i] = -(sum[i-1]//abs(sum[i-1]))
print(cnt)

Submission status: Runtime Error

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

cnt = 0
sum = [0]*n
sum[0] = a[0]

for i in range(1,n):
    sum[i] = sum[i-1]+a[i]
    if sum[i]*sum[i-1]<0:
        continue
    elif sum[i-1]*a[i]<0:
        cnt += abs(sum[i-1])-abs(a[i])+1
        sum[i] = -(sum[i-1]//abs(sum[i-1]))
    else:
        cnt += abs(sum[i-1])+abs(a[i])+1
        sum[i] = -(sum[i-1]//abs(sum[i-1]))
print(cnt)

Submission status: Runtime Error

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

cnt = 0
sum = [0]*n
sum[0] = a[0]

for i in range(1,n):
    sum[i] = sum[i-1]+a[i]
    if sum[i]*sum[i-1]<0:
        continue
    elif sum[i-1]*a[i]<0:
        cnt += abs(sum[i-1])-abs(a[i])+1
        sum[i] = -sum[i-1]//abs(sum[i-1])
    else:
        cnt += abs(sum[i-1])+abs(a[i])+1
        sum[i] = -sum[i-1]//abs(sum[i-1])
print(cnt)

Submission status: Runtime Error

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

cnt = 0
sum = [0]*n
sum[0] = a[0]

for i in range(1,n):
    sum[i] = sum[i-1]+a[i]
    if sum[i]*sum[i-1]<0:
        continue
    if sum[i-1]*a[i]<0:
        cnt += abs(sum[i-1])-abs(a[i])+1
        sum[i] = -sum[i-1]//abs(sum[i-1])
    else:
        cnt += abs(sum[i-1])+abs(a[i])+1
        sum[i] = -sum[i-1]//abs(sum[i-1])
print(cnt)

Submission status: Runtime Error

Solution code:
n = int(input())
list_a = list(map(int,input().split()))
i = 0
k = 0
count = 0
ans = list_a[0]
if list_a[0] > 0:
    for i in range(1,n):
        ans += list_a[i]
        if ans / ((-1) ** i) <= 0:
            count += abs(ans - (-1) ** i)
            ans = (-1) ** i

else:
    for i in range(1,n):
        ans += list_a[i]
        if ans / ((-1) ** (i+1)) <= 0:
            count += abs(ans - (-1) ** (i+1))
            ans = (-1) ** (i+1)

count1 = count
count = 0
if list_a[0] <= 0:
    for i in range(1,n):
        ans += list_a[i]
        if ans / ((-1) ** i) <= 0:
            count += abs(ans - (-1) ** i)
            ans = (-1) ** i

else:
    for i in range(1,n):
        ans += list_a[i]
        if ans / ((-1) ** (i+1)) <= 0:
            count += abs(ans - (-1) ** (i+1))
            ans = (-1) ** (i+1)
print(min(count1, count2))

Submission status: Runtime Error

Solution code:
N = int( input())
A = list( map( int, input().split()))
ans = 10**15
for i in [1, -1]:
    ansi, sums = 0, 0
    for a in A:
        sums += a
        if sums*i <= 0:
            ansi += abs(sums-s)
            sums = s
        s *= -1
    ans = min( ans, ansi)
print(ans)

Submission status: Runtime Error

Solution code:
N = int( input())
A = list( map( int, input().split()))
ans = 10**15
for i in [1, -1]:
    ansi, sums = 0, 0
    for a in A:
        sums += A
        if sums*i <= 0:
            ansi += abs(sums-s)
            sums = s
        s *= -1
    ans = min( ans, ansi)
print(ans)

Submission status: Runtime Error

Solution code:
n = int( input())
A = list( map( int, input().split()))
ansp = 0
sums = A[0]
if sums == 0:
    ansp += 1
    sums += 1
for i in range(1,n-1):
    sums += A[i]
    if i%2 == 1:
        if sums < 0:
            pass
        else:
            ansp += abs(-1-sums)
            sums = -1
    else:
        if sums > 0:
            pass
        else:
            ansp += abs(1 - sums)
            sums = 1
if (n-1)%2 == 0:
    if A[n-1] > 0:
        sums += A[n-1]
        pass
    else:
        ansp += abs(1-A[n])
        sums += 1
else:
    if A[n-1] < 0:
        sums += A[n-1]
        pass
    else:
        ansp += abs(-1-A[n-1])
        sums -= 1
if sums == 0:
    ansp += 1

sums = A[0]
ansm = 0
if sums == 0:
    ansm += 1
    sums -= 1
for i in range(1,n-1):
    sums += A[i]
    if i%2 == 0:
        if sums < 0:
            pass
        else:
            ansm += abs(-1-sums)
            sums = -1
    else:
        if sums > 0:
            pass
        else:
            ansm += abs(1 - sums)
            sums = 1
if (n-1)%2 == 1:
    if A[n-1] > 0:
        sums += A[n-1]
        pass
    else:
        ansm += abs(1-A[n-1])
        sums += 1
else:
    if A[n-1] < 0:
        sums += A[n-1]
        pass
    else:
        ansm += abs(-1-A[n-1])
        sums -= 1
if sums == 0:
    ansm += 1
print( min(ansp, ansm))

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:
        cnt += abs(a[0])+1
        a[0] = -a[0]/abs(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, 0, 0, -1)
cnt1 = f(n, a, 0, 0, 1)
cnt = min([cnt0,cnt1])
print(cnt)