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
dummyn = int(input())
a = list(map(int, input().split()))

b = []
for i in range(len(a)):
    b.append(sum(a[:i+1]))

b_np = np.array(b)

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)

Submission status: Time Limit Exceeded

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

import copy
l1 = copy.copy(l)
l2 = copy.copy(l)
ans1 = 0
ans2 = 0

goukei = 0

#最初が正の時
if l1[0]<0:
	while l1[0]==+1:
		l1[0]+1
		ans1 = ans1 + 1

for i in range(n):
	goukei = sum(l1[:i+1])
	#print(i,goukei)
	
	if i%2 == 0:
		while goukei<=0:
			l1[i] = l1[i] + 1
			goukei = sum(l1[:i+1])
			ans1 = ans1 + 1
	
	else:
		while goukei>=0:		
			l1[i] = l1[i] - 1
			goukei = sum(l1[:i+1])
			ans1 = ans1 + 1			
	
#print(l)
#print(l1)
#print(ans1)
	
	
#最初が負の時	
if l2[0]>0:
	while l2[0]==-1:
		l2[0]-1
		ans2 = ans2 + 1
	
for i in range(n):
	goukei = sum(l2[:i+1])
	#print(i,goukei)
	
	if i%2 != 0:
		while goukei<=0:
			l2[i] = l2[i] + 1
			goukei = sum(l2[:i+1])
			ans2 = ans2 + 1
	
	else:
		while goukei>=0:		
			l2[i] = l2[i] - 1
			goukei = sum(l2[:i+1])
			ans2 = ans2 + 1			
				
#print(l)
#print(l2)
#print(ans2)

print(min(ans1,ans2))

Submission status: Time Limit Exceeded

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

c = 0
s = a[0]
fugo = a[0]

# print(a)

for i in range(1, n):
    s = s + a[i]
    while fugo * s >= 0:  # 符号が一緒だったら
        if a[i - 1] < 0:
            s += 1
        else:
            s -= 1
        c += 1
        # print(s, c)
    fugo = s
    # print(s, c, fugo)

print(c)

Submission status: Time Limit Exceeded

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

ans1 = 0 #偶数インデックスが正
for i in range(n):
    s1 = sum(a[:i+1])
    if i % 2 == 0:
        if s1 <= 0:
            x = abs(s1) + 1
            ans1 += x
            a[i] += x
        else:
            continue
    else:
        if s1 >= 0:
            x = abs(s1) + 1
            ans1 += x
            a[i] -= x
ans2 = 0 #偶数インデックスが負
for i in range(n):
    s2 = sum(a2[:i+1])
    if i % 2 == 1:
        if s2 <= 0:
            x = abs(s2) + 1
            ans2 += x
            a2[i] += x
        else:
            continue
    else:
        if s2 >= 0:
            x = abs(s2) + 1
            ans2 += x
            a2[i] -= x

print(min(ans1, ans2))

Submission status: Time Limit Exceeded

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

ans1 = 0 #偶数インデックスが正
for i in range(n):
    s1 = sum(a[:i+1])
    if i % 2 == 0:
        if s1 <= 0:
            x = abs(s1) + 1
            ans1 += x
            a[i] += x
        else:
            continue
    else:
        if s1 >= 0:
            x = abs(s1) + 1
            ans1 += x
            a[i] -= x
ans2 = 0 #偶数インデックスが負
for i in range(n):
    s2 = sum(a2[:i+1])
    if i % 2 == 1:
        if s2 <= 0:
            x = abs(s2) + 1
            ans2 += x
            a2[i] += x
        else:
            continue
    else:
        if s2 >= 0:
            x = abs(s2) + 1
            ans2 += x
            a2[i] -= x

print(min(ans1, ans2))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
s1 = []
s2 = []
for i in range(n):
    s1.append(sum(a[:i+1]))
    s2.append(sum(a[:i+1]))
