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:
def sequence(lis, first_value):
    count = 0
    now_sum = first_value
    pre_sum = first_value
    for num in lis[1:]:
        now_sum += num
        while now_sum == 0 or now_sum * pre_sum > 0:
            now_sum -= pre_sum/abs(pre_sum)
            count += 1
        pre_sum = now_sum
    return count

n = int(input())
a = [int(i) for i in input().split()]
if a[0] == 0:
    print(min(sequence(a, 1), sequence(a, -1))+1)
else:
    print(sequence(a, a[0]))

Submission status: Time Limit Exceeded

Solution code:

def read_input():
    n = int(input())
    alist = list(map(int, input().split()))
    return n, alist

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

def submit():
    n, alist = read_input()

    s = alist[0]
    sign = get_sign(s)
    edit = 0
    for a in alist[1:]:
        temp = s + a
        temp_sign = get_sign(temp)

        while temp_sign == sign or temp == 0:
            edit += 1
            temp -= sign
            temp_sign = get_sign(temp)

        s = temp
        sign = temp_sign

    print(edit)





if __name__ == '__main__':
    submit()

Submission status: Time Limit Exceeded

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

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

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

count = 0
for i in range(1, n):
    subl = l[i:i + 2]
    if 0 in subl:
        if subl[0] > 0:
            count += 1
            l[i + 1] = -1
        else:
            count += 1
            l[i + 1] = 1
    if subl[0] * subl[1] > 0:
        t = subl[1]
        m = True
        if t > 0:
            t = 1 + t
        else:
            t = 1 - t
            m = False
        count += t
        for j in range(i + 1, n + 1):
            if m:
                l[j] -= t
            else:
                l[j] += t
print(count)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
count=0
A=[int(i) for i in input().split()]
if A[0]==0:
  A[0]+=1
  count+=1
elif A[0]<0:
  for i in range(len(A)):
    A[i]*=(-1)
for i in range(1,len(A)):
  s=sum(A[:i+1])
  if i%2==0:
    if s<=0:
      A[i]+=1-s
      count+=1-s
  else:
    if s>=0:
      A[i]-=1+s
      count+=1+s
print(count)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
count=0
A=[int(i) for i in input().split()]
if A[0]==0:
  A[0]+=1
  count+=1
elif A[0]<0:
  for i in range(len(A)):
    A[i]*=(-1)
for i in range(1,len(A)):
  if i%2==0:
    while sum(A[:i+1])<=0:
      A[i]+=1
      count+=1
  else:
    while sum(A[:i+1])>=0:
      A[i]-=1
      count+=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np 
n = int(input())
a = list(map(int, input().split()))
cum_a = [0 for _ in range(n)]
cum_a[0] = a[0]
for i in range(n-1):
  cum_a[i+1] = cum_a[i] + a[i+1]
cum_a = np.array(cum_a)
count = 0
flag = 0

def r_flag(n):
  if n > 0:
    return 1
  elif n < 0:
    return -1
  else:
    return 0

flag = r_flag(cum_a[0])
for i in range(1, len(cum_a)):
  tmp_flag = r_flag(cum_a[i])
  if tmp_flag * flag == -1:
    flag = tmp_flag
  elif tmp_flag * flag == 1:
    count += abs(-1*tmp_flag-(cum_a[i]))
    cum_a[i:] += -1*tmp_flag-(cum_a[i])
    flag = -1 * tmp_flag
  else:
    try:
      next_flag = r_flag(cum_a[i+1])
      if next_flag == 1:
        cum_a[i:] -= 1
        count += 1
      else:
        cum_a[i:] += 1
        count += 1
    except:
      count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np 
n = int(input())
a = list(map(int, input().split()))
cum_a = [0 for _ in range(n)]
cum_a[0] = a[0]
for i in range(n-1):
  cum_a[i+1] = cum_a[i] + a[i+1]
cum_a = np.array(cum_a)
count = 0
flag = 0
diff = 0
def r_flag(n):
  if n > 0:
    return 1
  elif n < 0:
    return -1
  else:
    return 0

