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: Time Limit Exceeded

Solution code:
import numpy as np

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

# 奇数がプラス、偶数がマイナス
cost1 = 0
cum = arr.cumsum()
for i in range(n):
    tmp = abs(cum[i]) + 1
    if i%2==1 and cum[i]<=0: cost1 += tmp ; cum[i:] += tmp
    if i%2==0 and cum[i]>=0: cost1 += tmp ; cum[i:] -= tmp

# 奇数がマイナス、偶数がプラス
cost2 = 0
cum = arr.cumsum()
for i in range(n):
    tmp = abs(cum[i]) + 1
    if i%2==1 and cum[i]>=0: cost2 += tmp ; cum[i:] -= tmp
    if i%2==0 and cum[i]<=0: cost2 += tmp ; cum[i:] += tmp

ans = min(cost1, cost2)
print(ans)

Submission status: Time Limit Exceeded

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

'''
True = positive
False = negative
'''
# sign = True
# if now_a < 0:
#     sign = False
#
# for i in range(1, n):
#     next_a = now_a + a[i]
#     if sign:
#         if next_a >= 0:
#             count += next_a + 1
#             now_a = -1
#         else:
#             now_a = next_a
#         sign = False
#     else:
#         if next_a <= 0:
#             count += abs(next_a) + 1
#             now_a = 1
#         else:
#             now_a = next_a
#         sign = True
# print(count)

sa = a[0]
sign = True
if sa < 0:
    sign = False

for i in range(1, n):
    na = sum(a[:i+1])
    if sign:
        if na >= 0:
            a[i] += -1 * (na + 1)
            count += na + 1
        sign = False
    elif not sign:
        if na <= 0:
            a[i] += 1 - na
            count += 1 - na
        sign = True
print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def sgn(n):
    if n>0:
        return 1
    elif n==0:
        return 0
    else:
        return -1
    
class SegmentTree:
    # 初期化処理
    # f : SegmentTreeにのせるモノイド
    # default : fに対する単位元
    def __init__(self, size, f=lambda x,y : x+y, default=0):
        self.size = 2**(size-1).bit_length() # 簡単のため要素数Nを2冪にする
        self.default = default
        self.dat = [default]*(self.size*2-1) # 要素を単位元で初期化
        self.f = f

    def update(self, i, x):
        i += self.size-1
        self.dat[i] = x
        while i > 0:
            i = (i-1) >> 1
            self.dat[i] = self.f(self.dat[i*2+1], self.dat[i*2+2])

    def query(self, l, r, k=0, L=0, R=None):
        if R is None:
            R = self.size
        if R <= l or r <= L:
            return self.default
        if l <= L and R <= r:
            return self.dat[k]
        else:
            lres = self.query(l, r, k*2+1, L, (L+R) >> 1)
            rres = self.query(l, r, k*2+2, (L+R) >> 1, R)
            return self.f(lres, rres)
        
n=int(input())
b=[int(i) for i in input().split()]
l = SegmentTree(size=n)
p0=0
for i in range(n):
    l.update(i,b[i])
for i in range(n):
    sg=(-1)**(i)
    v=l.query(0,i+1)
    #print(v,p0)
    if sgn(v) != sg:
        tmp= abs(v)+1
        #print(f'{b[i]}---{tmp}--->{sg}')
        p0 += tmp
        l.update(i,b[i]+tmp*sg)
        
l = SegmentTree(size=n)
for i in range(n):
    l.update(i,b[i])
p1=0
#print()
for i in range(n):
    sg=(-1)**(i+1)
    v=l.query(0,i+1)
    #print(v,p1)
    if sgn(v) != sg:
        tmp= abs(v)+1
        #print(f'{b[i]}---{tmp}--->{sg}')
        p1 += tmp
        l.update(i,b[i]+tmp*sg)
    #print(l.query(0,i+1),p1)
print(min(p0,p1))

Submission status: Time Limit Exceeded

Solution code:
from itertools import accumulate
def sgn(n):
    if n>0:
        return 1
    elif n==0:
        return 0
    else:
        return -1