ans1 = 0 #偶数インデックスが正
for i in range(n):
    if i % 2 == 0:
        if s1[i] <= 0:
            x = abs(s1[i]) + 1
            ans1 += x
            for j in range(i, n):
                s1[j] += x
        else:
            continue
    else:
        if s1[i] >= 0:
            x = abs(s1[i]) + 1
            ans1 += x
            for j in range(i,n):
                s1[j] -= x
ans2 = 0 #偶数インデックスが負
for i in range(n):
    if i % 2 == 1:
        if s2[i] <= 0:
            x = abs(s2[i]) + 1
            ans2 += x
            for j in range(i, n):
                s2[j] += x
        else:
            continue
    else:
        if s2[i] >= 0:
            x = abs(s2[i]) + 1
            ans2 += x
            for j in range(i,n):
                s2[j] -= x

print(min(ans1, ans2))

Submission status: Time Limit Exceeded

Solution code:
def decision(i):
    sum_for_i = sum(A[:i+1]) # 0~i までの sum
    sum_before_i = sum(A[:i]) # 0~i-1までの sum
    if sum_for_i == 0:
        if sum_before_i <0:
            return 1
        if sum_before_i >0:
            return 2
    if sum_before_i > 0 and sum_for_i >0:
        return 3
    if sum_before_i < 0 and sum_for_i <0:
        return 4
    return 0

n = int(input())
original_A = [int(x) for x in input().split()]
A = original_A.copy()

for i in range(1, n):
    result = decision(i)
    if result == 0:
        continue
    elif result == 1:
        A[i] += 1
    elif result == 2:
        A[i] -= 1
    elif result == 3:
        A[i] -= (abs((sum(A[:i+1]))+1))
    elif result == 4:
        A[i] += (abs(sum(A[:i+1]))+1)
print(sum([abs(original_A[x] - A[x]) for x in range(n)]))

Submission status: Time Limit Exceeded

Solution code:
def decision(i):
    sum_for_i = sum(A[:i+1]) # 0~i までの sum
    sum_before_i = sum(A[:i]) # 0~i-1までの sum
    if sum_for_i == 0:
        if sum_before_i <0:
            return 1
        if sum_before_i >0:
            return 2
    if sum_before_i > 0 and sum_for_i >0:
        return 3
    if sum_before_i < 0 and sum_for_i <0:
        return 4
    return 0

n = int(input())
A = [int(x) for x in input().split()]
count = 0

for i in range(1, n):
    result = decision(i)
    #print(i)
    #print(i, A[i], result)
    if result == 0:
        count += 0
    elif result == 1 or result == 4:
        #print('加算するケース')
        while decision(i) != 0:
            A[i] += 1
            count += 1
    elif result == 2 or result == 3:
        #print('減算するケース')
        while decision(i) != 0:
            A[i] -= 1
            count += 1
    #print(A)
print(count)

Submission status: Time Limit Exceeded

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

flag = True if A[0] > 0 else False

count = 0
for i in range(n):
    if i == 0:
        continue
    sum_for_i = sum(A[:i])
    sum_for_next = sum(A[:i+1])
    if (sum_for_i != 0 and ((sum_for_i > 0 and sum_for_next <0) or (sum_for_i < 0 and sum_for_next >0))):
        continue
    else:
        #print("needs to be changed: A[{}] ({})".format(i, A[i]))
        while not (sum_for_i != 0 and ((sum_for_i > 0 and sum_for_next <0) or (sum_for_i < 0 and sum_for_next >0))):
            if (sum_for_i < 0):
                A[i] += 1
                count += 1
            else:
                A[i] -= 1
                count += 1
            sum_for_i = sum(A[:i])
            sum_for_next = sum(A[:i+1])

print(count)

Submission status: Time Limit Exceeded

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

flag = True if A[0] > 0 else False

count = 0
for i in range(n):
    if i == 0:
        continue
    sum_for_i = sum(A[:i])
    sum_for_next = sum(A[:i+1])
    if (sum_for_i != 0 and ((sum_for_i > 0 and sum_for_next <0) or (sum_for_i < 0 and sum_for_next >0))):
        continue
    else:
        #print("needs to be changed: A[{}] ({})".format(i, A[i]))
        while not (sum_for_i != 0 and ((sum_for_i > 0 and sum_for_next <0) or (sum_for_i < 0 and sum_for_next >0))):
            if (flag and not i%2) or (not flag and i%2):
                A[i] += 1
                count += 1
            else:
                A[i] -= 1
                count += 1
            sum_for_i = sum(A[:i])
            sum_for_next = sum(A[:i+1])

