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: Wrong Answer

Solution code:
def c(ints):
    
    for i in range(len(ints)):
        if ints[i] != 0:
            sig = 1 if ints[i] > 0 else -1
            total = ints[i]
            mov = i
            if i > 0:
                mov += 1
            j = i
            break
        if i == len(ints) - 1:
            return i + 1
    
    for i_ in ints[j+1:]:
        tmp = total + i_
        if tmp == 0:
            mov +=1
            tmp = -sig
            
        elif sig * tmp > 0:
            mov += abs(tmp) + 1
            tmp = -sig
            
        sig *= -1
        total = tmp
        
    return mov

_ = input()
inp = input()
inp = inp.split(' ')
inp = [int(i_) for i_ in inp]
print(c(inp))

Submission status: Wrong Answer

Solution code:

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

count_a = 0
count_b = 0

nowsum = a[0]
if nowsum != 0:
    for n in range(1, N):
        if nowsum * (nowsum + a[n]) >= 0:
            count_a += abs(nowsum + a[n]) + 1
            if nowsum < 0:
                nowsum = 1
            else:
                nowsum = -1
        else:
            nowsum += a[n]
    print(count_a)

else:
    a[0] = 1
    count_a += 1
    nowsum = 1
    for n in range(1, N):
        if nowsum * (nowsum + a[n]) >= 0:
            count_a += abs(nowsum + a[n]) + 1
            if nowsum < 0:
                nowsum = 1
            else:
                nowsum = -1
        else:
            nowsum += a[n]
    
    a[0] = -1
    count_b += 1
    nowsum = -1
    for n in range(1, N):
        if nowsum * (nowsum + a[n]) >= 0:
            count_b += abs(nowsum + a[n]) + 1
            if nowsum < 0:
                nowsum = 1
            else:
                nowsum = -1
        else:
            nowsum += a[n]   
    print(min(count_a, count_b))

Submission status: Wrong Answer

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

count = 0
sum_i = a[0]
if a[0] != 0:
    for i in range(1, n):
        if sum_i < 0:
            sum_i += a[i]
            if sum_i <= 0:
                count += 1 - sum_i
                sum_i = 1
        elif sum_i > 0:
            sum_i += a[i]
            if sum_i >= 0:
                count += sum_i + 1
                sum_i = -1
else:
    sum_i = 1
    pos_count = 1
    for i in range(1, n):
        if sum_i < 0:
            sum_i += a[i]
            if sum_i <= 0:
                pos_count += 1 - sum_i
                sum_i = 1
        elif sum_i > 0:
            sum_i += a[i]
            if sum_i >= 0:
                pos_count += sum_i + 1
                sum_i = -1
    sum_i = -1
    neg_count = 1
    for i in range(1, n):
        if sum_i < 0:
            sum_i += a[i]
            if sum_i <= 0:
                neg_count += 1 - sum_i
                sum_i = 1
        elif sum_i > 0:
            sum_i += a[i]
            if sum_i >= 0:
                neg_count += sum_i + 1
                sum_i = -1

    if neg_count < pos_count:
        count = neg_count
    else:
        count = pos_count

print(count)

Submission status: Wrong Answer

Solution code:
n = input()
a = map(int ,raw_input().split())
if a[0] > 0:
    sign = 1
if a[0] < 0:
    sign = -1
c = 0
sum = a[0]
for i in xrange(1,n):
    temp = a[i]+sum
    if temp == 0 and sum > 0:
        c += 1
        sum = -1
        continue
    if temp == 0 and sum < 0:
        c += 1
        sum = 1
        continue
    if temp < 0 and sum > 0 :
        sum = temp
        continue
    if temp > 0 and sum < 0:
        sum = temp
        continue
    if temp > 0 and sum > 0:
        c += abs(sum  + a[i]) + 1
        sum = -1
        continue
    if temp < 0 and sum <0 :
        c += abs(sum + a[i]) + 1
        sum = 1
        continue

print c

Submission status: Wrong Answer

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


c=0
for i in range(n):
    s=sum(a[0:i+1])
    if s==0:
        if i==n-1:
            a[i]+=1
            c+=1
        elif a[i+1]>=0:
            a[i]-=1
            c+=1
        else:
            a[i]+=1
            c+=1

    if i==(n-1): break

    s=sum(a[0:i+1])
    n_s=s+a[i+1]
    if s*n_s>0:
        if s>=0:
            a[i+1]-=(s+1)
            c+=(s+1)
        else:
            a[i+1]+=(s+1)
            c+=(s+1)