flag = r_flag(cum_a[0])
for i in range(1, len(cum_a)):
  tmp_flag = r_flag(cum_a[i])
  if tmp_flag * flag == -1:
    flag = tmp_flag
  elif tmp_flag * flag == 1:
    if tmp_flag == 1:
      count += abs(-1-(cum_a[i]))
      diff += -1-(cum_a[i]+diff)
      cum_a[i:] += diff
      flag = -1
    else:
      count += abs(1-(cum_a[i]))
      diff += 1-(cum_a[i]+diff)
      cum_a[i:] += diff
      flag = 1
  else:
    try:
      next_flag = r_flag(cum_a[i+1]-diff)
      if next_flag == 1:
        diff -= 1
        cum_a[i:] -= 1
        count += 1
      else:
        diff += 1
        cum_a[i:] += 1
        count += 1
    except:
      diff += 1
      count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
n,a=int(input()),list(map(int,input().split()));ans=0
if a[0]==0:
  bol=True
  for i in range(1,n):
    if a[i-1]!=a[i]:
      bol=False
      if a[i]>0:a[0]=(1if i%2==0else-1)
      else:a[0]=(1if i%2!=0else-1)
    ans+=1;break
  if bol:print(n*2-1);exit()
b=[a[0]];m=("+"if a[0]<0else"-")
for i in range(1,n):
  while b[-1]*(b[-1]+a[i])>=0:
    ans+=1
    a[i]+=eval(m+"1")
  b.append(b[-1]+a[i])
  m=("+"if b[i]<0else"-")
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = list(map(int,input().split()))
odd = 0
even = 0
temp = 0
for i in range(N):
    temp += a[i]
    if i % 2 == 0:
        # 奇数番目が正
        while temp < 1:
            temp += 1
            odd += 1
    else:
        while temp > -1:
            temp -= 1
            odd += 1       
temp = 0
for i in range(N):
    temp += a[i]
    if i % 2 == 0:
        # 奇数番目が負
        while temp > -1:
            temp -= 1
            even += 1
    else:
        while temp < 1:
            temp += 1
            even += 1
print(min(odd,even))

Submission status: Time Limit Exceeded

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

cp = copy.copy(l)
 
if l[0]!=0:

  for k in range(1,N):
    if sum(l[:k])*sum(l[:k+1])>0:
      l[k]=-np.sign(sum(l[:k]))-sum(l[:k])

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

  
else:#頭0のとき；和0なので変える =>1,-1にした時両方やって良いほうに
  #1にする場合
  sei_l=copy.copy(l)
  sei_l[0]=1
  for k in range(1,N):

    if sum(sei_l[:k])*sum(sei_l[:k+1])>0:

      sei_l[k]=-np.sign(sum(sei_l[:k]))-sum(sei_l[:k])

  if sum(sei_l)==0:
    c1=1+sum([abs(sei_l[n]-cp[n]) for n in range(N)])
  else:
    c1=sum([abs(sei_l[n]-cp[n]) for n in range(N)])  

  #-1にする場合
  fu_l=copy.copy(l)
  fu_l[0]=-1
  for k in range(1,N):

    if sum(fu_l[:k])*sum(fu_l[:k+1])>0:

      fu_l[k]=-np.sign(sum(fu_l[:k]))-sum(fu_l[:k])

  if sum(fu_l)==0:
    c2=1+sum([abs(fu_l[n]-cp[n]) for n in range(N)])
  else:
    c2=sum([abs(fu_l[n]-cp[n]) for n in range(N)])  
  
  print(max(c1,c2))

Submission status: Time Limit Exceeded

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

sum_an = []
tmp = 0
for a in an:
    tmp += a
    sum_an.append(tmp)

c = 0
before_positive = sum_an[0] <= 0
for i in range(n):
    if before_positive and sum_an[i] >= 0:
        p = abs(sum_an[i]) + 1
        c += p
        for j in range(i, n):
            sum_an[j] -= p
        before_positive = False
    elif not before_positive and sum_an[i] <= 0:
        p = abs(sum_an[i]) + 1
        c += p
        for j in range(i, n):
            sum_an[j] += p
        before_positive = True
    else:
        before_positive = not before_positive