print(count)

Submission status: Time Limit Exceeded

Solution code:
i=0
while True:
  i+=1

Submission status: Time Limit Exceeded

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

count = 0
for i in range(n):
    if i == 0:
        continue
    sum_for_i = sum(A[:i])
    sum_for_next = sum(A[:i+1])
    if (sum_for_i != 0 and ((sum_for_i > 0 and sum_for_next <0) or (sum_for_i < 0 and sum_for_next >0))):
        continue
    else:
        #print("needs to be changed: A[{}] ({})".format(i, A[i]))
        while not (sum_for_i != 0 and ((sum_for_i > 0 and sum_for_next <0) or (sum_for_i < 0 and sum_for_next >0))):
            if A[i-1] < 0:
                A[i] += 1
                count += 1
            else:
                A[i] -= 1
                count += 1
            sum_for_i = sum(A[:i])
            sum_for_next = sum(A[:i+1])

print(count)

Submission status: Time Limit Exceeded

Solution code:
import copy
n = int(input())
a = [int(i) for i in input().split()]
b=a.copy()
s0p = a[0]
s0n = b[0]
countp = 0
countn = 0

if a.count(0)==n:
  print(2*n+1)
  exit()

if s0p<=0:
  while s0p<=0:
    s0p+=1
    countp+=1

if s0n>=0:
  while s0n>=0:
    s0n-=1
    countn+=1

for i in range(1,n):
  s1 = s0p+a[i]
  if s0p*s1>=0:
    if s1>0:
      a[i]-=(abs(s1)+1)
      countp+=(abs(s1)+1)
    elif s1<0:
      a[i]+=(abs(s1)+1)
      countp+=(abs(s1)+1)
    elif s1==0:
      if s0p>0:
        a[i]-=1
        countp+=1
      elif s0p<0:
        a[i]+=1
        countp+=1
  s0p += a[i]

for i in range(1,n):
  s1 = s0n+b[i]
  if s0n*s1>=0:
    if s1>0:
      b[i]-=(abs(s1)+1)
      countn+=(abs(s1)+1)
    elif s1<0:
      b[i]+=(abs(s1)+1)
      countn+=(abs(s1)+1)
    elif s1==0:
      if s0n>0:
        b[i]-=1
        countn+=1
      elif s0n<0:
        b[i]+=1
        countn+=1
  s0n += b[i]

print(countp if countp<=countn else(countn))

Submission status: Time Limit Exceeded

Solution code:
import copy
n = int(input())
a = [int(i) for i in input().split()]
b=a.copy()
s0p = a[0]
s0n = b[0]
countp = 0
countn = 0

if s0p<=0:
  while s0p<=0:
    s0p+=1
    countp+=1

if s0n>=0:
  while s0n>=0:
    s0n-=1
    countn+=1

for i in range(1,n):
  s1 = s0p+a[i]
  """print("i",i,"a[i]",a[i],"s0p",s0p,"s1",s1,"countp",countp)"""
  if s0p*s1>=0:
    if s1>0:
      a[i]-=(abs(s1)+1)
      countp+=(abs(s1)+1)
    elif s1<0:
      a[i]+=(abs(s1)+1)
      countp+=(abs(s1)+1)
    elif s1==0:
      if s0p>0:
        a[i]-=1
        countp+=1
      elif s0p<0:
        a[i]+=1
        countp+=1
  s0p += a[i]

