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:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    a = input().split()
    a = list(map(lambda x: x.capitalize(), a))
    a,b,c = a
    print(a[0]+b[0]+c[0])
    return

#B
def B():
    a = II()
    b = II()
    if a > b:
        print("GREATER")
    if a < b:
        print("LESS")
    if a == b:
        print("EQUAL")
    return

#C
def C():
    n = II()
    a = LI()
    if a[0] == 0:
        suma = 1
        b = 1
    else:
        suma = a[0]
        b = 0
    for i in a[1:]:
        if (suma + i) * suma < 0:
            suma += i
            continue
        b += abs(suma + i) + 1
        suma = -1 * (suma > 0) or 1
    ans = b
    if a[0] == 0:
        suma = -1
        b = 1
    else:
        suma = -a[0]
        b = 2 * abs(a[0])
    for i in a[1:]:
        if (suma + i) * suma < 0:
            suma += i
            continue
        suma = -1 * (suma > 0) or 1
        b += abs(suma + i) + 1
    print(min(ans,b))
    return

#D
def D():
    s = S()
    for i in range(len(s) - 1):
        if s[i] == s[i+1]:
            print(i + 1, i + 2)
            return
    for i in range(len(s) - 2):
        if s[i] == s[i + 2]:
            print(i + 1, i + 3)
            return
    print(-1, -1)

    return


#Solve
if __name__ == '__main__':
    C()

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    a = input().split()
    a = list(map(lambda x: x.capitalize(), a))
    a,b,c = a
    print(a[0]+b[0]+c[0])
    return

#B
def B():
    a = II()
    b = II()
    if a > b:
        print("GREATER")
    if a < b:
        print("LESS")
    if a == b:
        print("EQUAL")
    return

#C
def C():
    n = II()
    a = LI()
    if a[-1] == 0:
        suma = 1
    else:
        suma = a[0]
    b = 0
    for i in a[1:]:
        if (suma + i) * suma < 0:
            suma += i
            continue
        b += abs(suma + i) + 1
        suma = -1 * (suma > 0) or 1
    ans = b
    if a[-1] == 0:
        suma = -1
    else:
        suma = -a[0]
    b = 0
    for i in a[1:]:
        if (suma + i) * suma < 0:
            suma += i
            continue
        suma = -1 * (suma > 0) or 1
        b += abs(suma + i) + 1
    print(min(ans,b))
    return

#D
def D():
    s = S()
    for i in range(len(s) - 1):
        if s[i] == s[i+1]:
            print(i + 1, i + 2)
            return
    for i in range(len(s) - 2):
        if s[i] == s[i + 2]:
            print(i + 1, i + 3)
            return
    print(-1, -1)

    return


#Solve
if __name__ == '__main__':
    C()

Submission status: Wrong Answer

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

res = 0
former = 0
s = 0
if A[0] == 0:
    if A[1] >= 0:
        A[0] = -1
    else:
        A[0] = 1
    res += 1
    
for i in range(N):
    former = s
    s += A[i]
    if i == 0:
        pass
    else:
        if s == 0:
            if former > 0:
                s -= 1
                res += 1
            elif former < 0:
                s += 1
                res += 1
        elif former*s > 0:
            if s > 0:
                res += s+1
                s = -1
            else:
                res += -s+1
                s = 1
    #print(s)
print(res)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
sum_a = 0
ans1 = 0
ans2 = 0

for i in range(n):
    sum_a += a[i]
    if i % 2 == 0:
        if sum_a <= 0:
            ans1 += abs(sum_a) + 1
            sum_a = 1
    else:
        if sum_a >= 0:
            ans1 += abs(sum_a) + 1
            sum_A = -1

sum_a = 0

for i in range(n):
    sum_a += a[i]
    if i % 2 == 0:
        if sum_a >= 0:
            ans2 += abs(sum_a) + 1
            sum_a = -1
    else:
        if sum_a <= 0:
            ans2 += abs(sum_a) + 1
            sum_A = 1

print(min(ans1,ans2))

Submission status: Wrong Answer

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