print c

Submission status: Wrong Answer

Solution code:
def c(ints):
    
    for i in range(len(ints)):
        if ints[i] != 0:
            sig = 1 if ints[i] > 0 else -1
            total = ints[i]
            mov = i + 1
            j = i
            break
        if i == len(ints) - 1:
            return i + 1
    
    for i_ in ints[j+1:]:
        tmp = total + i_
        if tmp == 0:
            mov +=1
            tmp = -sig
            
        elif sig * tmp > 0:
            mov += abs(tmp) + 1
            tmp = -sig
            
        sig *= -1
        total = tmp
        
    return mov
 
_ = input()
inp = input()
inp = inp.split(' ')
inp = [int(i_) for i_ in inp]
print(c(inp))

Submission status: Wrong Answer

Solution code:
import math

n = int(input())
ai = tuple(map(int, input().split()))

kisuu = 0.0
guusuu = 0.0
for i, a in enumerate(ai):
    if i % 2:
        kisuu += a
    else:
        guusuu += a
kisuu /= math.floor(n / 2)
guusuu /= n - math.floor(n / 2)

if kisuu > guusuu:
    kisuu = True
    guusuu = False
else:
    kisuu = False
    guusuu = True

sum = 0
ans = 0
for i, a in enumerate(ai):
    sum += a
    if i % 2:
        if kisuu:
            if sum <= 0:
                ans += 1 - sum
                sum = 1
        else:
            if sum >= 0:
                ans += 1 + sum
                sum = -1
    else:
        if guusuu:
            if sum <= 0:
                ans += 1 - sum
                sum = 1
        else:
            if sum >= 0:
                ans += 1 + sum
                sum = -1
print (ans)

Submission status: Wrong Answer

Solution code:
import math

n = int(input())
ai = tuple(map(int, input().split()))

kisuu = 0.0
guusuu = 0.0
for i, a in enumerate(ai):
    if i % 2:
        kisuu += a
    else:
        guusuu += a
kisuu /= math.floor(n / 2)
guusuu /= n - math.floor(n / 2)

if kisuu > guusuu:
    kisuu = True
    guusuu = False
else:
    kisuu = False
    guusuu = True

sum = 0
ans = 0
for i, a in enumerate(ai):
    sum += a
    if i % 2:
        if kisuu:
            if sum <= 0:
                ans += 1 - sum
                sum = 1
        else:
            if sum >= 0:
                ans += 1 + sum
                sum = -1
    else:
        if guusuu:
            if sum <= 0:
                ans += 1 - sum
                sum = 1
        else:
            if sum >= 0:
                ans += 1 + sum
                sum = -1
    print(sum)
print (ans)

Submission status: Wrong Answer

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

count = 0
sum_i = a[0]
if a[0] != 0:
    for i in range(1, n):
        if sum_i < 0:
            sum_i += a[i]
            if sum_i <= 0:
                count += 1 - sum_i
                sum_i = 1
        elif sum_i > 0:
            sum_i += a[i]
            if sum_i >= 0:
                count += sum_i + 1
                sum_i = -1
else:
    sum_i = 1
    pos_count = 0
    for i in range(1, n):
        if sum_i < 0:
            sum_i += a[i]
            if sum_i <= 0:
                pos_count += 1 - sum_i
                sum_i = 1
        elif sum_i > 0:
            sum_i += a[i]
            if sum_i >= 0:
                pos_count += sum_i + 1
                sum_i = -1
    sum_i = -1
    neg_count = 0
    for i in range(1, n):
        if sum_i < 0:
            sum_i += a[i]
            if sum_i <= 0:
                neg_count += 1 - sum_i
                sum_i = 1
        elif sum_i > 0:
            sum_i += a[i]
            if sum_i >= 0:
                neg_count += sum_i + 1
                sum_i = -1

    if neg_count < pos_count:
        count = neg_count
    else:
        count = pos_count

print(count)

Submission status: Wrong Answer

Solution code:
import sys
import math

INF = 10**9+7

def k(i):
    if(i == 1):
        return 1
    else:
        return(i * k(i-1))

def comb(n, r):
    if(n == r or r == 1):
        return 1
    else:
        return k(n) / (k(n-r) * k(r))

