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 解():
    iN = int(input())
    aA = [int(_) for _ in input().split()]
    iL = len(aA)


    iStart = 0
    if sum(aA[0::2]) < sum(aA[2::2]):
        iStart = 1


    iC = 0
    aD = [0]*iL
    if 0 % 2 == iStart :
        if aA[0] <= 0:
            aA[0] = 1
            iC += -1 * aA[0] + 1
    else:
        if 0 <= aA[0] :
            aA[0] = -1
            iC += aA[0] + 1

    aD[0] = aA[0]
    for i in range(1,iL):
        aD[i] = aD[i-1]+aA[i]
        if i % 2 == iStart:
            if aD[i] <= 0:
                iC += -1*aD[i] +1
                aD[i] = 1
        else:
            if aD[i] >= 0:
                iC += aD[i] +1
                aD[i] = -1
    print(iC)
解()

Submission status: Wrong Answer

Solution code:
def 解():
    iN = int(input())
    aA = [int(_) for _ in input().split()]
    iL = len(aA)


    iStart = 0
    if sum(aA[0::2]) < sum(aA[2::2]):
        iStart = 1


    iC = 0
    aD = [0]*iL
    if 0 % 2 == iStart :
        if aA[0] < 0:
            aA[0] = 1
            iC += -1 * aA[0] + 1
    else:
        if 0 < aA[0] :
            aA[0] = -1
            iC += aA[0] + 1

    aD[0] = aA[0]
    for i in range(1,iL):
        aD[i] = aD[i-1]+aA[i]
        if i % 2 == iStart:
            if aD[i] <= 0:
                iC += -1*aD[i] +1
                aD[i] = 1
        else:
            if aD[i] >= 0:
                iC += aD[i] +1
                aD[i] = -1
    print(iC)
解()

Submission status: Wrong Answer

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

a_orig = a[:]

ans1 = 0
ans2 = 0
tot = [0 for i in range(n)]
tot[0] = a[0]

if a[0] <= 0:
    a[0] = 1
    tot[0] = 1


for i in range(1, n):
    tot[i] = tot[i-1] + a[i]
    if i % 2 == 0:
        if tot[i] <= 0:
            tot[i] = 1
            a[i] = tot[i] - tot[i-1]

    else:
        if tot[i] >= 0:
            tot[i] = -1
            a[i] = tot[i] - tot[i-1]
print(tot)
print(a)
for i in range(n):
    ans1 += abs(a[i]-a_orig[i])



a = a_orig[:]
tot = [0 for i in range(n)]
tot[0] = a[0]

if a[0] >= 0:
    a[0] = -1
    tot[0] = -1

for i in range(1, n):
    tot[i] = tot[i-1] + a[i]
    if i % 2 == 1:
        if tot[i] <= 0:
            tot[i] = 1
            a[i] = tot[i] - tot[i-1]

    else:
        if tot[i] >= 0:
            tot[i] = -1
            a[i] = tot[i] - tot[i-1]

for i in range(n):
    ans2 += abs(a[i]-a_orig[i])
print(tot)
print(a)
print(min(ans1, ans2))

Submission status: Wrong Answer

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

a_orig = a[:]

ans1 = 0
ans2 = 0
tot = [0 for i in range(n)]
tot[0] = a[0]

for i in range(1, n):
    tot[i] = tot[i-1] + a[i]
    if i % 2 == 0:
        if tot[i] <= 0:
            tot[i] = 1
            a[i] = tot[i] - tot[i-1]

    else:
        if tot[i] >= 0:
            tot[i] = -1
            a[i] = tot[i] - tot[i-1]

for i in range(n):
    ans1 += abs(a[i]-a_orig[i])

a = a_orig[:]
tot = [0 for i in range(n)]
tot[0] = a[0]

for i in range(1, n):
    tot[i] = tot[i-1] + a[i]
    if i % 2 == 1:
        if tot[i] <= 0:
            tot[i] = 1
            a[i] = tot[i] - tot[i-1]

    else:
        if tot[i] >= 0:
            tot[i] = -1
            a[i] = tot[i] - tot[i-1]