sum_a = [a[0]]
sum_tmp = a[0]
ans_v = 0
ans = 0
for i in range(1, len(a)):
    sum_tmp += a[i] 
    sum_a.append(sum_tmp)
    if sum_a[i] * sum_a[i - 1] >= 0:
        if sum_a[i] > 0:
            diff = - 1 - sum_a[i]
            ans += abs(diff)
            sum_a[i] += diff
            sum_tmp += diff
        elif sum_a[i] < 0:
            diff = 1 - sum_a[i]
            ans += abs(diff)
            sum_a[i] += diff
            sum_tmp += diff
        else:
            if sum_a[i - 1] < 0:
                sum_a[i] = 1
                ans += 1
                sum_tmp += 1
            elif sum_a[i - 1] > 0:
                sum_a[i] = -1
                ans += 1
                sum_tmp += -1
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
#ABC59 C
n = int(input())
a = list(map(int,input().split()))

ans1 = 0
s1 = a[0]
if s1 < 0:
    ans1 += abs(s1) + 1
for i in range(1,n):
    if i % 2 == 0:
        if s1 + a[i] > 0:
            s1 += a[i]
        else:
            ans1 += abs(s1 + a[i]) + 1
            s1 = 1
    else:
        if s1 + a[i] < 0:
            s1 += a[i]
        else:
            ans1 += abs(s1 + a[i]) + 1
            s1 = -1
ans2 = 0
s2 = a[0]
if s2 > 0:
    ans2 += abs(s2) + 1
for i in range(1,n):
    if i % 2 == 0:
        if s2 + a[i] < 0:
            s2 += a[i]
        else:
            ans2 += abs(s2 + a[i]) + 1
            s2 = -1
    else:
        if s2 + a[i] > 0:
            s2 += a[i]
        else:
            ans2 += abs(s2 + a[i]) + 1
            s2 = 1
print(min(ans1,ans2))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
S = A[0]
cnt = 0
if S!=0:
    for i in range(1,N):
        K = S+A[i]
        if S>0 and K>=0:
            cnt += 1+K
            S = -1
        elif S<0 and K<=0:
            cnt += 1-K
            S = 1
        else: S=K
else:
    S,cnt = 1,1
    for i in range(1,N):
        K = S+A[i]
        if S>0 and K>=0:
            cnt += 1+K
            S = -1
        elif S<0 and K<=0:
            cnt += 1-K
            S = 1
        else: S=K
    S,cnt1 = -1,1
    for i in range(1,N):
        K = S+A[i]
        if S>0 and K>=0:
            cnt1 += 1+K
            S = -1
        elif S<0 and K<=0:
            cnt1 += 1-K
            S = 1
        else: S=K
    cnt = min(cnt,cnt1)