for i in range(1,n):
  s1 = s0n+b[i]
  """print("i",i,"b[i]",b[i],"s0n",s0n,"s1",s1,"countn",countn)"""
  if s0n*s1>=0:
    if s1>0:
      b[i]-=(abs(s1)+1)
      countn+=(abs(s1)+1)
    elif s1<0:
      b[i]+=(abs(s1)+1)
      countn+=(abs(s1)+1)
    elif s1==0:
      if s0n>0:
        b[i]-=1
        countn+=1
      elif s0n<0:
        b[i]+=1
        countn+=1
  s0n += b[i]

print(countp if countp<=countn else(countn))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
count=0
for i in range(1,N):
  if sum(A[0:i])*sum(A[0:i+1]) >=0:
    count += abs(sum(A[0:i])+A[i])+1
    if sum(A[0:i])<0:
      A[i]=-sum(A[0:i])+1
    else:
      A[i]=-sum(A[0:i])-1
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
count=0
for i in range(1,N):
  if sum(A[0:i])*sum(A[0:i+1]) >=0:
    count += abs(sum(A[0:i])+A[i])+1
    if sum(A[0:i])<0:
      A[i]=-sum(A[0:i])+1
    else:
      A[i]=-sum(A[0:i])-1
print(count)

Submission status: Time Limit Exceeded

Solution code:
input()
iter_ = iter(map(int, input().rstrip("\n").split()))

sum_ = next(iter_)
ans = 0

for num in iter_:
    if sum_ == 0:
        sum_ += 1
        ans += 1
    presum = sum_
    sum_ += num
    while presum * sum_ >= 0:
        sum_ += -1 if presum > 0 else 1
        ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
input()
iter_ = iter(map(int, input().rstrip("\n").split()))

sum_ = next(iter_)
ans = 0

for num in iter_:
    presum = sum_
    sum_ += num
    while presum * sum_ >= 0:
        sum_ += -1 if presum > 0 else 1
        ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

sum = a[0]
count = 0

if a[0] == 0:
    if a[1] > 0:
        sum -= 1
        count += 1
    elif a[1] < 0:
        sum += 1
        count += 1

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

print(count)

Submission status: Time Limit Exceeded

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

check=a[0]
ans=0
if check==0:
    for i in range(1,n):
        if check+a[i]==0:
            continue
            
        elif check+a[i]>0:
            check=-1
            ans=1
            break
        else:
            check=1
            ans=1
            break

for i in range(1,n):
    check2=check+a[i]
    if check<0:
        if check2>0:
            check=check2
        else:
            while check2<=0:
                ans+=1
                check2+=1
            check=check2
    else:
        if check2<0:
            check=check2
        else:
            while check2>=0:
                ans+=1
                check2-=1
            check=check2
print(ans)

Submission status: Time Limit Exceeded

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

c = 0
prev = 0
for i in range(n):
    flag = False
    s = sum(arr[:i+1])
    if i > 0:
        if prev > 0 and s >= 0:
            diff = s + 1
            c += diff
            arr[i] -= diff 
        elif prev < 0 and s <= 0:
            diff = -1 * s + 1
            c += diff
            arr[i] += diff
        else:
            flag = True
    prev = s if flag else sum(arr[:i+1])
print(c)

Submission status: Time Limit Exceeded

Solution code:
from itertools import accumulate
import copy

N = int(input())
a = list(map(int,input().split()))
a = list(accumulate(a))
ans1 = 0
ans2 = 0
b = copy.copy(a)
for i in range(N):
    if i % 2 == 0:
        if a[i] > 0:
            pass
        else:
            ans1 += abs(a[i]) + 1
            a[i+1:] = list(map(lambda n:n+abs(a[i])+1, a[i+1:]))
    else:
        if a[i] < 0:
            pass
        else:
            ans1 += abs(a[i]) + 1
            a[i+1:] = list(map(lambda n:n-(abs(a[i])+1), a[i+1:]))

for i in range(N):
    if i % 2 == 1:
        if b[i] > 0:
            pass
        else:
            ans2 += abs(b[i]) + 1
            b[i+1:] = list(map(lambda n:n+abs(b[i])+1, b[i+1:]))
    else:
        if b[i] < 0:
            pass
        else:
            ans2 += abs(b[i]) + 1
            b[i+1:] = list(map(lambda n:n-(abs(b[i])+1), b[i+1:]))