print(c)

Submission status: Time Limit Exceeded

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

cp = copy.copy(l)
 
if l[0]!=0:

  for k in range(1,N):
    if sum(l[:k])*sum(l[:k+1])>0:
      l[k]=-np.sign(sum(l[:k]))-sum(l[:k])

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

  
else:#頭0のとき；和0なので変える =>1,-1にした時両方やって良いほうに
  #1にする場合
  sei_l=copy.copy(l)
  sei_l[0]=1
  for k in range(1,N):

    if sum(sei_l[:k])*sum(sei_l[:k+1])>0:

      sei_l[k]=-np.sign(sum(sei_l[:k]))-sum(sei_l[:k])

  if sum(sei_l)==0:
    c1=1+sum([abs(sei_l[n]-cp[n]) for n in range(N)])
  else:
    c1=sum([abs(sei_l[n]-cp[n]) for n in range(N)])  

  #-1にする場合
  fu_l=copy.copy(l)
  sei_l[0]=-1
  for k in range(1,N):

    if sum(fu_l[:k])*sum(fu_l[:k+1])>0:

      fu_l[k]=-np.sign(sum(fu_l[:k]))-sum(fu_l[:k])

  if sum(fu_l)==0:
    c2=1+sum([abs(fu_l[n]-cp[n]) for n in range(N)])
  else:
    c2=sum([abs(fu_l[n]-cp[n]) for n in range(N)])  
  
  print(max(c1,c2))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import copy
N=int(input())
l=list(map(int, input().split()))  #リスト入力
cp = copy.copy(l)

#even_c=0
#odd_c=0
c=0

if l[0]!=0:
  #l[0]=-1
  #even_c=even_c+1
  for k in range(1,N):
    #print(sum(l[:k]),sum(l[:k+1]))
    s=sum(l[:k])
    if s*sum(l[:k+1])>0:
      #print(k)
      c=c+abs(-np.sign(s)-s-l[k])
      l[k]=-np.sign(s)-s
  #print(l)
  if sum(l)==0:
    print(c+1)
    #print(1+sum([abs(l[n]-cp[n]) for n in range(N)]))
  else:
    print(c)
    #print(sum([abs(l[n]-cp[n]) for n in range(N)]))

  
else:
  #1
  sei_l=copy.copy(l)
  sei_l[0]=1
  for k in range(1,N):
    #print(sum(l[:k]),sum(l[:k+1]))
    if sum(sei_l[:k])*sum(sei_l[:k+1])>0:
      #print(k)
      sei_l[k]=-np.sign(sum(sei_l[:k]))-sum(sei_l[:k])
  #print(l)
  if sum(sei_l)==0:
    c1=1+sum([abs(sei_l[n]-cp[n]) for n in range(N)])
  else:
    c1=sum([abs(sei_l[n]-cp[n]) for n in range(N)])  
  #-1
  fu_l=copy.copy(l)
  sei_l[0]=-1
  for k in range(1,N):
    #print(sum(l[:k]),sum(l[:k+1]))
    if sum(fu_l[:k])*sum(fu_l[:k+1])>0:
      #print(k)
      fu_l[k]=-np.sign(sum(fu_l[:k]))-sum(fu_l[:k])
  #print(l)
  if sum(fu_l)==0:
    c2=1+sum([abs(fu_l[n]-cp[n]) for n in range(N)])
  else:
    c2=sum([abs(fu_l[n]-cp[n]) for n in range(N)])  
  
  print(max(c1,c2))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import copy
N=int(input())
l=list(map(int, input().split()))  #リスト入力
cp = copy.copy(l)
 
if l[0]!=0:

  for k in range(1,N):
    if sum(l[:k])*sum(l[:k+1])>0:
      l[k]=-np.sign(sum(l[:k]))-sum(l[:k])

  if sum(l)==0:
    print(1+sum([abs(l[n]-cp[n]) for n in range(N)]))
  else:
    print(sum([abs(l[n]-cp[n]) for n in range(N)]))
  exit()
  
