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()))
L = [0 for _ in range(n)]
L[0] = a[0]
for i in range(1, n):
  L[i] = L[i-1] + a[i]
delay = 0
all_over = 0
if a[0] != 0:
  sign = (a[0] > 0) - (a[0] < 0)
  for i in range(1, n):
    L[i] += delay
    if L[i] <= 0 and sign == -1:
      delay += 1 - L[i]
      all_over += 1 - L[i]
      L[i] = 1
    elif L[i] >= 0 and sign == 1:
      delay -= L[i] + 1
      all_over += L[i] + 1
      L[i] = -1
    sign *= -1
  print(all_over)
else:
  posL = L[:]
  negL = L[:]
  pos_delay, neg_delay = 1, -1
  pos_all_over, neg_all_over = 1, 1
  sign = 1
  for i in range(1, n):
    posL[i] += pos_delay
    if posL[i] <= 0 and sign == -1:
      pos_delay += 1 - posL[i]
      pos_all_over += 1 - posL[i]
      posL[i] = 1
    elif posL[i] >= 0 and sign == 1:
      pos_delay -= posL[i] + 1
      pos_all_over += posL[i] + 1
      posL[i] = -1
    sign *= -1
  sign = -1
  for i in range(1, n):
    negL[i] += neg_delay
    if negL[i] <= 0 and sign == -1:
      neg_delay += 1 - negL[i]
      neg_all_over += 1 - negL[i]
      negL[i] = 1
    elif negL[i] >= 0 and sign == 1:
      neg_delay -= negL[i] + 1
      neg_all_over += negL[i] + 1
      negL[i] = -1
    sign *= -1
  print(pos_all_over, min(pos_all_over, neg_all_over))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
L = [0 for _ in range(n)]
L[0] = a[0]
for i in range(1, n):
  L[i] = L[i-1] + a[i]
delay = 0
all_over = 0
if a[0] != 0:
  sign = (a[0] > 0) - (a[0] < 0)
  for i in range(1, n):
    L[i] += delay
    if L[i] <= 0 and sign == -1:
      delay += 1 - L[i]
      all_over += 1 - L[i]
      L[i] = 1
    elif L[i] >= 0 and sign == 1:
      delay -= L[i] + 1
      all_over += L[i] + 1
      L[i] = -1
    sign *= -1
  print(all_over)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
L = [0 for _ in range(n)]
L[0] = a[0]
for i in range(1, n):
  L[i] = L[i-1] + a[i]
delay = 0
all_over = 0
sign = (a[0] > 0) - (a[0] < 0)
for i in range(1, n):
  L[i] += delay
  if L[i] <= 0 and sign == -1:
    delay += 1 - L[i]
    all_over += 1 - L[i]
    L[i] = 1
  elif L[i] >= 0 and sign == 1:
    delay -= L[i] + 1
    all_over += L[i] + 1
    L[i] = -1
  sign *= -1
print(all_over)

Submission status: Wrong Answer