for i in range(n):
    ans2 += abs(a[i]-a_orig[i])

print(min(ans1, ans2))

Submission status: Wrong Answer

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

if a[0] < 0:
    for i in range(n):
        a[i] *= -1
a_orig = a[:]

ans1 = 0
ans2 = 0
tot = [0 for i in range(n)]
tot[0] = a[0]

for i in range(1, n):
    tot[i] = tot[i-1] + a[i]
    if i % 2 == 0:
        if tot[i] <= 0:
            tot[i] = 1
            a[i] = tot[i] - tot[i-1]

    else:
        if tot[i] >= 0:
            tot[i] = -1
            a[i] = tot[i] - tot[i-1]

for i in range(n):
    ans1 += abs(a[i]-a_orig[i])


tot = [0 for i in range(n)]
tot[0] = a[0]

for i in range(1, n):
    tot[i] = tot[i-1] + a[i]
    if i % 2 == 1:
        if tot[i] <= 0:
            tot[i] = 1
            a[i] = tot[i] - tot[i-1]

    else:
        if tot[i] >= 0:
            tot[i] = -1
            a[i] = tot[i] - tot[i-1]

for i in range(n):
    ans2 += abs(a[i]-a_orig[i])

print(min(ans1, ans2))

Submission status: Wrong Answer

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

if a[0] < 0:
    for i in range(n):
        a[i] *= -1
a_orig = a[:]

ans = 0
tot = [0 for i in range(n)]
tot[0] = a[0]

for i in range(1, n):
    tot[i] = tot[i-1] + a[i]
    if i % 2 == 0:
        if tot[i] <= 0:
            tot[i] = 1
            a[i] = tot[i] - tot[i-1]

    else:
        if tot[i] >= 0:
            tot[i] = -1
            a[i] = tot[i] - tot[i-1]

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

cnt = 0
sum = [0]*n
sum[0] = a[0]

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

Submission status: Wrong Answer

Solution code:
n = int(input())
list_a = list(map(int,input().split()))
i = 0
k = 0
count = 0
ans = 0
if list_a[0] > 0:
    for i in range(0,n):
        ans += list_a[i]
        if ans / ((-1) ** i) <= 0:
            count += abs(ans - (-1) ** i)
            ans = (-1) ** i

else:
    for i in range(0,n):
        ans += list_a[i]
        if ans / ((-1) ** (i+1)) <= 0:
            count += abs(ans - (-1) ** (i+1))
            ans = (-1) ** (i+1)

count1 = count
count = 0
if list_a[0] <= 0:
    for i in range(0,n):
        ans += list_a[i]
        if ans / ((-1) ** i) <= 0:
            count += abs(ans - (-1) ** i)
            ans = (-1) ** i

else:
    for i in range(0,n):
        ans += list_a[i]
        if ans / ((-1) ** (i+1)) <= 0:
            count += abs(ans - (-1) ** (i+1))
            ans = (-1) ** (i+1)
count2 = count
print(min(count1, count2))

Submission status: Wrong Answer

Solution code:
n = int(input())
list_a = list(map(int,input().split()))
i = 0
k = 0
count = 0
ans = list_a[0]
if list_a[0] > 0:
    for i in range(0,n):
        ans += list_a[i]
        if ans / ((-1) ** i) <= 0:
            count += abs(ans - (-1) ** i)
            ans = (-1) ** i

else:
    for i in range(0,n):
        ans += list_a[i]
        if ans / ((-1) ** (i+1)) <= 0:
            count += abs(ans - (-1) ** (i+1))
            ans = (-1) ** (i+1)

count1 = count
count = 0
if list_a[0] <= 0:
    for i in range(0,n):
        ans += list_a[i]
        if ans / ((-1) ** i) <= 0:
            count += abs(ans - (-1) ** i)
            ans = (-1) ** i