else:
  #1
  sei_l=copy.copy(l)
  sei_l[0]=1
  for k in range(1,N):

    if sum(sei_l[:k])*sum(sei_l[:k+1])>0:

      sei_l[k]=-np.sign(sum(sei_l[:k]))-sum(sei_l[:k])

  if sum(sei_l)==0:
    c1=1+sum([abs(sei_l[n]-cp[n]) for n in range(N)])
  else:
    c1=sum([abs(sei_l[n]-cp[n]) for n in range(N)])  
  #-1
  fu_l=copy.copy(l)
  sei_l[0]=-1
  for k in range(1,N):

    if sum(fu_l[:k])*sum(fu_l[:k+1])>0:

      fu_l[k]=-np.sign(sum(fu_l[:k]))-sum(fu_l[:k])

  if sum(fu_l)==0:
    c2=1+sum([abs(fu_l[n]-cp[n]) for n in range(N)])
  else:
    c2=sum([abs(fu_l[n]-cp[n]) for n in range(N)])  
  
  print(max(c1,c2))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import copy
N=int(input())
l=list(map(int, input().split()))  #リスト入力
cp = copy.copy(l)

#even_c=0
#odd_c=0


if l[0]!=0:
  #l[0]=-1
  #even_c=even_c+1
  for k in range(1,N):
    #print(sum(l[:k]),sum(l[:k+1]))
    if sum(l[:k])*sum(l[:k+1])>0:
      #print(k)
      l[k]=-np.sign(sum(l[:k]))-sum(l[:k])
  #print(l)
  if sum(l)==0:
    print(1+sum([abs(l[n]-cp[n]) for n in range(N)]))
  else:
    print(sum([abs(l[n]-cp[n]) for n in range(N)]))

  
else:
  #1
  sei_l=copy.copy(l)
  sei_l[0]=1
  for k in range(1,N):
    #print(sum(l[:k]),sum(l[:k+1]))
    if sum(sei_l[:k])*sum(sei_l[:k+1])>0:
      #print(k)
      sei_l[k]=-np.sign(sum(sei_l[:k]))-sum(sei_l[:k])
  #print(l)
  if sum(sei_l)==0:
    c1=1+sum([abs(sei_l[n]-cp[n]) for n in range(N)])
  else:
    c1=sum([abs(sei_l[n]-cp[n]) for n in range(N)])  
  #-1
  fu_l=copy.copy(l)
  sei_l[0]=-1
  for k in range(1,N):
    #print(sum(l[:k]),sum(l[:k+1]))
    if sum(fu_l[:k])*sum(fu_l[:k+1])>0:
      #print(k)
      fu_l[k]=-np.sign(sum(fu_l[:k]))-sum(fu_l[:k])
  #print(l)
  if sum(fu_l)==0:
    c2=1+sum([abs(fu_l[n]-cp[n]) for n in range(N)])
  else:
    c2=sum([abs(fu_l[n]-cp[n]) for n in range(N)])  
  
  print(max(c1,c2))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from copy import deepcopy
n = int(input())
a = list(map(int, input().split()))
c = [0] * n
for i in range(n):
    c[i] = c[i - 1] + a[i]
c = np.array(c)
ans1, ans2 = 0, 0
tmp = deepcopy(c)
for i in range(n):
    t = tmp[i]
    if i % 2 == 0 and tmp[i] >= 0:
        tmp -= t + 1
        ans1 += t + 1
    elif i % 2 == 1 and tmp[i] <= 0:
        tmp += -t + 1
        ans1 += -t + 1
tmp = deepcopy(c)
for i in range(n):
    t = tmp[i]
    if i % 2 == 1 and tmp[i] >= 0:
        tmp -= t + 1
        ans2 += t + 1
    elif i % 2 == 0 and tmp[i] <= 0:
        tmp += -t + 1
        ans2 += -t + 1
print(min(ans1, ans2))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from copy import deepcopy
n = int(input())
a = list(map(int, input().split()))
c = [0] * n
for i in range(n):
    c[i] = c[i - 1] + a[i]