n=int(input())
a=[int(i) for i in input().split()]
b=list(accumulate(a))
#print(list(accumulate(a)))
if b[0]>0:
    f=1
else:
    f=-1
ans=0
for i in range(n):
    #print(i,b)
    if ((i%2==0) and(sgn(b[i])!=f)) or ((i%2==1) and(sgn(b[i])!=-f)):
        tmp = abs(b[i])+1
        ans += tmp
        for j in range(i,n):
            b[j] += -tmp*f*(-1)**(i-1)
#print(b)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
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: Time Limit Exceeded

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)  
    return cnt
            
def ans(L):
#     SL = [sum(L[:(i+1)]) for i in range(len(L))]
    a = L[0]
    c0,c1=0,0
    SL = []
    s = 0
    for i,l in enumerate(L):
        s += L[i]
        SL.append(s)

    if (a>0):
        c0 = resolve(SL)
        c1 = (a+1) + resolve(list(map(lambda x:x-(a+1), SL)))
    elif (a<0):
        c0 = resolve(SL)
        c1 = (-a+1) + resolve(list(map(lambda x:x+(-a+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(x) for x in input().split(' ')]

print(ans(L))

Submission status: Time Limit Exceeded

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)  
    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(SL)
        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(x) for x in input().split(' ')]
 
print(ans(L))

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import sys
import math

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

pair = 0
odd = 0


for i in range(0, len(a), 2):
    pair += a[i]

for i in range(1, len(a), 2):
    odd += a[i]

#print(odd)
#print(pair)


count = 0
current_sum = 0
for i in range(len(a)):
    ## odd
    if i % 2 == 1 and odd > pair:
        while current_sum + a[i] < 1:
            a[i] += 1
            count += 1
    ## odd
    elif i % 2 == 1 and odd < pair:
        while current_sum + a[i] > -1:
            a[i] -= 1
            count += 1
    ## pair
    elif i % 2 == 0 and odd > pair:
        while current_sum + a[i] > -1:
            a[i] -= 1
            count += 1
    elif i % 2 == 0 and odd < pair:
        while current_sum + a[i] < 1:
            a[i] += 1
            count += 1
    else:
        print("error")

    current_sum += a[i]


print(count)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

ans = 0
for i in range(n):
    if i == 0:
        if a[i] == 0:
            f = "+"
            a[i] = 1
        elif 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)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int,input().split()))
A = [n for n in a]

cnt1 = 0
cnt2 = 0
S = 0

if a[0] > 0:
    for i in range(n-1):
        S += a[i]
        if a[i] > 0:
            while S + a[i+1] >= 0:
                a[i+1] -= 1
                cnt1 += 1
        elif a[i] < 0:
            while S + a[i+1] <= 0:
                a[i+1] += 1
                cnt1 += 1
    A[0] = -1
    cnt2 = a[0] + 1
    for i in range(n-1):
        S += A[i]
        if A[i] > 0:
            while S + A[i+1] >= 0:
                A[i+1] -= 1
                cnt2 += 1
        elif A[i] < 0:
            while S + A[i+1] <= 0:
                A[i+1] += 1
                cnt2 += 1
elif a[0] < 0:
    for i in range(n-1):
        S += a[i]
        if a[i] > 0:
            while S + a[i+1] >= 0:
                a[i+1] -= 1
                cnt1 += 1
        elif a[i] < 0:
            while S + a[i+1] <= 0:
                a[i+1] += 1
                cnt1 += 1
    A[0] = 1
    cnt2 = -a[0] + 1
    for i in range(n-1):
        S += A[i]
        if A[i] > 0:
            while S + A[i+1] >= 0:
                A[i+1] -= 1
                cnt2 += 1
        elif A[i] < 0:
            while S + A[i+1] <= 0:
                A[i+1] += 1
                cnt2 += 1