stdin = sys.stdin
def na(): return map(int, stdin.readline().split())
def ns(): return stdin.readline().strip()
def nsl(): return list(stdin.readline().strip())
def ni(): return int(stdin.readline())
def nil(): return list(map(int, stdin.readline().split()))

n = ni()
a = nil()


sum = 0

c = 0

for i in range(0, n-1):
    sum += a[i]

    sum2 = sum + a[i+1]

    if(sum * sum2 >= 0):
        k = abs(sum2) + 1

        h = k - (abs(sum) - 1)
        l = k - h

        if sum > 0 :
            a[i] -= l
            sum -= l
            a[i + 1] -= h
        else:
            a[i] += l
            sum += l
            a[i + 1] += h

        c += h+l

print(c)

Submission status: Wrong Answer

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


c=0
for i in range(n):
    s=sum(a[0:i+1])
    if s==0:
        if i==n-1:
            a[i]+=1
            c+=1
        elif a[i+1]>=0:
            a[i]-=1
            c+=1
        else:
            a[i]+=1
            c+=1

    if i==(n-1): break

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

print c

Submission status: Wrong Answer

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


c=0
for i in range(n):
    s=sum(a[0:i+1])
    if s==0:
        if i==n-1:
            a[i]+=1
            c+=1
        elif a[i+1]>=0:
            a[i]-=1
            c+=1
        else:
            a[i]+=1
            c+=1

    if i==(n-1): break

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

print c,a

Submission status: Wrong Answer

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

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

print(count)

Submission status: Wrong Answer

Solution code:
import numpy as np

n = int(input())
a = list(map(int,input().split()))
#print (a)
sum = 0
count = 0
p = 0
for i in range(n):
    sum += a[i]
    if i>0:
        p = a[i]
        if sum >=0:
            if (sum - a[i]) > 0:
                a[i] = -(sum-a[i])-1
                count += np.abs(p-a[i])
                sum = -1
        if sum <= 0:
            if (sum - a[i]) < 0:
                a[i] = -(sum-a[i])+1
                count += np.abs(p-a[i])
                sum = 1
    #print (sum)
print (count)

Submission status: Wrong Answer

Solution code:
def c(ints):
    
    for i in range(len(ints)):
        if ints[i] != 0:
            sig = 1 if ints[i] > 0 else -1
            total = ints[i]
            mov = i
            j = i
            break
        if i == len(ints) - 1:
            return i + 1
    
    for i_ in ints[j+1:]:
        tmp = total + i_
        if tmp == 0:
            mov +=1
            tmp = -sig
            
        elif sig * tmp > 0:
            mov += abs(tmp) + 1
            tmp = -sig
            
        sig *= -1
        total = tmp
        
    return mov
 
_ = input()
inp = input()
inp = inp.split(' ')
inp = [int(i_) for i_ in inp]
print(c(inp))

Submission status: Wrong Answer

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

count_a = 0
count_b = 0

nowsum = a[0]
if nowsum != 0:
    for n in range(1, n):
        if nowsum * (nowsum + a[n]) >= 0:
            count_a += abs(nowsum + a[n]) + 1
            if nowsum < 0:
                nowsum = 1
            else:
                nowsum = -1
        else:
            nowsum += a[n]
    print(count_a)

else:
    a[0] = 1
    count_a += 1
    nowsum = 1
    for n in range(1, n):
        if nowsum * (nowsum + a[n]) >= 0:
            count_a += abs(nowsum + a[n]) + 1
            if nowsum < 0:
                nowsum = 1
            else:
                nowsum = -1
        else:
            nowsum += a[n]
    
    a[0] = -1
    count_b += 1
    nowsum = -1
    for n in range(1, n):
        if nowsum * (nowsum + a[n]) >= 0:
            count_b += abs(nowsum + a[n]) + 1
            if nowsum < 0:
                nowsum = 1
            else:
                nowsum = -1
        else:
            nowsum += a[n]     
    print(min(count_a, count_b))

Submission status: Wrong Answer

Solution code:
def c(ints):
    
    for i in range(len(ints)):
        if ints[i] != 0:
            sig = 1 if ints[i] > 0 else -1
            total = ints[i]
            mov = i
            j = i
            break
        if i == len(ints) - 1:
            return i + 1
    
    for i_ in ints[j+1:]:
        tmp = total + i_
        if tmp == 0:
            mov +=1
            tmp = -sig
            
        elif sig * tmp > 0:
            mov += abs(tmp) + 1
            tmp = -sig
            
        sig *= -1
        total = tmp
        
    return mov