c = np.array(c)
ans1, ans2 = 0, 0
tmp = deepcopy(c)
for i in range(n):
    t = tmp[i]
    if i % 2 == 0 and tmp[i] >= 0:
        tmp[i:] -= t + 1
        ans1 += t + 1
    elif i % 2 == 1 and tmp[i] <= 0:
        tmp[i:] += -t + 1
        ans1 += -t + 1
tmp = deepcopy(c)
for i in range(n):
    t = tmp[i]
    if i % 2 == 1 and tmp[i] >= 0:
        tmp[i:] -= t + 1
        ans2 += t + 1
    elif i % 2 == 0 and tmp[i] <= 0:
        tmp[i:] += -t + 1
        ans2 += -t + 1
print(min(ans1, ans2))

Submission status: Time Limit Exceeded

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

sum = a[0]
count = 0

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

if sum == 0 :
    count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
b = a[:]
s1 = s2 = 0
for i in range(n):
  c = sum(a[0:i+1])
  if (i % 2 == 0 and c <= 0):
    s1 += abs(c) + 1
    a[i] += abs(c) + 1
  elif(i % 2 == 1 and c >= 0):
    s1 += abs(c) + 1
    a[i] -= abs(c) + 1
for i in range(n):
  c = sum(b[0:i+1])
  if (i % 2 == 0 and c >= 0):
    s2 += abs(c) + 1
    b[i] -= abs(c) + 1
  elif (i % 2 == 1 and c <= 0):
    s2 += abs(c) + 1
    b[i] += abs(c) + 1
print(min(s1,s2))

Submission status: Time Limit Exceeded

Solution code:
def main():
    _ = input()
    a = tuple(int(s) for s in input().split())
    print(solve(a))

def solve(a):
    if a[0] > 0:
        return min(rec(a[0], a[1:], 0), rec(-1, a[1:], a[0] + 1))
    else:
        return min(rec(a[0], a[1:], 0), rec(1, a[1:], 1 - a[0]))

def rec(s, a, r):
    if not a:
        return r
    elif s < 0:
        n = max(s + a[0], 1)
        return rec(n, a[1:], r + (n - (s + a[0])))
    else:
        n = min(s + a[0], -1)
        return rec(n, a[1:], r + s + a[0] - n)

main()

Submission status: Time Limit Exceeded

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):
            a[i]-=1
            ans+=1
        else:
            a[i]+=1
            ans+=1
        # print(a)
    sum[i] += a[i]
print(ans)
# print(a)
# print(sum)

Submission status: Time Limit Exceeded

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

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

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

print(min(ct1,ct2))

Submission status: Time Limit Exceeded

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

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

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

print(min(ct1,ct2))

Submission status: Time Limit Exceeded

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

m = 0
c = 0
for i in range(N):
	if i%2 == 0:
		m += A[i]
		while m <= 0:
			m += 1
			c += 1
	else:
		m += A[i]
		while m >= 0:
			m -= 1
			c += 1

o = 0
d = 0
for i in range(N):
	if i%2 == 1:
		o += A[i]
		while o <= 0:
			o += 1
			d += 1
	else:
		o += A[i]
		while o >= 0:
			o -= 1
			d += 1

print(min(c, d))

Submission status: Time Limit Exceeded

Solution code:
l = int(input())
n = [int(i) for i in input().split()] 
count = 0
nsum = n[0]
for i in range(1, l):
  tmp = n[i]
  tmp_1 = n[i-1]
  while(True):
    if (tmp_1 * tmp >= 0) or (nsum * (nsum + tmp) >= 0):
      if tmp_1 < 0:
        count += 1
        tmp += 1
      else:
        count += 1
        tmp -= 1     
    else:
      n[i] = tmp
      nsum += tmp
      break     
print(count)

Submission status: Time Limit Exceeded

Solution code:
l = int(input())
n = [int(i) for i in input().split()] 
count = 0
nsum = n[0]
for i in range(1, l):
  while(True):
    if (n[i-1] * n[i] >= 0) or (nsum * (nsum + n[i]) >= 0):
      if n[i-1] < 0:
        count += 1
        n[i] += 1
      else:
        count += 1
        n[i] -= 1     
    else:
      nsum += n[i]
      break     
