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:
from sys import stdin
import sys
import math

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

pair = 0
odd = 0


for i in range(0, len(a), 2):
    pair += a[i]

for i in range(1, len(a), 2):
    odd += a[i]

#print(odd)
#print(pair)


count = 0
current_sum = 0
for i in range(len(a)):
    ## odd
    if i % 2 == 1 and odd > pair:
        if current_sum + a[i] < 1:
            diff = 1 - (current_sum + a[i])
            a[i] += diff
            count += diff
    ## odd
    elif i % 2 == 1 and odd < pair:
        if current_sum + a[i] > -1:
            diff = -1 - (current_sum + a[i])
            a[i] += diff
            count += -1 * diff
    ## pair
    elif i % 2 == 0 and odd > pair:
        if current_sum + a[i] > -1:
            diff = -1 - (current_sum + a[i])
            a[i] += diff
            count += -1 * diff
    elif i % 2 == 0 and odd < pair:
        if current_sum + a[i] < 1:
            diff = 1 - (current_sum + a[i])
            a[i] += diff
            count += diff
    else:
        print("error")

    current_sum += a[i]


print(count)

Submission status: Wrong Answer

Solution code:
from sys import stdin
import sys
import math

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

pair = 0
odd = 1


for i in range(0, len(a), 2):
    pair += a[i]

for i in range(1, len(a), 2):
    odd += a[i]

#print(odd)
#print(pair)


count = 0
current_sum = 0
for i in range(len(a)):
    ## odd
    if i % 2 == 1 and odd > pair:
        if current_sum + a[i] < 1:
            diff = 1 - (current_sum + a[i])
            a[i] += diff
            count += diff
    ## odd
    elif i % 2 == 1 and odd < pair:
        if current_sum + a[i] > -1:
            diff = -1 - (current_sum + a[i])
            a[i] += diff
            count += -1 * diff
    ## pair
    elif i % 2 == 0 and odd > pair:
        if current_sum + a[i] > -1:
            diff = -1 - (current_sum + a[i])
            a[i] += diff
            count += -1 * diff
    elif i % 2 == 0 and odd < pair:
        if current_sum + a[i] < 1:
            diff = 1 - (current_sum + a[i])
            a[i] += diff
            count += diff
    else:
        print("error")

    current_sum += a[i]


print(count)

Submission status: Wrong Answer

Solution code:
from sys import stdin
import sys
import math

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

pair = 1
odd = 0


#for i in range(0, len(a), 2):
#    pair += a[i]

#for i in range(1, len(a), 2):
#    odd += a[i]

#print(odd)
#print(pair)


count = 0
current_sum = 0
for i in range(len(a)):
    ## odd
    if i % 2 == 1 and odd > pair:
        while current_sum + a[i] < 1:
            a[i] += 1
            count += 1
    ## odd
    elif i % 2 == 1 and odd < pair:
        while current_sum + a[i] > -1:
            a[i] -= 1
            count += 1
    ## pair
    elif i % 2 == 0 and odd > pair:
        while current_sum + a[i] > -1:
            a[i] -= 1
            count += 1
    elif i % 2 == 0 and odd < pair:
        while current_sum + a[i] < 1:
            a[i] += 1
            count += 1
    else:
        print("error")

    current_sum += a[i]


print(count)

Submission status: Wrong Answer

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

seq_sum = A[0]
sign = 1 if seq_sum >= 0 else -1
count = 0
for i in range(1, n):
    if A[i] * sign * (-1) > seq_sum * sign:
        seq_sum += A[i]
        sign = sign * (-1)
    elif A[i] * sign * (-1) <= seq_sum * sign:
        count += abs(sign * (-1) - (seq_sum + A[i]))
        A[i] += sign * (-1) - (seq_sum + A[i])
        seq_sum += A[i]
        sign = sign * (-1)
print(count)

Submission status: Wrong Answer

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

seq_sum = A[0]
sign = 1 if seq_sum >= 0 else -1
count = 0
for i in range(1, n):
    if A[i] * sign * (-1) > seq_sum * sign:
        seq_sum += A[i]
        sign = sign * (-1)
    else:
        count += abs(sign * (-1) - (seq_sum + A[i]))
        A[i] += sign * (-1) - (seq_sum + A[i])
        seq_sum += A[i]
        sign = sign * (-1)
print(count)

Submission status: Wrong Answer

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