_ = input()
inp = input()
inp = inp.split(' ')
inp = [int(i_) for i_ in inp]
c(inp)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import math


if __name__ == "__main__":
	n = int(input())
	nums = list(map(int, input().split()))
	temp = 0
	counts = 0
	for num in nums:
		if temp * (temp+num)>0:
			counts += abs(temp+num)+1
			temp = -temp//abs(temp)

		elif temp * (temp+num)<0:
			temp = temp+num

		else:
			if temp == 0:
				temp = temp+num
			else:
				counts += 1
				temp = -temp//abs(temp)

	print(counts)

Submission status: Wrong Answer

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

n = input()
a = map(int, raw_input().split())
asum = [0]*len(a)

asum[0] = a[0]
cnt = 0
for i in range(len(a)-1):
    asum[i+1] = a[i+1] + asum[i]
    if(asum[i]*asum[i+1]>=0):
        if(asum[i+1]>=0):
            cnt += asum[i+1]+1
            asum[i+1] = -1
        else:
            cnt += (-1)*asum[i+1]+1
            asum[i+1] = 1
    
print(cnt)

Submission status: Wrong Answer

Solution code:
#coding:utf-8

if __name__ == "__main__":
    n = int(raw_input())
    seq = map(int, raw_input().split(" "))

    is_positive = True

    if seq[0] < 0:
        is_positive = False
    sum = seq[0]

    operation = 0
    for i in range(1, n):
        sum += seq[i]
        if sum == 0:
            operation += 1
            if is_positive:
                sum -= 1
            else:
                sum += 1
        elif sum > 0 and is_positive:
            operation += abs(sum) + 1
            sum = -1
        elif sum < 0 and not is_positive:
            operation += abs(sum) + 1
            sum = 1

        if sum > 0:
            is_positive = True
        else:
            is_positive = False

    print operation

Submission status: Wrong Answer

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

sum = a[0]
cnt = 0

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

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import copy
n = int(input())
a = list(map(int,input().split()))
b = copy.copy(a)
#sum_a = []
#sum_b = []
ans_a = 0
ans_b = 0
for i in range(n):
    if i%2==0:
        if sum(a[:i+1])>0:
            #sum_a.append(sum(a[:i+1]))
            tmp = 0
        else:
            tmp = abs(sum(a[:i+1]))+1
            a[i] += tmp
            ans_a += tmp
            #sum_a.append(sum(a[:i+1]))
    else:
        if sum(a[:i+1])<0:
            #sum_a.append(sum(a[:i+1]))
            tmp = 0
        else:
            tmp = abs(sum(a[:i+1]))+1
            a[i] -= tmp
            ans_a += tmp
            #sum_a.append(sum(a[:i+1]))
    if i%2==1:
        if sum(b[:i+1])>0:
            #sum_b.append(sum(b[:i+1]))
            tmp = 0
        else:
            tmp = abs(sum(b[:i+1]))+1
            b[i] += tmp
            ans_b += tmp
            #sum_b.append(sum(b[:i+1]))
    else:
        if sum(b[:i+1])<0:
            #sum_b.append(sum(b[:i+1]))
            tmp = 0
        else:
            tmp = abs(sum(b[:i+1]))+1
            b[i] -= tmp
            ans_b += tmp
            #sum_b.append(sum(b[:i+1]))

#print(a)
#print(sum_a)
print(min(ans_a,ans_b))

Submission status: Time Limit Exceeded

Solution code:
import copy
n = int(input())
a = list(map(int,input().split()))
b = copy.copy(a)
sum_a = []
sum_b = []
ans_a = 0
ans_b = 0
for i in range(n):
    if i%2==0:
        if sum(a[:i+1])>0:
            sum_a.append(sum(a[:i+1]))
            tmp = 0
        else:
            tmp = abs(sum(a[:i+1]))+1
            a[i] += tmp
            ans_a += tmp
            sum_a.append(sum(a[:i+1]))
    else:
        if sum(a[:i+1])<0:
            sum_a.append(sum(a[:i+1]))
            tmp = 0
        else:
            tmp = abs(sum(a[:i+1]))+1
            a[i] -= tmp
            ans_a += tmp
            sum_a.append(sum(a[:i+1]))
    if i%2==1:
        if sum(b[:i+1])>0:
            sum_b.append(sum(b[:i+1]))
            tmp = 0
        else:
            tmp = abs(sum(b[:i+1]))+1
            b[i] += tmp
            ans_b += tmp
            sum_b.append(sum(b[:i+1]))
    else:
        if sum(b[:i+1])<0:
            sum_b.append(sum(b[:i+1]))
            tmp = 0
        else:
            tmp = abs(sum(b[:i+1]))+1
            b[i] -= tmp
            ans_b += tmp
            sum_b.append(sum(b[:i+1]))