print(count)

Submission status: Time Limit Exceeded

Solution code:
l = int(input())
n = [int(i) for i in input().split()] 

ans = []
ans.append(n[0])
count = 0
for i in range(1, l):
  nsum = sum(n[j] for j in range(0, i))
  while(True):
    #print("i:" + str(i))
    #print("n:" + str(n[i]))
    #print(("nsum:" + str(nsum)))
    if (n[i-1] < 0 and n[i] <= 0) or (n[i-1] > 0 and n[i] >= 0) or (nsum * (nsum + n[i]) >= 0):
      if n[i-1] < 0:
        count += 1
        n[i] += 1
      else:
        count += 1
        n[i] -= 1     
    else:
      ans.append(n[i])
      break
      
print(count)

Submission status: Time Limit Exceeded

Solution code:
l = int(input())
n = [int(i) for i in input().split()] 

ans = []
ans.append(n[0])
count = 0
for i in range(1, l):
  while(True):
    if (n[i-1] < 0 and n[i] <= 0) or (n[i-1] > 0 and n[i] >= 0) or (sum(n[j] for j in range(0, i)) == 0) or (sum(n[j] for j in range(0, i)) * sum(n[j] for j in range(0, i+1)) >= 0):
      if n[i-1] < 0:
        count += 1
        n[i] += 1
      else:
        count += 1
        n[i] -= 1     
    else:
      ans.append(n[i])
      break
      
print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from itertools import accumulate
N=int(input())
AX=list(accumulate(list(map(int, input().split()))))
ans=0
for i in range(1,N):
    if (AX[i]>=0 and AX[i-1]>=0):
        tmp=-1-AX[i]
        for j in range(i,N):
            AX[j]+=tmp
        ans+=abs(tmp)
    if (AX[i]<=0 and AX[i-1]<=0):
        tmp=1-AX[i]
        for j in range(i,N):
            AX[j]+=tmp
        ans+=abs(tmp)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int, input().split()))
sum_now=a[0]
sum_before=-a[0]
count_1=0
count_2=0
for i in range(n):
    while sum_now*sum_before>=0:
        if sum_before==0:
            sum_now=-a[1]/abs(a[1])
            count_1+=1
        else:
            count_1+=abs(int(sum_now))+1
            sum_now=-sum_before/abs(sum_before)
    if i!=n-1:
        sum_before=sum_now
        sum_now=sum_now+a[i+1]

sum_now=a[0]
sum_before=-a[0]
if sum_before==0:
    sum_now=a[1]/abs(a[1])
    sum_before=-sum_now
    count_2+=1
else:
    count_2+=abs(int(sum_now))+1
    sum_now=-sum_now/abs(sum_now)
    sum_before=-sum_now
for i in range(n):
    while sum_now*sum_before>=0:
        if sum_before==0:
            sum_now=a[1]/abs(a[1])
            count_2+=1
        else:
            count_2+=abs(int(sum_now))+1
            sum_now=-sum_before/abs(sum_before)
    if i!=n-1:
        sum_before=sum_now
        sum_now=sum_now+a[i+1]
print(min(count_1, count_2))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int, input().split()))
sum_now=a[0]
sum_before=-a[0]
count=0
for i in range(n):
    while sum_now*sum_before>=0:
        if sum_before==0:
            sum_now=-a[1]/abs(a[1])
            count+=1
        else:
            sum_now=sum_now-sum_before/abs(sum_before)
            count+=1
    if i!=n-1:
        sum_before=sum_now
        sum_now=sum_now+a[i+1]
print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(_) for _ in input().split()]
new_a_plus = [a[i] for i in range(n)]
new_a_minus = [a[i] for i in range(n)]

count_plus = 0
if a[0] <= 0:
    new_a_plus[0] = 1
    count_plus += 1 - a[0]

for i in range(1, n):
    if i % 2 == 0:
        if sum(new_a_plus[:i+1]) <= 0:
            new_a_plus[i] = -sum(new_a_plus[:i])+1
            count_plus += new_a_plus[i] - a[i]
    else:
        if sum(new_a_plus[:i+1]) >= 0:
            new_a_plus[i] = -sum(new_a_plus[:i])-1
            count_plus += a[i] - new_a_plus[i]