print(min(ans1,ans2))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import copy

N = int(input())
a = list(map(int,input().split()))
a = np.cumsum(a)
ans1 = 0
ans2 = 0
b = copy.copy(a)
for i in range(N):
    if i % 2 == 0:
        if a[i] > 0:
            pass
        else:
            ans1 += abs(a[i]) + 1
            a[i+1:] = list(map(lambda n:n+abs(a[i])+1, a[i+1:]))
    else:
        if a[i] < 0:
            pass
        else:
            ans1 += abs(a[i]) + 1
            a[i+1:] = list(map(lambda n:n-(abs(a[i])+1), a[i+1:]))

for i in range(N):
    if i % 2 == 1:
        if b[i] > 0:
            pass
        else:
            ans2 += abs(b[i]) + 1
            b[i+1:] = list(map(lambda n:n+abs(b[i])+1, b[i+1:]))
    else:
        if b[i] < 0:
            pass
        else:
            ans2 += abs(b[i]) + 1
            b[i+1:] = list(map(lambda n:n-(abs(b[i])+1), b[i+1:]))
print(min(ans1,ans2))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import copy

N = int(input())
a = list(map(int,input().split()))
a = np.cumsum(a)
ans1 = 0
ans2 = 0
b = copy.copy(a)
for i in range(N):
    if i % 2 == 0:
        if a[i] > 0:
            pass
        else:
            ans1 += abs(a[i]) + 1
            a = list(map(lambda n:n+abs(a[i])+1, a))
    else:
        if a[i] < 0:
            pass
        else:
            ans1 += abs(a[i]) + 1
            a = list(map(lambda n:n-(abs(a[i])+1), a))

for i in range(N):
    if i % 2 == 1:
        if b[i] > 0:
            pass
        else:
            ans2 += abs(b[i]) + 1
            b = list(map(lambda n:n+abs(b[i])+1, a))
    else:
        if b[i] < 0:
            pass
        else:
            ans2 += abs(b[i]) + 1
            b = list(map(lambda n:n-(abs(b[i])+1), b))
print(min(ans1,ans2))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

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

cnt=0

for i in range(n-1):
    sum_a = sum(a[0:i+1])
    if abs(sum_a) >= abs(a[i+1]) or sum_a*a[i+1]>=0:
        if sum_a<0:
            cnt += abs(-sum_a+1 -a[i+1])
            a[i+1]=-sum_a+1
        elif sum_a>=0:
            cnt+=abs(-sum_a-1-a[i+1])
            a[i+1]=-sum_a-1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
# https://atcoder.jp/contests/abc059/tasks/arc072_a
# C - Sequence
import copy
N = int(input().split()[0])
a_list = list(map(int, input().split()))

w_list_1 = copy.copy(a_list)
w_list_2 = copy.copy(a_list)
c_1, c_2 = 0, 0
c_list = []

for i in range(N):  # 10 ** 5
    # 偶数項を正にする場合
    s_1 = sum(w_list_1[:i+1])

    if i % 2 == 0 and s_1 < 0:
        w_list_1[i] += abs(s_1)
        c_1 += abs(s_1)
    elif i % 2 != 0 and s_1 >= 0:
        w_list_1[i] -= abs(s_1+1)
        c_1 += abs(s_1+1)

    s_1 = sum(w_list_1[:i+1])

    if s_1 == 0:
        a = w_list_1[i]
        w_list_1[i] = a + 1 if a >= 0 else a - 1
        c_1 += 1

    # 偶数項を負にする場合
    s_2 = sum(w_list_2[:i+1])

    if i % 2 == 1 and s_2 < 0:
        w_list_2[i] += abs(s_2)
        c_2 += abs(s_2)
    elif i % 2 != 1 and s_2 >= 0:
        w_list_2[i] -= abs(s_2+1)
        c_2 += abs(s_2+1)

    s_2 = sum(w_list_2[:i+1])

    if s_2 == 0:
        a = w_list_2[i]
        w_list_2[i] = a + 1 if a >= 0 else a - 1
        c_2 += 1