else:
    a[0] = 1
    cnt1 = 1
    for i in range(n-1):
        S += a[i]
        if a[i] > 0:
            while S + a[i+1] >= 0:
                a[i+1] -= 1
                cnt1 += 1
        elif a[i] < 0:
            while S + a[i+1] <= 0:
                a[i+1] += 1
                cnt1 += 1
    A[0] = -1
    cnt2 = 1
    for i in range(n-1):
        S += A[i]
        if A[i] > 0:
            while S + A[i+1] >= 0:
                A[i+1] -= 1
                cnt2 += 1
        elif A[i] < 0:
            while S + A[i+1] <= 0:
                A[i+1] += 1
                cnt2 += 1
cnt = cnt2 if cnt1 >= cnt2 else cnt1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
#
# Written by NoKnowledgeGG @YlePhan
# ('ω')
#
#import math
#mod = 10**9+7
#import itertools
#import fractions
#import numpy as np
#mod = 10**4 + 7
"""def kiri(n,m):
  r_ = n / m
  if (r_ - (n // m)) > 0:
    return (n//m) + 1
  else:
    return (n//m)"""

""" n! mod m 階乗
mod = 1e9 + 7
N = 10000000
fac = [0] * N
def ini():
  fac[0] = 1 % mod
  for i in range(1,N):
    fac[i] = fac[i-1] * i % mod"""

"""mod = 1e9+7
N = 10000000
pw = [0] * N
def ini(c):
  pw[0] = 1 % mod
  for i in range(1,N):
    pw[i] = pw[i-1] * c % mod"""

"""
def YEILD():
  yield 'one'
  yield 'two'
  yield 'three'
generator = YEILD()
print(next(generator))
print(next(generator))
print(next(generator))
"""
"""def gcd_(a,b):
  if b == 0:#結局はc,0の最大公約数はcなのに
    return a
  return gcd_(a,a % b) # a = p * b + q"""
"""def extgcd(a,b,x,y):
  d = a
  if b!=0:
    d = extgcd(b,a%b,y,x)
    y -= (a//b) * x
    print(x,y)
  else:
    x = 1
    y = 0
  return d"""


def readInts():
  return list(map(int,input().split()))
mod = 10**9 + 7
def main():
  n = int(input())
  A = readInts()
  # 符号 positive?
  #po_ = True 
  # 変わったか変わってないか
  if A[0] >= 0: # if positive
    po_ = True
  else: # negative
    po_ = False
  Cost = 0
  for i in range(1,n):
    #print(sum(A[:i+1]),A[i],po_)
    if sum(A[:i+1]) >= 0 and not po_: # sumがpositiveで前がnegativeだった
      po_ = True # positiveに
      if sum(A[:i+1]) == 0:
        A[i] += 1
        Cost += 1
      # これで終わり
    elif sum(A[:i+1]) >= 0 and po_: # posi : posi ?
      # 負にしなければならない
      Cost += abs(-1 - sum(A[:i+1])) # 先にこれやれ
      A[i] += -1 - sum(A[:i+1])
      po_ = False
    elif sum(A[:i+1]) < 0 and not po_: #nega : nega
      # -1 はここ
      # print(A[i])
      Cost += abs(1 - sum(A[:i+1])) # 先にこれやれ
      A[i] += 1 - sum(A[:i+1])
      po_ = True
    else: # nega: pos
      po_ = False
      if sum(A[:i+1]) == 0:
        A[i] -=1
        Cost -=1
    #print(A[i])
  print(Cost)
if __name__ == '__main__':
  main()

Submission status: Time Limit Exceeded

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

for i in range(1,n):
    if a[0]>0:
        if i%2==1 and sum(a[0:i+1])>=0:
              count += abs(sum(a[0:i+1])+1)
              a[i] = -(sum(a[0:i])+1)
        elif i%2==0 and sum(a[0:i+1])<=0:
              count += abs(sum(a[0:i+1])-1)
              a[i] = a[i]+abs(sum(a[0:i+1])-1)
    else:
        if i%2==1 and sum(a[0:i+1])<=0:
              count += abs(sum(a[0:i+1])-1)
              a[i] = a[i]+abs(sum(a[0:i+1])-1)
        elif i%2==0 and sum(a[0:i+1])>=0:
              count += abs(sum(a[0:i+1])+1)
              a[i] = -(sum(a[0:i])+1)
print(count)

