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

s = a[0]
count = 0
total_delta = 0
delta = 0

if a[0] == 0:
    i = 0
    while i < len(a):
        if a[i] != 0:
            break
        i += 1
    if i == len(a):
        delta = 1
        total_delta += delta
        count += 1
    else:
        delta = -1 * (a[i] // abs(a[i])) * ((1, -1)[(i + 1) % 2])
        total_delta += delta
        count += 1
for i in range(1, n):
    print('loop : ', i )
    print(s)
    sign = (s + total_delta) // abs(s + total_delta)
    if (s + a[i] + total_delta) * sign > 0 :
        delta  = (sign * -1) - (s + a[i] + total_delta)
        total_delta += delta
        count += abs(delta)
    elif (s + a[i] + total_delta) == 0:
        total_delta += sign * -1
        count += 1
    s += a[i]
print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(x) for x in input().split()]
temp = 0
count1 = 0
count2 = 0

if a[0] == 0:
	a[0] = 1
	count1 = 1

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

count2 = abs(a[0]) + 1
if a[0] > 0:
	a[0] = -1
else:
	a[0] = 1

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

print(min(count1, count2))

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
            sig_ = -sig
            total = ints[i]
            total_ = -sig
            mov = i
            mov_ = abs(total) + 1
            if i > 0:
                mov += 1
                mov_ += 2
            j = i
            break
        if i == len(ints) - 1:
            return i + 1
    
    for i_ in ints[j+1:]:
        tmp = total + i_
        tmp_ = total_ + i_
        if tmp == 0:
            mov +=1
            tmp = -sig
        elif sig * tmp > 0:
            mov += abs(tmp) + 1
            tmp = -sig
            
        if tmp_ == 0:
            mov_ +=1
            tmp_ = -sig_
        elif sig_ * tmp_ > 0:
            mov_ += abs(tmp_) + 1
            tmp_ = -sig_
            
        sig *= -1
        total = tmp
        sig_ *= -1
        total_ = tmp_
    return min(mov, mov_)

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

Submission status: Wrong Answer

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

W=[]
wa=0
for i in range(n):
    wa=A[i]+wa
    W.append(wa)
counter=0
for i in range(n):
    if i==n-1:
        break
   
                           
    elif W[i]<0 and W[i+1]<0:
        counter=abs(W[i])-abs(A[i+1])+1+counter
    elif W[i]>0 and W[i+1]>0:
        counter=abs(W[i])+1+counter-abs(A[i+1])
if A[n-1]==0:
        counter=counter+abs(A[n-2])
print(counter)

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()

b = []

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


sum = 0

c1 = 0
c2 = 0

if a[0] == 0:
    a[0] = 1
    c1 += 1;

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

    sum2 = sum + a[i+1]

    if sum2 == 0:
        if sum >0:
            a[i+1] -= 1
            sum2 -= 1
            c1 +=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

        c1 += h+l


sum = 0

a = b

if a[0] == 0:
    a[0] = 1
    c2 += 1;
else:
    c2 = abs(a[0]) + 1
    if a[0] > 0:
        a[0] = -1
    else:
        a[0] = 1

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

    sum2 = sum + a[i+1]

    if sum2 == 0:
        if sum >0:
            a[i+1] -= 1
            sum2 -= 1
            c2 +=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

        c2 += k

print(min(c1, c2))

Submission status: Wrong Answer

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

count_a = 0 #+start
count_b = 0 #-start

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]

    count_b += abs(a[0]) + 1
    a[0] = a[0] / abs(a[0]) * -1
    nowsum = a[0]

    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))

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
            sig_ = -sig
            total = ints[i]
            total_ = -sig
            mov = i
            mov_ = abs(total) + 1
            if i > 0:
                mov += 1
                mov_ += 1
            j = i
            break
        if i == len(ints) - 1:
            return i + 1
    
    for i_ in ints[j+1:]:
        tmp = total + i_
        tmp_ = total_ + i_
        if tmp == 0:
            mov +=1
            tmp = -sig
        elif sig * tmp > 0:
            mov += abs(tmp) + 1
            tmp = -sig
            
        if tmp_ == 0:
            mov_ +=1
            tmp_ = -sig_
        elif sig_ * tmp_ > 0:
            mov_ += abs(tmp_) + 1
            tmp_ = -sig_
            
        sig *= -1
        total = tmp
        sig_ *= -1
        total_ = tmp_
    return min(mov, mov_)
 
