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 sign(x):
    if x>0:
        return 1
    elif x<0:
        return -1
    else:
        return 0


n = int(input())
a = [int(i) for i in input().split()]
s = [sum(a[:i+1]) for i in range(n)]
sp = [s[i] for i in range(n)]
sm = [s[i] for i in range(n)]
P0=0
M0=0
if s[0]==0:
    P0+=1
    M0+=1
    for j in range(n):
        sp[j]+=1
        sm[j]+=1
elif s[0]>0:
    M0+=abs(s[0]+1)
    for j in range(n):
        sm[j]-=(abs(s[0])+1)
else:
    P0+=abs(s[0]+1)
    for j in range(n):
        sp[j]+=(abs(s[0])+1)
for i in range(1,n):
    if sm[i-1]*sm[i]>=0:
        M0+=abs(sm[i])+1
        h=sign(sm[i-1])*(-1)*(abs(sm[i])+1)
        for j in range(i,n):
            sm[j]+=h
    if sp[i-1]*sp[i]>=0:
        P0+=abs(sp[i])+1
        h=sign(sp[i-1])*(-1)*(abs(sp[i])+1)
        for j in range(i,n):
            sp[j]+=h
print(min([M0,P0]))

Submission status: Time Limit Exceeded

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

gokei = 0
ans1 = 0

for i in range(N):
    gokei = sum(A[:i+1])
    # 偶数番目を負に
    if i % 2 == 0:
        if gokei >= 0:
            ans1 += abs(-1 - gokei)
            A[i] -= abs(-1 - gokei)
    if i % 2 == 1:
        if gokei <= 0:
            ans1 += 1 - gokei
            A[i] += (1 - gokei)


gokei = 0
ans2 = 0
for i in range(N):
    gokei = sum(B[:i+1])
    # 偶数番目を正に
    if i % 2 == 0:
        if gokei <= 0:
            ans2 += 1 - gokei
            B[i] += (1 - gokei)
    else:
        if gokei >= 0:
            ans2 += abs(-1 - gokei)
            B[i] -= abs(-1 - gokei)

print(min(ans1, ans2))

Submission status: Time Limit Exceeded

Solution code:
n=input()
a=map(int,raw_input().split())
#print n
#print a

a1_sum=0
a1=[]
for i in a:
	a1_sum+=i
	a1.append(a1_sum)

#print a1


#+,-
b=[]
for i,val in enumerate(a1):
	for j in b:
		val+=j
	if i%2==0:
		if val>0:
			pass
		else:
			t_val=1-val
			b.append(t_val)
	else:
		if val<0:
			pass
		else:
			t_val=-1-val
			b.append(t_val)
ans1=0
if len(b)==0:
	ans1=0
else:
	for i in b:
		ans1+=abs(i)

#-,+
b=[]
for i,val in enumerate(a1):
	for j in b:
		val+=j
	if i%2==0:
		if val<0:
			pass
		else:
			t_val=-1-val
			b.append(t_val)
	else:
		if val>0:
			pass
		else:
			t_val=1-val
			b.append(t_val)

ans2=0
if len(b)==0:
	ans2=0
else:
	for i in b:
		ans2+=abs(i)


print min(ans1,ans2)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = [int(i) for i in input().split()]
sam = a[0]
old = sam
num = 0
for i in range(1, len(a)):
    sam += a[i]
    if sam >= 0 and old > 0:
        while sam >= 0:
            sam -= 1
            num += 1
    elif sam <= 0 and old < 0:
        while sam <= 0:
            sam += 1
            num += 1
    old = sam
print(num)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
arr = list(map(int,input().split()))
def S1(a,b):
    c,last,ans = 0,0,0
    for t in range(N):
        c += 1
        if c % 2 == a:
            while last+arr[t] <= 0:
                ans += 1
                last += 1
        elif c % 2 == b:
            while last +arr[t] >= 0:
                ans +=1
                last -= 1
        last += arr[t]
    return ans
aa = S1(1,0)
bb = S1(0,1)
print(min(aa,bb))