print(cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
flg = A[0]
cnt = 0
for i in range(1,N):
    flg1 = flg + A[i]
    if flg>0 and flg1>=0:
        flg1 = flg1+1
        flg = -1
        cnt += flg1
    elif flg<0 and flg1<=0:
        flg1 = 1-flg1
        flg = 1
        cnt += flg1
    else:
        flg = flg1
print(cnt)

Submission status: Wrong Answer

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

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

	elif s > 0:
		s += a[i]
		if s >= 0:
			k += 1 + s
			s = -1

	else:
		print("okashi")

print(k)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

import sys

sys.setrecursionlimit(10**7)


def read_h(typ=int):
    return list(map(typ, input().split()))


def read_v(n, m=1, typ=int):
    return [read_h() if m > 1 else typ(input()) for _ in range(n)]


def calc_num_op(cumul, a):
    tmp_cumul = cumul + a
    #  print('current cumul:', cumul)
    #  print('current a:', a)
    #  print('bare cumul:', tmp_cumul)
    #  print('goodiness:', tmp_cumul != 0 and cumul // abs(cumul) != tmp_cumul // abs(tmp_cumul))
    if (cumul <= 0 and tmp_cumul <= 0):
        return abs(-cumul - a + 1), 1
    if (cumul >= 0 and tmp_cumul >= 0):
        return abs(-cumul - a - 1), -1
    return 0, tmp_cumul


def solve(arr, sign):
    num_op = 0
    cumul = arr[0]
    #  print('current cumul:', cumul)
    if cumul == 0:
        num_op += 1
        cumul += 1 if sign == '+' else -1
        #  print('modified cumul:', cumul)
    for a in arr[1:]:
        delta, cumul = calc_num_op(cumul, a)
        #  print('delta:', delta)
        #  print('modified cumul:', cumul)
        num_op += delta
    return num_op


def main():
    _ = read_h()
    arr = read_h()

    op_plus = solve(arr, '+')
    #  print('op plus:', op_plus)
    op_minus = solve(arr, '-')
    #  print('op minus:', op_minus)

    print(min(op_plus, op_minus))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

import sys

sys.setrecursionlimit(10**7)


def read_h(typ=int):
    return list(map(typ, input().split()))


def read_v(n, m=1, typ=int):
    return [read_h() if m > 1 else typ(input()) for _ in range(n)]


def calc_num_op(cumul, a):
    tmp_cumul = cumul + a
    #  print('current cumul:', cumul)
    #  print('current a:', a)
    #  print('bare cumul:', tmp_cumul)
    #  print('goodiness:', tmp_cumul != 0 and cumul // abs(cumul) != tmp_cumul // abs(tmp_cumul))
    if (cumul <= 0 and tmp_cumul <= 0):
        return abs(-cumul - a + 1), 1
    if (cumul >= 0 and tmp_cumul >= 0):
        return abs(-cumul - a - 1), -1
    return 0, tmp_cumul


def solve(arr, sign):
    num_op = 0
    cumul = arr[0]
    print('current cumul:', cumul)
    if cumul == 0:
        num_op += 1
        cumul += 1 if sign == '+' else -1
        #  print('modified cumul:', cumul)
    for a in arr[1:]:
        delta, cumul = calc_num_op(cumul, a)
        #  print('delta:', delta)
        #  print('modified cumul:', cumul)
        num_op += delta
    return num_op


def main():
    _ = read_h()
    arr = read_h()

    op_plus = solve(arr, '+')
    #  print('op plus:', op_plus)
    op_minus = solve(arr, '-')
    #  print('op minus:', op_minus)

    print(min(op_plus, op_minus))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
i = 0
sum_before = 0
sum_after = 0
count = 0
if a[0] == 0:
    if sum(a) > 0:
        a[0] = 1
    else:
        a[0] = -1
    count += 1

while i < n:
    sum_after = sum_before + a[i]
    if sum_after * sum_before > 0 or sum_after == 0:
        if sum_after < 0:
            a[i] = a[i] - sum_after + 1
        elif sum_after > 0:
            a[i] = a[i] - sum_after - 1
        elif sum_before < 0:
            a[i] += 1
        else:
            a[i] -= 1
        count += abs(sum_after) + 1
        sum_after = sum_before + a[i]
    i += 1
    sum_before = sum_after

print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
i = 0
sum_before = 0
sum_after = 0
count = 0
while i < n:
    sum_after = sum_before + a[i]
    if sum_after * sum_before > 0 or sum_after == 0:
        if sum_after < 0:
            a[i] = a[i] - sum_after + 1
        elif sum_after > 0:
            a[i] = a[i] - sum_after - 1
        elif sum_before < 0:
            a[i] += 1
        else:
            a[i] -= 1
        count += abs(sum_after) + 1
        sum_after = sum_before + a[i]
    i += 1
    sum_before = sum_after
#print(a)
print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
i = 0
sum_before = 0
sum_after = 0
count = 0
while i < n:
    sum_after = sum_before + a[i]
    if sum_after * sum_before > 0 or sum_after == 0:
        if sum_after < 0:
            a[i] += abs(sum_after) + 1
        elif sum_after > 0:
            a[i] -= abs(sum_after) + 1
        elif sum_before < 0:
            a[i] += 1
        else:
            a[i] -= 1
        count += abs(sum_after) + 1
        sum_after = sum_before + a[i]
    i += 1
    sum_before = sum_after

print(count)

Submission status: Wrong Answer

Solution code:
n=int(input())
b=list(map(int,input().split()))
a=b
condition=''
cnt=0
wa=0
for i in range(n):
  wa+=a[i]
  if i == 0:
    if a[i]>0:
      condition='minus'
    else:
      condition='plus'
  elif condition == 'plus':
    condition='minus'
    if wa<=0:
      cnt+=abs(wa)+1
      a[i]+=abs(wa)+1
      wa+=abs(wa)+1
  elif condition == 'minus':
    condition='plus'
    if wa>=0:
      cnt+=abs(wa)+1
      a[i]-=abs(wa)-1
      wa-=abs(wa)-1

print(cnt)

Submission status: Wrong Answer

Solution code:
n=int(input())
b=list(map(int,input().split()))
a=b
condition=''
cnt=0
wa=0
for i in range(n):
  wa+=a[i]
  if i == 0:
    if a[i]>0:
      condition='minus'
    else:
      condition='plus'
  elif condition == 'plus':
    condition='minus'
    if wa<=0:
      cnt+=abs(wa)+1
      a[i]+=abs(wa)+1
      wa+=abs(wa)+1
  elif condition == 'minus':
    condition='plus'
    if wa>=0:
      cnt+=abs(wa)+1
      a[i]-=abs(wa)+1
      wa-=abs(wa)+1

print(cnt)

Submission status: Wrong Answer

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

cnt = 0
sum_a = 0

for i, a in enumerate(A):
    sum_a += a
    if i % 2 == 0 and sum_a <= 0:
        sum_a += abs(sum_a) + 1
        cnt += abs(sum_a) + 1
    elif i % 2 == 1 and sum_a >= 0:
        sum_a -= abs(sum_a) + 1
        cnt += abs(sum_a) + 1

if A[0] < 0:
    cnt -= abs(A[0]) + 1
    print(cnt)
else:
    print(cnt)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
ans=10**13
for i in [1,-1]:
    b=0
    c=0
    for j in a:
        b+=j
        if b*i<=0:
            c+=abs(b-i)
            b=i
        i*=-1
    ans=min(ans,c)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
cur = 0
ans1, ans2 = 0,0
for i, x in enumerate(a):
    cur += x
    if i%2 == 0:
        if cur >= 1:
            continue
        else:
            ans1 += abs(1-cur)
            cur = 1
    else:
        if cur <= -1:
            continue
        else:
            ans1 += abs(-1-cur)
            cur = -1
for i, x in enumerate(a):
    cur += x
    if i%2 != 0:
        if cur >= 1:
            continue
        else:
            ans2 += abs(1-cur)
            cur = 1
    else:
        if cur <= -1:
            continue
        else:
            ans2 += abs(-1-cur)
            cur = -1
print(min(ans1, ans2))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
cur = 0
ans1, ans2 = 0,0
for i, x in enumerate(a):
    cur += x
    if i%2 == 0:
        if cur >= 1:
            continue
        else:
            ans1 += abs(1-x)
    else:
        if cur <= -1:
            continue
        else:
            ans1 += abs(-1-x)
for i, x in enumerate(a):
    cur += x
    if i%2 != 0:
        if cur >= 1:
            continue
        else:
            ans2 += abs(1-x)
    else:
        if cur <= -1:
            continue
        else:
            ans2 += abs(-1-x)
print(min(ans1, ans2))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
ls1 = [1] + [-2 if i%2 == 0 else 2 for i in range(n-1)]
ls2 = [-1] + [2 if i%2 == 0 else -2 for i in range(n-1)]
ans1, ans2 = 0,0
for x, y in zip(a, ls1):
    ans1 += abs(x-y)
for x, y in zip(a, ls2):
    ans2 += abs(x-y)
print(min(ans1, ans2))

Submission status: Wrong Answer

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

wa=a[0]
ans1,ans2=0,0
if a[0]==0:
    ans1+=1
    wa=1

for i in range(1,n):
    # print(wa)
    if wa>0:
        if wa+a[i]<0:
            wa+=a[i]
        else:
            ans1+=abs(wa+a[i])+1
            wa=-1
    else:
        if wa+a[i]>0:
            wa+=a[i]
        else:
            ans1+=abs(wa+a[i])+1
            wa=1

if a[0]>0:
    ans2+=a[0]+1
    wa=-1
else:
    ans2+=-a[0]+1
    wa=1

for i in range(1,n):
    if wa>0:
        if wa+a[i]<0:
            wa+=a[i]
        else:
            ans2+=abs(wa+a[i])+1
            wa=-1
    else:
        if wa+a[i]>0:
            wa+=a[i]
        else:
            ans2+=abs(wa+a[i])+1
            wa=1
print(min(ans1,ans2))

Submission status: Wrong Answer

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

wa=a[0]
ans1,ans2=0,0

for i in range(1,n):
    # print(wa)
    if wa>=0:
        if wa+a[i]<0:
            wa+=a[i]
        else:
            ans1+=abs(wa+a[i])+1
            wa=-1
    else:
        if wa+a[i]>0:
            wa+=a[i]
        else:
            ans1+=abs(wa+a[i])+1
            wa=1

if a[0]>0:
    ans2+=a[0]+1
    wa=-1
else:
    ans2+=-a[0]+1
    wa=1

for i in range(1,n):
    if wa>0:
        if wa+a[i]<0:
            wa+=a[i]
        else:
            ans2+=abs(wa+a[i])+1
            wa=-1
    else:
        if wa+a[i]>0:
            wa+=a[i]
        else:
            ans2+=abs(wa+a[i])+1
            wa=1
print(min(ans1,ans2))

Submission status: Wrong Answer

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

wa=a[0]
ans1,ans2=0,0

for i in range(1,n):
    # print(wa)
    if wa>0:
        if wa+a[i]<0:
            wa+=a[i]
        else:
            ans1+=abs(wa+a[i])+1
            wa=-1
    else:
        if wa+a[i]>0:
            wa+=a[i]
        else:
            ans1+=abs(wa+a[i])+1
            wa=1

if a[0]>0:
    ans2+=a[0]+1
    wa=-1
else:
    ans2+=-a[0]+1
    wa=1

for i in range(1,n):
    if wa>0:
        if wa+a[i]<0:
            wa+=a[i]
        else:
            ans2+=abs(wa+a[i])+1
            wa=-1
    else:
        if wa+a[i]>0:
            wa+=a[i]
        else:
            ans2+=abs(wa+a[i])+1
            wa=1
print(min(ans1,ans2))

Submission status: Wrong Answer

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

hugo=0
wa=a[0]

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

print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(300000)


def solve(n: int, a: "List[int]"):
    o, e = 0, 0
    tmp = 0
    for i, v in enumerate(a):
        if i % 2 == 0:
            e += v
            if e < 0:
                tmp += abs(e) + 1
                e = 1
            else:
                tmp += max(0, abs(o) - abs(e) + 1)
                e += tmp
        else:
            o += v
            if o > 0:
                tmp += abs(o) + 1
                o = -1
            else:
                tmp += max(0, abs(e) - abs(o) + 1)
                o -= tmp
    ret = tmp

    o, e = 0, 0
    tmp = 0
    for i, v in enumerate(a):
        if i % 2 == 0:
            e += v
            if e > 0:
                tmp += abs(e) + 1
                e = -1
            else:
                tmp += max(0, abs(o) - abs(e) + 1)
                e -= tmp
        else:
            o += v
            if o < 0:
                tmp += abs(o) + 1
                o = 1
            else:
                tmp += max(0, abs(e) - abs(o) + 1)
                o += tmp
    ret = min(ret, tmp)
    print(ret)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    n = int(next(tokens))  # type: int
    a = [ int(next(tokens)) for _ in range(n) ]  # type: "List[int]"
    solve(n, a)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n=int(input())
A=list(map(int,input().split()))
a,memo1=A[0],0
if a<0:
    memo1+=1-a
    a=1
for i in range(2,n):
    if a*(a+A[i])<=-1:a=a+A[i]
    else:
        memo1+=abs(a+A[i])+1
        if a>=1:a=-1
        else:a=-1
a,memo2=A[0],0
if a>0:
    memo2+=a+1
    a=-1
for i in range(2,n):
    if a*(a+A[i])<=-1:a=a+A[i]
    else:
        memo2+=abs(a+A[i])+1
        if a>=1:a=-1
        else:a=-1
print(min(memo1,memo2))

Submission status: Wrong Answer

Solution code:
n=int(input())
A=list(map(int,input().split()))
a,memo1,memo2=A[0],0,0
if a<0:
    memo1+=1-a
    a=1
for i in range(2,n):
    if a*(a+A[i])<=-1:a=a+A[i]
    else:
        memo1+=abs(a+A[i])+1
        if a>=1:a=-1
        else:a=-1
if a>0:
    memo2+=a+1
    a=-1
for i in range(2,n):
    if a*(a+A[i])<=-1:a=a+A[i]
    else:
        memo2+=abs(a+A[i])+1
        if a>=1:a=-1
        else:a=-1
print(min(memo1,memo2))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(_) for _ in input().split()]
even_p = 0
even_n = 0
odd_p = 0
odd_n = 0
for i in range(n):
    if i % 2 == 0:
        if a[i] > 0:
            odd_p += 1
        elif a[i] < 0:
            odd_n += 1
    else:
        if a[i] > 0:
            even_p += 1
        elif a[i] < 0:
            even_n += 1