count_minus = 0
if a[0] >= 0:
    new_a_minus[0] = -1
    count_minus += a[0] + 1

for i in range(1, n):
    if i % 2 == 0:
        if sum(new_a_minus[:i+1]) >= 0:
            new_a_minus[i] = -sum(new_a_minus[:i])-1
            count_minus += a[i] - new_a_minus[i]
    else:
        if sum(new_a_minus[:i+1]) <= 0:
            new_a_minus[i] = -sum(new_a_minus[:i])+1
            count_minus += new_a_minus[i] - a[i]

print(min(count_plus, count_minus))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = 0
temp=a[0]
for i in range(n - 1):
    if temp > 0:
        temp+=a[i+1]
        if temp < 0:
            pass
        else:
            while temp >= 0:
                temp -= 1
                ans += 1
    else:
        temp+=a[i+1]
        if temp > 0:
            pass
        else:
            while temp <= 0:
                temp += 1
                ans += 1
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-1):
    if a[i]>0:
        if sum(a[:i+2])<0:
            pass
        else:
            while sum(a[:i+2])>=0:
                 a[i+1]-=1
                 ans+=1
    else:
        if sum(a[:i+2])>0:
            pass
        else:
            while sum(a[:i+2])<=0:
                a[i+1]+=1
                ans+=1
print(ans)

Submission status: Time Limit Exceeded

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

cnt = 0
x = []
x.append(a[0])

for i in range(n-1):
    if x[i] > 0:
        while x[i]+a[i+1] >= 0:
            cnt += 1
            a[i+1] -= 1
        x += [x[i] + a[i+1]]
    else:
        while x[i]+a[i+1] <= 0:
            cnt += 1
            a[i+1] += 1
        x += [x[i] + a[i+1]]

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from numpy import cumsum,copy
n = int(input())
a = cumsum([int(i) for i in input().split()])
b = copy(a)
ans1 = ans2 = 0
sign = 1
for i in range(n):
    if a[i] * sign <= 0:
        k = abs(a[i] - sign)
        a += sign*k
        ans1 += k
    sign *= -1
sign = -1
for i in range(n):
    if b[i] * sign <= 0:
        k = abs(b[i] - sign)
        b += sign*k
        ans2 += k
    sign *= -1
print(min(ans1, ans2))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

n = int(input())
a_array = np.array([int(elem) for elem in input().split(' ')])


def calculate_cumulative_sum(a_array, n):
    cumul_sum_array = np.zeros(n, dtype=np.int32)
    cumul_sum_array[0] = a_array[0]
    for i in range(1, n):
        cumul_sum_array[i] = cumul_sum_array[i - 1] + a_array[i]
    return cumul_sum_array


def check_1(cumul_sum_array):
    result = (cumul_sum_array != 0).all()
    return result


def check_2(cumul_sum_array):
    odd = np.sign(cumul_sum[::2])
    even = np.sign(cumul_sum[1::2])
    odd_sign = np.unique(odd)
    even_sign = np.unique(even)
    return (len(odd_sign) == 1) * (len(even_sign) == 1) * (odd_sign[0] != even_sign[0])


cumul_sum = calculate_cumulative_sum(a_array, n)
count = {'plus': 0, 'minus': 0}

def calc_num_manipulation(cumul_sum, start, n):
    cumul_sum = cumul_sum.copy()
    count = 0
    if check_1(cumul_sum) and check_2(cumul_sum):
        return count
    
    for i in range(n):
        if i == 0:
            diff = cumul_sum[i] - start
            count += abs(diff)
            cumul_sum[i:] -= diff
            
        else:
            c_back, c_just = cumul_sum[i - 1], cumul_sum[i]
            if c_back < 0 and c_just <= 0:
                diff = c_just - 1
                count += abs(diff)
                cumul_sum[i:] -= diff
            elif c_back > 0 and c_just >= 0:
                diff = c_just - (-1)
                count += abs(diff)
                cumul_sum[i:] -= diff
            else:
                continue
    return count