Submission status: Time Limit Exceeded

Solution code:
import copy

N = int(input())
a_list = list(map(int,input().split()))

a_listg = copy.deepcopy(a_list)

a_sumg = 0
xg = 0

for i in range(10**10):
    if a_listg[0] < 1:
        a_listg[0] += 1
        xg += 1
    else:
        break
a_sumg += a_listg[0]

for j in range(1,N):
    if j%2 != 0:
        for k in range(10**10):
            if a_sumg + a_listg[j] > -1:
                a_listg[j] -= 1
                xg += 1
            else:
                a_sumg += a_listg[j]
                break
    else:
        for l in range(10**10):
            if a_sumg + a_listg[j] < 1:
                a_listg[j] += 1
                xg += 1
            else:
                a_sumg += a_listg[j]
                break
a_listk = copy.deepcopy(a_list)

a_sumk = 0
xk = 0

for m in range(10**10):
    if a_listk[0] > -1:
        a_listk[0] -= 1
        xk += 1
    else:
        break
a_sumk += a_listk[0]

for n in range(1,N):
    if n%2 == 0:
        for k in range(10**10):
            if a_sumk + a_listk[n] > -1:
                a_listk[n] -= 1
                xk += 1
            else:
                a_sumk += a_listk[n]
                break
    else:
        for o in range(10**10):
            if a_sumk + a_listk[n] < 1:
                a_listk[n] += 1
                xk += 1
            else:
                a_sumk += a_listk[n]
                break

print(min([xg,xk]))

Submission status: Time Limit Exceeded

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

total = 0
fugo = 0
count = 0

for i in a:
    total += i

    if(fugo == 0):
        total = i
        if(total > 0):
            fugo = 1
        else:
            fugo = -1
        continue
    elif(fugo > 0):
        fugo = -1
        if(total >= 0):
            while(total>=0):
                count += 1
                total -= 1
    elif(fugo < 0):
        fugo = 1
        if(total <= 0):
            while(total<=0):
                count += 1
                total += 1

print(count)

Submission status: Time Limit Exceeded

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

total = 0
fugo = 0
count = 0

for i in a:
    if(fugo == 0):
        total = i
        if(total > 0):
            fugo = 1
        else:
            fugo = -1
        continue


    total += i

    if(fugo > 0):
        fugo = -1
        if(total >= 0):
            while(total>=0):
                count += 1
                total -= 1
    elif(fugo < 0):
        fugo = 1
        if(total <= 0):
            while(total<=0):
                count += 1
                total += 1

print(count)

Submission status: Time Limit Exceeded

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

total = 0
fugo = 0
count = 0

for i in a:
    if(fugo == 0):
        total = i
        if(total > 0):
            fugo = 1
        else:
            fugo = -1
        continue


    total += i

    if(fugo > 0 and total < 0):
        fugo = -1
    elif(fugo < 0 and total > 0):
        fugo = 1
    elif(fugo > 0 and total >= 0):
        fugo = -1
        while(total>=0):
            count += 1
            total -= 1
    elif(fugo < 0 and total <= 0):
        fugo = 1
        while(total<=0):
            count += 1
            total += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':

    N = input()
    array = raw_input().split()

    ans = 0
    total = int(array[0])

    totalZero = False
    if total == 0:
        totalZero = True
    
    
    flag = False
    if total >= 0:
        flag = True
        
    for a in array[1:]:

        if totalZero == True:
            ans += 1
            if a > 0:
                total = -1
            else:
                total = 1
            totalZero = False

        total += int(a)
        
        if total > 0 and flag == True:
            while True:
                ans += 1
                total -= 1
                if total == -1:
                    flag = False
                    break
                
        elif total < 0 and flag ==False:
            while True:
                ans += 1
                total += 1
                if total == 1:
                    flag == True
                    break
                
        elif total == 0:
            totalZero = True

        if total > 0:
            flag = True
        elif total < 0:
            flag = False

        
    if totalZero == True:
        ans += 1

    print ans

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a_list = [int(i) for i in input().split(' ')]
total = [a_list[0]]
counter = 0