_ = input()
inp = input()
inp = inp.split(' ')
inp = [int(i_) for i_ in inp]
print(c(inp))

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
            sig_ = -sig
            total = ints[i]
            total_ = -sig
            mov = i
            mov_ = abs(total) + 1
            if i > 0:
                mov += 1
                mov_ += 1
            j = i
            break
        if i == len(ints) - 1:
            return i + 1
    
    for i_ in ints[j+1:]:
        tmp = total + i_
        tmp_ = total_ + i_
        if tmp == 0:
            mov +=1
            tmp = -sig
        elif sig * tmp > 0:
            mov += abs(tmp) + 1
            tmp = -sig
            
        if tmp_ == 0:
            mov_ +=1
            tmp_ = -sig_
        elif sig_ * tmp_ > 0:
            mov_ += abs(tmp_) + 1
            tmp_ = -sig_
            
        sig *= -1
        total = tmp
        sig_ *= -1
        total_ = tmp_
    print(mov, mov_)
    return min(mov, mov_)

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

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()

b = []

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


sum = 0

c1 = 0
c2 = 0

if a[0] == 0:
    a[0] = 1
    c1 += 1;

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

    sum2 = sum + a[i+1]

    if sum2 == 0:
        if sum >0:
            a[i+1] -= 1
            sum2 -= 1
            c1 -=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

        c1 += h+l


sum = 0

a = b

if a[0] == 0:
    a[0] = 1
    c2 += 1;
else:
    c2 = abs(a[0]) + 1
    if a[0] > 0:
        a[0] = -1
    else:
        a[0] = 1

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

    sum2 = sum + a[i+1]

    if sum2 == 0:
        if sum >0:
            a[i+1] -= 1
            sum2 -= 1
            c2 -=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

        c2 += k

print(min(c1, c2))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import math


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

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

		else:
			if acm == 0:
				counts += 1
				acm = 1

			else:
				counts += 1
				acm = -acm//abs(acm)

	print(counts)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import math


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

		elif acm * (acm+num)<0:
			acm = acm+num
			print('counts:', counts)
			print('acm:', acm)

		else:
			if acm == 0:
				counts += 1
				acm = 1
				print('counts:', counts)
				print('acm:', acm)

			else:
				counts += 1
				acm = -acm//abs(acm)
				print('counts:', counts)
				print('acm:', acm)

	print(counts)

Submission status: Wrong Answer

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

ans1 = ans2 = 0
s = s2 = a[0]
if a[0] < 0:
    s = 1
    ans1 = -a[0] + 1
if a[0] > 0:
    s2 = -1
    ans2 = a[0] + 1

for i in range(1, len(a)):
    s += a[i]
    if i % 2 == 1 and s >= 0:
        ans1 += s + 1
        s = -1
    elif i % 2 == 0 and s <= 0:
        ans1 += -s + 1
        s = 1
    s2 += a[i]
    if i % 2 == 0 and s2 >= 0:
        ans2 += s2 + 1
        s2 = -1
    elif i % 2 == 1 and s2 <= 0:
        ans2 += -s2 + 1
        s2 = 1

print(min(ans1, ans2))

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 - a[i]) > 0:
            if sum >= 0:
                a[i] = -(sum-a[i])-1
                count += np.abs(p-a[i])
                sum = -1
        else:
            if sum <= 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:
n = input()
a = map(int, raw_input().split())
sum =  a[0]

c = 0
if sum == 0:
    if a[1] > 0:
        sum = - 1
        c +=1
    if a[1] < 0:
        sum = 1
        c += 1
    if a[1] == 0:
        sum = 1
        c += 1