#print(a)
#print(sum_a)
print(min(ans_a,ans_b))

Submission status: Time Limit Exceeded

Solution code:
#k = int(input())
#s = input()
#a, b = map(int, input().split())
#s, t = map(str, input().split())
#l = list(map(int, input().split()))
#l = [list(map(int,input().split())) for i in range(n)]
#a = [input() for _ in range(n)]

import copy

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

b = copy.copy(a)

lastSum = a[0]
for i in range(1, n):
    nowSum = sum(a[:i+1])
    #print(nowSum)
    if lastSum > 0:
        if nowSum >=0:
            a[i] = a[i] - (nowSum+1)
    else: #lastSum < 0
        if nowSum <= 0:
            a[i] = a[i] - (nowSum-1)
    lastSum = sum(a[:i+1])

ans = 0
for i in range(n):
    ans += abs(a[i]-b[i])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys 
import numpy as np
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N = int(readline())
A = list(map(int,readline().split()))

S1 = np.cumsum(A)
S2 = np.cumsum(A)
ans1 = 0
ans2 = 0
# 偶数項-,奇数項を+にする
for i in range(N):
    if i & 1: 
        if S1[i] >= 0:
            ans1 += S1[i]+1
            S1 -= S1[i]+1
        if S2[i] <= 0:
            ans2 += 1-S2[i]
            S2 += 1-S2[i]
    else: 
        if S1[i] <= 0:
            ans1 += 1-S1[i]
            S1 += 1-S1[i]
        if S2[i] >= 0:
            ans2 += S2[i]+1
            S2 -= S2[i]+1

print(min(ans1,ans2))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys 
import numpy as np
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N = int(readline())
A = list(map(int,readline().split()))

S = np.cumsum(A)
ans1 = 0
ans2 = 0
# 偶数項-,奇数項を+にする
for i in range(N):
    if i & 1: 
        if S[i] >= 0:
            ans1 += S[i]+1
            S -= S[i]+1
    else: 
        if S[i] <= 0:
            ans1 += 1-S[i]
            S += 1-S[i]

S = np.cumsum(A)

for i in range(N):
    if i & 1: 
        if S[i] <= 0:
            ans2 += 1-S[i]
            S += 1-S[i]
    else: 
        if S[i] >= 0:
            ans2 += S[i]+1
            S -= S[i]+1
            
print(min(ans1,ans2))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

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

import itertools
alsum = list(itertools.accumulate(al))

temp1 = 0
temp2 = 0
for i in range(n):
    if i % 2 ==0:
        if alsum[i] >0:
            pass
        else:
            y = abs(alsum[i])+1
            temp1 += y
            alsum = list(map(lambda x:x +y ,alsum))
    else:
        if alsum[i] <0:
            pass
        else:
            y = alsum[i]+1
            temp1 += y
            alsum = list(map(lambda x:x-y,alsum))
    
import itertools
alsum = list(itertools.accumulate(al))

for j in range(n):
    if j % 2 ==0:
        if alsum[j] <0:
            pass
        else:
            y = abs(alsum[j])+1
            temp2 += y
            alsum = list(map(lambda x:x-y,alsum))
    else:
        if alsum[j] >0:
            pass
        else:
            y = abs(alsum[j])+1
            temp2 += y
            alsum = list(map(lambda x:x+y,alsum))

print(min(temp1,temp2))

Submission status: Time Limit Exceeded

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

import numpy as np

ans = 0

sum0 = a[0]
sum1 = a[0]