else:
    for i in range(0,n):
        ans += list_a[i]
        if ans / ((-1) ** (i+1)) <= 0:
            count += abs(ans - (-1) ** (i+1))
            ans = (-1) ** (i+1)
count2 = count
print(min(count1, count2))

Submission status: Wrong Answer

Solution code:
n = int(input())
list_a = list(map(int,input().split()))
i = 0
k = 0
count = 0
ans = list_a[0]
if list_a[0] > 0:
    for i in range(1,n):
        ans += list_a[i]
        if ans / ((-1) ** i) <= 0:
            count += abs(ans - (-1) ** i)
            ans = (-1) ** i

else:
    for i in range(1,n):
        ans += list_a[i]
        if ans / ((-1) ** (i+1)) <= 0:
            count += abs(ans - (-1) ** (i+1))
            ans = (-1) ** (i+1)

count1 = count
count = 0
if list_a[0] <= 0:
    for i in range(1,n):
        ans += list_a[i]
        if ans / ((-1) ** i) <= 0:
            count += abs(ans - (-1) ** i)
            ans = (-1) ** i

else:
    for i in range(1,n):
        ans += list_a[i]
        if ans / ((-1) ** (i+1)) <= 0:
            count += abs(ans - (-1) ** (i+1))
            ans = (-1) ** (i+1)
count2 = count
print(min(count1, count2))

Submission status: Wrong Answer

Solution code:
n = int(input())
list_a = list(map(int,input().split()))
i = 0
k = 0
count = 0
if list_a[0] == 0:
    while list_a[i] == 0:
        if i == n:
            ans = 2*n -1
            break
        k = i
        i += 1
    else:
        if list_a[k+1] > 0:
            count += abs(list_a[0] - (-1) ** (k+1))
            list_a[0] = (-1) ** (k+1)
        else:
            count += abs(list_a[0] - (-1) ** k)
            list_a[0] = (-1) ** k
ans = list_a[0]
if list_a[0] > 0:
    for i in range(1,n):
        ans += list_a[i]
        if ans / ((-1) ** i) <= 0:
            count += abs(ans - (-1) ** i)
            ans = (-1) ** i

else:
    for i in range(1,n):
        ans += list_a[i]
        if ans / ((-1) ** (i+1)) <= 0:
            count += abs(ans - (-1) ** (i+1))
            ans = (-1) ** (i+1)

print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
list_a = list(map(int,input().split()))
i = 0
k = 0
count = 0
if list_a[0] == 0:
    while list_a[i] != 0:
        if i == n:
            ans = 2*n -1
            break
        k = i
        i += 1
    else:
        if list_a[k+1] > 0:
            count += abs(list_a[0] - (-1) ** (k+1))
            list_a[0] = (-1) ** (k+1)
        else:
            count += abs(list_a[0] - (-1) ** k)
            list_a[0] = (-1) ** k
ans = list_a[0]
if list_a[0] > 0:
    for i in range(1,n):
        ans += list_a[i]
        if ans / ((-1) ** i) <= 0:
            count += abs(ans - (-1) ** i)
            ans = (-1) ** i

else:
    for i in range(1,n):
        ans += list_a[i]
        if ans / ((-1) ** (i+1)) <= 0:
            count += abs(ans - (-1) ** (i+1))
            ans = (-1) ** (i+1)

print(count)

Submission status: Wrong Answer

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

S = A[0]
total = 0
for i in range(1, N):
    if S > 0:
        if S + A[i] < 0: S += A[i]
        else:
            total += abs(-1 - (S + A[i]))
            S = -1
    elif S < 0:
        if S + A[i] > 0: S += A[i]
        else: 
            total += 1 - (S + A[i])
            S = 1

print(total)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = 0
key = a[0]
if key == 0:
    flag = 1
    a[0] = 1
    ans += 1
elif a[0] > 0:
    flag = 1
else:
    flag = -1