Submission status: Time Limit Exceeded

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

x=0#pmpm
y=0

for i in range(n):
  if(i%2==0):
    if(sum(a[:i+1])<=0):
      x+=1-sum(a[:i+1])
      a[i]+=1-sum(a[:i+1])
    if(sum(b[:i+1])>=0):
      y+=sum(b[:i+1])+1
      b[i]-=sum(b[:i+1])+1
  else:
    if(sum(a[:i+1])>=0):
      x+=sum(a[:i+1])+1
      a[i]-=1+sum(a[:i+1])
    if(sum(b[:i+1])<=0):
      y+=1-sum(b[:i+1])
      b[i]+=1-sum(b[:i+1])
print(min(x,y))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
cnt = 0
if a[0] < 0:
    flag = -1
else:
    flag = 1
for i in range(1,n):
    if flag == -1:
        if sum(a[0:i]) + a[i] <= 0:
            cnt += -(sum(a[0:i]) + a[i]) + 1
            a[i] = -sum(a[0:i]) + 1
        flag = 1
    elif flag == 1:
        if sum(a[0:i]) + a[i] >= 0:
            cnt += sum(a[0:i]) + a[i] + 1
            a[i] = -sum(a[0:i]) - 1
        flag = -1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
S = list(map(int, input().split()))

mod_count = 0
pre_fugou = 1 ## 前の符号がなんであったか　1ならplus　0ならマイナス

for i in range(N):
    if i==0 and S[i]==0:
        S[i] += 1
        mod_count += 1
        pre_fugou = 1
    elif i==0 and S[i]>0:
        pre_fugou = 1
    elif i==0 and S[i]<0:
        pre_fugou = 0
    else:
        if pre_fugou == 1:
            while True:
                if sum(S[:i+1]) > -1:
                    S[i] -= 1
                    mod_count += 1
                    pre_fugou = 0
                else:
                    pre_fugou = 0
                    break
        elif pre_fugou == 0:
            while True:
                if sum(S[:i+1]) < 1:
                    S[i] += 1
                    mod_count += 1
                    pre_fugou = 1
                else:
                    pre_fugou = 1
                    break
print(mod_count)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n=int(input())
a=[int(i) for i in input().split()]
ap = np.array(a)
an = np.array(a)
#+-+-...
if ap[0] <= 0:
    ap[0] = 1
for i in range(1, n):
    if sum(ap[:i+1]) <= 0 and i%2 == 0:
        ap[i] = 1 - sum(ap[:i]) 
    if sum(ap[:i+1]) >= 0 and i%2 == 1:
        ap[i] = -1 - sum(ap[:i])

#-+-+...
if an[0] >= 0:
    an[0] = -1
for i in range(1, n):
    if sum(an[:i+1]) <= 0 and i%2 == 1:
        an[i] = 1 - sum(an[:i]) 
    if sum(an[:i+1]) >= 0 and i%2 == 0:
        an[i] = -1 - sum(an[:i])
        
print(min(sum(abs(np.array(a) - ap)), sum(abs(np.array(a) - an))))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

numbers = list(map(int, input().split()))
counter = 0

for i in range(len(numbers) - 1):
    sum_i_n = sum(numbers[:i + 1])
    sum_i_n_1 = sum(numbers[:i + 2])

    if sum_i_n == 0:
        numbers[i] += 1
        counter += 1
    if sum_i_n * sum_i_n_1 > 0:
        if sum_i_n_1 > 0:
            numbers[i + 1] -= (sum_i_n_1 + 1)
            counter += sum_i_n_1 + 1
        else:
            numbers[i + 1] += (abs(sum_i_n_1) + 1)
            counter += abs(sum_i_n_1) + 1
if sum(numbers) == 0:
    counter += 1

print(counter)

Submission status: Time Limit Exceeded

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