for a in a_list[1:]:
    total.append(total[-1] + a)
    
if total[0]==0:
    total = list(map(lambda n: n-int(total[1]/abs(total[1])), total))
for i in range(1,n):    
    if total[i-1]<0 and total[i]<=0:
        counter += abs(total[i])+1
        total[i:] = list(map(lambda n: n-(total[i])+1, total[i:]))
    elif total[i-1]>0 and total[i]>=0:
        counter += abs(total[i])+1
        total[i:] = list(map(lambda n: n-(total[i])-1, total[i:]))
    else:
        continue

print(counter)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())                        # 数列の長さ
a = [int(x) for x in input().split()]   # 数列の各項
# print(a)
cnt = 0             # 操作回数
while True:
    sum = a[0]      # １列目までの和
    # １～Ｎ列目までの和を求める
    # print ('和 → ' + str(sum), end = ' ')
    # print (str(sum), end = ' ')
    for i in range(1, len(a)):
        pre = sum
        sum += a[i]
        # print (str(sum), end = ' ')
        # 条件を満たしてない場合は和を求めるのを中断する
        if (pre > 0 and sum > 0) or (pre < 0 and sum < 0) or (sum == 0):
            break
    # print('')
    # print('i = ' + str(i))
    # print('pre = ' + str(pre) + ', sum = ', str(sum))
    if (pre > 0 and sum < 0) or (pre < 0 and sum > 0):
        # 条件を満たした場合の処理
        # print(a)
        print(str(cnt))
        break
    else:
        # 操作が必要な場合の処理
        if pre < 0:
            j = 1
        elif pre > 0:
            j = -1
        if pre < -1 or pre > 1:
            # preの絶対値が１でない場合（i - 1番目を操作する）
            if pre < 0 and i != len(a) - 1:
                j = -(pre + 1)
            elif pre > 0 and i != len(a) - 1:
                j = -(pre - 1)
            a[i - 1] += j
        else:
            # preの絶対値が１の場合（i番目を操作する）
            if pre < 0:
                j = -(sum - 1)
            elif pre > 0:
                j = -(sum + 1)
            a[i] += j
        # 操作回数の更新
        if j < 0:
            j = -j
        cnt += j
        # print ('cnt = ' + str(cnt))
    # print (a)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())                        # 数列の長さ
a = [int(x) for x in input().split()]   # 数列の各項
cnt = 0             # 操作回数
while True:
    sum = a[0]      # １列目までの和
    # １～Ｎ列目までの和を求める
    # print (sum, end = ' ')
    for i in range(1, len(a)):
        pre = sum
        sum += a[i]
        # print (sum, end = ' ')
        # 条件を満たしてない場合は和を求めるのを中断する
        if (pre > 0 and sum > 0) or (pre < 0 and sum < 0) or (sum == 0):
            break
    # print('')
    # print('i = ' + str(i))
    # print('pre = ' + str(pre) + ', sum = ', str(sum))
    if (pre > 0 and sum < 0) or (pre < 0 and sum > 0):
        # 条件を満たした場合の処理
        print(str(cnt))
        break
    else:
        # 操作が必要な場合の処理
        cnt = cnt + 1   # 操作回数の更新
        if pre < 0:
            j = 1
        elif pre > 0:
            j = -1
        if pre < -1 or pre > 1:
            a[i - 1] += j
        else:
            a[i] += j
    # print (a)

Submission status: Time Limit Exceeded

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

cnt =0

def check(a,i,cnt):
    sum1 = sum(a[:i])   
    sum2 = sum(a[:i+1])
    if sum1<0 and sum2 <=0:
         a[i] += abs(sum2)+1
         cnt  += abs(sum2)+1
    elif sum1>0 and sum2 >=0:
         a[i] -= abs(sum2)+1
         cnt  += abs(sum2)+1
    return cnt
    
for i in range(n):
    cnt=check(a,i,cnt)

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding:utf-8 -*-

n = int(raw_input())
numlist = (raw_input()).split(' ')

sumlist = [int(numlist[0])]
count = 0