ans = min([c_1, c_2])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# https://atcoder.jp/contests/abc059/tasks/arc072_a
# C - Sequence
import copy
N = int(input().split()[0])
a_list = list(map(int, input().split()))

c = 0
w_list = copy.copy(a_list)

c_list = []

for mode in [0, 1]:
    s_list = []
    w_list = copy.copy(a_list)
    c = 0
    for i in range(N):
        s = sum(w_list[:i+1])

        if i % 2 == mode and s < 0:
            w_list[i] += abs(s)
            c += abs(s)
        elif i % 2 != mode and s >= 0:
            w_list[i] -= abs(s+1)
            c += abs(s+1)

        s = sum(w_list[:i+1])

        if s == 0:
            a = w_list[i]
            w_list[i] = a + 1 if a >= 0 else a - 1
            c += 1

        s = sum(w_list[:i+1])
        s_list.append(s)

    c_list.append(c)

ans = min(c_list)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
prv_total =0
cnt = 0
for i in range(n-1):
  total = prv_total + a[i]
  nxt_total = total+a[i+1]
  while total*nxt_total >= 0:
    if total == nxt_total:
      if total > 0:
        a[i+1] -= 1
        nxt_total -=1
      else:
        a[i+1] += 1
        nxt_total +=1        
    elif total-nxt_total == 1:
      a[i+1] -= 1
      nxt_total -=1
    elif nxt_total-total == 1:
      a[i+1] += 1
      nxt_total += 1
    elif (total<nxt_total and total >= 0) or (nxt_total<total and nxt_total >= 0):
      total -= 1
      nxt_total -= 1
    elif (total<nxt_total and nxt_total <= 0) or (nxt_total<total and total <= 0):
      total += 1
      nxt_total += 1
    cnt += 1
  prv_total = total

total = prv_total + a[-1]
if total == 0:
  cnt += 1
  
print(cnt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
import numpy as np
na = np.array(a).cumsum()
cnt = 0
if(na[0] > 0):
  for i in range(n):
    delta = abs(na[i]) + 1
    if(i % 2 == 0 and na[i] <= 0):
      cnt = cnt + delta
      for j in range(i, n):
        na[j] += delta 
    elif(i % 2 == 1 and na[i] >= 0):
      cnt = cnt + delta
      for j in range(i, n):
        na[j] -= delta
    else:
      na[i]
else:
  for i in range(n):
    delta = abs(na[i]) + 1
    if(i % 2 == 1 and na[i] <= 0):
      cnt = cnt + delta
      for j in range(i, n):
        na[j] += delta
    elif(i % 2 == 0 and na[i] >= 0):
      cnt = cnt + delta
      for j in range(i, n):
        na[j] -= delta
    else:
      na[i]
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from copy import copy
n = int(input())
a = [int(x) for x in input().split()]
ans1=[(-1)**i for i in range(n)]

b=copy(a)
res_b=0
c=copy(a)
res_c=0
for i in range(n):
  if ans1[i]*sum(b[:i+1])>0:
    pass
  else:
    b[i]=ans1[i]-sum(b[:i])
    res_b+=abs(b[i]-a[i])
  if -1*ans1[i]*sum(c[:i+1])>0:
    pass
  else:
    c[i]=-1*ans1[i]-sum(c[:i])
    res_c+=abs(c[i]-a[i])
  
print(min(res_b,res_c))

Submission status: Time Limit Exceeded

Solution code:
from copy import copy
n = int(input())
a = [int(x) for x in input().split()]
ans1=[(-1)**i for i in range(n)]
ans2=[(-1)**(i+1) for i in range(n)]

b=copy(a)
res_b=0
for i in range(n):
  if ans1[i]*sum(b[:i+1])>0:
    pass
  else:
    b[i]=ans1[i]-sum(b[:i])
    res_b+=abs(b[i]-a[i])
    
c=copy(a)
res_c=0
for i in range(n):
  if ans2[i]*sum(c[:i+1])>0:
    pass
  else:
    c[i]=ans2[i]-sum(c[:i])
    res_c+=abs(c[i]-a[i])
print(min(res_b,res_c))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = list(map(int,input().split()))
cnt = 0
for i in range(1,n):
  if A[0] >= 0:
    if i%2!=0 and A[i]>=0:
      cnt += A[i]
      A[i] -= A[i]
      while sum(A[:i+1])>=0:
        A[i] -= 1
        cnt += 1
    elif i%2==0 and A[i]<0:
      cnt += abs(A[i])
      A[i] += abs(A[i])
      while sum(A[:i+1])<=0:
        A[i] += 1
        cnt += 1
    else:
      if A[i] >= 0:
        while sum(A[:i+1])<=0:
          A[i] += 1
          cnt += 1
      else:
        while sum(A[:i+1])>=0:
          A[i] -= 1
          cnt += 1
  else:
    if i%2!=0 and A[i]<0:
      cnt += abs(A[i])
      A[i] += abs(A[i])
      while sum(A[:i+1])<=0:
        A[i] += 1
        cnt += 1
    elif i%2==0 and A[i]>=0:
      cnt += A[i]
      A[i] -= A[i]
      while sum(A[:i+1])>=0:
        A[i] -= 1
        cnt += 1
    else:
      if A[i] >= 0:
        while sum(A[:i+1])<=0:
          A[i] += 1
          cnt += 1
      else:
        while sum(A[:i+1])>=0:
          A[i] -= 1
          cnt += 1  
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int,input().split()))
s = []
for i in range(n):
    s.append(sum(a[0:i+1]))