seq_sum = A[0]
sign = 1 if seq_sum > 0 else -1
count = 0
for i in range(1, n):
    if A[i] * sign * (-1) > seq_sum * sign:
        seq_sum += A[i]
        sign = sign * (-1)
    else:
        count += abs(sign * (-1) - (seq_sum + A[i]))
        A[i] += (sign * (-1) - (seq_sum + A[i])) * (sign * (-1))
        seq_sum += A[i]
        sign = sign * (-1)
print(count)

Submission status: Wrong Answer

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

ans = 0
ans_2 = 0
o = 0
for i in range(n):
    if i == 0:
        if a[i] == 0:
            f = "+"
            a[i] = 1
        elif a[0] > 0:
            f = "+"
        elif a[0] < 0:
            f = "-"
    else:
        o += a[i-1]
        if f == "+":
            if a[i] + o > 0:
                c = -1 - o
                ans += abs(c - a[i])
                a[i] = c
                f = "-"
            else:
                if a[i] + o == 0:
                    a[i] -= 1
                    ans += 1
                f = "-"
        elif f == "-":
            if a[i] + o < 0:
                c = 1 - o
                ans += abs(c - a[i])
                a[i] = c
                f = "+"
            else:
                if a[i] + o == 0:
                    a[i] += 1
                    ans += 1
                f = "+"
o = 0
a = a_1
#print(a)
for i in range(n):
    if i == 0:
        if a[i] == 0:
            f = "+"
            a[i] = 1
        elif a[0] > 0:
            f = "-"
            ans_2 += abs(-1 - a[0])
            a[i] = -1
        elif a[0] < 0:
            ans_2 += abs(1 - a[0])
            a[i] = 1
            f = "+"
    else:
        o += a[i-1]
        if f == "+":
            if a[i] + o > 0:
                c = -1 - o
                ans_2 += abs(c - a[i])
                a[i] = c
                f = "-"
            else:
                if a[i] + o == 0:
                    a[i] -= 1
                    ans_2 += 1
                f = "-"
        elif f == "-":
            if a[i] + o < 0:
                c = 1 - o
                ans_2 += abs(c - a[i])
                a[i] = c
                f = "+"
            else:
                if a[i] + o == 0:
                    a[i] += 1
                    ans_2 += 1
                f = "+"
#print(a)
print(min(ans,ans_2))

Submission status: Wrong Answer

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

ans = 0
ans_2 = 0
o = 0
for i in range(n):
    if i == 0:
        if a[i] == 0:
            f = "+"
            a[i] = 1
        elif a[0] > 0:
            f = "+"
        elif a[0] < 0:
            f = "-"
    else:
        o += a[i-1]
        if f == "+":
            if a[i] + o > 0:
                c = -1 - o
                ans += abs(c - a[i])
                a[i] = c
                f = "-"
            else:
                if a[i] + o == 0:
                    a[i] -= 1
                    ans += 1
                f = "-"
        elif f == "-":
            if a[i] + o < 0:
                c = 1 - o
                ans += abs(c - a[i])
                a[i] = c
                f = "+"
            else:
                if a[i] + o == 0:
                    a[i] += 1
                    ans += 1
                f = "+"
o = 0
a = a_1
for i in range(n):
    if i == 0:
        if a[i] == 0:
            f = "+"
            a[i] = 1
        elif a[0] > 0:
            f = "-"
            ans_2 += abs(-1 - a[0])
            a[i] = -1
        elif a[0] < 0:
            ans_2 += abs(1 - a[0])
            a[i] = 1
            f = "+"
    else:
        o += a[i-1]
        if f == "+":
            if a[i] + o > 0:
                c = -1 - o
                ans_2 += abs(c - a[i])
                a[i] = c
                f = "-"
            else:
                if a[i] + o == 0:
                    a[i] -= 1
                    ans_2 += 1
                f = "-"
        elif f == "-":
            if a[i] + o < 0:
                c = 1 - o
                ans_2 += abs(c - a[i])
                a[i] = c
                f = "+"
            else:
                if a[i] + o == 0:
                    a[i] += 1
                    ans_2 += 1
                f = "+"
#print(a)
print(min(ans,ans_2))

Submission status: Wrong Answer

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