for i in range(1, n):
  sumlist.append(sumlist[i-1] + int(numlist[i]))
  while (True):
    if (sumlist[i-1] > 0 and sumlist[i] > 0):  #i-1,i番目までのsumがともに正
      #numlist[i] = int(numlist[i]) - 1
      sumlist[i] -= 1
      count += 1
    elif (sumlist[i-1] < 0 and sumlist[i] < 0):  #i-1,i番目までのsumがともに負
      #numlist[i] = int(numlist[i]) + 1
      sumlist[i] += 1
      count += 1
    elif (sumlist[i] == 0):  #i番目までのsum=0
      if (sumlist[i-1] > 0):
        #numlist[i] = int(numlist[i]) - 1
        sumlist[i] -= 1
      if (sumlist[i-1] < 0):
        #numlist[i] = int(numlist[i]) + 1
        sumlist[i] += 1
      count += 1
    else:
      break

print count

Submission status: Time Limit Exceeded

Solution code:
# -*- coding:utf-8 -*-

n = int(raw_input())
numlist = (raw_input()).split(' ')

sumlist = [int(numlist[0])]
count = 0

for i in range(1, n):
  sumlist.append(sumlist[i-1] + int(numlist[i]))
  while (True):
    if (sumlist[i-1] > 0 and sumlist[i] > 0):  #i-1,i番目までのsumがともに正
      numlist[i] = int(numlist[i]) - 1
      sumlist[i] -= 1
      count += 1
    elif (sumlist[i-1] < 0 and sumlist[i] < 0):  #i-1,i番目までのsumがともに負
      numlist[i] = int(numlist[i]) + 1
      sumlist[i] += 1
      count += 1
    elif (sumlist[i] == 0):  #i番目までのsum=0
      if (sumlist[i-1] > 0):
        numlist[i] = int(numlist[i]) - 1
        sumlist[i] -= 1
      if (sumlist[i-1] < 0):
        numlist[i] = int(numlist[i]) + 1
        sumlist[i] += 1
      count += 1
    else:
      break

print count

Submission status: Time Limit Exceeded

Solution code:
# -*- coding:utf-8 -*-

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


n = int(raw_input())
numlist = (raw_input()).split(' ')

sumlist = [int(numlist[0])]
count = 0

for i in range(1, n):
  sumlist.append(sumlist[i-1] + int(numlist[i]))
  while ((sign(sumlist[i-1]) == sign(sumlist[i])) or (sumlist[i] == 0)):
    if (sumlist[i] > 0):  #i-1,i番目までのsumがともに正
      numlist[i] = int(numlist[i]) - 1
      sumlist[i] -= 1
      count += 1
    elif (sumlist[i] < 0):  #i-1,i番目までのsumがともに負
      numlist[i] = int(numlist[i]) + 1
      sumlist[i] += 1
      count += 1
    else:
      if (sumlist[i-1] > 0):
        numlist[i] = int(numlist[i]) - 1
        sumlist[i] -= 1
      if (sumlist[i-1] < 0):
        numlist[i] = int(numlist[i]) + 1
        sumlist[i] += 1
      count += 1

print count

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力
n=int(input())
a=input().split()
counter=0
# 出力
for i in range(1,n):
  S=0
  for j in range(0,i):
    S+=int(a[j])
  if S<0 and S+int(a[i])<=0:
    counter+=-S-int(a[i])+1
    a[i]=-S+1
  elif S>0 and S+int(a[i])>=0:
    counter+=S+int(a[i])+1
    a[i]=-S-1
print(counter)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力
n=int(input())
a=input().split()
counter=0
# 出力
for i in range(1,n):
  S=0
  for j in range(0,i):
    S=S+int(a[j])
  if S<0 and S+int(a[i])<=0:
    counter=counter-S-int(a[i])+1
    a[i]=-S+1
  elif S>0 and S+int(a[i])>=0:
    counter=counter+S+int(a[i])+1
    a[i]=-S-1
print(counter)

Submission status: Time Limit Exceeded

Solution code:

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

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



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