cnt = 0
if odd_p - odd_n > even_p - even_n:
    if a[0] > 0:
        sum_i = a[0]
    else:
        cnt += abs(a[0]-1)
        sum_i = 1
else:
    if a[0] < 0:
        sum_i = a[0]
    else:
        cnt += abs(a[0] + 1)
        sum_i = -1
for i in range(1, n):
    if sum_i > 0:
        if sum_i + a[i] < 0:
            sum_i += a[i]
        else:
            cnt += abs(a[i]+sum_i+1)
            sum_i = -1
    elif sum_i < 0:
        if sum_i + a[i] > 0:
            sum_i += a[i]
        else:
            cnt += abs(a[i]+sum_i-1)
            sum_i = 1
print(cnt)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(_) for _ in input().split()]
even_p = 0
even_n = 0
odd_p = 0
odd_n = 0
for i in range(n):
    if i % 2 == 0:
        if a[i] > 0:
            odd_p += 1
        elif a[i] < 0:
            odd_n += 1
    else:
        if a[i] > 0:
            even_p += 1
        elif a[i] < 0:
            even_n += 1
cnt = 0
if odd_p > even_p:
    if a[0] > 0:
        sum_i = a[0]
    else:
        cnt += abs(a[0]-1)
        sum_i = 1
else:
    if a[0] < 0:
        sum_i = a[0]
    else:
        cnt += abs(a[0] + 1)
        sum_i = -1