Solution code:
from itertools import takewhile
n, *A = map(int, open(0).read().split())
k = len(list(takewhile(lambda a: a == 0, A)))
if k == n:
  print((k+1)*k//2)
  exit()
if A[k] > 0:
  C = [-1]
else:
  C = [1]
B = [0]
D = [(k+1)*k//2]

for i in range(k, n):
  if (C[-1] + A[i]) * C[-1] < 0:
    B.append(A[i])
    C.append(C[-1] + A[i])
    D.append(D[-1])
  else:
    if C[-1] > 0:
      B.append(-(C[-1]+1))
      D.append(D[-1] + C[-1]+1 + A[i])
      C.append(-1)
    else:
      B.append(-C[-1]+1)
      D.append(D[-1] + -C[-1]+1 - A[i])
      C.append(1)
print(D[-1])
# print(A[k-1:])
# print(B)
# print(C)
# print(D)

Submission status: Wrong Answer

Solution code:
n, *A = map(int, open(0).read().split())
B = [A[0]]
C = [A[0]]
D = [0]
for i in range(1, n):
  if (C[i-1] + A[i]) * C[i-1] < 0:
    B.append(A[i])
    C.append(C[i-1] + A[i])
    D.append(D[i-1])
  else:
    if C[i-1] > 0:
      B.append(-(C[i-1]+1))
      C.append(-1)
      D.append(D[i-1] + C[i-1]+1 + A[i])
    else:
      B.append(-C[i-1]+1)
      C.append(1)
      D.append(D[i-1] + -C[i-1]+1 - A[i])
print(D[-1])

Submission status: Wrong Answer

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

ans = 0
previous_sum = A[0] # total to i - 1
for i in range(1, N):
  # if prev_sum is plus and A[i] is more minus than prev_sum, no action.
  # if prev_sum is plus and minus of A[i] is larger than or equal to prev_sum.
  if previous_sum > 0 and previous_sum + A[i] >= 0:
    # the number that need to be subtracted to meet the requirements.
    require_subtraction = previous_sum + A[i] + 1
    # correcting the current number.
    A[i] -= require_subtraction
    ans += require_subtraction
  # if prev_sum is minus and A[i] is more plus than prev_sum, no action.
  # if prev_sum is minus and minus of A[i] is more smaller than or equal to prev_sum.
  elif previous_sum < 0 and previous_sum + A[i] <= 0:
    # the number that need to be added to meet the requirements.
    require_addition = -(previous_sum + A[i] - 1)
    # correcting the current number.
    A[i] += require_addition
    ans += require_addition
  previous_sum += A[i]

print(ans)

Submission status: Wrong Answer

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

ans = 0
previous_sum = A[0] # total to i - 1
for i in range(1, N):
  # if prev_sum is plus and A[i] is more minus than prev_sum, no action.
  # if prev_sum is plus and minus of A[i] is larger than or equal to prev_sum.
  if previous_sum > 0 and previous_sum + A[i] >= 0:
    # the number that need to be subtracted to meet the requirements.
    require_subtraction = previous_sum + A[i] + 1
    # the number of wastes in meeting the requirement.
    distance_from_ideal = previous_sum - 1
    # restration to an ideal list.
    A[i - 1] -= distance_from_ideal
    previous_sum -= distance_from_ideal
    ans += distance_from_ideal
    # adjustment for the manupilation of the previous values.
    require_subtraction -= distance_from_ideal
    # correcting the current number.
    A[i] -= require_subtraction
    ans += require_subtraction
  # if prev_sum is minus and A[i] is more plus than prev_sum, no action.
  # if prev_sum is minus and minus of A[i] is more smaller than or equal to prev_sum.
  elif previous_sum < 0 and previous_sum + A[i] <= 0:
    # the number that need to be added to meet the requirements.
    require_addition = -(previous_sum + A[i] - 1)
    # the number of wastes in meeting the requirement.
    distance_from_ideal = -1 - previous_sum 
    # restration to an ideal list.
    A[i - 1] += distance_from_ideal
    previous_sum += distance_from_ideal
    ans += distance_from_ideal
    # adjustment for the manupilation of the previous values.
    require_addition -= distance_from_ideal
    # correcting the current number.
    A[i] += require_addition
    ans += require_addition
  previous_sum += A[i]

print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# hello worldと表示する
import sys
import numpy
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial
from operator import itemgetter
from copy import deepcopy
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

n=I()
seq=LI()
if seq[0]>0:
    pm=-1
    sm=seq[0]
    count=0
    for i in range(n-1):
        sm=sm+seq[i+1]
        if pm>0:
            if sm<=0:
                count+=1-sm
                sm=1
            pm=-1
        else:
            if sm>=0:
                count+=1+sm
                sm=-1
            pm=1
    print(count)
elif seq[0]<0:
    pm=1
    sm=seq[0]
    count=0
    for i in range(n-1):
        sm=sm+seq[i+1]
        if pm>0:
            if sm<=0:
                count+=1-sm
                sm=1
            pm=-1
        else:
            if sm>=0:
                count+=1+sm
                sm=-1
            pm=1
    print(count)
else:
    pm=-1
    sm=1
    count=1
    for i in range(n-1):
        sm=sm+seq[i+1]
        if pm>0:
            if sm<=0:
                count+=1-sm
                sm=1
            pm=-1
        else:
            if sm>=0:
                count+=1+sm
                sm=-1
            pm=1
    count1=count
    pm=1
    sm=-1
    count=1
    for i in range(n-1):
        sm=sm+seq[i+1]
        if pm>0:
            if sm<=0:
                count+=1-sm
                sm=1
            pm=-1
        else:
            if sm>=0:
                count+=1+sm
                sm=-1
            pm=1
    count2=count
    print(min(count1,count2))

Submission status: Wrong Answer

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

ans = 0
prev_sm = A[0] # total to i - 1
for i in range(1, N):
  # if prev_sum is plus and a is more minus than prev_sum.
  if prev_sm > 0 and prev_sm + A[i] < 0:
    prev_sm += A[i]
    continue
  # if prev_sum is plus and a is larger than or equal to prev_sum.
  elif prev_sm > 0 and prev_sm + A[i] >= 0:
    diff = prev_sm + A[i] + 1
    prev_sm += A[i]
    ans += diff
    A[i] -= diff
  # if prev_sum is minus and a is more plus than prev_sum.
  elif prev_sm < 0 and prev_sm + A[i] > 0:
    prev_sm += A[i]
    continue
  # if prev_sum is minus and a is more smaller than or equal to prev_sum.
  elif prev_sm < 0 and prev_sm + A[i] <= 0:
    diff = -(prev_sm + A[i] - 1)
    prev_sm += A[i]
    ans += diff
    A[i] += diff

print(ans)

Submission status: Wrong Answer

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

    res = 0
    sums = []

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

ans = 0
prev_sm = A[0] # total to i - 1
for i in range(1, N):
  # if prev_sum is plus and a is more minus than prev_sum.
  if prev_sm > 0 and prev_sm + A[i] < 0:
    prev_sm += A[i]
    continue
  # if prev_sum is plus and a is larger than or equal to prev_sum.
  elif prev_sm > 0 and prev_sm + A[i] >= 0:
    ans += prev_sm + A[i] + 1
    A[i] -= prev_sm + A[i] + 1
    prev_sm += A[i]
  # if prev_sum is minus and a is more plus than prev_sum.
  elif prev_sm < 0 and prev_sm + A[i] > 0:
    prev_sm += A[i]
    continue
  # if prev_sum is minus and a is more smaller than or equal to prev_sum.
  elif prev_sm < 0 and prev_sm + A[i] <= 0:
    ans += -(prev_sm + A[i] - 1)
    A[i] += -(prev_sm + A[i] + 1)
    prev_sm += A[i]

print(ans)

Submission status: Wrong Answer

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

ans = 0
prev_sm = A[0] # total to i - 1
for i in range(1, N):
  # if prev_sum is plus and a is more minus than prev_sum.
  if prev_sm > 0 and prev_sm + A[i] < 0:
    pass
  # if prev_sum is plus and a is larger than or equal to prev_sum.
  elif prev_sm > 0 and prev_sm + A[i] >= 0:
    ans += prev_sm + A[i] + 1
    A[i] -= prev_sm + A[i] + 1
  # if prev_sum is minus and a is more plus than prev_sum.
  elif prev_sm < 0 and prev_sm + A[i] > 0:
    pass
  # if prev_sum is minus and a is more smaller than or equal to prev_sum.
  elif prev_sm < 0 and prev_sm + A[i] <= 0:
    ans += -(prev_sm + A[i] - 1)
    A[i] += -(prev_sm + A[i] + 1)
  prev_sm += A[i]

print(ans)

Submission status: Wrong Answer

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

    res = 0
    sums = []

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

    res = 0
    sums = []

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
cnt = 0
s = a[0]

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

print(cnt)

Submission status: Wrong Answer

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


@lru_cache(maxsize=None)
def cost_e():
    res = 0
    sum = 0
    for j, y in enumerate(s, 1):
        tmp = sum + y
        if j & 1 and tmp >= 0:
            sum = -1
            res += abs(tmp) + 1
        elif not j & 1 and tmp <= 0:
            sum = 1
            res += abs(tmp) + 1
        else:
            sum = tmp
    return res


@lru_cache(maxsize=None)
def cost_o():
    res = 0
    sum = 0
    for j, y in enumerate(s, 1):
        tmp = sum + y
        if j & 1 and tmp <= 0:
            sum = 1
            res += abs(tmp) + 1
        elif not j & 1 and tmp >= 0:
            sum = -1
            res += abs(tmp) + 1
        else:
            sum = tmp
    return res


print(cost_e(), cost_o())

Submission status: Wrong Answer

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


@lru_cache(maxsize=None)
def cost_e():
    res = 0
    sum = 0
    for j, y in enumerate(s, 1):
        tmp = sum + y
        if j & 1 and tmp >= 0:
            sum = -1
            res += abs(tmp) + 1
        elif not j & 1 and tmp <= 0:
            sum = 1
            res += abs(tmp) + 1
        else:
            sum = tmp
    return res


@lru_cache(maxsize=None)
def cost_o():
    res = 0
    sum = 0
    for j, y in enumerate(s, 1):
        tmp = sum + y
        if j & 1 and tmp <= 0:
            sum = -1
            res += abs(tmp) + 1
        elif not j & 1 and tmp >= 0:
            sum = 1
            res += abs(tmp) + 1
        else:
            sum = tmp
    return res


print(min(cost_e(), cost_o()))

Submission status: Wrong Answer

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


@lru_cache(maxsize=None)
def cost(z):
    res = abs(z - s[0])
    sum = 0
    for j, y in enumerate(s):
        tmp = sum + y
        if j == 0:
            sum = tmp
            continue
        if sum * tmp >= 0:
            c = -1 if tmp > 0 else 1
            x = c - tmp
            res += abs(x)
            sum = c
        else:
            sum = tmp
    return res


ans1 = cost(s[0])
ans2 = cost(1)
ans3 = cost(-1)
print(min(ans1, ans2, ans3))

Submission status: Wrong Answer

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

#マイナススタート
now = 0
count = 0
for i,j in enumerate(a):
    now += j
    if i%2 == 0:
        if now >= 0:
            count += abs(now)+1
            now = -1
    else:
        if now < 0:
            count += abs(now)+1
            now = 1

#プラススタート
now = 0
count2 = 0
for i,j in enumerate(a):
    now += j
    if i%2 == 0:
        if now < 0:
            count2 += abs(now)+1
            now = 1
    else:
        if now >= 0:
            count2 += abs(now)+1
            now = -1

print(min(count,count2))

Submission status: Wrong Answer

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

sm = a[0]


def f(sm):
    ret = 0
    for e in a[1:]:
        if sm * (sm + e) < 0:
            sm += e
            continue

        else:
            if sm > 0:
                a_mx = -sm - 1
                ret += e - a_mx
                sm += a_mx
            else:
                a_mn = -sm + 1
                ret += a_mn - e
                sm += a_mn

    return ret


if sm > 0:
    ans = min(f(sm), f(-1) + sm + 1)
elif sm < 0:
    ans = min(f(sm), f(1) + sm + 1)
else:
    ans = min(f(1), f(-1)) + 1

print(ans)

Submission status: Wrong Answer

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

sm = a[0]


def f(sm):
    ret = 0
    for e in a[1:]:
        if sm * (sm + e) < 0:
            sm += e
            continue

        else:
            if sm > 0:
                a_mx = -sm - 1
                ret += e - a_mx
                sm += a_mx
            else:
                a_mn = -sm + 1
                ret += a_mn - e
                sm += a_mn

    return ret


if sm != 0:
    ans = f(sm)
else:
    ans = min(f(1), f(-1)) + 1

print(ans)

Submission status: Wrong Answer

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

B = [0]*(n+1)
ans = 0
for i in range(n):
    temp = B[i]+A[i]
    if i == 0:
        if temp == 0:
            ans += 1
            temp += 1
            B[i+1] = temp
        else:
            B[i+1] =temp
    else:
        if B[i]*temp > 0:
            if temp < 0:
                ans += 1-temp
                B[i+1] = 1
            else:
                ans += temp+1
                B[i+1] = -1
        elif temp == 0:
            if B[i] > 0:
                ans += 1
                B[i+1] = -1
            else:
                ans += 1
                B[i+1] = 1
        else:
            B[i+1] = temp

ans_ = 0
for i in range(n):
    temp = B[i]+A[i]
    if i == 0:
        if temp == 0:
            ans_ += 1
            temp -= 1
            B[i+1] = temp
        else:
            B[i+1] =temp
    else:
        if B[i]*temp > 0:
            if temp < 0:
                ans_ += 1-temp
                B[i+1] = 1
            else:
                ans_ += temp+1
                B[i+1] = -1
        elif temp == 0:
            if B[i] > 0:
                ans_ += 1
                B[i+1] = -1
            else:
                ans_ += 1
                B[i+1] = 1
        else:
            B[i+1] = temp
print(min(ans, ans_))

Submission status: Wrong Answer

Solution code:

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


ans1 = 0
curr_sum = al[0]
next_sign = 1
for a in al[1:]:
    next_sum = curr_sum + a
    if next_sum >= 0 and next_sign == -1: 
        ans1 += next_sum+1
        next_sum = -1
    elif next_sum <= 0 and next_sign == 1:
        ans1 += (1-next_sum)
        next_sum = 1
    curr_sum = next_sum
    next_sign *= -1


ans2 = 0
curr_sum = al[0]
next_sign = -1
for a in al[1:]:
    next_sum = curr_sum + a
    if next_sum >= 0 and next_sign == -1: 
        ans2 += next_sum+1
        next_sum = -1
    elif next_sum <= 0 and next_sign == 1:
        ans2 += (1-next_sum)
        next_sum = 1
    curr_sum = next_sum
    next_sign *= -1

print(min(ans1,ans2))

Submission status: Wrong Answer

Solution code:

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


ans1 = al[0]
curr_sum = 0
next_sign = 1
for a in al[1:]:
    next_sum = curr_sum + a
    if next_sum >= 0 and next_sign == -1: 
        ans1 += next_sum+1
        next_sum = -1
    elif next_sum <= 0 and next_sign == 1:
        ans1 += (1-next_sum)
        next_sum = 1
    curr_sum = next_sum
    next_sign *= -1


ans2 = al[0]
curr_sum = 0
next_sign = -1
for a in al[1:]:
    next_sum = curr_sum + a
    if next_sum >= 0 and next_sign == -1: 
        ans2 += next_sum+1
        next_sum = -1
    elif next_sum <= 0 and next_sign == 1:
        ans2 += (1-next_sum)
        next_sum = 1
    curr_sum = next_sum
    next_sign *= -1

print(min(ans1,ans2))

Submission status: Wrong Answer

Solution code:

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

if al[0] == 0:
    ans1 = 0
    ans1 += 1
    curr_sum = 1
    for a in al[1:]:
        next_sum = curr_sum + a
        if next_sum >= 0 and curr_sum > 0:
            ans1 += next_sum+1
            next_sum = -1
        elif next_sum <= 0 and curr_sum < 0:
            ans1 += (1-next_sum)
            next_sum = 1

    ans2 = 0
    ans2 += 1
    curr_sum = 1
    for a in al[1:]:
        next_sum = curr_sum + a
        if next_sum >= 0 and curr_sum > 0:
            ans2 += next_sum+1
            next_sum = -1
        elif next_sum <= 0 and curr_sum < 0:
            ans2 += (1-next_sum)
            next_sum = 1

    print(min(ans1,ans2))

else:
    ans3 = 0
    curr_sum = al[0]
    for a in al[1:]:
        next_sum = curr_sum + a
        if next_sum >= 0 and curr_sum > 0:
            ans3 += next_sum+1
            next_sum = -1
        elif next_sum <= 0 and curr_sum < 0:
            ans3 += (1-next_sum)
            next_sum = 1
        curr_sum = next_sum
    print(ans3)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

n = int(input())
lst = [int(x) for x in input().split()]
acm = list(accumulate(lst))

ret1 = 0
tmp = 0
for i, x in enumerate(acm):
    y = abs(x) + 1
    if i % 2 == 0:
        if x + tmp <= 0:
            ret1 += (x + tmp) * -1 + 1
            tmp += y
    elif i % 2 == 1:
        if x + tmp >= 0:
            ret1 += (x + tmp) + 1
            tmp -= y

ret2 = 0
tmp = 0
for i, x in enumerate(acm):
    y = abs(x) + 1
    if i % 2 == 0:
        if x + tmp >= 0:
            ret2 += (x + tmp) + 1
            tmp -= y
    elif i % 2 == 1:
        if x + tmp <= 0:
            ret2 += (x + tmp) * -1 + 1
            tmp += y

print(min(ret1, ret2))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)


#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

#####MakePrimes######
def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum

#####GCD#####
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

#############
# Main Code #
#############

N = I()
A = IL()

s = 0
count1 = 0
for i in range(N):
  s += A[i]
  if i%2:
    if s>-1:
      count1 += s+1
      s = -1
  else:
    if s<1:
      count1 += 1-s
      s = 1

      
s = A[0]
count2 = 0
for i in range(N):
  s += A[i]
  if i%2==0:
    if s>-1:
      count2 += s+1
      s = -1
  else:
    if s<1:
      count2 += 1-s
      s = 1

print(min(count1,count2))