ans = 0
ans_2 = 0
o = 0
for i in range(n):
    if i == 0:
        if a[i] == 0:
            f = "+"
            a[i] = 1
        elif a[0] > 0:
            f = "+"
        elif a[0] < 0:
            f = "-"
    else:
        o += a[i-1]
        if f == "+":
            if a[i] + o > 0:
                c = -1 - o
                ans += abs(c - a[i])
                a[i] = c
                f = "-"
            else:
                if a[i] + o == 0:
                    a[i] -= 1
                    ans += 1
                f = "-"
        elif f == "-":
            if a[i] + o < 0:
                c = 1 - o
                ans += abs(c - a[i])
                a[i] = c
                f = "+"
            else:
                if a[i] + o == 0:
                    a[i] += 1
                    ans += 1
                f = "+"
o = 0
a = a_1
for i in range(n):
    if i == 0:
        if a[i] == 0:
            f = "+"
            a[i] = 1
        elif a[0] > 0:
            f = "-"
            ans_2 += abs(-1 - a[0])
            a[i] = -1
        elif a[0] < 0:
            ans_2 += abs(1 - a[0])
            a[i] = 1
            f = "+"
    else:
        o += a[i-1]
        if f == "+":
            if a[i] + o > 0:
                c = -1 - o
                ans_2 += abs(c - a[i])
                a[i] = c
                f = "-"
            else:
                if a[i] + o == 0:
                    a[i] -= 1
                    ans += 1
                f = "-"
        elif f == "-":
            if a[i] + o < 0:
                c = 1 - o
                ans_2 += abs(c - a[i])
                a[i] = c
                f = "+"
            else:
                if a[i] + o == 0:
                    a[i] += 1
                    ans += 1
                f = "+"
#print(a)
print(min(ans,ans_2))

Submission status: Wrong Answer

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

ans = 0
ans_2 = 0
o = 0
for i in range(n):
    if i == 0:
        if a[i] == 0:
            f = "+"
            a[i] = 1
        elif a[0] > 0:
            f = "+"
        elif a[0] < 0:
            f = "-"
    else:
        o += a[i-1]
        if f == "+":
            if a[i] + o > 0:
                c = -1 - o
                ans += abs(c - a[i])
                a[i] = c
                f = "-"
            else:
                if a[i] + o == 0:
                    a[i] -= 1
                    ans += 1
                f = "-"
        elif f == "-":
            if a[i] + o < 0:
                c = 1 - o
                ans += abs(c - a[i])
                a[i] = c
                f = "+"
            else:
                if a[i] + o == 0:
                    a[i] += 1
                    ans += 1
                f = "+"
o = 0
a = a_1
for i in range(n):
    if i == 0:
        if a[i] == 0:
            f = "+"
            a[i] = 1
        elif a[0] > 0:
            f = "-"
            c = -1 - a[0]
            ans_2 += abs(c - a[0])
            a[i] = c
        elif a[0] < 0:
            c = 1 - a[0]
            ans_2 += abs(c - a[0])
            a[i] = c
            f = "+"
    else:
        o += a[i-1]
        if f == "+":
            if a[i] + o > 0:
                c = -1 - o
                ans_2 += abs(c - a[i])
                a[i] = c
                f = "-"
            else:
                if a[i] + o == 0:
                    a[i] -= 1
                    ans += 1
                f = "-"
        elif f == "-":
            if a[i] + o < 0:
                c = 1 - o
                ans_2 += abs(c - a[i])
                a[i] = c
                f = "+"
            else:
                if a[i] + o == 0:
                    a[i] += 1
                    ans += 1
                f = "+"
#print(a)
print(min(ans,ans_2))

Submission status: Wrong Answer

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

ans = 0
ans_2 = 0
o = 0
for i in range(n):
    if i == 0:
        if a[i] == 0:
            f = "+"
            a[i] = 1
        elif a[0] > 0:
            f = "+"
        elif a[0] < 0:
            f = "-"
    else:
        o += a[i-1]
        if f == "+":
            if a[i] + o > 0:
                c = -1 - o
                ans += abs(c - a[i])
                a[i] = c
                f = "-"
            else:
                if a[i] + o == 0:
                    a[i] -= 1
                    ans += 1
                f = "-"
        elif f == "-":
            if a[i] + o < 0:
                c = 1 - o
                ans += abs(c - a[i])
                a[i] = c
                f = "+"
            else:
                if a[i] + o == 0:
                    a[i] += 1
                    ans += 1
                f = "+"