for i in range(1, n):
    if sum_i > 0:
        if sum_i + a[i] < 0:
            sum_i += a[i]
        else:
            cnt += abs(a[i]+sum_i+1)
            sum_i = -1
    elif sum_i < 0:
        if sum_i + a[i] > 0:
            sum_i += a[i]
        else:
            cnt += abs(a[i]+sum_i-1)
            sum_i = 1
print(cnt)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=int(input())
A=list(map(int,input().split()))
ans=10**9
for t in range(2):
    a=0
    s=0
    for i in range(n):
        a+=A[i]
        if t and a<=0:
            s+=-a+1
            a=1
            t=0
        elif not(t) and a>=0:
            s+=a+1
            a=-1
            t=1
        else:
            t=1-t
    ans=min(s,ans)
print(ans)

Submission status: Wrong Answer

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

cur = 1
ans1 = abs(A[0] - 1)
ans2 = abs(A[0] + 1)


for a in A[1:]:
    if cur > 0:
        if a + cur >= 0:
            ans1 += abs(-1 - (a + cur))
            cur  = -1
        else:
            cur = a + cur
    elif cur < 0:
        if a + cur <= 0:
            ans1 += abs(1 - (a + cur))
            cur = 1
        else:
            cur = a + cur

for a in A[1:]:
    if cur > 0:
        if a + cur >= 0:
            ans2 += abs(-1 - (a + cur))
            cur  = -1
        else:
            cur = a + cur
    elif cur < 0:
        if a + cur <= 0:
            ans2 += abs(1 - (a + cur))
            cur = 1
        else:
            cur = a + cur