dp = [0 for i in range(n)]
dp[0] = a[0]
for i in range(1, n):
    dp[i] = dp[i - 1] + a[i]
    if dp[i] == 0:
        dp[i] = flag * -1
        ans += 1
    elif flag == 1 and dp[i] > 0:
        ans += abs(-1 - dp[i])
        dp[i] = -1
    elif flag == -1 and dp[i] < 0:
        ans += abs(1 - dp[i])
        dp[i] = 1
    flag *= -1

    
ans1 = ans
ans = 0
if key == 0:
    flag = -1
    a[0] = -1
    ans += 1
elif a[0] > 0:
    flag = 1
else:
    flag = -1
dp = [0 for i in range(n)]
dp[0] = a[0]
for i in range(1, n):
    dp[i] = dp[i - 1] + a[i]
    if dp[i] == 0:
        dp[i] = flag * -1
        ans += 1
    elif flag == 1 and dp[i] > 0:
        ans += abs(-1 - dp[i])
        dp[i] = -1
    elif flag == -1 and dp[i] < 0:
        ans += abs(1 - dp[i])
        dp[i] = 1
    flag *= -1
print(min(ans1, ans))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = 0
if a[0] == 0:
    flag = 1
    a[0] = 1
    ans += 1
elif a[0] > 0:
    flag = 1
else:
    flag = -1
dp = [0 for i in range(n)]
dp[0] = a[0]
for i in range(1, n):
    dp[i] = dp[i - 1] + a[i]
    if dp[i] == 0:
        dp[i] = flag * -1
        ans += 1
    elif flag == 1 and dp[i] > 0:
        ans += abs(-1 - dp[i])
        dp[i] = -1
    elif flag == -1 and dp[i] < 0:
        ans += abs(1 - dp[i])
        dp[i] = 1
    flag *= -1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
if a[0] == 0:
    flag = 1
    a[0] = 1
elif a[0] > 0:
    flag = 1
else:
    flag = -1
ans = 0
dp = [0 for i in range(n)]
dp[0] = a[0]
for i in range(1, n):
    dp[i] = dp[i - 1] + a[i]
    if dp[i] == 0:
        dp[i] = flag * -1
        ans += 1
    elif flag == 1 and dp[i] > 0:
        ans += abs(-1 - dp[i])
        dp[i] = -1
    elif flag == -1 and dp[i] < 0:
        ans += abs(1 - dp[i])
        dp[i] = 1
    flag *= -1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
if a[0] >= 0:
    flag = 1
else:
    flag = -1
ans = 0
dp = [0 for i in range(n)]
dp[0] = a[0]
for i in range(1, n):
    dp[i] = dp[i - 1] + a[i]
    if dp[i] == 0:
        dp[i] = flag * -1
        ans += 1
    elif flag == 1 and dp[i] > 0:
        ans += abs(-1 - dp[i])
        dp[i] = -1
    elif flag == -1 and dp[i] < 0:
        ans += (1 - dp[i])
        dp[i] = 1
    flag *= -1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
if a[0] >= 0:
    flag = 1
else:
    flag = -1
ans = 0
dp = [0 for i in range(n)]
dp[0] = a[0]
for i in range(1, n):
    dp[i] = dp[i - 1] + a[i]
    if flag == 1 and dp[i] >= 0:
        ans += abs(-1 - dp[i])
        dp[i] = -1
    elif flag == -1 and dp[i] <= 0:
        ans += (1 - dp[i])
        dp[i] = 1
    flag *= -1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int( input())
A = list( map( int, input().split()))
ansp = 0
sums = A[0]
if sums == 0:
    ansp += 1
    sums += 1
for i in range(1,n):
    sums += A[i]
    if i%2 == 1:
        if sums < 0:
            pass
        else:
            ansp += abs(-1-sums)
            sums = -1
    else:
        if sums > 0:
            pass
        else:
            ansp += abs(1 - sums)
            sums = 1