ans1 = 0
ans2 = 0

while(True):
  c = check1(a)
  if(c == True):
    break

  a[c[0]] += c[1]
  ans1 += abs(c[1])


while(True):
  c = check2(a2)
  if(check2(a2) == True):
    break
  a2[c[0]] += c[1]
  ans2 += abs(c[1])


print(ans1) if(ans1<ans2) else print(ans2)

Submission status: Time Limit Exceeded

Solution code:

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

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



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

ans1 = 0
ans2 = 0

while(True):
  c = check1(a)
  if(c == True):
    break

  a[c[0]] += c[1]
  ans1 += 1


while(True):
  c = check2(a2)
  if(check2(a2) == True):
    break
  a2[c[0]] += c[1]
  ans2 += 1


print(ans1) if(ans1<ans2) else print(ans2)

Submission status: Time Limit Exceeded

Solution code:

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

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



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

ans1 = 0
ans2 = 0

while(True):
  c = check1(a)
  if(c == True):
    break

  a[c[0]] += c[1]
  ans1 += 1


while(True):
  c = check2(a2)
  if(check2(a2) == True):
    break
  a2[c[0]] += c[1]
  ans2 += 1


print(ans1) if(ans1<ans2) else print(ans2)

Submission status: Time Limit Exceeded

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


def check(a):
  sum = 0
  for i in range(len(a)):
    if(i == 0):
      if(a[i] == 0):
        return (i, +1)
      sum += a[0]
      continue
    if(sum > 0):
      sum += a[i]
      if(a[i] == 0 or sum >= 0):
        return (i, -1)

    elif(sum < 0):
      sum += a[i]
      if(a[i] == 0 or sum <= 0):
        return (i, +1)
    else:
      if(a[i-1] > 0):
        return (i, -1)
      else:
        return (i, +1)



  return True

ans = 0
while(True):
  c = check(a)
  if(c == True):
    break

  a[c[0]] += c[1]

  ans += 1



print(ans)

Submission status: Time Limit Exceeded

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


def check(a):
  sum = 0
  for i in range(len(a)):
    if(i == 0):
      sum += a[0]
      continue
    if(sum > 0):
      sum += a[i]
      if(a[i] == 0 or sum >= 0):
        return (i, -1)

    elif(sum < 0):
      sum += a[i]
      if(a[i] == 0 or sum <= 0):
        return (i, +1)
    else:
      if(a[i-1] > 0):
        return (i, -1)
      else:
        return (i, +1)



  return True

ans = 0
while(True):
  c = check(a)
  if(c == True):
    break

  a[c[0]] += c[1]

  ans += 1



print(ans)

Submission status: Time Limit Exceeded

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


def check(a):
  sum = 0
  for i in range(len(a)):
    if(i == 0):
      sum += a[0]
      continue
    if(sum > 0):
      sum += a[i]
      if(a[i] == 0 or sum >= 0):
        return (i, -1)

    else:
      sum += a[i]
      if(a[i] == 0 or sum <= 0):
        return (i, +1)



  return True

ans = 0
while(True):
  c = check(a)
  if(c == True):
    break

  a[c[0]] += c[1]

  ans += 1



print(ans)

Submission status: Time Limit Exceeded

Solution code:
n1=int(input())
l1=list(map(int,input().split()))
total=l1[0]
Num=0
for j in range(1,n1):
    if total ==0:
        total=total+1
        Num=Num+1
        
    pretotal=total
    total=total+l1[j]
    while (pretotal*total>0) or (total ==0):
        if pretotal<=0:
                Num=Num+1
                total=total+1
        elif pretotal>0:
                total=total-1
                Num=Num+1
print(Num)

Submission status: Time Limit Exceeded