cnt=0
for i in range(1,n):
    
    # 条件満たすまでループ
    while True:
        #print(a)
    
        now_tmp = sum(a[:i])
        next_tmp = sum(a[:i+1])
        #print(i, now_tmp, next_tmp)
        
        # 符号が逆転していればOK かつ 現在までの総和が0でない
        #  異なる符号を掛けるとマイナスになる
        if now_tmp * next_tmp <0 and now_tmp !=0:
            break
        else:
            # 現在の合計がマイナスの場合
            if now_tmp < 0:
                a[i] += next_tmp+1
                cnt +=abs(next_tmp+1)

            # 現在の合計がプラスの場合
            elif now_tmp > 0 :
                a[i] += -next_tmp-1
                cnt +=abs(next_tmp+1)
            # 現在の合計が0の場合
            elif now_tmp == 0 :
                # 1個前がプラスの場合、
                if sum(a[:i-1]) > 0:
                    a[i] -=1
                    cnt +=1
                # 1個前がマイナスの場合
                else:
                    a[i] +=1
                    cnt +=1
                
print(cnt)

Submission status: Time Limit Exceeded

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

cnt=0
for i in range(1,n):
    
    # 条件満たすまでループ
    while True:
        #print(a[:i])
    
        now_tmp = sum(a[:i])
        next_tmp = sum(a[:i+1])
        #print(now_tmp, next_tmp)
        
        # 符号が逆転していればOK かつ 現在までの総和が0でない
        #  異なる符号を掛けるとマイナスになる
        if now_tmp * next_tmp <0 and now_tmp !=0:
            break
        else:
            # 現在の合計がマイナスの場合
            if sum(a[:i]) < 0 :
                a[i] +=1
                cnt +=1

            # 現在の合計がプラスの場合
            elif sum(a[:i]) > 0 :
                a[i] -=1
                cnt +=1
            # 現在の合計が0の場合
            elif sum(a[:i]) == 0 :
                a[i] +=1
                cnt +=1
                
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

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


def solve(cnt,A,N):
    for i in range(1, N):
        if sum(A[0:i])>0:
            if sum(A[0:i+1])>=0:
                r = A[i]
                A[i]=-sum(A[0:i])-1
                cnt+=abs(r-A[i])
        else:
            if sum(A[0:i+1])<=0:
                r = A[i]
                A[i]=-sum(A[0:i])+1
                cnt+=abs(r-A[i])
    return cnt

cnt1=0
if b[0]<=0:
    ini=b[0]
    b[0]=1
    cnt1=abs(1-ini)


ans1=solve(cnt1,b,n)
cnt2=0
if a[0]>=0:
    ini=a[0]
    a[0]=-1
    cnt2=abs(-1-ini)


ans2=solve(cnt2,a,n)
print(min(ans1,ans2))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

a = list(int(i) for i in input().split())
b = [i for i in a]


def solve(cnt,A,N):
    for i in range(1, N):
        if sum(A[0:i])>0:
            if sum(A[0:i+1])>=0:
                r = A[i]
                A[i]=-sum(A[0:i])-1
                cnt+=abs(r-A[i])
        else:
            if sum(A[0:i+1])<=0:
                r = A[i]
                A[i]=-sum(A[0:i])+1
                cnt+=abs(r-A[i])
    return cnt

cnt1=0
if b[0]<=0:
    ini=b[0]
    b[0]=1
    cnt1=abs(1-ini)


ans1=solve(cnt1,b,n)
cnt2=0
if a[0]>=0:
    ini=a[0]
    a[0]=-1
    cnt2=abs(-1-ini)


ans2=solve(cnt2,a,n)
print(min(ans1,ans2))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

a = list(int(i) for i in input().split())
b = []
for i in range(0,len(a)):
    b.append(a[i])


def solve(cnt,A,N):
    for i in range(1, N):
        if sum(A[0:i])>0:
            if sum(A[0:i+1])>=0:
                r = A[i]
                A[i]=-sum(A[0:i])-1
                cnt+=abs(r-A[i])
        else:
            if sum(A[0:i+1])<=0:
                r = A[i]
                A[i]=-sum(A[0:i])+1
                cnt+=abs(r-A[i])
    return cnt

cnt1=0
if b[0]<=0:
    ini=b[0]
    b[0]=1
    cnt1=abs(1-ini)