sums = A[0]
ansm = 0
if sums == 0:
    ansm += 1
    sums -= 1
for i in range(1,n):
    sums += A[i]
    if i%2 == 0:
        if sums < 0:
            pass
        else:
            ansm += abs(-1-sums)
            sums = -1
    else:
        if sums > 0:
            pass
        else:
            ansm += abs(1 - sums)
            sums = 1
print( min(ansp, ansm))

Submission status: Wrong Answer

Solution code:
n = int( input())
A = list( map( int, input().split()))
ansp = 0
sums = A[0]
if sums == 0:
    ansp += 1
    sums += 1
for i in range(1,n):
    sums += A[i]
    if i%2 == 1:
        if sums < 0:
            pass
        else:
            ansp += abs(-1-sums)
            sums = -1
    else:
        if sums > 0:
            pass
        else:
            ansp += 1 - sums
            sums = 1
sums = A[0]
ansm = 0
if sums == 0:
    ansm += 1
    sums -= 1
for i in range(1,n):
    sums += A[i]
    if i%2 == 0:
        if sums < 0:
            pass
        else:
            ansm += abs(-1-sums)
            sums = -1
    else:
        if sums > 0:
            pass
        else:
            ansm += 1 - sums
            sums = 1
print( min(ansp, ansm))

Submission status: Wrong Answer

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

count1 = 0
memo = 0
a = b
for i in range(n):
    if i % 2 == 0:
        if a[i] >= 0:
            count1 += a[i] + 1
            memo = -(a[i] + 1)

    else:
        if a[i] <= 0:
            count1 += 1 - a[i]
            memo = 1 - a[i]
    if i + 1 < n:
        a[i+1] = a[i+1] + a[i] + memo

print(min(count0, count1))

Submission status: Wrong Answer

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

count0 = 0
memo = 0
for i in range(n):
    if i % 2 == 0:
        if a[i] <= 0:
            count0 += 1 - a[i]
            memo = 1 - a[i]
    else:
        if a[i] >= 0:
            count0 += a[i] + 1
            memo = -(a[i] + 1)
    if i + 1 < n:
        a[i+1] = a[i+1] + a[i] + memo

count1 = 0
memo = 0
for i in range(n):
    if i % 2 == 0:
        if a[i] >= 0:
            count1 += a[i] + 1
            memo = -(a[i] + 1)

    else:
        if a[i] <= 0:
            count1 += 1 - a[i]
            memo = 1 - a[i]
    if i + 1 < n:
        a[i+1] = a[i+1] + a[i] + memo

print(min(count0, count1))

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin

def li(): return [int(x) for x in stdin.readline().split()]
def li_(): return [int(x)-1 for x in stdin.readline().split()]
def lf(): return [float(x) for x in stdin.readline().split()]
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(ns())
def nf(): return float(ns())

n = ni()
a = li()

# 正→負→正→...
cur = a[0]
ans_pn = 0
if cur <= 0:
    ans_pn = abs(a[0])+1

for i in range(1,n):
    cur += a[i]
    if i%2 == 0 and cur <= 0:
        ans_pn += abs(cur)+1
        cur = 1
    elif i%2 == 1 and cur >= 0:
        ans_pn += abs(cur)+1
        cur = -1


# 負→正→負...
cur = a[0]
ans_np = 0
if cur >= 0:
    ans_np = abs(a[0])+1

for i in range(1,n):
    cur += a[i]
    if i%2 == 0 and cur >= 0:
        ans_np += abs(cur)+1
        cur = -1
    elif i%2 == 1 and cur <= 0:
        ans_np += abs(cur)+1
        cur = 1
        

print(min(ans_np, ans_pn))

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin

def li(): return [int(x) for x in stdin.readline().split()]
def li_(): return [int(x)-1 for x in stdin.readline().split()]
def lf(): return [float(x) for x in stdin.readline().split()]
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(ns())
def nf(): return float(ns())

n = ni()
a = li()