Solution code:
n1=int(input())
l1=list(map(int,input().split()))
suml=[sum(l1[:i]) for i in range(1,n1+1)]
Num=0
for j in range(1,n1):
    if j==1 and suml[j]==0:
        l1[j]=l1[j]+1
        Num=Num+1
        suml=[sum(l1[:i]) for i in range(1,n1+1)]
    else:    
        while (suml[j-1]*suml[j]>0) or (suml[j] ==0):
            if suml[j-1]<0:
                l1[j]=l1[j]+1
                Num=Num+1
                suml=[sum(l1[:i]) for i in range(1,n1+1)]
            elif suml[j-1]>0:
                l1[j]=l1[j]-1
                Num=Num+1
                suml=[sum(l1[:i]) for i in range(1,n1+1)]
print(Num)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n = int(input())
list1 = (input().split())
list2 = [int(i) for i in list1]

cnt=0
for i in range(n):
    #最初だけ、ゼロの場合は次を見て判定
    if i <= 0 and list2[0] == 0:
        cnt += 1
        list2[0] = 1 if list2[1] <=0  else -1
    
    #i項までの和がゼロ
    if i > 0 and sum(list2[0:i+1])  == 0:
        cnt += 1
        #手前までの和が正なら1減算、負なら1加算
        list2[i] -= np.sign(sum(list2[0:i]))
    
    #i項までの和が手前の和と符号が同じ
    elif i > 0 and np.sign(sum(list2[0:i+1])) == np.sign(sum(list2[0:i])):
        cnt += abs(sum(list2[0:i+1]))+1
        list2[i] = list2[i] + ( abs(sum(list2[0:i+1])) + 1 ) * -np.sign(sum(list2[0:i+1]))

print(cnt)

Submission status: Time Limit Exceeded

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

def judge_pm(a,b):
    if a*b<0:
        return True
    else:
        return False

a1 = copy.deepcopy(a)
tmp_sum = a1[0]
operate_num1 = 0
for i in range(1, n):
    if judge_pm(tmp_sum, tmp_sum+a1[i]):
        pass 
    elif tmp_sum<0:
        tmp_operate_num1 =  - tmp_sum + 1 - a1[i]
        operate_num1 += tmp_operate_num1
        a1[i] += tmp_operate_num1
    else:
        tmp_operate_num1 = tmp_sum + 1 + a1[i]
        operate_num1 += tmp_operate_num1
        a1[i] -= tmp_operate_num1
    tmp_sum += a1[i]
        
a2 = copy.deepcopy(a)
tmp_sum = a2[0]
operate_num2 = 0
for i in range(1, n):
    if judge_pm(tmp_sum, tmp_sum+a2[i]):
        pass 
    elif tmp_sum<0:
        tmp_operate_num2 =  - tmp_sum + 1 - a2[i]
        operate_num2 += tmp_operate_num2
        a2[i-1] -= tmp_operate_num2
        tmp_sum -= tmp_operate_num2
    else:
        tmp_operate_num2 = tmp_sum + 1 + a2[i]
        operate_num2 += tmp_operate_num2
        a2[i-1] += tmp_operate_num2
        tmp_sum += tmp_operate_num2
    tmp_sum += a2[i]
print(min(operate_num1, operate_num2))

Submission status: Time Limit Exceeded

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


a1 = a.copy()
cnt1 = 0
sum1 = 0
if a1[0] == 0:
    a1[0] = 1
    cnt1 +=1

a2 = a.copy()
cnt2 = 0
sum2 = 0
if a2[0] == 0:
    a2[0] = -1
    cnt2 +=1
    
    
for i in range(n-1):
    sum1 = sum(a1[:i+1])
    if sum1 > 0 and (sum1+a1[i+1]) >= 0:
        cnt1 += abs(((sum1 * -1)-1) - a1[i+1])
        a1[i+1] = ((sum1 * -1)-1)
    
    elif sum1 < 0 and (sum1 + a1[i+1]) <= 0:
        cnt1 += abs(abs(sum1)+1 - a1[i+1])
        a1[i+1] = abs(sum1)+1

for i in range(n-1):
    sum2 = sum(a2[:i+1])
    if sum2 > 0 and (sum2+a2[i+1]) >= 0:
        cnt2 += abs(((sum2 * -1)-1) - a2[i+1])
        a2[i+1] = ((sum2 * -1)-1)
        
    elif sum2 < 0 and (sum2+a2[i+1]) <= 0:
        cnt2 += abs(abs(sum2)+1 - a2[i+1])
        a2[i+1] = abs(sum2)+1
        
