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()))
sum=A[0]
ans=0
for i in range(n-1):#1_n-1
    if sum*(sum+A[i+1])<0:
        sum+=A[i+1]
    else:
        ans+=abs(sum+A[i+1])+1
        if sum>0:
            sum=-1
        else:
            sum=1
print(ans)

Submission status: Wrong Answer

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

ans1 = 0
ans2 = 0
cur1 = a[0]
cur2 = a[0]

for i in range(1, n):
    cun = a[i]
    # iが偶数のときプラス
    if i % 2 == 0:
        # プラスじゃないとき
        if a[i] + cur1 <= 0:
            ans1 += -(a[i] + cur1) + 1
            cun = -cur1 + 1
    # iが奇数のときマイナス
    else:
        # マイナスじゃないとき
        if a[i] + cur1 >= 0:
            ans1 += a[i] + cur1 + 1
            cun = -cur1 - 1
    cur1 += cun

for i in range(1, n):
    cun = a[i]
    # iが偶数のときマイナス
    if i % 2 == 0:
        # マイナスじゃないとき
        if a[i] + cur2 >= 0:
            ans2 += a[i] + cur2 + 1
            cun = -cur2 - 1
    # iが奇数のときプラス
    else:
        # プラスじゃないとき
        if a[i] + cur2 <= 0:
            ans2 += -(a[i] + cur2) + 1
            cun = -cur2 + 1

    cur2 += cun

print(min(ans1, ans2))

Submission status: Wrong Answer

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

ans1 = 0
ans2 = 0
cur = a[0]

for i in range(1, n):
    cun = a[i]
    # iが偶数のときプラス
    if i % 2 == 0:
        # プラスじゃないとき
        if a[i] + cur <= 0:
            ans1 += -(a[i] + cur) + 1
            cun = -cur + 1
    # iが奇数のときマイナス
    else:
        # マイナスじゃないとき
        if a[i] + cur >= 0:
            ans1 += a[i] + cur + 1
            cun = -cur - 1
    cur += cun

for i in range(1, n):
    cun = a[i]
    # iが偶数のときマイナス
    if i % 2 == 0:
        # マイナスじゃないとき
        if a[i] + cur >= 0:
            ans2 += a[i] + cur + 1
            cun = -cur - 1
    # iが奇数のときプラス
    else:
        # プラスじゃないとき
        if a[i] + cur <= 0:
            ans2 += -(a[i] + cur) + 1
            cun = -cur + 1

    cur += cun

print(min(ans1, ans2))

Submission status: Wrong Answer

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

ans = 0
cur = a[0]


if a[0] > 0:
    for i in range(1, n):
        cun = a[i]
        # iが偶数のときプラス
        if i % 2 == 0:
            # プラスじゃないとき
            if a[i] + cur <= 0:
                ans += -(a[i] + cur) + 1
                cun = -cur + 1
        # iが奇数のときマイナス
        else:
            # マイナスじゃないとき
            if a[i] + cur >= 0:
                ans += a[i] + cur + 1
                cun = -cur - 1
        cur += cun
else:
    for i in range(1, n):
        cun = a[i]
        # iが偶数のときマイナス
        if i % 2 == 0:
            # マイナスじゃないとき
            if a[i] + cur >= 0:
                ans += a[i] + cur + 1
                cun = -cur - 1
        # iが奇数のときプラス
        else:
            # プラスじゃないとき
            if a[i] + cur <= 0:
                ans += -(a[i] + cur) + 1
                cun = -cur + 1

        cur += cun

print(ans)

Submission status: Wrong Answer

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

def check(f):
        flag = f 
        t = 0 
        c = 0 
        for i in range(n):
                if t + a[i] <= 0 and flag == 1:
                        c += 1 - t - a[i]
                        t = 1 
                elif t + a[i] >= 0 and flag == -1: 
                        c += 1 + t + a[i]
                        t = -1
                else:
                        t += a[i] 
                flag *= -1
                print(t)
        return c 
    