for i in range(1,n):
    temp = sum + a[i]
    if temp*sum > 0:
        if sum > 0:
            c += abs(-1-sum-a[i])
            sum = -1
            continue
        if sum < 0:
            c += abs(1-sum-a[i])
            sum = 1
            continue
    if temp == 0:
        c += 1
        if sum > 0:
            sum = -1
            continue
        if sum < 0:
            sum = 1
            continue
    if temp*sum < 0:
        sum = temp
        continue
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
            sig_ = -sig
            total = ints[i]
            total_ = -sig
            mov = i
            mov_ = i
            if i > 0:
                mov += 1
                mov_ += abs(total) + 1
            j = i
            break
        if i == len(ints) - 1:
            return i + 1
    
    for i_ in ints[j+1:]:
        tmp = total + i_
        tmp_ = total_ + i_
        if tmp == 0:
            mov +=1
            tmp = -sig
        elif sig * tmp > 0:
            mov += abs(tmp) + 1
            tmp = -sig
            
        if tmp_ == 0:
            mov_ +=1
            tmp_ = -sig_
        elif sig_ * tmp_ > 0:
            mov_ += abs(tmp_) + 1
            tmp_ = -sig_
            
        sig *= -1
        total = tmp
        sig_ *= -1
        total_ = tmp_
    return min(mov, mov_)

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

Submission status: Wrong Answer

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

sum_a.append(a[0])
if len(a) != 1:
    if sum_a[0] == 0:
        count += 1
        if a[1] > 0:
            sum_a[0] = -1
        else:
            sum_a[0] = 1
else:
    if sum_a[0] == 0:
        count += 1
for i in range(1,n):
    sum_a.append(sum_a[i-1] + a[i])

    if sum_a[i] == 0:
        if sum_a[i-1] < 0:
            sum_a[i] += 1
            count += 1
        else:
            sum_a[i] -= 1
            count += 1
    elif sum_a[i-1] * sum_a[i] > 0:
        if sum_a[i] > 0:
            sum_a[i] = -1
            count += 1 + abs(sum_a[i-1]+a[i])
        else:
            sum_a[i] = 1
            count += 1 + abs(sum_a[i-1]+a[i])
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()

b = []

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


sum = 0

c1 = 0
c2 = 0

if a[0] == 0:
    a[0] = 1
    c1 += 1;

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

        c1 += h+l


sum = 0

a = b

if a[0] == 0:
    a[0] = 1
    c2 += 1;
else:
    c2 = abs(a[0]) + 1
    if a[0] > 0:
        a[0] = -1
    else:
        a[0] = 1

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

        c2 += k

print(min(c1, c2))

Submission status: Wrong Answer

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

count_a = 0 #+start
count_b = 0 #-start

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]

    count_b += nowsum + 1
    a[0] = -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))
    
elif 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]

    count_b += abs(nowsum) + 1
    a[0] = 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))    

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()))

W=[]
wa=0
for i in range(n):
    wa=A[i]+wa
    W.append(wa)
counter=0
for i in range(n):
    if i==n-1:
        break
    elif W[i]<0 and W[i+1]<0:
        counter=abs(W[i+1])+1-abs(W[i])+counter
    elif W[i]>0 and W[i+1]>0:
        counter=abs(W[i+1])+1+counter-abs(W[i])
print(counter)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

asum[0] = a[0]
bsum[0] = b[0]
cnt_a = 0
cnt_b = 0
if (a[0]==0):
    a[0] = 1
    asum[0] = 1
    cnt_a = 1 
    b[0] = -1
    bsum[0] = -1
    cnt_b = 1

for i in range(len(a)-1):
    asum[i+1] = a[i+1] + asum[i]
    bsum[i+1] = b[i+1] + bsum[i]
    if(asum[i]*asum[i+1]>=0):
        if(asum[i+1]>0):
            cnt_a += asum[i+1]+1
            asum[i+1] = -1
        elif(asum[i+1]==0):
            cnt_a += 1
            if(asum[i]>0):
                asum[i+1] = -1
            else:
                asum[i+1] = 1
        else:
            cnt_a += (-1)*asum[i+1]+1
            asum[i+1] = 1
    
    if(bsum[i]*bsum[i+1]>=0):
        if(bsum[i+1]>0):
            cnt_b += bsum[i+1]+1
            bsum[i+1] = -1
        elif(bsum[i+1]==0):
            cnt_b += 1
            if(bsum[i]>0):
                bsum[i+1] = -1
            else:
                bsum[i+1] = 1
        else:
            cnt_b += (-1)*bsum[i+1]+1
            bsum[i+1] = 1