# 正→負→正→...
cur = a[0]
ans_pn = 0
if cur <= 0:
    ans_pn = abs(a[0]-1)

for i in range(1,n):
    cur += a[i]
    if i%2 == 0 and cur <= 0:
        ans_pn += abs(cur)+1
        cur = 1
    elif i%2 == 1 and cur >= 0:
        ans_pn += abs(cur)+1
        cur = -1


# 負→正→負...
cur = a[0]
ans_np = 0
if cur <= 0:
    ans_np = abs(a[0]+1)

for i in range(1,n):
    cur += a[i]
    if i%2 == 0 and cur >= 0:
        ans_np += abs(cur)+1
        cur = -1
    elif i%2 == 1 and cur <= 0:
        ans_np += abs(cur)+1
        cur = 1
        

print(min(ans_np, ans_pn))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Sep  8 15:51:53 2018

@author: maezawa
"""
def f(n, a0, cnt, sa, sign):
    a = a0[:]
    if a[0] == 0:
        a[0] = 1
        cnt += 1
    if sign == -1:
        a[0] = -a[0]
        cnt += 2*abs(a[0])
    for i in range(n-1):
        sa += a[i]
        na = -sa//abs(sa)*(abs(sa)+1)
        if abs(a[i+1]) > abs(na) and a[i+1]*na > 0:
            continue
        else:
            cnt += abs(na-a[i+1])
            a[i+1] = na
    return cnt

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

cnt0 = f(n, a, 0, 0, -1)

cnt1 = f(n, a, 0, 0, 1)
cnt = min([cnt0,cnt1])
print(cnt)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Sep  8 15:51:53 2018

@author: maezawa
"""
def f(n, a0, cnt, sa, sign):
    a = a0[:]
    if a[0] == 0:
        a[0] = 1
        cnt += 1
    if sign == -1:
        a[0] = -a[0]
        cnt += 2*abs(a[0])
    for i in range(n-1):
        sa += a[i]
        na = -sa//abs(sa)*(abs(sa)+1)
        if abs(a[i+1]) > abs(na) and a[i+1]*na > 0:
            continue
        else:
            cnt += abs(na-a[i+1])
            a[i+1] = na
    return cnt

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

cnt0 = f(n, a, cnt, sa, -1)

cnt1 = f(n, a, cnt, sa, 1)
cnt = min([cnt0,cnt1])
print(cnt)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Sep  8 15:51:53 2018

@author: maezawa
"""
def f(n, a0, cnt, sa):
    a = a0[:]
    for i in range(n-1):
        sa += a[i]
        na = -sa//abs(sa)*(abs(sa)+1)
        if abs(a[i+1]) > abs(na) and a[i+1]*na > 0:
            continue
        else:
            cnt += abs(na-a[i+1])
            a[i+1] = na
    return cnt

n = int(input())
a = list(map(int, input().split()))
sa = 0
cnt = 0
if a[0] == 0:
    a[0] = 1
    cnt =1
    cnt0 = f(n, a, cnt, sa)
    a[0] = -1
    cnt1 = f(n, a, cnt, sa)
    cnt = min([cnt0,cnt1])
else:
    cnt = f(n, a, cnt, sa)
        

print(cnt)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Sep  8 15:51:53 2018

@author: maezawa
"""

n = int(input())
a = list(map(int, input().split()))
sa = 0
cnt = 0
for i in range(n-1):
    sa += a[i]
    if sa == 0:
        sa += 1
        cnt += 1
    na = -sa//abs(sa)*(abs(sa)+1)
    if abs(a[i+1]) > abs(na) and a[i+1]*na > 0:
        continue
    else:
        cnt += abs(na-a[i+1])
        a[i+1] = na

print(cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())

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

S = [0] * (N+1)

for i in range(N):
  S[i+1] = S[i] + A[i]