a = a_1
for i in range(n):
    if i == 0:
        if a[i] == 0:
            f = "+"
            a[i] = 1
        elif a[0] > 0:
            f = "-"
            c = -1 - a[0]
            ans_2 += abs(c - a[0])
            a[i] = c
        elif a[0] < 0:
            c = 1 - a[0]
            ans_2 += abs(c - a[0])
            a[i] = c
            f = "+"
    else:
        o += a[i-1]
        if f == "+":
            if a[i] + o > 0:
                c = -1 - o
                ans_2 += abs(c - a[i])
                a[i] = c
                f = "-"
            else:
                if a[i] + o == 0:
                    a[i] -= 1
                    ans += 1
                f = "-"
        elif f == "-":
            if a[i] + o < 0:
                c = 1 - o
                ans_2 += abs(c - a[i])
                a[i] = c
                f = "+"
            else:
                if a[i] + o == 0:
                    a[i] += 1
                    ans += 1
                f = "+"
#print(a)
print(min(ans,ans_2))

Submission status: Wrong Answer

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

ans = 0
ans_2 = 0
o = 0
for i in range(n):
    if i == 0:
        if a[i] == 0:
            f = "+"
            a[i] = 1
        elif a[0] > 0:
            f = "+"
        elif a[0] < 0:
            f = "-"
    else:
        o += a[i-1]
        if f == "+":
            if a[i] + o > 0:
                c = -1 - o
                ans += abs(c - a[i])
                a[i] = c
                f = "-"
            else:
                if a[i] + o == 0:
                    a[i] -= 1
                    ans += 1
                f = "-"
        elif f == "-":
            if a[i] + o < 0:
                c = 1 - o
                ans += abs(c - a[i])
                a[i] = c
                f = "+"
            else:
                if a[i] + o == 0:
                    a[i] += 1
                    ans += 1
                f = "+"
a = a_1
for i in range(n):
    if i == 0:
        if a[i] == 0:
            f = "+"
            a[i] = 1
        elif a[0] > 0:
            f = "-"
        elif a[0] < 0:
            f = "+"
    else:
        o += a[i-1]
        if f == "+":
            if a[i] + o > 0:
                c = -1 - o
                ans_2 += abs(c - a[i])
                a[i] = c
                f = "-"
            else:
                if a[i] + o == 0:
                    a[i] -= 1
                    ans += 1
                f = "-"
        elif f == "-":
            if a[i] + o < 0:
                c = 1 - o
                ans_2 += abs(c - a[i])
                a[i] = c
                f = "+"
            else:
                if a[i] + o == 0:
                    a[i] += 1
                    ans += 1
                f = "+"
#print(a)
print(min(ans,ans_2))

Submission status: Wrong Answer

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

ans = 0
ans_2 = 0
o = 0
for i in range(n):
    if i == 0:
        if a[i] == 0:
            f = "+"
            a[i] = 1
        elif a[0] > 0:
            f = "+"
        elif a[0] < 0:
            f = "-"
    else:
        o += a[i-1]
        if f == "+":
            if a[i] + o > 0:
                c = -1 - o
                ans += abs(c - a[i])
                a[i] = c
                f = "-"
            else:
                if a[i] + o == 0:
                    a[i] -= 1
                    ans += 1
                f = "-"
        elif f == "-":
            if a[i] + o < 0:
                c = 1 - o
                ans += abs(c - a[i])
                a[i] = c
                f = "+"
            else:
                if a[i] + o == 0:
                    a[i] += 1
                    ans += 1
                f = "+"
a = a_1
for i in range(n):
    if i == 0:
        if a[i] == 0:
            f = "+"
            a[i] = 1
        elif a[0] > 0:
            f = "-"
        elif a[0] < 0:
            f = "+"
    else:
        o += a[i-1]
        if f == "+":
            if a[i] + o > 0:
                c = -1 - o
                ans_2 += abs(c - a[i])
                a[i] = c
                f = "-"
            else:
                if a[i] + o == 0:
                    a[i] -= 1
                    ans += 1
                f = "-"
        elif f == "-":
            if a[i] + o < 0:
                c = 1 - o
                ans_2 += abs(c - a[i])
                a[i] = c
                f = "+"
            else:
                if a[i] + o == 0:
                    a[i] += 1
                    ans += 1
                f = "+"
print(a)
print(min(ans,ans_2))

Submission status: Wrong Answer

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