print(min(cnt_a,cnt_b))

Submission status: Wrong Answer

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

count = 0
sum_i = a[0]

if sum_i < 0:
    sum_i = 1
    pos_count = 1 - sum_i
    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 = a[0]
    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


elif sum_i > 0:
    sum_i = a[0]
    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 = sum_i + 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


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 = int(input())
a = list([int(i) for i in input().split()])
sum_a = []
count = 0

sum_a.append(a[0])
for i in range(1,n):
    sum_a.append(sum_a[i-1] + a[i])

    if sum_a[i] == 0:
        if sum_a[i-1] < 0:
            sum_a[i] += 1
            count += 1
        else:
            sum_a[i] -= 1
            count += 1
    elif sum_a[i-1] * sum_a[i]>0:
        if sum_a[i] > 0:
            sum_a[i] = -1
            count += 1 + abs(sum_a[i-1]+a[i])
        else:
            sum_a[i] = 1
            count += 1 + abs(sum_a[i-1]+a[i])
print(count)

Submission status: Wrong Answer

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

sum_a.append(a[0])
print(a)
for i in range(1,n):
    sum_a.append(sum_a[i-1] + a[i])

    if sum_a[i] == 0:
        if sum_a[i-1] < 0:
            sum_a[i] += 1
            count += 1
        else:
            sum_a[i] -= 1
            count += 1
    elif sum_a[i-1] * sum_a[i]>0:
        if sum_a[i] > 0:
            sum_a[i] = -1
            count += 1 + abs(sum_a[i-1]+a[i])
        else:
            sum_a[i] = 1
            count += 1 + abs(sum_a[i-1]+a[i])
print(sum_a)
print(count)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

a = [0 for i in range(n)]
sumeven = 0
sumodd = 0
for i,sai in enumerate(sa):
    a[i] = int(sai)

    if i % 2 == 0:
        sumeven += a[i]
    else:
        sumodd += a[i]

ret = 0
sm = 0
for i,ai in enumerate(a):
    if sumeven < sumodd:
        if i == 0:
            ret += max(ai + 1, 0)
            sm += min(-1, ai)
        elif i % 2 == 1:
            ret += max(-sm+1-ai,0)
            sm += max(ai, -sm+1)
        elif i % 2 == 0:
            ret += max(sm+1 + ai, 0)
            sm += min(ai, -(sm+1))
    else:
        if i == 0:
            ret += max(1 - ai, 0)
            sm += max(1, ai)
        elif i % 2 == 0:
            ret += max(-sm+1-ai,0)
            sm += max(ai, -sm+1)
        elif i % 2 == 1:
            ret += max(sm+1 + ai, 0)
            sm += min(ai, -(sm+1))
print(ret)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

count = 0
sum_i = a[0]

if sum_i < 0:
    sum_i = 1
    pos_count = 1 - sum_i
    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 = a[0]
    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
elif sum_i > 0:
    sum_i = a[0]
    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 = sum_i + 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
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 = int(input())
i = input()

i = i.split()
for item in range(len(i)):
    i[item] = int(i[item])

if i[0] > 0:
    signN = 'neg'
else:
    signN = 'pos'
signM = None
tot = 0
count = 0

for x in range(len(i)):
    tot += i[x]
    
    if tot > 0:
        signM = 'pos'
    elif tot < 0:
        signM = 'neg'
    elif tot == 0:
        signM = 'z'

    if signN != signM and signM != 'z':
        signN = signM
    elif signN == 'pos' and signM == 'pos':
        count += (tot + 1)
        tot -= tot
        tot -= 1
        signM = 'neg'
    elif signN == 'neg' and signM == 'neg':
        count -= (tot - 1)
        tot = 1
        signM = 'pos'
        
    if tot == 0:
        count += 1
        if signN == 'neg':
            tot += 1
            signM = 'pos'
        elif signN == 'pos':
            tot -= 1
            signM = 'neg'

    signN = signM
            
print(count)