print(S)
#S[1] > 0
ans1 = 0
work = 0
for i in range(1,N+1):
  if i%2 == 1:
    # pos is required
    if S[i] + work <= 0:
      temp = 1 - (S[i] + work)
      ans1 += temp
      work += temp
  else:
    # neg is required
    if S[i] + work >= 0:
      temp = (S[i] + work) + 1
      ans1 += temp
      work -= temp

#S[1] < 0
ans2 = 0
work = 0
for i in range(1,N+1):
  if i%2 == 1:
    # neg is required
    if S[i] + work >= 0:
      temp = (S[i] + work) + 1
      ans2 += temp
      work -= temp
  else:
    # neg is required
    if S[i] + work <= 0:
      temp = 1 - (S[i] + work)
      ans2 += temp
      work += temp

print(min(ans1, ans2))

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int, input().split()))
sum_now=a[0]
sum_before=-a[0]
count_1=0
count_2=0
for i in range(n):
    while sum_now*sum_before>=0:
        if sum_before==0:
            sum_now=-a[1]/abs(a[1])
            count_1+=1
        else:
            count_1+=abs(int(sum_now))+1
            sum_now=-sum_before/abs(sum_before)
    if i!=n-1:
        sum_before=sum_now
        sum_now=sum_now+a[i+1]

sum_now=a[0]
sum_before=-a[0]
if sum_before==0:
    sum_now=a[1]/abs(a[1])
    count_2+=1
else:
    count_2+=abs(int(sum_now))+1
    sum_now=-sum_now/abs(sum_now)
for i in range(n):
    while sum_now*sum_before>=0:
        if sum_before==0:
            sum_now=a[1]/abs(a[1])
            count_2+=1
        else:
            count_2+=abs(int(sum_now))+1
            sum_now=-sum_before/abs(sum_before)
    if i!=n-1:
        sum_before=sum_now
        sum_now=sum_now+a[i+1]
print(min(count_1, count_2))

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int, input().split()))
sum_now=a[0]
sum_before=-a[0]
count=0
for i in range(n):
    while sum_now*sum_before>=0:
        if sum_before==0:
            sum_now=-a[1]/abs(a[1])
            count+=1
        else:
            count+=abs(int(sum_now))+1
            sum_now=-sum_before/abs(sum_before)
    if i!=n-1:
        sum_before=sum_now
        sum_now=sum_now+a[i+1]
print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
lis = list(map(int,input().split()))
li = lis
cou = 0
co = 0
for i in range(n):
  if i % 2 == 0:
    if sum(li[:i+1]) <= 0:
      cou += (1-sum(li[:i+1]))
      li[i] += (1-sum(li[:i+1]))
  else:
    if sum(li[:i+1]) >= 0:
      cou += sum(li[:i+1])+1
      li[i] -= (sum(li[:i+1])+1)
for i in range(n):
  if i % 2 != 0:
    if sum(lis[:i+1]) <= 0:
      co += (1-sum(lis[:i+1]))
      lis[i] += (1-sum(lis[:i+1]))
  else:
    if sum(lis[:i+1]) >= 0:
      co += sum(lis[:i+1])+1
      lis[i] -= (sum(lis[:i+1])+1)
      
print(min(co,cou))

Submission status: Wrong Answer

Solution code:
import functools
n = int(input())
a = list(map(int, input().split()))
r = a[0]
@functools.lru_cache()
def memo(n):
   global r
   cnt = 0
   for i in range(1,n):
        if r>0:
            if r+a[i]<0:
                r+=a[i]
            else:
                cnt += a[i] +r +1
                r = -1
        elif r<0:
            if r+a[i]>0:
                r+=a[i]
            else:
                cnt += -a[i] - r+1
                r = 1
   return cnt

print(memo(n))

Submission status: Wrong Answer

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

@functools.lru_cache()
def memo(n):
   r = a[0]
   cnt = 0
   for i in range(1,n):
        if r>0:
            if r+a[i]<0:
                r+=a[i]
            else:
                cnt += a[i] +r +1
                r = -1
        elif r<0:
            if r+a[i]>0:
                r+=a[i]
            else:
                cnt += -a[i] - r+1
                r = 1
   return cnt