for i in range(1, n):
  sum1 += a[i]
  if np.sign(sum0) != np.sign(sum1) and sum1 != 0: #合計の符号が逆となっており、0でない
    sum0 = sum1
    pass
  elif sum1 == 0: #合計が0になった場合は、符号が逆になるよう１か-1を足す
    sum1 -= 1 * np.sign(sum0)
    ans += 1
    sum0 = sum1
  elif np.sign(sum0) == np.sign(sum1): #符号が同じ場合は、+1か-1になるまで足す
    if np.sign(sum1) == 1:
      while sum1 >= 0:
        sum1 -= 1
        ans += 1
    else:
      while sum1 <= 0:
        sum1 += 1
        ans += 1
    sum0 = sum1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import copy

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

pcnt = 0
mcnt = 0

f = 0  #前の項の符号（1:+、2:-）
wa = 0

#最初の数字を正とするとき
a = copy.copy(am)
f = 2

for i in range(len(a)):
    wa += a[i]

    if f == 1:#-にする必要あり
        if wa > -1:
            pcnt += wa + 1
            a[i] -= wa + 1

        f = 2

    elif f == 2:  #+にする必要あり

        if wa < 1:
            pcnt += -wa + 1
            a[i] += -wa + 1
        
        f = 1
    
    wa = sum(a[0: i + 1])

#最初の数字を負とするとき
a = copy.copy(am)
f = 1

for i in range(len(a)):
    wa += a[i]

    if f == 1:  # -にする必要あり
        if wa > -1:
            mcnt += wa + 1
            a[i] -= wa + 1

        f = 2

    elif f == 2:  # +にする必要あり

        if wa < 1:
            mcnt += -wa + 1
            a[i] += -wa + 1

        f = 1

    wa = sum(a[0: i + 1])

print(min(pcnt, mcnt))

Submission status: Time Limit Exceeded

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

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

sum_odd = sum(a[1::2])
sum_eve = sum(a[::2])

count = 0
sum_a  = 0
if sum_odd >= sum_eve:#奇数の和がおおきいので、偶数　0,2,4...を負にする
    for i in range(n):
        
        sum_a = sum_a + a[i]
        #print('sum_a : ' +str(sum_a))
        #print('sum_a *(-1)**(i+1) : '+str(sum_a *(-1)**(i+1)))
        if sum_a *(-1)**(i+1) < 1:
            while sum_a*(-1)**(i+1) <1:
                count += 1
                a[i] += 1*(-1)**(i+1)
                sum_a += 1*(-1)**(i+1)
else:
    for i in range(n):
       
        sum_a = sum_a + a[i]
        if sum_a *(-1)**(i) < 1:
            while sum_a*(-1)**(i) <1:
                count += 1
                a[i] += 1*(-1)**(i)
                sum_a += 1*(-1)**(i)
print(count)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    A = list(map(int, input().split()))

    res = 0
    sums = []

    for i in range(n):
        sums.append(sum(A[:i]) + A[i])
        if i == 0:
            if A[i] == 0:
                index = -1
                for num in A:
                    if num != 0:
                        index = A.index(num)
                        break
                
                if index == -1:
                    res += 3
                    A[i] = 1
                    A[i+1] = -2
                    sums[i] = 1
                elif (index % 2 and A[index] > 0) or (index % 2 == 0 and A[index] < 0):
                    A[i] = -1
                    sums[i] = -1
                    res += 1
                else:
                    A[i] = 1
                    sums[i] = 1
                    res += 1
        else:
            if sums[i] == 0:
                if sums[i-1] > 0:
                    A[i] -= 1
                    sums[i] = sums[i-1] + A[i]
                    res += 1
                else:
                    A[i] += 1
                    sums[i] = sums[i-1] + A[i]
                    res += 1
            elif (sums[i-1] > 0) and (sums[i] > 0):
                res += A[i] - (-sums[i-1] - 1)
                A[i] = -sums[i-1] - 1
                sums[i] = sums[i-1] + A[i]
            elif (sums[i-1] < 0) and (sums[i] < 0):
                res += 1 - (sums[i-1] + A[i])
                A[i] = abs(sums[i-1]) + 1
                sums[i] = sums[i-1] + A[i]
    
    print(res)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    A = list(map(int, input().split()))

    res = 0
    sums = []

    for i in range(n):
        sums.append(sum(A[:i]) + A[i])
        if i == 0:
            if A[i] == 0:
                index = -1
                for num in A:
                    if num != 0:
                        index = A.index(num)
                        break
                
                if index == -1:
                    res += 1
                    A[i] = 1
                    sums[i] = 1
                elif (index % 2 and A[index] > 0) or (index % 2 == 0 and A[index] < 0):
                    A[i] = -1
                    sums[i] = -1
                    res += 1
                else:
                    A[i] = 1
                    sums[i] = 1
                    res += 1
        else:
            if sums[i] == 0:
                if sums[i-1] > 0:
                    sums[i] -= 1
                    A[i] -= 1
                    res += 1
                else:
                    sums[i] += 1
                    A[i] += 1
                    res += 1
            elif (sums[i-1] > 0) and (sums[i] > 0):
                res += sums[i] + 1
                A[i] -= sums[i] + 1
                sums[i] -= sums[i] + 1
            elif (sums[i-1] < 0) and (sums[i] < 0):
                res += abs(sums[i]) + 1
                A[i] += abs(sums[i]) + 1
                sums[i] += abs(sums[i]) + 1
    
    print(res)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