print(min(ans1,ans2))

Submission status: Wrong Answer

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

cur = A[0]
ans = 0

for a in A[1:]:
    if cur > 0:
        if a + cur >= 0:
            ans += abs(-1 - (a + cur))
            cur  = -1
        else:
            cur = a + cur
    elif cur < 0:
        if a + cur <= 0:
            ans += abs(1 - (a + cur))
            cur = 1
        else:
            cur = a + cur
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
d1 = [0] * (n + 1)
d2 = [0] * (n + 1)
ans1 = 0
ans2 = 0
for i in range(n):
    d1[i+1] += d1[i] + a[i]
    d2[i+1] += d2[i] + a[i]

# - + - + - の順番
for i in range(n):
    d1[i+1] = d1[i] + a[i]
    if(i % 2 == 0 and d1[i+1] >= 0):
        ans1 += abs(d1[i+1])+1
        d1[i + 1] = -1
    if(i % 2 == 1 and d1[i+1] <= 0):
        ans1 += abs(d1[i+1])+1
        d1[i + 1] = 1
    #print(d1, ans1)

#print(ans1)
#print()
# + - + - + の順番
for i in range(n):
    d2[i+1] = d2[i] + a[i]
    if(i % 2 == 1 and d2[i+1] >= 0):
        ans2 += abs(d1[i + 1]) + 1
        d2[i+1] = -1
    if(i % 2 == 0 and d2[i+1] <= 0):
        ans2 += abs(d2[i + 1]) + 1
        d2[i+1] = 1
    #print(d2, ans2)