print(memo(n))

Submission status: Wrong Answer

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

@functools.lru_cache()
def long_func(n):
   r = a[0]
   cnt = 0
   for i in range(1,n):
        if r>0:
            if r+a[i]<0:
                r+=a[i]
            else:
                cnt += a[i] +r +1
                r = -1
        else:
            if r+a[i]>0:
                r+=a[i]
            else:
                cnt += -a[i] - r+1
                r = 1
   return cnt

print(long_func(n))

Submission status: Wrong Answer

Solution code:
N = int(input())
L = list(map(int, input().split()))
#print(L)
sum = L[0]
count = 0
for i in range(1, N):
    if sum * (sum + L[i]) >= 0:
        count = count + (abs(sum + L[i]) + 1)
        if sum > 0 and sum + L[i] >= 0:
            sum = -1
        else:
            sum = 1
    else:
        sum = sum + L[i]
    #print(sum)
print(count)

Submission status: Wrong Answer

Solution code:
n=int(input())
l=list(map(int,input().split()))
s=l[0]
c=0
if not s:
  s+=1
  c+=1

for i in l[1:]:
  if (s+i)*s>=0:
    b=s
    s=s//abs(s)*(-1)
    c+=abs((b+i)-s)
  else:
    s+=i
print(c)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

value = a[0]
sign = value >= 0
result = 0
for i in a[1:]:
    value += i
    diff = 0
    if sign and value >= 0:
        diff = value + 1
    elif not sign and value < 0:
        diff = value - 1
    result += abs(diff)
    sign = not sign
    value -= diff
    if value == 0:
        diff += sign
        value += diff
        result += diff
print(result)

Submission status: Wrong Answer

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

ans = 0
cumsum = a[0]
p = a[0] > 0
for i in range(1, n):
    if p:
        if cumsum+a[i] >= 0:
            ans += cumsum+a[i]+1
            cumsum = -1
        else:
            cumsum += a[i]
    else:
        if cumsum+a[i] <= 0:
            ans += 1-(cumsum+a[i])
            cumsum = 1
        else:
            cumsum += a[i]

    if p: p = False
    else: p = True
    # print(cumsum)
print(ans)

Submission status: Wrong Answer

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

ans = 0
cumsum = a[0]
p = a[0] > 0
for i in range(1, n):
    if p:
        if cumsum+a[i] >= 0:
            ans += cumsum+a[i]+1
            cumsum = -1
        else:
            cumsum += a[i]
        p = False
    else:
        if cumsum+a[i] <= 0:
            ans += 1-(cumsum+a[i])
            cumsum = 1
        else:
            cumsum += a[i]
        p = True
    # print(cumsum)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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


def solve(isPosi,N,A):
    ans = 0
    sm = A[0]
    if sm>0:
        isPosi = True
    elif sm<0:
        isPosi = False
    
    for a in A[1:]:
        if isPosi:
            sm += a
            if sm >= 0:
                ans += abs(-1-sm)
                sm = -1
            isPosi = False
        
        else:
            sm += a
            if sm <= 0:
                ans += abs(1-sm)
                sm = 1
            isPosi = True
        
    return ans

ans_p = solve(True,N,A)
ans_n = solve(False,N,A)

print(min(ans_p,ans_n))

Submission status: Wrong Answer

Solution code:

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


def solve(isPosi,N,A):
    ans = 0
    sm = A[0]
    for a in A[1:]:
        if isPosi:
            sm += a
            if sm >= 0:
                ans += abs(-1-sm)
                sm = -1
            isPosi = False
        
        else:
            sm += a
            if sm <= 0:
                ans += abs(1-sm)
                sm = 1
            isPosi = True
        
    return ans

ans_p = solve(True,N,A)
ans_n = solve(False,N,A)

print(min(ans_p,ans_n))