ans = 0

sum_list1 = np.zeros(n)
to_minus1 = np.zeros(n)
to_plus1 = np.zeros(n)
s = 0
for i in range(n):
    s += a[i]
    sum_list1[i] += s
    to_minus1[i] = sum_list1[i] + 1
    to_plus1[i] = sum_list1[i] - 1


def check(sum_list, to_minus, to_plus, t, ans):
    for i in range(n):
        if (i+t) % 2 == 0:
            if to_minus[i] <= 0:
                continue
            else:
                hiku = to_minus[i]
                to_minus[i::] -= hiku
                to_plus[i::] -= hiku
                ans += abs(hiku)
        else:
            if to_plus[i] >= 0:
                continue
            else:
                tasu = abs(to_plus[i])
                to_minus[i::] += tasu
                to_plus[i::] += tasu
                ans += abs(tasu)
    return ans

t = np.copy(to_minus1)
u = np.copy(to_plus1)

ans1 = check(sum_list1, to_minus1, to_plus1, 0, 0)
ans2 = check(sum_list1, t, u, 1, 0)

print(int(min(ans1, ans2)))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n = int(input())
a = list(map(int, input().split()))
c = 0
sum = a[0]
for i in range (1, n):
    while np.sign(sum + a[i]) == np.sign(sum) or sum + a[i] == 0:
        c += 1
        if sum > 0:
            a[i] -= 1
        else:
            a[i] += 1
    sum += a[i]
print(c)

Submission status: Time Limit Exceeded

Solution code:
import itertools
n=int(input())
a=list(map(int,input().split()))
ans=0
res=0#0が正、1が負
if a[0]<0:
    res=1
for i in range(n-1):
    aa=list(itertools.accumulate(a))
    if aa[i+1]<0:
        if res==1:
            adj = 1-aa[i+1]
            a[i+1]=a[i+1]+adj
            ans=ans+abs(adj)
            res = 0
        else:
            res=1
    if aa[i+1]>0:
        if res==0:
            adj = -1-aa[i+1]
            a[i+1]=a[i+1]+adj
            ans=ans+abs(adj)
            res = 1
        else:
            res=0
    if aa[i+1]==0:
        if res == 1:
            a[i+1]=a[i+1]+1
            ans+=1
            res=0
        else:
            a[i+1]=a[i+1]-1
            ans+=1
            res=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import functools
n=int(input())
a=list(map(int,input().split()))
ans=0
res=0#0が正、1が負
if a[0]<0:
    res=1
for i in range(n-1):
    aa=functools.reduce(lambda x,y:x+y,a[:i+2])
    if aa<0:
        if res==1:
            adj = 1-aa
            a[i+1]=a[i+1]+adj
            ans=ans+abs(adj)
            res = 0
        else:
            res=1
    if aa>0:
        if res==0:
            adj = -1-aa
            a[i+1]=a[i+1]+adj
            ans=ans+abs(adj)
            res = 1
        else:
            res=0
    if aa==0:
        if res == 1:
            a[i+1]=a[i+1]+1
            ans+=1
            res=0
        else:
            a[i+1]=a[i+1]-1
            ans+=1
            res=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int , input().split()))
res=[0]*n
res[0]=a[0]
c=0
for i in range(1,n):
    res[i]=res[i-1]+a[i]


for i in range(1,n):
    s=sum(a[:i])
    if s*sum(a[:i+1])>=0:
        c+=abs(a[i]+s)+1
        if s>0:
            a[i]=-s-1
        else:
            a[i]=-s+1
print(c)