count['plus'] = calc_num_manipulation(cumul_sum, 1, n)
count['minus'] = calc_num_manipulation(cumul_sum, -1, n)
print(min(count.values()))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

n = int(input())
a_array = np.array([int(elem) for elem in input().split(' ')])


def calculate_cumulative_sum(a_array, n):
    cumul_sum_array = np.zeros(n, dtype=np.int32)
    cumul_sum_array[0] = a_array[0]
    for i in range(1, n):
        cumul_sum_array[i] = cumul_sum_array[i - 1] + a_array[i]
    return cumul_sum_array


def check_1(cumul_sum_array):
    result = (cumul_sum_array != 0).all()
    return result


def check_2(cumul_sum_array):
    odd = np.sign(cumul_sum[::2])
    even = np.sign(cumul_sum[1::2])
    odd_sign = np.unique(odd)
    even_sign = np.unique(even)
    return (len(odd_sign) == 1) * (len(even_sign) == 1) * (odd_sign[0] != even_sign[0])


cumul_sum = calculate_cumulative_sum(a_array, n)
count = {'plus': 0, 'minus': 0}

def calc_num_manipulation(cumul_sum, start, n):
    cumul_sum = cumul_sum.copy()
    count = 0
    if check_1(cumul_sum) and check_2(cumul_sum):
        return count
    
    for i in range(n):
        if i == 0:
            diff = cumul_sum[i] - start
            count += abs(diff)
            cumul_sum[i:] -= diff
        else:
            if cumul_sum[i - 1] < 0 and cumul_sum[i] <= 0:
                diff = cumul_sum[i] - 1
                count += abs(diff)
                cumul_sum[i:] -= diff
            elif cumul_sum[i - 1] > 0 and cumul_sum[i] >= 0:
                diff = cumul_sum[i] - (-1)
                count += abs(diff)
                cumul_sum[i:] -= diff
            else:
                continue
    return count

count['plus'] = calc_num_manipulation(cumul_sum, 1, n)
count['minus'] = calc_num_manipulation(cumul_sum, -1, n)
print(min(count.values()))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

n = int(input())
a_array = np.array([int(elem) for elem in input().split(' ')])


def calculate_cumulative_sum(a_array, n):
    cumul_sum_array = np.zeros(n, dtype=np.int32)
    cumul_sum_array[0] = a_array[0]
    for i in range(1, n):
        cumul_sum_array[i] = cumul_sum_array[i - 1] + a_array[i]
    return cumul_sum_array


def check_1(cumul_sum_array):
    result = (cumul_sum_array != 0).all()
    return result


def check_2(cumul_sum_array):
    odd = np.sign(cumul_sum[::2])
    even = np.sign(cumul_sum[1::2])
    odd_sign = np.unique(odd)
    even_sign = np.unique(even)
    return (len(odd_sign) == 1) * (len(even_sign) == 1) * (odd_sign[0] != even_sign[0])


cumul_sum = calculate_cumulative_sum(a_array, n)
count = {'plus': 0, 'minus': 0}

def calc_num_manipulation(cumul_sum, start, n):
    cumul_sum = cumul_sum.copy()
    count = 0
    for i in range(n):
        if check_1(cumul_sum) and check_2(cumul_sum):
            break
        else:
            if i == 0:
                diff = cumul_sum[i] - start
                count += abs(diff)
                cumul_sum[i:] -= diff
            else:
                if cumul_sum[i - 1] < 0 and cumul_sum[i] <= 0:
                    diff = cumul_sum[i] - 1
                    count += abs(diff)
                    cumul_sum[i:] -= diff
                elif cumul_sum[i - 1] > 0 and cumul_sum[i] >= 0:
                    diff = cumul_sum[i] - (-1)
                    count += abs(diff)
                    cumul_sum[i:] -= diff
                else:
                    continue
    return count

count['plus'] = calc_num_manipulation(cumul_sum, 1, n)
count['minus'] = calc_num_manipulation(cumul_sum, -1, n)
print(min(count.values()))

Submission status: Time Limit Exceeded

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