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: Accepted

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
a = [int(n) for n in input().split()]

count_a = 0 #+start
count_b = 0 #-start

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

Submission status: Accepted

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

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

    # first positive
    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

    # first negative
    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()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Runtime Error

Solution code:
def f():
  for i in a[1:]:
    if c > 0:
      if c + i >= 0:
        cost += c + i + 1
        c = -1
      else:
        c += i
    else:
      if c + i <= 0:
        cost += 1 - c - i
        c = 1
      else:
        c += i


n = int(input())
a = list(map(int, input().split()))
cost = (a[0] == 0)
c = a[0] + (a[0] == 0)
f()
tmp = cost
cost = 0
if a[0] >= 0:
  cost = a[0] + 1
  c = -1
else:
  cost = -a[0] + 1
  c = 1
f()
print(min(cost, tmp))

Submission status: Runtime Error

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

Cost = lambda a, Sum:0 if (Sum > 0) ^ (Sum + a > 0) else abs(Sum + a) + 1
Sum = lambda a, preSum:preSum+a if (preSum > 0) ^ (preSum + a > 0) else -preSum//abs(preSum)

pSum = Sum(A[0],0)
nSum = Sum(A[0],0)
pCost = 0
nCost = 0
for a in A[1:]:
    pCost += Cost(a, pSum)
    pSum = Sum(a, pSum)
    nCost += Cost(a, nSum)
    nSum = Sum(a, nSum)
print(min(pCost, nCost))

Submission status: Runtime Error

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

Cost = lambda a, Sum:0 if (Sum > 0) ^ (Sum + a > 0) else abs(Sum + a) + 1
Sum = lambda a, preSum:preSum+a if (Sum > 0) ^ (preSum + a > 0) else -preSum//abs(preSum)

pSum = Sum(A[0],0)
nSum = Sum(A[0],0)
pCost = 0
nCost = 0
for a in A[1:]:
    pCost += Cost(a, pSum)
    pSum = Sum(a, pSum)
    nCost += Cost(a, nSum)
    nSum = Sum(a, nSum)
print(min(pCost, nCost))

Submission status: Runtime Error

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
#import bisect
import numpy as np
#from copy import deepcopy
#from collections import deque
#from decimal import Decimal
#from numba import jit

INF = 1 << 50
EPS = 1e-8


def run():
    n, *A = map(int, read().split())
    v = 0
    acum = []
    for a in A:
        v += a
        acum.append(v)
    # greedy
    cums = 0
    count = 0
    V = A[0] // abs(A[0])

    for a in acum[1:]:
        #print(a, '---------')
        V *= -1
        if (a + cums) * V > 0:
            continue
        else:
            update = abs(a + cums) + 1
            cums += (update) * V
            count += update
            #print(V, cums, count)
    print(count)



if __name__ == "__main__":
    run()

Submission status: Runtime Error

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

def odd_positive(List, n):
    sum_a = 0
    cost = 0
    for a in List:
        nextsum = sum_a + a
        if i & 1 and nextsum <= 0:
            cost = - nextsum + 1
            sum_a = 1
        elif (not i & 1) and nextsum >= 0:
            cost = nextsum + 1
            sum_a = -1
    return cost


def odd_negative(List, n):
    sum_a = 0
    cost = 0
    for a in List:
        nextsum = sum_a + a
        if i & 1 and nextsum >= 0:
            cost = nextsum + 1
            sum_a = -1
        elif (not i & 1) and nextsum <= 0:
            cost = - nextsum + 1
            sum_a = 1
    return cost
    
ans = min(odd_positive(a,n), odd_negative(a,n)) 
print(ans)

Submission status: Runtime Error

Solution code:
def chk(a,t):     # t=True(奇数項が正), t=False(偶数項が正) 
    cnt=0   # 操作回数
    x=0     # 塁積和        
    for i in a:
        x+=i      
        if t==True and x<=0:       # 正項予定なのに累積が負か0
            cnt+=1-x                              
            x=x+(1-x)               # 今回のcntで符号変更
        elif t==False and x>=0:   # 負項予定なのに累積が正か0
            cnt+=1+x              
            x=x-(1+x)              # 今回のcntで符号変換
        t = not t                  # 次の項は符号が逆
    return cnt