print(min(cnt1,cnt2))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
s = [ sum(a[0:i+1]) for i in range(n)]
s2 = [ s[i]*(-1) for i in range(n)]
c1 = 0
for i in range(n):
    if s[i]*(-1)**i <= 0:
        c1 = c1 + s[i]*(-1)**(i+1) + 1
        s = [s[j] if j <= i-1 else s[j] - (s[i])*(-1)**i + (-1)**i for j in range(n)]
c2 = 0
for i in range(n):
    if s2[i]*(-1)**i <= 0:
        c2 = c2 + s2[i]*(-1)**(i+1) + 1
        s2 = [s2[j] if j <= i-1 else s2[j] - (s2[i])*(-1)**i + (-1)**i for j in range(n)]
print(min(c1,c2))

Submission status: Time Limit Exceeded

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

count = 0

if a[0] == 0:
    count += 1
    if 0 < a[1]:
        a[0] = 1
    else:
        a[0] = -1

for i in range(1, n):
    SUM = [sum(a[:i]), sum(a[:(i + 1)])]
    if 0 <= SUM[0] * SUM[1]:
        if 0 < SUM[0]:
            a[i] -= (SUM[1] + 1)
            count += (SUM[1] + 1)
        else:
            a[i] += (-SUM[1] + 1)
            count += (-SUM[1] + 1)

print(count)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def seiyasum(num,alist):
	sum1 = 0
	for a in range(num):
		sum1 = sum1+a
	return sum1



n = int(raw_input())
a = map(int,raw_input().split(" "))

k = a[0]

answer = 0

for m in range(n-1):
	i = a[m+1]
	if(k > 0):
		l = -1
	else:
		l = 1
	while((k+i)*k>=0):
		i = i + l
		answer = answer + 1
	k = k+i

print answer

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
data = input().split()

for i in range(n):
	data[i] = int(data[i])
count = 0
sum = data[0]
if(sum == 0):
	i = 1
	while(data[i] == 0):
		i += 1
	if(i % 2 == 0):
		if(data[i] > 0):
			sum += 1
		else:
			sum -= 1
	else:
		if(data[i] > 0):
			sum -= 1
		else:
			sum += 1

i = 1
while(i < n):
	while(sum * (sum + data[i]) >= 0):
		if sum > 0:
			data[i] -= 1
			count += 1
		elif sum < 0:
			data[i] += 1
			count += 1
	sum += data[i]
	if(sum == 0):
		if(data[i] > 0):
			sum += 1
		else:
			sum -= 1
	i += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
a = int(input())
b = list(map(int, input().split()))
num = 0
k = b[0] + b[1]

if k == 0:
 if b[0] >= 0:
  k -= 1
 else:
  k += 1
 num += 1

for i in range(a-2): 
 if k > 0:
  k += b[i+2]
  while k > -1:
   k -= 1
   num += 1
 else:
  k += b[i+2]
  while k < 1:
   k += 1
   num += 1
 
print(num)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
data = input().split()

for i in range(n):
	data[i] = int(data[i])
count = 0
sum = data[0]

i = 1
while(i < n):
	while(sum * (sum + data[i]) >= 0):
		if sum > 0:
			data[i] -= 1
			count += 1
		elif sum < 0:
			data[i] += 1
			count += 1
	sum += data[i]
	i += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
c = 0
i = 1
k = 1
sum = a[0]

if a[0] > 0:
	while i < n:
		if i == 2*k-1:
			sum = sum + a[2*k-1]
			while sum >= 0:
				sum -= 1
				c += 1
			i += 1
		else:
			sum = sum + a[2*k]
			while sum <= 0:
				sum += 1
				c += 1
			i += 1
			k += 1
	print(c)