ans1=solve(cnt1,b,n)
cnt2=0
if a[0]>=0:
    ini=a[0]
    a[0]=-1
    cnt2=abs(-1-ini)


ans2=solve(cnt2,a,n)
print(min(ans1,ans2))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

a = list(int(i) for i in input().split())
b = []
for i in range(0,len(a)):
    b.append(a[i])


def solve(cnt,A,N):
    for i in range(1, N):
        if sum(A[0:i])>0:
            if sum(A[0:i+1])>=0:
                r = A[i]
                A[i]=-sum(A[0:i])-1
                cnt+=abs(r-A[i])
        else:
            if sum(A[0:i+1])<=0:
                r = A[i]
                A[i]=-sum(A[0:i])+1
                cnt+=abs(r-A[i])
    return cnt

cnt1=0
if b[0]<=0:
    while b[0]<=0:
        b[0]+=1
        cnt1+=1

ans1=solve(cnt1,b,n)
cnt2=0
if a[0]>=0:
    while a[0]>=0:
        a[0]-=1
        cnt2+=1

ans2=solve(cnt2,a,n)
print(min(ans1,ans2))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

a = list(int(i) for i in input().split())
b = []
for i in range(0,len(a)):
    b.append(a[i])


def solve(cnt,A,N):
    for i in range(1, N):
        if sum(A[0:i])>0:
            while sum(A[0:i+1])>=0:
                A[i]-=1
                cnt+=1
        else:
            while sum(A[0:i+1])<=0:
                A[i]+=1
                cnt+=1
    return cnt

cnt1=0
if b[0]<=0:
    while b[0]<=0:
        b[0]+=1
        cnt1+=1

ans1=solve(cnt1,b,n)
cnt2=0
if a[0]>=0:
    while a[0]>=0:
        a[0]-=1
        cnt2+=1

ans2=solve(cnt2,a,n)
print(min(ans1,ans2))

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    input = sys.stdin.readline

    n = int(input())
    a = list(map(int, input().split()))
    A = []
    A_append = A.append
    cnt = 0

    for i in range(n-1):
        A_append((a[i]))
        x = sum(A)
        if x > 0 and x + a[i+1] > 0:
            y = -(x + a[i+1] + 1)
            cnt -= y
            a[i+1] += y

        elif x < 0 and x + a[i+1] < 0:
            y = -(x - a[i+1]+ 1)
            cnt += y
            a[i+1] += y

    if sum(a) == 0:
        cnt += 1

    print(cnt)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:

def main():
    import sys
    input = sys.stdin.readline

    n = int(input())
    a = list(map(int, input().split()))
    A = []
    A_append = A.append
    cnt = 0

    for i in range(n-1):
        A_append((a[i]))
        x = sum(A) + a[i+1]
        if sum(A) > 0 and x > 0:
            y = -(x+1)
            cnt -= y
            a[i+1] += y

        elif sum(A) < 0 and x < 0:
            y = -(sum(A) - a[i+1])-1
            cnt += y
            a[i+1] += y

    if sum(a) == 0:
        cnt += 1

    print(cnt)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    input = sys.stdin.readline

    n = int(input())
    a = list(map(int, input().split()))
    A = []
    A_append = A.append
    cnt = 0

    for i in range(n-1):
        A_append((a[i]))
        x = sum(A) + a[i+1]
        if sum(A) > 0 and x > 0:
            y = abs(x)+1
            cnt += y
            a[i+1] -= y

        elif sum(A) < 0 and x < 0:
            y = abs(sum(A) - a[i+1])-1
            cnt += y
            a[i+1] += y

    if sum(a) == 0:
        cnt += 1

    print(cnt)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    input = sys.stdin.readline

    n = int(input())
    a = list(map(int, input().split()))
    A = []
    A_append = A.append

    cnt = 0


    for i in range(n-1):
        A_append(a[i])
        x = sum(A) + a[i+1]
        if sum(A) > 0 and x > 0:
            y = abs(x)+1
            cnt += y
            a[i+1] -= y

        elif sum(A) < 0 and x < 0:
            y = abs(sum(A) - a[i+1])-1
            cnt += y
            a[i+1] += y

    if sum(a) == 0:
        cnt += 1

    print(cnt)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