#print(ans2)
#print()
ans = min(ans1, ans2)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())

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

ans = 0
cnt = 0
s = 0

for i in range(n):
    if i == 0:
        ans += li[i]
        if ans > 0:
            s = 1
        else:
            s = -1
    else:
        ans += li[i]
        if ans <= 0 and s == -1:
            cnt += abs(ans) + 1
            ans = 1
        if ans >= 0 and s == 1:
            cnt += abs(ans) + 1
            ans = -1
        s *= -1

print(cnt)

Submission status: Wrong Answer

Solution code:
n = int(input())

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

ans = 0
cnt = 0
s = 0

for i in range(n):
    if i == 0:
        ans += li[i]
        if ans > 0:
            s = 1
        else:
            s = -1
    else:
        ans += li[i]
        if ans <= 0 and s == -1:
            cnt += -ans + 1
            ans = 1
        if ans >= 0 and s == 1:
            cnt += ans + 1
            ans = -1
        s *= -1

print(cnt)

Submission status: Wrong Answer

Solution code:
n = int(input())

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

ans = 0
cnt = 0
s = 0

for i in range(n):
    if i == 0:
        ans += li[i]
        if ans > 0:
            s = 1
        else:
            s = -1
    else:
        ans += li[i]
        if ans <= 0 and s == -1:
            cnt += -ans + 1
            ans = 1
            s == 1
        if ans >= 0 and s == 1:
            cnt += ans + 1
            ans = -1
            s == -1
        s *= -1

print(cnt)

Submission status: Wrong Answer

Solution code:
def sign(x):
    if x > 0:
        return 1
    elif x < 0:
        return -1
    else:
        return 0


n = int(input())
a = list(map(int, input().split()))
ac=0
ans1 = 0
for i in range(n):
    ac += a[i]
    if ac * (-1) ** i > 0:
        continue
    else:
        ans1 += abs(ac) + 1
        ac = -sign(ac)
ac=0
ans2 = 0
for i in range(n):
    ac += a[i]
    if ac * (-1) ** i < 0:
        continue
    else:
        ans2 += abs(ac) + 1
        ac = -sign(ac)

print(min(ans1, ans2))

Submission status: Wrong Answer

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

sm=a[0]
ans=[0, 0]
for i in range(n-1):
    sm1=sm+a[i+1]
    if sm*sm1>=0:
        ans[0]+=abs(sm1)+1
        if sm>0:
            sm=-1
        else:
            sm=1
    else:
        sm=sm1


sm=-1
ans[1]+=abs(a[0])+1
for i in range(n-1):
    sm1=sm+a[i+1]
    if sm*sm1>=0:
        ans[1]+=abs(sm1)+1
        if sm>0:
            sm=-1
        else:
            sm=1
    else:
        sm=sm1
print(min(ans))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = 0
tmp = a[0]
for i in range(n-1):
    if (tmp > 0):
        while not (-tmp > a[i+1]):
            n = -tmp - a[i+1] - 1
            a[i+1] += n
            ans += -n
    elif (tmp < 0):
        while not (-tmp < a[i+1]):
            n = -tmp - a[i+1] + 1
            a[i+1] += n
            ans += n
    tmp += a[i+1]

print(ans)

Submission status: Wrong Answer

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

print(ans)