ans = 0
o = 0
for i in range(n):
    if i == 0:
        if a[i] == 0:
            f = "+"
            a[i] = 1
        elif a[0] > 0:
            f = "+"
        elif a[0] < 0:
            f = "-"
    else:
        o += a[i-1]
        if f == "+":
            if a[i] + o > 0:
                c = -1 - o
                ans += abs(c - a[i])
                a[i] = c
                f = "-"
            else:
                if a[i] + o == 0:
                    a[i] -= 1
                    ans += 1
                f = "-"
        elif f == "-":
            if a[i] + o < 0:
                c = 1 - o
                ans += abs(c - a[i])
                a[i] = c
                f = "+"
            else:
                if a[i] + o == 0:
                    a[i] += 1
                    ans += 1
                f = "+"
#print(a)
print(ans)

Submission status: Wrong Answer

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

ans = 0
o = 0
for i in range(n):
    o += a[i-1]
    if i == 0:
        if a[i] == 0:
            f = "+"
            a[i] = 1
        elif a[0] > 0:
            f = "+"
        elif a[0] < 0:
            f = "-"
    else:
        if f == "+":
            if a[i] + o > 0:
                c = -1 - o
                ans += abs(c - a[i])
                a[i] = c
                f = "-"
            else:
                if a[i] + o == 0:
                    a[i] -= 1
                    ans += 1
                f = "-"
        elif f == "-":
            if a[i] + o < 0:
                c = 1 - o
                ans += abs(c - a[i])
                a[i] = c
                f = "+"
            else:
                if a[i] + o == 0:
                    a[i] += 1
                    ans += 1
                f = "+"
#print(a)
print(ans)

Submission status: Wrong Answer

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

ans = 0
for i in range(n):
    if i == 0:
        if a[i] == 0:
            f = "+"
            a[i] = 1
        elif a[0] > 0:
            f = "+"
        elif a[0] < 0:
            f = "-"
    else:
        if f == "+":
            if a[i] + sum(a[:i]) > 0:
                c = -1 - sum(a[:i])
                ans += abs(c - a[i])
                a[i] = c
                f = "-"
            else:
                if a[i] + sum(a[:i]) == 0:
                    a[i] -= 1
                    ans += 1
                f = "-"
        elif f == "-":
            if a[i] + sum(a[:i]) < 0:
                c = 1 - sum(a[:i])
                ans += abs(c - a[i])
                a[i] = c
                f = "+"
            else:
                if a[i] + sum(a[:i]) == 0:
                    a[i] += 1
                    ans += 1
                f = "+"
print(a)
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
A=list(map(int,input().split()))
S=[A[0]]
for i in range(n-1):
  S.append(S[-1]+A[i+1])
ans1=0
s1=0
a=-1
for i in range(n):
  a=a*(-1)
  if a==1:
    ans1=ans1+max(0,1-(S[i]+s1))
    s1=s1+max(0,1-(S[i]+s1))
  else:
    ans1=ans1-min(0,-1-(S[i]+s1))
    s1=s1+min(0,1-(S[i]+s1))
ans2=0
a=1
s2=0
for i in range(n):
  a=a*(-1)
  if a==1:
    ans2=ans2+max(0,1-(S[i]+s2))
    s2=s2+max(0,1-(S[i]+s2))
  else:
    ans2=ans2-min(0,-1-(S[i]+s2))
    s2=s2+min(0,-1-(S[i]+s2))
print(min(ans1,ans2))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
A = [j for j in a]

cnt1 = 0
cnt2 = 0
S = 0

if a[0] > 0:
    for i in range(n-1):
        S += a[i]
        if a[i] > 0:
            if S + a[i+1] >= 0:
                cnt1 += a[i+1] + S + 1 if a[i+1] + S + 1 > 0 else -(a[i+1] + S + 1)
                a[i+1] = -1-S
        elif a[i] < 0:
            if S + a[i+1] <= 0:
                cnt1 += a[i+1] + S - 1 if a[i+1] + S - 1 > 0 else -(a[i+1] + S - 1)
                a[i+1] = 1-S
    A[0] = -1
    cnt2 = a[0] + 1
    for i in range(n-1):
        S += A[i]
        if A[i] > 0:
            if S + A[i+1] >= 0:
                cnt2 += A[i+1] + S + 1 if A[i+1] + S + 1 > 0 else -(A[i+1] + S + 1)
                A[i+1] = -1-S
        elif A[i] < 0:
            if S + A[i+1] <= 0:
                cnt2 += a[i+1] + S - 1 if a[i+1] + S - 1 > 0 else -(a[i+1] + S - 1)
                A[i+1] = 1-S