total_p = check(1)
total_m = check(-1)
print(min(total_p, total_m))

Submission status: Wrong Answer

Solution code:
from numpy import sign
n = int(input())
A = [i for i in map(int,input().split())]
x = 0
ans = 0

if A[0] == 0:
  A[0] = -1 * sign(A[1])
  ans += 1
  
flag = 0
for i in A:
  x += i
  if flag == 0:
    flag = 1
    continue
  if x == 0:
    x += 1 * sign(i)
    ans += 1
  if sign(x) == sign(x-i):
    diff = abs(x - (x-i))
    x += -1*sign(x)*(diff + 1)
    ans += diff + 1
    if x == 0:
      x += 1 * sign(i)
      ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
rui = [0] * n
cnt = 0
if a[0] > a[1]:
    rui[0] = a[0]
else:
    if a[0] >= 0:
        rui[0] = -1
        cnt += a[0] + 1
    else:
        rui[0] = 1
        cnt += abs(a[0]) + 1

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

print(cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
currentSum = 0
count3 = 0
count4 = 0
        
currentSum = 0
for i in range(N):
    restSum = currentSum
    currentSum += A[i]
    if currentSum <= 0 and restSum < 0:
        count4 += abs(currentSum) + 1
        currentSum = 1
    elif currentSum >= 0 and restSum > 0:
        count4 += abs(currentSum) + 1
        currentSum = -1
    elif A[i] >= 0 and restSum == 0:
        count4 += abs(currentSum) + 1
        currentSum = -1
        
print(count4)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
currentSum = 0
count3 = 0
count4 = 0

currentSum = 0
for i in range(N):
    restSum = currentSum
    currentSum += A[i]
    if currentSum <= 0 and restSum < 0:
        count3 += abs(currentSum) + 1
        currentSum = 1
    elif currentSum >= 0 and restSum > 0:
        count3 += abs(currentSum) + 1
        currentSum = -1
    elif A[i] <= 0 and restSum == 0:
        count3 += abs(currentSum) + 1
        currentSum = 1
        
        
print(count3)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
currentSum = 0
count1 = 0
count2 = 0
count3 = 0
count4 = 0

for i in range(N):
    restSum = currentSum
    currentSum += A[i]
    if currentSum <= 0 and restSum < 0:
        count1 += abs(currentSum) + 1
        currentSum = 1
    elif currentSum >= 0 and restSum > 0:
        count1 += abs(currentSum) + 1
        currentSum = -1
    elif currentSum == 0 and restSum == 0:
        count1 += 1
        currentSum = -1

currentSum = 0
for i in range(N):
    restSum = currentSum
    currentSum += A[i]
    if currentSum <= 0 and restSum < 0:
        count2 += abs(currentSum) + 1
        currentSum = 1
    elif currentSum >= 0 and restSum > 0:
        count2 += abs(currentSum) + 1
        currentSum = -1
    elif currentSum == 0 and restSum == 0:
        count2 += 1
        currentSum = 1

currentSum = 0
for i in range(N):
    restSum = currentSum
    currentSum += A[i]
    if currentSum <= 0 and restSum < 0:
        count3 += abs(currentSum) + 1
        currentSum = 1
    elif currentSum >= 0 and restSum > 0:
        count3 += abs(currentSum) + 1
        currentSum = -1
    elif A[i] <= 0 and restSum == 0:
        count3 += abs(currentSum) + 1
        currentSum = 1
        
currentSum = 0
for i in range(N):
    restSum = currentSum
    currentSum += A[i]
    if currentSum <= 0 and restSum < 0:
        count4 += abs(currentSum) + 1
        currentSum = 1
    elif currentSum >= 0 and restSum > 0:
        count4 += abs(currentSum) + 1
        currentSum = -1
    elif A[i] >= 0 and restSum == 0:
        count4 += abs(currentSum) + 1
        currentSum = -1
        
print(count1, count2, count3, count4)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
currentSum = 0
count1 = 0
count2 = 0
count3 = 0
count4 = 0

for i in range(N):
    restSum = currentSum
    currentSum += A[i]
    if currentSum <= 0 and restSum < 0:
        count1 += abs(currentSum) + 1
        currentSum = 1
    elif currentSum >= 0 and restSum > 0:
        count1 += abs(currentSum) + 1
        currentSum = -1
    elif currentSum == 0 and restSum == 0:
        count1 += 1
        currentSum = -1

currentSum = 0
for i in range(N):
    restSum = currentSum
    currentSum += A[i]
    if currentSum <= 0 and restSum < 0:
        count2 += abs(currentSum) + 1
        currentSum = 1
    elif currentSum >= 0 and restSum > 0:
        count2 += abs(currentSum) + 1
        currentSum = -1
    elif currentSum == 0 and restSum == 0:
        count2 += 1
        currentSum = 1

currentSum = 0
for i in range(N):
    restSum = currentSum
    currentSum += A[i]
    if currentSum <= 0 and restSum < 0:
        count3 += abs(currentSum) + 1
        currentSum = 1
    elif currentSum >= 0 and restSum > 0:
        count3 += abs(currentSum) + 1
        currentSum = -1
    elif A[0] <= 0 and restSum == 0:
        count3 += 1
        currentSum = 1
        
currentSum = 0
for i in range(N):
    restSum = currentSum
    currentSum += A[i]
    if currentSum <= 0 and restSum < 0:
        count4 += abs(currentSum) + 1
        currentSum = 1
    elif currentSum >= 0 and restSum > 0:
        count4 += abs(currentSum) + 1
        currentSum = -1
    elif A[0] >= 0 and restSum == 0:
        count4 += 1
        currentSum = -1
        
print(count1, count2, count3, count4)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
currentSum = 0
count1 = 0
count2 = 0
count3 = 0
count4 = 0

for i in range(N):
    restSum = currentSum
    currentSum += A[i]
    if currentSum <= 0 and restSum < 0:
        count1 += abs(currentSum) + 1
        currentSum = 1
    elif currentSum >= 0 and restSum > 0:
        count1 += abs(currentSum) + 1
        currentSum = -1
    elif currentSum == 0 and restSum == 0:
        count1 += 1
        currentSum = -1

currentSum = 0
for i in range(N):
    restSum = currentSum
    currentSum += A[i]
    if currentSum <= 0 and restSum < 0:
        count2 += abs(currentSum) + 1
        currentSum = 1
    elif currentSum >= 0 and restSum > 0:
        count2 += abs(currentSum) + 1
        currentSum = -1
    elif currentSum == 0 and restSum == 0:
        count2 += 1
        currentSum = 1

currentSum = 0
for i in range(N):
    restSum = currentSum
    currentSum += A[i]
    if currentSum <= 0 and restSum < 0:
        count3 += abs(currentSum) + 1
        currentSum = 1
    elif currentSum >= 0 and restSum > 0:
        count3 += abs(currentSum) + 1
        currentSum = -1
    elif A[i] <= 0 and restSum == 0:
        count3 += 1
        currentSum = 1
        
currentSum = 0
for i in range(N):
    restSum = currentSum
    currentSum += A[i]
    if currentSum <= 0 and restSum < 0:
        count4 += abs(currentSum) + 1
        currentSum = 1
    elif currentSum >= 0 and restSum > 0:
        count4 += abs(currentSum) + 1
        currentSum = -1
    elif A[i] >= 0 and restSum == 0:
        count4 += 1
        currentSum = -1
        
print(min(count1, count2, count3, count4))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
currentSum = 0
count1 = 0
count2 = 0

for i in range(N):
    restSum = currentSum
    currentSum += A[i]
    if currentSum <= 0 and restSum < 0:
        count1 += abs(currentSum) + 1
        currentSum = 1
    elif currentSum >= 0 and restSum > 0:
        count1 += abs(currentSum) + 1
        currentSum = -1
    elif currentSum == 0 and restSum == 0:
        count1 += 1
        currentSum = -1
        
currentSum = 0
for i in range(N):
    restSum = currentSum
    currentSum += A[i]
    if currentSum <= 0 and restSum < 0:
        count2 += abs(currentSum) + 1
        currentSum = 1
    elif currentSum >= 0 and restSum > 0:
        count2 += abs(currentSum) + 1
        currentSum = -1
    elif currentSum == 0 and restSum == 0:
        count2 += 1
        currentSum = 1
        
print(min(count1, count2))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
currentSum = 0
count1 = 0
count2 = 0

for i in range(N):
    restSum = currentSum
    currentSum += A[i]
    if currentSum <= 0 and restSum < 0:
        count1 += abs(currentSum) + 1
        currentSum = 1
    elif currentSum >= 0 and restSum > 0:
        count1 += abs(currentSum) + 1
        currentSum = -1
    elif currentSum == 0 and restSum == 0:
        count1 += 1
        currentSum = -1
        
currentSum = 0
for i in range(N):
    restSum = currentSum
    currentSum += A[i]
    if currentSum <= 0 and restSum < 0:
        count2 += abs(currentSum) + 1
        currentSum = 1
    elif currentSum >= 0 and restSum > 0:
        count2 += abs(currentSum) + 1
        currentSum = -1
    elif currentSum == 0 and restSum == 0:
        count2 += 1
        currentSum = -1
        
print(min(count1, count2))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
currentSum = 0
count1 = 0
count2 = 0

for i in range(N):
    restSum = currentSum
    currentSum += A[i]
    if currentSum <= 0 and restSum < 0:
        count1 += abs(currentSum) + 1
        currentSum = 1
    elif currentSum >= 0 and restSum > 0:
        count1 += abs(currentSum) + 1
        currentSum = -1
    elif currentSum == 0 and restSum == 0:
        count1 += 1
        currentSum = -1
        
for i in range(N):
    restSum = currentSum
    currentSum += A[i]
    if currentSum <= 0 and restSum < 0:
        count2 += abs(currentSum) + 1
        currentSum = 1
    elif currentSum >= 0 and restSum > 0:
        count2 += abs(currentSum) + 1
        currentSum = -1
    elif currentSum == 0 and restSum == 0:
        count2 += 1
        currentSum = -1
        
print(min(count1, count2))

Submission status: Wrong Answer

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

for i in range(N):
    restSum = currentSum
    currentSum += A[i]
    if currentSum <= 0 and restSum < 0:
        count += abs(currentSum) + 1
        currentSum = 1
    elif currentSum >= 0 and restSum > 0:
        count += abs(currentSum) + 1
        currentSum = -1
    elif currentSum == 0 and restSum == 0 and A[i + 1] >= 0:
        count += abs(currentSum) + 1
        currentSum = -1
    elif currentSum == 0 and restSum == 0 and A[i + 1] <= 0:
        count += abs(currentSum) + 1
        currentSum = 1
print(count)

Submission status: Wrong Answer

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

for i in range(N):
    restSum = currentSum
    currentSum += A[i]
    if currentSum <= 0 and restSum < 0:
        count += abs(currentSum) + 1
        currentSum = 1
    elif currentSum >= 0 and restSum > 0:
        count += abs(currentSum) + 1
        currentSum = -1
print(count)

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    ans1 = 0
    S1 = [0] * N
    S1[0] = A[0]
    for i in range(1, N):
        S1[i] = S1[i-1] + A[i]
        if S1[i]*S1[i-1] < 0:
            continue
        if S1[i-1] > 0:
            S1[i] = -1
            ans1 += abs((-1) - (S1[i-1]+A[i]))
        else:
            S1[i] = 1
            ans1 += abs(1 - (S1[i-1]+A[i]))
    S2 = [0] * N
    ans2 = 0
    if A[0] > 0:
        S2[0] = -1
        ans2 += abs((-1) - A[0])
    else:
        S2[0] = 1
        ans2 += abs(1 - A[0])
    for i in range(1, N):
        S2[i] = S2[i-1] + A[i]
        if S2[i]*S2[i-1] < 0:
            continue
        if S2[i-1] > 0:
            S2[i] = -1
            ans2 += abs((-1) - (S2[i-1]+A[i]))
        else:
            S2[i] = 1
            ans2 += abs(1 - (S2[i-1]+A[i]))

    print(min(ans1, ans2))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    ans1 = 0
    S = [0] * N
    S[0] = A[0]
    for i in range(1, N):
        S[i] = S[i-1] + A[i]
        if S[i]*S[i-1] < 0:
            continue
        if S[i-1] > 0:
            S[i] = -1
            ans1 += abs((-1) - (S[i-1]+A[i]))
        else:
            S[i] = 1
            ans1 += abs(1 - (S[i-1]+A[i]))
    S2 = [0] * N
    ans2 = 0
    if A[0] > 0:
        S2[0] = -1
        ans2 += abs((-1) - A[0]) + A[0]
    else:
        S2[0] = 1
        ans2 += abs(1 - A[0]) + A[0]
    for i in range(1, N):
        S2[i] = S2[i-1] + A[i]
        if S2[i]*S2[i-1] < 0:
            continue
        if S2[i-1] > 0:
            S2[i] = -1
            ans2 += abs((-1) - (S2[i-1]+A[i]))
        else:
            S2[i] = 1
            ans2 += abs(1 - (S2[i-1]+A[i]))

    print(min(ans1, ans2))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    ans1 = 0
    S = [0] * N
    S[0] = A[0]
    for i in range(1, N):
        S[i] = S[i-1] + A[i]
        if S[i]*S[i-1] < 0:
            continue
        if S[i-1] > 0:
            S[i] = -1
            ans1 += abs((-1) - (S[i-1]+A[i]))
        else:
            S[i] = 1
            ans1 += abs(1 - (S[i-1]+A[i]))
    S2 = [0] * N
    ans2 = 0
    if A[0] > 0:
        S[0] = -1
        ans2 += abs((-1) - A[i])
    else:
        S[0] = 1
        ans2 += abs(1 - A[i])
    ans2 = 0
    for i in range(1, N):
        S2[i] = S2[i-1] + A[i]
        if S2[i]*S2[i-1] < 0:
            continue
        if S2[i-1] > 0:
            S2[i] = -1
            ans2 += abs((-1) - (S2[i-1]+A[i]))
        else:
            S2[i] = 1
            ans2 += abs(1 - (S2[i-1]+A[i]))

    print(min(ans1, ans2))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    ans = 0
    S = [0] * N
    S[0] = A[0]
    for i in range(1, N):
        S[i] = S[i-1] + A[i]
        if S[i]*S[i-1] < 0:
            continue
        if S[i-1] > 0:
            S[i] = -1
            ans += abs((-1) - (S[i-1]+A[i]))
        else:
            S[i] = 1
            ans += abs(1 - (S[i-1]+A[i]))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

c = 0
s = a[0]
fugo = a[0]

# print(a)

for i in range(1, n):
    s = s + a[i]
    if fugo * s >= 0:  # 符号が一緒だったら
        if a[i - 1] < 0:
            c += 1 - s
            s = 1
        else:
            c += s + 1
            s = -1
    fugo = s


print(c)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ans = 0
s = A[0]
if s == 0:
    for i in range(1, n):
        if A[i] > 0:
            s = -1
            ans += 1
            break
        elif A[i] < 0:
            s = 1
            ans += 1
            break
            
if s == 0:
    ans = n
else:
    for i in range(1, n):
        if s > 0:
            s += A[i]
            if s >= 0:
                ans += s+1
                s = -1
        elif s < 0:
            s += A[i]
            if s <= 0:
                ans -= s-1
                s = 1
                
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

n = input()
a_s = [int(x) for x in input().split()]
sum_i = 0
num = 0
for i in range(len(a_s)):
    a = a_s[i]
    new_sum_i = sum_i + a
#     print(f"a:{a}, tmp_sum_i:{new_sum_i}")
    if (new_sum_i == 0):
        if sum_i == 0:
            new_sum_i = 1 if a_s[i+1] == 0 else np.sign(a_s[i+1])*(-1)
        else:
            new_sum_i = np.sign(sum_i)*(-1)
        adjusted_a = new_sum_i - sum_i
        num += abs(max(adjusted_a, a) - min(adjusted_a, a))
        sum_i = new_sum_i
    elif new_sum_i * sum_i > 0:
        new_sum_i = np.sign(new_sum_i)*-1
        adjusted_a = new_sum_i - sum_i
        num += abs(max(adjusted_a, a) - min(adjusted_a, a))
        sum_i = new_sum_i
    else:
        sum_i = new_sum_i
#     print(f"a:{a}, new_sum_i:{sum_i}, num:{num}")
#     print()
    
print(num)

Submission status: Wrong Answer

Solution code:
import numpy as np

n = input()
a_s = [int(x) for x in input().split()]
sum_i = 0
num = 0
for a in a_s:
    new_sum_i = sum_i + a
#     print(f"a:{a}, tmp_sum_i:{new_sum_i}")
    if (new_sum_i == 0):
            new_sum_i = 1 if sum_i == 0 else np.sign(sum_i)*(-1)
            adjusted_a = new_sum_i - sum_i
            num += abs(max(adjusted_a, a) - min(adjusted_a, a))
            sum_i = new_sum_i
    elif new_sum_i * sum_i > 0:
        new_sum_i = np.sign(new_sum_i)*-1
        adjusted_a = new_sum_i - sum_i
        num += abs(max(adjusted_a, a) - min(adjusted_a, a))
        sum_i = new_sum_i
    else:
        sum_i = new_sum_i
#     print(f"a:{a}, new_sum_i:{sum_i}, num:{num}")
#     print()

print(num)

Submission status: Wrong Answer

Solution code:
import numpy as np

n = input()
a_s = [int(x) for x in input().split()]

sum_i = 0
manipulate_num = 0
for a in a_s:
    new_sum_i = sum_i + a
#     print(f"a:{a}, sum_i:{sum_i}, tmp_sum_i:{new_sum_i}")
    if new_sum_i == 0:
        manipulate_num += 1
        new_sum_i = 1
    if new_sum_i * sum_i > 0:
        new_sum_i = np.sign(new_sum_i)*(-1)
        manipulate_num += abs(a - (new_sum_i - sum_i))
    sum_i = new_sum_i
#     print(f"a:{a}, new_sum_i:{sum_i}, num:{manipulate_num}")
#     print()
    
print(manipulate_num)

Submission status: Wrong Answer

Solution code:
import numpy as np

n = input()
a_s = [int(x) for x in input().split()]

sum_i = 0
manipulate_num = 0
for a in a_s:
    new_sum_i = sum_i + a
#     print(f"a:{a}, sum_i:{sum_i}, new_sum_i:{new_sum_i}")
    if new_sum_i == 0:
        manipulate_num += 1
    if new_sum_i * sum_i > 0:
        manipulate_num += abs(a)+1
        new_sum_i = np.sign(new_sum_i)*(-1)
    sum_i = new_sum_i
#     print(f"a:{a}, sum_i:{sum_i}")
#     print()
    
print(manipulate_num)

Submission status: Wrong Answer

Solution code:
import numpy as np

n = input()
a_s = [int(x) for x in input().split()]

sum_i = 0
manipulate_num = 0
for a in a_s:
    new_sum_i = sum_i + a
#     print(f"a:{a}, sum_i:{sum_i}, new_sum_i:{new_sum_i}")
    if new_sum_i == 0:
        manipulate_num += 1
    if new_sum_i * sum_i > 0:
        manipulate_num += abs(new_sum_i)+1
        new_sum_i -= np.sign(new_sum_i)*(abs(new_sum_i)+1)
    sum_i = new_sum_i
#     print(f"a:{a}, sum_i:{sum_i}")
#     print()
    
print(manipulate_num)

Submission status: Wrong Answer

Solution code:
n=int(input())
l=list(map(int,input().split()))
l.append(0)
a1=0
b1=0
a2=0
b2=0
for i in range(n):
    b1+=l[i]
    b2+=l[i]
    if i%2==0:
        if b1>=0:
            a1+=abs(-1-b1)
            b1=-1-l[i+1]
        if b2<=0:
            a2+=abs(1-b2)
            b2=1-l[i+1]
    else:
        if b1<=0:
            a1+=abs(1-b1)
            b1=1-l[i+1]
        if b2>=0:
            a2+=abs(-1-b2)
            b2=-1-l[i+1]
print(min(a1,a2))

Submission status: Wrong Answer

Solution code:
#例の答えはあってる。
n=int(input())
x=list(map(int,input().split()))
k=x[0]
s=""
if x[0]>0:
  s="True"
else:
  s="Folus"
l=0
for i in range(1,n):
  k+=x[i]
  
  if k>=0 and s=="True":
    l+=k+1
    k=-1
    s="Folus"
  elif k<=0 and s=="Folus":
    l+=k*(-1)+1
    k=1
    s="True"
  else:
    if s=="True":
      s="Folus"
    else:
      s="True"
print(l)

Submission status: Wrong Answer

Solution code:
n=int(input())
x=list(map(int,input().split()))
k=x[0]
s=""
if x[0]>0:
  s="True"
else:
  s="Folus"
l=0
for i in range(1,n):
  k+=x[i]
  
  if k>=0 and s=="True":
    l+=k+1
    k=-1
    s="Folus"
  elif k<=0 and s=="Folus":
    l+=k*(-1)+1
    k=1
    s="True"
  else:
    if s=="True":
      s="Folus"
    else:
      s="True"
print(l)

Submission status: Wrong Answer

Solution code:
n = int(input())
original_A = [int(x) for x in input().split()]

ans = []

for k in range(2):
    A = original_A.copy()
    count = 0
    
    if k%2:
        if A[0] >= 0:
            A[0] = -1
            count += (abs(A[0]) +1)
    else:
        if A[0] < 0:
            A[0] = 1
            count += (abs(A[0]) +1)

    sum_before = A[0]
    #print('***', k, A, '***')
    for i in range(n):
        if i == 0:
            continue
        sum_for_i = sum_before + A[i]
        #print('[',i,']: before',sum_before,'after',sum_for_i, 'before', A)
        if sum_for_i == 0 and sum_before > 0:
            #print("case 1")
            A[i] -= 1
            count += 1
        elif sum_for_i == 0 and sum_before <0:
            #print("case 2")
            A[i] += 1
            count += 1
        elif sum_before >0 and sum_for_i>0:
            #print("case 3")
            count += (abs(sum_for_i)+1)
            A[i] -= (abs(sum_for_i)+1)
        elif sum_before <0 and sum_for_i<0:
            #print("case 4")
            count += (abs(sum_for_i)+1)
            A[i] += (abs(sum_for_i)+1)
        #print('[',i,']: ','modified', A, 'count', count)
        sum_before += A[i]
    ans.append(count)
print(min(ans))

Submission status: Wrong Answer

Solution code:
n = int(input())
original_A = [int(x) for x in input().split()]

ans = []

for k in range(2):
    A = original_A.copy()
    count = 0
    
    if not k%2:
        if A[0] >= 0:
            A[0] = -1
            count += (abs(A[0]) +1)
    else:
        if A[0] < 0:
            A[0] = 1
            count += (abs(A[0]) +1)

    sum_before = A[0]
    #print('***', k, A, '***')
    for i in range(n):
        if i == 0:
            continue
        sum_for_i = sum_before + A[i]
        #print('[',i,']: before',sum_before,'after',sum_for_i, 'before', A)
        if sum_for_i == 0 and sum_before > 0:
            #print("case 1")
            A[i] -= 1
            count += 1
        elif sum_for_i == 0 and sum_before <0:
            #print("case 2")
            A[i] += 1
            count += 1
        elif sum_before >0 and sum_for_i>0:
            #print("case 3")
            count += (abs(sum_for_i)+1)
            A[i] -= (abs(sum_for_i)+1)
        elif sum_before <0 and sum_for_i<0:
            #print("case 4")
            count += (abs(sum_for_i)+1)
            A[i] += (abs(sum_for_i)+1)
        #print('[',i,']: ','modified', A, 'count', count)
        sum_before += A[i]
    ans.append(count)
print(min(ans))

Submission status: Wrong Answer

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

count = 0
sum_before = A[0]
for i in range(n):
    if i == 0:
        continue
    sum_for_i = sum_before + A[i]
    #print('[',i,']: before',sum_before,'after',sum_for_i, A)
    if sum_for_i == 0 and sum_before > 0:
        #print("case 1")
        A[i] -= 1
        count += 1
    elif sum_for_i == 0 and sum_before <0:
        #print("case 2")
        A[i] += 1
        count += 1
    elif sum_before >0 and sum_for_i>0:
        #print("case 3")
        count += (abs(sum_for_i)+1)
        A[i] -= (abs(sum_for_i)+1)
    elif sum_before <0 and sum_for_i<0:
        #print("case 4")
        count += (abs(sum_for_i)+1)
        A[i] += (abs(sum_for_i)+1)
    #print('[',i,']: ',A, 'count', count)
    sum_before += A[i]
print(count)

Submission status: Wrong Answer

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

count = 0
sum_before = A[0]
for i in range(n):
    if i == 0:
        continue
    sum_for_i = sum_before + A[i]
    #print('[',i,']: before',sum_before,'after',sum_for_i, A)
    if sum_for_i == 0 and sum_before > 0:
        #print("case 1")
        A[i] -= 1
        count += 1
    elif sum_for_i == 0 and sum_before <0:
        #print("case 2")
        A[i] += 1
        count += 1
    elif sum_before >0 and sum_for_i>0:
        #print("case 3")
        count += (abs(A[i])+1)
        A[i] -= (abs(A[i])+1)
    elif sum_before <0 and sum_for_i<0:
        #print("case 4")
        count += (abs(A[i])+1)
        A[i] += (abs(A[i])+1)
    #print('[',i,']: ',A, 'count', count)
    sum_before += A[i]
print(count)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
import copy

def sol(acmA, sign=1):
  add = 0
  ans = 0
  sumA = acmA[0]
  for i in range(1,N):
    acmA[i] += add
    if sign == 1:
      if (i%2==0 and acmA[i-1]<0 and 0<acmA[i]) or (i%2==1 and 0<acmA[i-1] and acmA[i]<0) :continue
    else:
      if (i%2==1 and acmA[i-1]<0 and 0<acmA[i]) or (i%2==0 and 0<acmA[i-1] and acmA[i]<0) :continue
  
    tmp_add = -acmA[i]-1 if acmA[i] > 0 else -acmA[i]+1
    #print(i, tmp_add, acmA[i])
    acmA[i] += tmp_add
    sumA += acmA[i]
    add +=  tmp_add
    ans +=abs(tmp_add)
  if sumA == 0: return ans +1
  else: return ans

N = int(input())
A = list(map(int,input().split()))
acmA = list(accumulate(A))
acmA2 = copy.deepcopy(acmA)
print(min(sol(acmA, 1),sol(acmA2,-1)))
#print(acmA)