count = 0
ans1 = 0
for i in range(n):
    if i%2 == 0:
        if s[i]+count > 0:
            continue
        else:
            ans1 += 1-(s[i]+count)
            count += 1-(s[i]+count)
    else:
        if s[i]+count < 0:
            continue
        else:
            ans1 += (s[i]+count) + 1
            count -= (s[i]+count) + 1
            
count1 = 0
ans2 = 0
for i in range(n):
    if i%2 == 1:
        if s[i]+count1 > 0:
            continue
        else:
            ans2 += 1-(s[i]+count1)
            count1 += 1-(s[i]+count1)
    else:
        if s[i]+count1 < 0:
            continue
        else:
            ans2 += (s[i]+count1) + 1
            count1 -= (s[i]+count1) + 1
            
print(min(ans1,ans2))

Submission status: Time Limit Exceeded

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

total = 0
count = 0
pos_or_neg = True  # True = +, False = -

if sum(a_list) >= 0:
    if n % 2 == 0:
        pos_or_neg = False
    else:
        pos_or_neg = True
else:
    if n % 2 == 0:
        pos_or_neg = True
    else:
        pos_or_neg = False

for a in a_list:
    total += a
    if pos_or_neg:
        while total <= 0:
            total += 1
            count += 1
        pos_or_neg = False

    else:
        while total >= 0:
            total -= 1
            count += 1
        pos_or_neg = True

print(count)

Submission status: Time Limit Exceeded

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

b=a[:]
for i in range(n):
    if i%2:b[i]=min(-sum(b[j] for j in range(i))-1,b[i])
    else: b[i]=max(-sum(b[j] for j in range(i))+1,b[i])
s=sum(abs(b[i]-a[i]) for i in range(n))

c=a[:]
for i in range(n):
    if i%2:c[i]=max(-sum(c[j] for j in range(i))+1,c[i])
    else: c[i]=min(-sum(c[j] for j in range(i))-1,c[i])
t=sum(abs(c[i]-a[i]) for i in range(n))

print(min(s,t))

Submission status: Time Limit Exceeded

Solution code:
import copy

n = int(input())
a = list(map(int,input().split()))
b = [0]

for i in range(len(a)):
    b.append(b[i] + a[i])

del b[0]

c = copy.copy(b)

first_minus = 0
first_plus = 0