elif a[0] < 0:
    for i in range(n-1):
        S += a[i]
        if a[i] > 0:
            if S + a[i+1] >= 0:
                cnt1 += a[i+1] + S + 1 if a[i+1] + S + 1 > 0 else -(a[i+1] + S + 1)
                a[i+1] = -1-S
        elif a[i] < 0:
            if S + a[i+1] <= 0:
                cnt1 += a[i+1] + S - 1 if a[i+1] + S - 1 > 0 else -(a[i+1] + S - 1)
                a[i+1] = 1-S
    A[0] = 1
    cnt2 = -a[0] + 1
    for i in range(n-1):
        S += A[i]
        if A[i] > 0:
            if S + A[i+1] >= 0:
                cnt2 += A[i+1] + S + 1 if A[i+1] + S + 1 > 0 else -(A[i+1] + S + 1)
                A[i+1] = -1-S
        elif A[i] < 0:
            if S + A[i+1] <= 0:
                cnt2 += a[i+1] + S - 1 if a[i+1] + S - 1 > 0 else -(a[i+1] + S - 1)
                A[i+1] = 1-S
else:
    a[0] = 1
    cnt1 = 1
    for i in range(n-1):
        S += a[i]
        if a[i] > 0:
            if S + a[i+1] >= 0:
                cnt1 += a[i+1] + S + 1 if a[i+1] + S + 1 > 0 else -(a[i+1] + S + 1)
                a[i+1] = -1-S
        elif a[i] < 0:
            if S + a[i+1] <= 0:
                cnt1 += a[i+1] + S - 1 if a[i+1] + S - 1 > 0 else -(a[i+1] + S - 1)
                a[i+1] = 1-S
    A[0] = -1
    cnt2 = 1
    for i in range(n-1):
        S += A[i]
        if A[i] > 0:
            if S + A[i+1] >= 0:
                cnt2 += A[i+1] + S + 1 if A[i+1] + S + 1 > 0 else -(A[i+1] + S + 1)
                A[i+1] = -1-S
        elif A[i] < 0:
            if S + A[i+1] <= 0:
                cnt2 += a[i+1] + S - 1 if a[i+1] + S - 1 > 0 else -(a[i+1] + S - 1)
                A[i+1] = 1-S
cnt = cnt2 if cnt1 >= cnt2 else cnt1

print(cnt)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
import sys
sum=0
cnt=0
# 奇数+
for i in range(n):
    z=sum+a[i]
    if i%2==0:
        if z<0:
            sum=z
        else:
            cnt+=(z+1)
            sum=-1
    else:
        if z>0:
            sum=z
        else:
            cnt+=(1-z)
            sum=1
cnt_sbst=cnt
# 奇数-
for i in range(n):
    z=sum+a[i]
    if i%2==1:
        if z<0:
            sum=z
        else:
            cnt+=(z+1)
            sum=-1
    else:
        if z>0:
            sum=z
        else:
            cnt+=(1-z)
            sum=1
cnt_plus=cnt
ans=min(cnt_plus,cnt_sbst)
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
import sys
sum=0
cnt=0
# 奇数+
for i in range(n):
    z=sum+a[i]
    if i%2==0:
        if z<0:
            sum=z
        else:
            sum=-1
            cnt+=(z+1)
    else:
        if z>0:
            sum=z
        else:
            sum=1
            cnt+=(1-z)
cnt_sbst=cnt
# 奇数-
for i in range(n):
    z=sum+a[i]
    if i%2==1:
        if z<0:
            sum=z
        else:
            sum=-1
            cnt+=(z+1)
    else:
        if z>0:
            sum=z
        else:
            sum=1
            cnt+=(1-z)
cnt_plus=cnt
ans=min(cnt_plus,cnt_sbst)
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
import sys
sum=a[0]
cnt=0
if a[0]==0:
    sum+=1
    cnt+=1
    for i in range(1,n):
        if sum<0:
            z=sum+a[i]
            if z>0:
                sum=z
            elif z<=0:
                cnt+=(1-z)
                sum=1
        elif sum>0:
            z=sum+a[i]
            if z>=0:
                cnt+=(z+1)
                sum=-1
            elif z<0:
                sum=z
    cnt_plus=cnt
    sum=-1
    cnt=1
    for i in range(1,n):
        if sum<0:
            z=sum+a[i]
            if z>0:
                sum=z
            elif z<=0:
                cnt+=(1-z)
                sum=1
        elif sum>0:
            z=sum+a[i]
            if z>=0:
                cnt+=(z+1)
                sum=-1
            elif z<0:
                sum=z
    cnt_sbst=cnt
    print(cnt_sbst)
    print(cnt_plus)
    print(min(cnt_plus,cnt_sbst))
    sys.exit()