print(min(chk(a,True),chk(a,False)))

Submission status: Runtime Error

Solution code:
#
# abc059 c
#

import sys
from io import StringIO
import unittest


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """4
1 -3 1 0"""
        output = """4"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """5
3 -6 4 -5 7"""
        output = """0"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """6
-1 4 3 2 -5 4"""
        output = """8"""
        self.assertIO(input, output)


def resolve():
    N = int(input())
    A = list(map(int, input().split()))

    ans = 0
    s = A[0]
    f = A[0] // abs(A[0])
    for i in range(1, N):
        a = A[i]
        if f == 1 and s+a >= 0:
            ans += abs(s+a) + 1
            s = -1
        elif f == -1 and s+a <= 0:
            ans += abs(s+a) + 1
            s = 1
        else:
            s += a
        f = s // abs(s)
    print(ans)


if __name__ == "__main__":
    # unittest.main()
    resolve()

Submission status: Runtime Error

Solution code:
#
# abc059 c
#

import sys
from io import StringIO
import unittest


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """4
1 -3 1 0"""
        output = """4"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """5
3 -6 4 -5 7"""
        output = """0"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """6
-1 4 3 2 -5 4"""
        output = """8"""
        self.assertIO(input, output)


def resolve():
    N = int(input())
    A = list(map(int, input().split()))

    ans = 0
    s = 0
    f = 0
    for a in A:
        if f == 1 and s+a >= 0:
            ans += abs(s+a) + 1
            s = -1
        elif f == -1 and s+a <= 0:
            ans += abs(s+a) + 1
            s = 1
        else:
            s += a
        f = s // abs(s)
    print(ans)


if __name__ == "__main__":
    # unittest.main()
    resolve()

Submission status: Runtime Error

Solution code:
def solve():

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

   # print(a)
   # print(b)

    i = 0
    sum = 0

    ans = 0

    for i in range(n-1):
        sum += a[i]
        if sum > 0 and sum+a[i+1] > 0:
            tmp = -1 - sum
            ans += abs(tmp - a[i+1])
            a[i+1] = tmp
        elif sum < 0 and sum+a[i+1] < 0:
            tmp = 1 - sum
            ans += abs(tmp - a[i+1])
            a[i+1] = tmp

   # print(ans)
   # print(a)

    tmp_ans_1 = ans

    ans = 0
    sum = 0
    i = 0

    if b[0] > 0:
        ans += abs(-1 - b[0])
        b[0] = -1
    elif b[0] < 0:
        ans += abs(1 - b[0])
        b[0] = 1
    for i in range(n-1):
        sum += b[i]
        if sum > 0 and sum+b[i+1] > 0:
            tmp = -1 - sum
            ans += abs(tmp - b[i+1])
            b[i+1] = tmp
        elif sum < 0 and sum+b[i+1] < 0:
            tmp = 1 - sum
            ans += abs(tmp - b[i+1])
            b[i+1] = tmp

   # print(ans)
   # print(b)           
    
    tmp_ans_2 = ans
    
    if tmp_ans_1 <= tmp_ans_2:
        ans = tmp_ans_1
    elif tmp_ans_2 < tnp_ans_1:
        ans = tmp_ans_2

    print(ans)

if __name__ ==  "__main__":
    solve()

Submission status: Runtime Error

Solution code:
n = int(input())
A = list(map(int,input().split()))
ans = 0
x = 0
S = []
S.append(A[0])
for i in range(1,n):
    x = A[i] + S[i - 1]
    if S[i - 1] > 0:
        if x >= 0:
            x = - x - 1
            ans += abs(x)
            S.append(-1)
        else:
            S.append(x)
    elif S[i - 1] < 0:
        if x <= 0:
            x = -x + 1
            ans += abs(x)
            S.append(1)
        else:
            S.append(x)
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
point = a[0]
ans = 0

for i in a[1:]:
    if point*i < 0:
        if abs(point) < abs(i):
            point += i
        else:
            ans += (abs(point)+1) - abs(i)
            point = -int(point/abs(point))
    else:
        ans += (abs(point)+1) + i
        point = -int(point/abs(point))
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
if a[0] != 0:
  print(0)
else:
  print(a[n+4])

Submission status: Runtime Error

Solution code:
n, *A = map(int, open(0).read().split())
B = [A[0]]
C = [A[0]]
D = [0]
for i in range(1, n):
  if (C[i-1] + A[i]) * C[i-1] < 0:
    B.append(A[i])
    C.append(C[i-1] + A[i])
    D.append(D[i-1])
  else:
    if C[i-1] > 0:
      B.append(-(C[i-1]+1))
      C.append(-1)
      D.append(C[i-1]+1 + A[i])
    elif C[i-1] < 0:
      B.append(-C[i-1]+1)
      C.append(1)
      D.append(-C[i-1]+1 + A[i])
print(A)
print(B)
print(C)
print(D)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int,input().split()))
num = a[0]
ans = 0
if a[0]>0:
    for i in range(1,n):
        num += a[i]
        if i%2==1:
            if num>=0:
                ans += num+1
                num = -1
        else:
            if num<=0:
                ans += 1-num
                num = 1
elif a[0]<0:
    for i in range(1,n):
        num += a[i]
        if i%2==1:
            if num <= 0:
                ans += 1-num
                num = 1
        else:
            if num >= 0:
                ans += num+1
                num = -1
elif a[0]= 0:
    ansp = 1
    ansm = 1
    for i in range(1,n):
        num += a[i]
        if i%2==1:
            if num>=0:
                ansp += num+1
                num = -1
        else:
            if num<=0:
                ansp += 1-num
                num = 1
    for i in range(1,n):
        num += a[i]
        if i%2==1:
            if num <= 0:
                ansm += 1-num
                num = 1
        else:
            if num >= 0:
                ansm += num+1
                num = -1
    ans = min(ansp,ansm)
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int,input().split()))
num = a[0]
ans = 0
if a[0]>0:
    for i in range(1,n):
        num += a[i]
        if i%2==1:
            if num>=0:
                ans += num+1
                num = -1
        else:
            if num<=0:
                ans += 1-num
                num = 1
elif a[0]<0:
    for i in range(1,n):
        num += a[i]
        if i%2==1:
            if num <= 0:
                ans += 1-num
                num = 1
        else:
            if num >= 0:
                ans += num+1
                num = -1
else:
    num = 0
    ansp = 1
    ansn = 1
    for i in range(1,n):
        num += a[i]
        if i%2==1:
            if num>=0:
                ansp += num+1
                num = -1
        else:
            if num<=0:
                ansp += 1-num
                num = 1
    for i in range(1,n):
        num += a[i]
        if i%2==1:
            if num <= 0:
                ansm += 1-num
                num = 1
        else:
            if num >= 0:
                ansm += num+1
                num = -1
    ans = min(ansp,ansm)
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
l = len(a)
b = [int(-a[0]/ abs(a[0]))]
for i in range(1, l):
  b.append(a[i])
ans = 0
summary = a[0]

if(summary == 0):
  if(a[1] > 0):
    summary = -1
    ans+= 1
  else:
    summary = 1
    ans+= 1

for i in range(1, l):
  if(summary* (summary+ a[i])>= 0):
    if(summary > 0):
      ans+= a[i]+ summary+ 1
      a[i] = -summary- 1 
      summary= -1
    else:
      ans+= -summary+ 1- a[i]
      a[i] = -summary+ 1
      summary= 1
  else:
    summary+= a[i]

Ans = 0
Summary = b[0]

if(Summary == 0):
  if(b[1] > 0):
    Summary = -1
    Ans+= 1
  else:
    Summary = 1
    Ans+= 1

for i in range(1, l):
  if(Summary* (Summary+ b[i])>= 0):
    if(Summary > 0):
      Ans+= b[i]+ Summary+ 1
      b[i] = -Summary- 1 
      Summary= -1
    else:
      Ans+= -Summary+ 1- b[i]
      b[i] = -Summary+ 1
      Summary= 1
  else:
    Summary+= b[i]
    
print(min(ans, Ans))

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
from itertools import accumulate

def main():
    n = int(input())
    a = list(map(int, input().split()))
    a = list(accumulate(a))
    
    if a[0] != 0:
        ans = 0
        diff = 0
        for i in range(1,n):
            p = a[i] + diff
            q = a[i-1] + diff
            if p * q >= 0:
                tmp = -q//abs(q) - p
                ans += abs(tmp)
                diff += tmp
        print(ans)
    else:
        ans = 10**18
        for d in [-1,1]:
            ans2 = 1
            for i in range(1,n):
                p = a[i] + d
                q = a[i-1] + d
                if p * q >= 0:
                    tmp = -q//abs(q) - p
                    ans2 += abs(tmp)
                    diff += tmp
            ans = min(ans, ans2)
        print(ans)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

def sol(S):
    ret = 0
    B = [S]
    for a in A[1:]:
        b = a
        if S * (S + b) > 0:
            b = (abs(S) + 1) * (1 if S < 0 else -1)
        if S + b == 0:
            b = b - 1 if b < 0 else b + 1
        ret += abs(b - a)
        S += b
        B.append(b)
    return ret

ans = min(
    sol(A[0]),
    sol(-A[0] // abs(A[0])) + abs(A[0]) + 1
)
print(ans)

Submission status: Runtime Error

Solution code:
import sys


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(20000000)


def main():
    N = int(input())
    A = list(map(int, input().split()))
    if A[0] == 0:
        answer = []
        for a in (1, -1):
            cnt = 1
            S = a
            for i in range(1, N):
                s = S + A[i]
                if A[i] == 0:
                    if S > 0:
                        cnt += S + 1
                        S = -1
                    else:
                        cnt += abs(S) + 1
                        S = 1
                else:
                    if s == 0:
                        if S < 0:
                            cnt += 1
                            S = 1
                        else:
                            cnt += 1
                            S = -1
                    else:
                        if S * s > 0:
                            if S < 0:
                                cnt += abs(s) + 1
                                S = 1
                            else:
                                cnt += s + 1
                                S = -1
                        else:
                            S = s
            answer.append(cnt)
        print(min(answer))
    else:
        if A[0] >0:
            cnt1 = 0
            S1 = A[0]
            cnt2 = A[0]+1
            S1 = -1
        else:
            cnt1 = 0
            S1= A[0]
            cnt2 = abs(A[0])+1
            S2 = 1
        answer = []
        for cnt,S in ((cnt1,S1),(cnt2,S2)):
            for i in range(1, N):
                s = S + A[i]
                if A[i] == 0:
                    if S > 0:
                        cnt += S + 1
                        S = -1
                    else:
                        cnt += abs(S) + 1
                        S = 1
                else:
                    if s == 0:
                        if S < 0:
                            cnt += 1
                            S = 1
                        else:
                            cnt += 1
                            S = -1
                    else:
                        if S * s > 0:
                            if S < 0:
                                cnt += abs(s) + 1
                                S = 1
                            else:
                                cnt += s + 1
                                S = -1
                        else:
                            S = s
            answer.append(cnt)
        print(min(answer))


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
counter = 0            ####操作回数
A.reverse()
S = 0           
a = A.pop()
if a==0:
  counter += 1
  while A:
    b = A.pop()
    if b == 0:
      counter += 2
    elif b>0:
      A.append(b)
      S = -1
      break
    elif b<0:
      A.append(b)
      S = 1
      break
else:
  S += a
    
    
while A:
  c = A.pop()
  if c>=0 and S>0:
    counter += abs(c+S)+1
    S = -1
  elif c<=0 and S<0:
    counter += abs(c+S)+1
    S = 1
  elif (c>=0 and S<0) and S+c<=0:
    counter += abs(S+c)+1
    S = 1
  elif (c=<0 and S>0) and S+c>=0:
    counter += abs(S+c)+1
    S = -1    
  else:
    S += c
    
print(counter)

Submission status: Runtime Error

Solution code:
def sign(x):
  if x<0:
    return -1
  elif x>0:
    return 1
  else:
    retrun 0
n = int(input())
a = list(map(int,input().split()))
cumulative_sum = a[0]
flag = sign(cumulative_sum)
ans = 0
for i in range(1,n):
  cumulative_sum += a[i]
  if sign(cumulative_sum) == flag or sign(cumulative_sum) == 0:
    ans += abs(-flag - cumulative_sum)
  flag = sign(cumulative_sum)
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
from functools import reduce

N =int(input())
A = map(int, input().split())
def check(A):
  arr = [0]*(N+1)
  for i in range(len(A)):
    arr[i+1] += arr[i-1] + ai
    if arr[i+1]*arr[i] <=0:
      return 0
  val = abs(min(arr))
  return val

print(val)

Submission status: Runtime Error

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

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

print(cnt)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
s, ans, i = 0, 0, 0
while a[i] == 0 and i < n:
    ans += 2
    i += 1
ans = max(0, ans - 1)
if i == n:
    print(ans)
    exit()

if ans > 0:
    ans -= 1
    if abs(a[i]) == 1:
        ans += 1
        s = a[i] // abs(a[i])
else:
    s = a[0]

i += 1
for j in range(i, n):
    if abs(a[j]) > abs(s) and (a[j] == abs(a[j])) != (s == abs(s)):
        s += a[j]
    else:
        pre_s = s
        s = -1 * s // abs(s)
        ans += abs(a[j] - s + pre_s)

print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
s, ans, i = 0, 0, 0
while a[i] == 0 and i < n:
    ans += 2
    i += 1
ans = max(0, ans - 1)
if i == n:
    print(ans)
    exit()

if ans > 0:
    if abs(a[i]) == 1:
        ans += 1
        s = a[i] // abs(a[i])
else:
    s = a[0]

i += 1
for j in range(i, n):
    if abs(a[j]) > abs(s) and (a[j] == abs(a[j])) != (s == abs(s)):
        s += a[j]
    else:
        pre_s = s
        s = -1 * s // abs(s)
        ans += abs(a[j] - s + pre_s)

print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
s, ans, i = 0, 0, 0
while a[i] == 0 and i < n:
    ans += 2
    i += 1
ans = max(0, ans - 1)
if i == n:
    print(ans)
    exit()

if ans > 0:
    if abs(a[i]) == 1:
        ans += 1
        s = a[i] // abs(a[i])
else:
    s = a[0]

i += 1
for j in range(i, n):
    if abs(a[j]) > abs(s) and a[j] // abs(a[j]) != s // abs(s):
        s += a[j]
    else:
        pre_s = s
        s = -1 * s // abs(s)
        ans += abs(a[j] - s + pre_s)

print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int,input().split()))
s, ans, i = 0,0,0
while a[i] == 0 and i < n:
  ans += 2
  i += 1
ans = max(0, ans - 1)
if i == n:
  print(ans)
  exit()

if ans > 0:
  if abs(a[i]) == 1:
    ans += 1
    s = a[i] // abs(a[i])
else:
  s = a[0]

i += 1
for j in range(i,n):
  if abs(a[j]) > abs(s) and a[j] // abs(a[j]) != s // abs(s):
    s += a[j]
  else:
    s = -1 * s // abs(s)
    ans += abs(abs(a[j]) - 2)
  
  
print(ans)

Submission status: Runtime Error

Solution code:
a = list(map(int,input().split()))
def judge(count):
  idx = 0
  for i in range(2,n+1):
    if count == -1:
      if sum(a[0:i]) < 0:
        pass
      else:
        check = sum(a[0:i]) + 1
        j = a[i-1]
        a[i-1] = j - check
        idx += check
    elif count == 1:
      if sum(a[0:i]) > 0:
        pass
      else:
        check = 1 - sum(a[0:i])
        j = a[i-1]
        a[i-1] = check+ j
        idx += check
    count *= -1
  return idx
if a[0] > 0:
  b = judge(-1)
  print(b)
elif a[0] < 0:
  c = judge(1)
  print(c)
else:
  a[0] = 1
  b = judge(-1)
  a[0] = -1
  c = judge(1)
  print(min(b,c))

Submission status: Runtime Error

Solution code:
def resolve(SL):
    # L[0]!=0を起点とする
    cnt = 0
    for i in range(len(SL)-1):
        s0 = SL[i]
        s1 = SL[i+1]
        if(s0>0 and s1>=0):
            SL[(i+1):] = [s-(s1+1) for s in SL[(i+1):]]
            cnt += (s1+1)
        elif(s0<0 and s1<=0):
            SL[(i+1):] = [s+(-s1+1) for s in SL[(i+1):]]
            cnt += (-s1+1)  
#         print(SL)
    return cnt
           
def ans(L):
    SL = [sum(L[:(i+1)]) for i in range(len(L))]
    c0,c1=0,0
    
    if (L[0]>0):
        c0 = resolve(SL)
        c1 = (L[0]+1) + resolve(list(map(lambda x:x-(L[0]+1), SL)))
    elif (L[0]<0):
        c0 = resolve(L)
        c1 = (-L[0]+1) + resolve(list(map(lambda x:x+(-L[0]+1), SL)))
    else:
        c0 = 1 + resolve(list(map(lambda x:x+1, SL)))
        c1 = 1 + resolve(list(map(lambda x:x-1, SL)))
    
    return(min(c0,c1))

N = int(input())
L = [int(input(x)) for x in range(N)]

print(ans(L))

Submission status: Runtime Error

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

#      0  1  2  3  evn odd
# kevn +  -  +  -   +   -
# kodd -  +  -  +   -   +
def kf(a,flag):
    a0=a[0]*flag
    if a0<=0:
        kevn=1-a0
        sevn=1
    elif a0>0:
        kevn=0
        sevn=a0

    for i in range(1,n):
        ai=a[i]*flag
        if i%2==0:
            if sevn+ai<=0:
                kevn=kevn+1-(ai+sevn)
                sevn=1
            elif sevn+ai>0:
                kevn=kevn
                sevn=sevn+ai
        elif i%2==1:
            if sevn+ai<0:
                kevn=kevn
                sevn=sevn+ai
            elif sevn+ai>=0:
                kevn=kevn+1+(ai+sevn)
                sevn=-1
    return kevn

kevn1=kf(a,1,n)
kevn=kf(a,-1,n)

kevn=min(kevn,kevn1)

print(kevn)

Submission status: Runtime Error

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

ans = 0
for i in range(n):
    if i == 0:
        if a[0] > 0:
            f = "+"
        elif a[0] < 0:
            f = "-"
    else:
        if f == "+":
            if a[i] + sum(a[:i]) > 0:
                c = -1 - sum(a[:i])
                ans += abs(c - a[i])
                a[i] = c
                f = "-"
            else:
                if a[i] + sum(a[:i]) == 0:
                    a[i] -= 1
                    ans += 1
                f = "-"
        elif f == "-":
            if a[i] + sum(a[:i]) < 0:
                c = 1 - sum(a[:i])
                ans += abs(c - a[i])
                a[i] = c
                f = "+"
            else:
                if a[i] + sum(a[:i]) == 0:
                    a[i] += 1
                    ans += 1
                f = "+"
#print(a)
print(ans)