for i in range(len(b)):
    if(i%2 == 0):
        if (b[i] >= 0):
            diff =  abs(b[i]) + 1
            first_minus = first_minus +  diff
            b = list(map(lambda x: x - diff, b))

        if (c[i] <= 0):
            diff =  abs(c[i]) + 1
            first_plus = first_plus +  diff
            c = list(map(lambda x: x + diff, c))
    else:
        if (b[i] <= 0):
            diff =  abs(b[i]) + 1
            first_minus = first_minus +  diff
            b = list(map(lambda x: x + diff, b))

        if (c[i] >= 0):
            diff =  abs(c[i]) + 1
            first_plus = first_plus +  diff
            c = list(map(lambda x: x - diff, c))

print(min(first_minus,first_plus))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
i = 0
sum_before = 0
sum_after = 0
count = 0
while i < n:
    sum_after = sum_before + a[i]
    if sum_after * sum_before > 0 or sum_after == 0:
        count += 1
        if sum_after < 0:
            a[i] += 1
        elif sum_after > 0:
            a[i] -= 1
        elif sum_before < 0:
            a[i] += 1
        else:
            a[i] -= 1
    else:
        i += 1
        sum_before = sum_after

print(count)

Submission status: Time Limit Exceeded

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

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

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

ans = 0
cnt = 0
s = 0

for i in range(n):
    if i == 0:
        ans += li[i]
        if ans > 0:
            s = 1
        else:
            s = -1
    else:
        ans += li[i]
        if ans <= 0 and s == -1:
            while True:
                ans += 1
                cnt += 1
                if ans > 0:
                    s == 1
                    break
        if ans >= 0 and s == 1:
            while True:
                ans -= 1
                cnt += 1
                if ans < 0:
                    s == -1
                    break
        s *= -1

print(cnt)

Submission status: Time Limit Exceeded

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

def countA(a):
    count = 0
    for i in range(1,len(a)):
        if i%2==1:
            if a[i] > -sum(a[0:i])-1:
                count = count + sum(a[0:i])+1+a[i]
                a[i] = -sum(a[0:i])-1
        if i%2==0:
            if a[i] < -sum(a[0:i])+1:
                count = count-sum(a[0:i])+1-a[i]
                a[i] = -sum(a[0:i])+1
    return count

def countB(a):
    count = 0
    for i in range(1,len(a)):
        if i%2==0:
            if a[i] > -sum(a[0:i])-1:
                count = count + sum(a[0:i])+1+a[i]
                a[i] = -sum(a[0:i])-1
        if i%2==1:
            if a[i] < -sum(a[0:i])+1:
                count = count-sum(a[0:i])+1-a[i]
                a[i] = -sum(a[0:i])+1
    return count


if a[0]>0:
    count = countA(a)
elif a[0]<0:
    count = countB(a)
else:
    a[0] = 1
    count_ = countA(a) + 1
    a[0] = -1
    count = min(countB(a)+1, count_)
    
print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = 0
tmp = a[0]
for i in range(n-1):
    if (tmp > 0):
        while not (-tmp > a[i+1]):
            a[i+1] += -1
            ans += 1
    else:
        while not (-tmp < a[i+1]):
            a[i+1] += 1
            ans += 1
    tmp += a[i+1]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n=int(input())
a=list(map(int,input().split()))
r=[0]
for i in range(n):
    r.append(r[i]+a[i])
r.pop(0)
q=[r[i] for i in range(n)]

pm=[1-2*(i%2) for i in range(n)]
mp=[1-2*((i+1)%2) for i in range(n)]

sum1,sum2=0,0
sousa1,sousa2=0,0

for i in range(n):
    if np.sign(r[i]) != pm[i]:
        sum1+=abs(pm[i]-r[i])
        sousa1=pm[i]-r[i]
        for j in range(n-i-1):
            r[i+j+1]=r[i+j+1]+sousa1
        
for i in range(n):
    if np.sign(q[i]) != mp[i]:
        sum2+=abs(mp[i]-q[i])
        sousa2=mp[i]-q[i]
        for j in range(n-i-1):
            q[i+j+1]=q[i+j+1]+sousa2
        
print(min(sum1,sum2))