for i in range(1,n):
    if sum<0:
        z=sum+a[i]
        if z>0:
            sum=z
        elif z<=0:
            cnt+=(1-z)
            sum=1
    elif sum>0:
        z=sum+a[i]
        if z>=0:
            cnt+=(z+1)
            sum=-1
        elif z<0:
            sum=z            
print(cnt)

# 6
# 0 0 -1 3 5 0

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
import sys
sum=a[0]
cnt=0
if a[0]==0:
    sum+=1
    cnt+=1
    for i in range(1,n):
        if sum<0:
            z=sum+a[i]
            if z>0:
                sum=z
            elif z<=0:
                cnt+=(1-z)
                sum=1
        elif sum>0:
            z=sum+a[i]
            if z>=0:
                cnt+=(z+1)
                sum=-1
            elif z<0:
                sum=z
    cnt_plus=cnt
    sum=-1
    cnt=1
    for i in range(1,n):
        if sum<0:
            z=sum+a[i]
            if z>0:
                sum=z
            elif z<=0:
                cnt+=(1-z)
                sum=1
        elif sum>0:
            z=sum+a[i]
            if z>=0:
                cnt+=(z+1)
                sum=-1
            elif z<0:
                sum=z
    cnt_sbst=cnt
    print(min(cnt_plus,cnt_sbst))
    sys.exit()
for i in range(1,n):
    if sum<0:
        z=sum+a[i]
        if z>0:
            sum=z
        elif z<=0:
            cnt+=(1-z)
            sum=1
    elif sum>0:
        z=sum+a[i]
        if z>=0:
            cnt+=(z+1)
            sum=-1
        elif z<0:
            sum=z            
print(cnt)

# 6
# 0 0 -1 3 5 0

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
import sys
sum=a[0]
cnt=0
if a[0]==0:
    sum+=1
    cnt+=1
    for i in range(1,n):
        if sum<0:
            z=sum+a[i]
            if z>0:
                sum=z
            elif z<0:
                cnt+=(1-z)
                sum=1
            else:
                if sum>0:
                    sum=-1
                    cnt+=1
                elif sum<0:
                    sum=1
                    cnt+=1
        elif sum>0:
            z=sum+a[i]
            if z>0:
                cnt+=(z+1)
                sum=-1
            elif z<0:
                sum=z
            else:
                if sum>0:
                    sum=-1
                    cnt+=1
                elif sum<0:
                    sum=1
                    cnt+=1
    cnt_plus=cnt
    sum=-1
    cnt=1
    for i in range(1,n):
        if sum<0:
            z=sum+a[i]
            if z>0:
                sum=z
            elif z<0:
                cnt+=(1-z)
                sum=1
            else:
                if sum>0:
                    sum=-1
                    cnt+=1
                elif sum<0:
                    sum=1
                    cnt+=1
        elif sum>0:
            z=sum+a[i]
            if z>=0:
                cnt+=(z+1)
                sum=-1
            elif z<0:
                sum=z
            else:
                if sum>0:
                    sum=-1
                    cnt+=1
                elif sum<0:
                    sum=1
                    cnt+=1
    cnt_sbst=cnt
    print(min(cnt_plus,cnt_sbst))
    sys.exit()
for i in range(1,n):
    if sum<0:
        z=sum+a[i]
        if z>0:
            sum=z
        elif z<0:
            cnt+=(1-z)
            sum=1
        else:
            if sum>0:
                sum=-1
                cnt+=1
            elif sum<0:
                sum=1
                cnt+=1
    elif sum>0:
        z=sum+a[i]
        if z>0:
            cnt+=(z+1)
            sum=-1
        elif z<0:
            sum=z
        else:
            if sum>0:
                sum=-1
                cnt+=1
            elif sum<0:
                sum=1
                cnt+=1
            
print(cnt)

# 6
# 0 0 -1 3 5 0