else:
	while i < n:
		if i == 2*k-1:
			sum = sum + a[2*k-1]
			while sum <= 0:
				sum += 1
				c += 1
			i += 1
		else:
			sum = sum + a[2*k]
			while sum >= 0:
				sum -= 1
				c += 1
			i += 1
			k += 1
	print(c)

Submission status: Time Limit Exceeded

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

s = [sum(a[0:i+1]) for i in range(n)]
count = 0
total_delta = 0
if s[0] == 0:
    delta = -1 * (s[1] // abs(s[1]))
    total_delta += delta
    count += 1
for i in range(1, n):
    sign = (s[i-1] + total_delta) // abs(s[i-1] + total_delta)
    if (s[i] + total_delta) * sign > 0 :
        delta  = (sign * -1) - (s[i] + total_delta)
        total_delta += delta
        count += abs(delta)
    elif (s[i] + total_delta) == 0:
        total_delta += sign * -1
        count += 1
print(count)

Submission status: Memory Limit Exceeded

Solution code:
def main():
    _ = input()
    a = [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: Accepted

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

ans1 = 0
cum1 = 0
for i in range(N):
    cum1 += A[i]
    if i % 2 == 0:
        if cum1 <= 0:
            ans1 += abs(cum1) + 1
            cum1 += abs(cum1) + 1
    else:
        if cum1 >= 0:
            ans1 += abs(cum1) + 1
            cum1 -= abs(cum1) + 1

ans2 = 0
cum2 = 0
for i in range(N):
    cum2 += A[i]
    if i % 2 == 1:
        if cum2 <= 0:
            ans2 += abs(cum2) + 1
            cum2 += abs(cum2) + 1
    else:
        if cum2 >= 0:
            ans2 += abs(cum2) + 1
            cum2 -= abs(cum2) + 1

print(min(ans1, ans2))

Submission status: Accepted

Solution code:
import sys

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

tin=lambda : map(int, input().split())
lin=lambda : list(tin())
mod=1000000007

#+++++

def cc(al, start):
	mc = 0
	is_m = start
	pre_total_sum=0
	for v in al:
		pre_total_sum += v
		if is_m*pre_total_sum <= 0:
			mc += abs(pre_total_sum-is_m)
			pre_total_sum = is_m
		is_m *= -1
	#pa(mc)
	return mc

def main():
	n = int(input())
	#b , c = tin()
	#s = input()
	al = lin()
	ret = min(cc(al, -1), cc(al, 1))
	return ret
	
	
	
#+++++
isTest=False

def pa(*vl):
	if not isTest:
		return
	for v in vl:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Submission status: Accepted

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

def solve(parity):
  wa=0
  ans=0
  for i in range(n):
    wa+=a[i]
    if i%2==parity:
      if wa>=0:
        ans+=wa+1
        wa-=wa+1
    else:
      if wa<=0:
        ans+=-wa+1
        wa+=-wa+1
  return ans

print(min(solve(0),solve(1)))

Submission status: Accepted

Solution code:
N=int(input())
array=[int(x) for x in input().split()]
currentsum=0
counter=0
for i in range(N):
	currentsum+=array[i]
	if i%2==0 and currentsum<=0:
		counter+=abs(1-currentsum)
		currentsum+=(1-currentsum)
	if i%2==1 and currentsum>=0:
		counter+=abs(currentsum+1)
		currentsum-=(currentsum+1)
currentsum1=0
counter1=0
for i in range(N):
	currentsum1+=array[i]
	if i%2==1 and currentsum1<=0:
		counter1+=abs(1-currentsum1)
		currentsum1+=(1-currentsum1)
	if i%2==0 and currentsum1>=0:
		counter1+=abs(currentsum1+1)
		currentsum1-=(currentsum1+1)
		
print(min(counter,counter1))

Submission status: Accepted

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

def calc(t):
  ans = su = 0
  for i in range(n):
    su += a[i]
    if i % 2 == t:
      if su > 0:
        pass
      else:
        x = 1 - su
        ans += abs(x)
        su = 1
    else:
      if su < 0:
        pass
      else:
        x = - 1 - su
        ans += abs(x)
        su = -1
  return ans
print(min(calc(1), calc(0)))