for i in range(n-1):
    A.append(a[i])
    x = sum(A) + a[i+1]
    if sum(A) > 0 and x > 0:
        y = abs(x)+1
        cnt += y
        a[i+1] -= y

    elif sum(A) < 0 and x < 0:
        y = abs(sum(A) - a[i+1])-1
        cnt += y
        a[i+1] += y

if sum(a) == 0:
    cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

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

for i in range(n-1):
    A.append(a[i])
    if sum(A) > 0 and sum(A) + a[i+1] > 0:
        cnt += abs(sum(A) + a[i+1])+1
        a[i+1] -= abs(sum(A) + a[i+1])+1

    elif sum(A) < 0 and sum(A) + a[i+1] < 0:
        cnt += abs(sum(A) - a[i+1])-1
        a[i+1] += abs(sum(A) - a[i+1])-1

if sum(a) == 0:
    cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

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

for i in range(n-1):
    A.append(a[i])
    if sum(A) > 0 and sum(A) + a[i+1] > 0:
        cnt += abs(sum(A) + a[i+1])+1
        a[i+1] -= abs(sum(A) + a[i+1])+1


    elif sum(A) < 0 and sum(A) + a[i+1] < 0:
        cnt += abs(sum(A) - a[i+1])-1
        a[i+1] += abs(sum(A) - a[i+1])-1


if sum(a) == 0:
    cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

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

for i in range(n-1):
    A.append(a[i])
    if sum(A) > 0:
        while sum(A) + a[i+1] >= 0:
            a[i+1] -= 1
            cnt += 1

    elif sum(A) < 0:
        while sum(A) + a[i+1] <= 0:
            a[i+1] += 1
            cnt += 1

if sum(a) == 0:
    cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

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

for i in range(n-1):
    A.append(a[i])
    if sum(A) > 0:
        while sum(A) + a[i+1] >= 0:
            a[i+1] -= 1
            cnt += 1

    elif sum(A) < 0:
        while sum(A) + a[i+1] <= 0:
            a[i+1] += 1
            cnt += 1
            
if sum(a) == 0:
    cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

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

# i番目までの和が正(i: 奇数)
res1 = 0
cum = 0
for i in range(n):
    cum += a[i]
    if i % 2 == 1:
        while cum <= 0:
            cum += 1
            res1 += 1
    else:
        while cum >= 0:
            cum -= 1
            res1 += 1

# i番目までの和が負(i: 奇数)
res2 = 0
cum = 0
for i in range(n):
    cum += a[i]
    if i % 2 == 0:
        while cum <= 0:
            cum += 1
            res2 += 1
    else:
        while cum >= 0:
            cum -= 1
            res2 += 1

print(min(res1,res2))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n = int(input())
a = np.array(list(map(int, input().split())))
b = a.copy()

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

print(sum(abs(b-a)))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
dummyn = int(input())
a = list(map(int, input().split()))
b_np = np.array([sum(a[:i+1]) for i in range(len(a))])

ans = 0
for i in range(len(b_np)-1):
    if b_np[i] * b_np[i+1] >= 0 and b_np[i] <= 0 and b_np[i] <= b_np[i+1]:
        ans += (1 - b_np[i+1])
        b_np[i+1:] += (1 - b_np[i+1])
    elif b_np[i] * b_np[i+1] >= 0 and b_np[i] < 0 and b_np[i] > b_np[i+1]:
        ans +=  (1 - b_np[i])
        b_np[i:] += (1 - b_np[i])
    elif b_np[i] * b_np[i+1] >= 0 and b_np[i] >= 0 and b_np[i] >= b_np[i+1]:
        ans += (b_np[i+1] + 1)
        b_np[i+1:] -= (b_np[i+1] + 1)
    elif b_np[i] * b_np[i+1] >= 0 and b_np[i] > 0 and b_np[i] < b_np[i+1]:
        ans += (b_np[i] + 1)
        b_np[i:] -= (b_np[i] + 1)

print(ans)