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()))
total = 0
ans = 0
for a in A:
  t = total + a
  if t != 0 and total * t <= 0:
    total = t
    continue
  ans += abs(t)+1
  if t >= 0:
    t = -1
  else:
    t = 1
  total = t

print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
def inpl(): return list(map(int, input().split()))

N = int(input())
A = inpl()
s = A[0]
res = 0

for a in A[1:]:
    if a*s < 0 and abs(s) < abs(a):
        s = a+s
    else:
        if s > 0:
            res += s + a +1
            s = -1
        else:
            res += 1 - a - s
            s = +1
print(res)

Submission status: Wrong Answer

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

prev_s = a[0]
res = 0
for i in range(1,n):
	curr_s = prev_s + a[i]
	if curr_s * prev_s > 0:
		cand = (1 if curr_s < 0 else -1)
		res += abs(cand - curr_s)
		a[i] += (cand - curr_s)
	elif curr_s == 0:
		if prev_s > 0:
			a[i] -= 1
		else:
			a[i] += 1
		res += 1
	curr_s = prev_s + a[i]
	prev_s = curr_s

print(res)

Submission status: Wrong Answer

Solution code:
#!usr/bim/python
#! -*- coding: utf-8 -*-

n = raw_input()
n = int(n)

a = raw_input()
a = a.split()
a = map(int,a)

x = 0
y = 0

for i in range(n):
    if i % 2 == 0:
        x += a[i]
    else:
        y += a[i]

w = []
ans = 0

if a[0] == 0:
    if x >= y:
        w.append(1)
    else:
        w.append(-1)
elif a[0] > 0:
    if x >= y:
        w.append(a[0])
    else:
        w.append(-1)
        ans += a[0] + 1
else:
    if x >= y:
        w.append(1)
        ans += 1 - a[0]
    else:
        w.append(a[0])

i = 1
while i < n:
    next_w = w[i-1]+a[i]
    if w[i-1] * next_w < 0:
        pass
    elif w[i-1] > 0:
        ans += next_w + 1
        next_w = -1
    else:
        ans += 1 - next_w
        next_w = 1
    w.append(next_w)
    i += 1

print ans

Submission status: Wrong Answer

Solution code:
#!usr/bim/python
#! -*- coding: utf-8 -*-

n = raw_input()
n = int(n)

a = raw_input()
a = a.split()
a = map(int,a)

x = 0
y = 0

for i in range(n):
    if i % 2 == 0:
        x += a[i]
    else:
        y += a[i]

w = []
ans = 0

if a[0] == 0:
    if x >= y:
        w.append(1)
    else:
        w.append(-1)
elif a[0] > 0:
    if x >= y:
        w.append(a[0])
    else:
        w.append(-1)
        ans += a[0] + 1
else:
    if x >= y:
        w.append(1)
        ans += 1 - a[0]
    else:
        w.append(a[0])

i = 1
while i < n:
    print i
    next_w = w[i-1]+a[i]
    if w[i-1] * next_w < 0:
        pass
    elif w[i-1] > 0:
        ans += next_w + 1
        next_w = -1
    else:
        ans += 1 - next_w
        next_w = 1
    w.append(next_w)
    i += 1

print ans

Submission status: Wrong Answer

Solution code:
# n, m = map(int, input().split())
N = int(input())
A = [int(i) for i in input().split()]
a = A[0]
ops = 0
if a == 0:
    for i in range(1, N):
        if A[i] == 0:
            continue
        else:
            if i % 2 == 0:
                a = -int(bool(a < 0)) + int(bool(a < 0))
            else:
                a = -int(bool(a > 0)) + int(bool(a > 0))
            ops += 1
            break
    if ops == 0:
        a = 1
        ops += 1
flag = bool(a > 0)
for i in range(1, N):
    a += A[i]
    if flag == bool(a > 0):
        ops += abs(a) + 1
        a = -int(bool(flag)) + int(bool(a < 0))
    flag = not flag
print(ops)

Submission status: Wrong Answer

Solution code:
# n, m = map(int, input().split())
N = int(input())
A = [int(i) for i in input().split()]
a = A[0]
ops = 0
if a == 0:
    if A[0] > 0:
        a = -1
    else:
        a = 1
    ops += 1
flag = bool(a > 0)
for i in range(1, N):
    a += A[i]
    if flag == bool(a > 0):
        ops += abs(a) + 1
        a = -int(bool(flag)) + int(bool(a < 0))
    flag = not flag
print(ops)

Submission status: Wrong Answer

Solution code:
#ARC72 C
n = int(input())
A = list(map(int, input().split()))
# print(A)

def get_ops(init_sign):
    prev_sign = init_sign
    sigma = A[0]
    operations = 0

    if A[0] == 0 and init_sign == '+':
        sigma = 1
        operations = 1
    elif A[0] == 0 and init_sign == '-':
        sigma = -1
        operations = 1

    for a in A[1:]:
        sigma += a
        if sigma >= 0 and prev_sign == '+':
            diff = sigma+1
            sigma -= diff
            operations += diff
        elif sigma <= 0 and prev_sign == '-':
            diff = abs(sigma)+1
            sigma += diff
            operations += diff
        if prev_sign == '+':
            prev_sign = '-'
        elif prev_sign == '-':
            prev_sign = '+'

    return operations

ops = 0
if A[0] > 0:
    ops = min(get_ops('+'), get_ops('-'))
elif A[0] < 0:
    ops = min(get_ops('+'), get_ops('-'))
else: # if A[0] is 0, try both case
    ops = min(get_ops('+'), get_ops('-'))

print(ops)

Submission status: Wrong Answer

Solution code:
#ARC72 C
n = int(input())
A = list(map(int, input().split()))
# print(A)

def get_ops(init_sign):
    prev_sign = init_sign
    sigma = A[0]
    diff = 0
    operations = 0

    if A[0] == 0 and init_sign == '+':
        sigma = 1
        diff = 1
        operations = 1
    elif A[0] == 0 and init_sign == '-':
        sigma = -1
        diff -1
        operations = 1

    for a in A[1:]:
        sigma += a
        if sigma >= 0 and prev_sign == '+':
            diff = sigma+1
            sigma -= diff
            operations += diff
        elif sigma <= 0 and prev_sign == '-':
            diff = abs(sigma)+1
            sigma += diff
            operations += diff
        if prev_sign == '+':
            prev_sign = '-'
        elif prev_sign == '-':
            prev_sign = '+'

    return operations

ops = 0
if A[0] > 0:
    ops = get_ops('+')
elif A[0] < 0:
    ops = get_ops('-')
else: # if A[0] is 0, try both case
    ops = min(get_ops('+'), get_ops('-'))

print(ops)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = 10000000000
p = lambda x, y: x * y >= 0
po = lambda x, y: abs(x + y) + 1 if p(x, x + y) else 0
popo = lambda x, y: -1 * x // abs(x) if p(x, x + y) else x + y


if a[0] == 0:
    s = [1, -1]
    tmpAns = [1, 1]
    for (_s, _ans) in zip(s, tmpAns):
        for i in range(1, n):
            _ans +=  po(_s, a[i])
            _s = popo(_s, a[i])
        ans = min((ans, _ans))
else:
    s = [a[0], -1 * a[0] // abs(a[0])]
    tmpAns = [0, abs(a[0]) + 1]
    for (_s, _ans) in zip(s, tmpAns):
        for i in range(1, n):
            _ans +=  po(_s, a[i])
            _s = popo(_s, a[i])
        ans = min((ans, _ans))

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = 10000000000
p = lambda x, y: x * y >= 0
po = lambda x, y: abs(x + y) + 1 if p(x, x + y) else 0
popo = lambda x, y: -1 * x // abs(x) if p(x, x + y) else x + y


if a[0] == 0:
    s = [1, -1]
    tmpAns = [1, 1]
    for (_s, _ans) in zip(s, tmpAns):
        for i in range(1, n):
            _ans +=  po(_s, a[i])
            _s = popo(_s, a[i])
        ans = min((ans, _ans))
else:
    s = [a[0], a[0]]
    tmpAns = [0, abs(a[0])]
    for (_s, _ans) in zip(s, tmpAns):
        for i in range(1, n):
            _ans +=  po(_s, a[i])
            _s = popo(_s, a[i])
        ans = min((ans, _ans))

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = 0
p = lambda x, y: x * y >= 0
po = lambda x, y: abs(x + y) + 1 if p(x, x + y) else 0
popo = lambda x, y: -1 * x // abs(x) if p(x, x + y) else x + y


if a[0] == 0:
    s = -1
    ans = 1
    for i in range(1, n):
        ans +=  po(s, a[i])
        s = popo(s, a[i])
    s = 1
    tmpAns = 1
    for i in range(1, n):
        tmpAns +=  po(s, a[i])
        s = popo(s, a[i])
    ans = min((ans, tmpAns))
else:
    s = a[0]
    for i in range(1, n):
        ans +=  po(s, a[i])
        s = popo(s, a[i])

    s = -1 * a[0] // abs(a[0])
    tmpAns = abs(a[i]) + 1
    for i in range(1, n):
        tmpAns +=  po(s, a[i])
        s = popo(s, a[i])
    ans = min((ans, tmpAns))

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = 0
p = lambda x, y: x * y >= 0

if a[0] == 0:
    s = -1
    ans = 1
    for i in range(1, n):
        ans +=  abs(s + a[i]) + 1 if p(s, s + a[i]) else 0
        s = -1 * s // abs(s) if p(s, s + a[i]) else s + a[i]
    s = 1
    tmpAns = 1
    for i in range(1, n):
        tmpAns +=  abs(s + a[i]) + 1 if p(s, s + a[i]) else 0
        s = -1 * s // abs(s) if p(s, s + a[i]) else s + a[i]
    ans = min(ans, tmpAns)
else:
    s = a[0]
    for i in range(1, n):
        ans +=  abs(s + a[i]) + 1 if p(s, s + a[i]) else 0
        s = -1 * s // abs(s) if p(s, s + a[i]) else s + a[i]

    s = -1 * a[0] // abs(a[0])
    tmpAns = abs(a[i]) + 1
    for i in range(1, n):
        tmpAns +=  abs(s + a[i]) + 1 if p(s, s + a[i]) else 0
        s = -1 * s // abs(s) if p(s, s + a[i]) else s + a[i]
    ans = min((ans, tmpAns))


print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
s = a[0]
ans = 0
p = lambda x, y: x * y >= 0

if s == 0:
    s = -1
    ans =1
    for i in range(1, n):
        ans +=  abs(s + a[i]) + 1 if p(s, s + a[i]) else 0
        s = -1 * s // abs(s) if p(s, s + a[i]) else s + a[i]
    s = 1
    tmpAns = 1
    for i in range(1, n):
        tmpAns +=  abs(s + a[i]) + 1 if p(s, s + a[i]) else 0
        s = -1 * s // abs(s) if p(s, s + a[i]) else s + a[i]
    ans = min(ans, tmpAns)
else:
    for i in range(1, n):
        ans +=  abs(s + a[i]) + 1 if p(s, s + a[i]) else 0
        s = -1 * s // abs(s) if p(s, s + a[i]) else s + a[i]

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
prog = list(map(int,input().split()))
def seq(N,prog):
    result_PM = [0]*N
    result_MP = [0]*N
    result_PM[0] = result_MP[0] = prog[0]
    for i in range(N-1):
        result_PM[i+1] = result_MP[i+1] = result_PM[i] + prog[i+1]
    answer_PM = answer_MP = 0
    PM_add = MP_add = 0
    for i in range(N):
        if (i+1)%2 == 0:
            if (result_PM[i] + PM_add) >= 0:
                answer_PM += abs(result_PM[i] + PM_add + 1)
                PM_add += (-1)*(result_PM[i] + PM_add+ 1)
            if (result_MP[i] + MP_add) <= 0:
                answer_MP += abs(result_MP[i]  + MP_add-- 1)
                MP_add += abs(result_MP[i] + MP_add- 1)
        else:
            if (result_PM[i] + PM_add) <= 0:
                answer_PM += abs(result_PM[i] + PM_add - 1)
                PM_add += abs(result_PM[i] + PM_add - 1)
            if (result_MP[i] + MP_add) >= 0:
                answer_MP += abs(result_MP[i] + MP_add+ 1)
                MP_add += (-1)*(result_MP[i] + MP_add + 1)
    answer = min(answer_MP,answer_PM)
    print(answer)
seq(N,prog)

Submission status: Wrong Answer

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

def count(sign):
	_sum=0
	_count=0
	global n,a
	for num in a:
		_sum+=num
		if sign:
			if _sum>0:
				pass
			else:
				_count+=(1-_sum)
				_sum+=(1-_sum)
			sign=False
		else:
			if _sum<0:
				pass
			else:
				_count+=(_sum+1)
				_sum-=(_sum+1)
			sign=True
	return _count
if n==0:
	print 0
elif a[0]>0:
	print count(True)
elif a[0]<0:
	print count(False)
else:
	print min(count(True),count(False))

Submission status: Wrong Answer

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

even=sum([A[i]-A[i+1] for i in range(len(A)-1) if i%2==0])
odd=sum([A[i]-A[i+1] for i in range(len(A)-1) if i%2!=0])

if even>odd:A=A[::-1]
s=A[0];c=0

for a in A[1::]:
    if s*(s+a)>=0:
        c+=abs(s+a)+1
        s=[-1,1][s<0]
        continue
    s+=a
print(c)

Submission status: Wrong Answer

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

even=sum([A[i]-A[i+1] for i in range(len(A)-1) if i%2==0])
odd=sum([A[i]-A[i+1] for i in range(len(A)-1) if i%2!=0])

if A[0]*(even-odd)>0:
    s=A[0];c=0
else:
    s=[-1,1][even>odd];c=1

for a in A[1:]:
    if s*(s+a)>=0:
        c+=abs(s+a)+1
        s=[-1,1][s<0]
        continue
    s+=a
print(c)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

n = int(input())
A = list(map(int,input().split()))
count = 0
i = 0

while i < n and A[i] == 0:
    if i == 0:
        count += 1
    else:
        count += 2
    i += 1

if i != n:
    if i != 0:
        c = -1 * A[i]//A[i]
        i -=1
    else:
        c = A[0]

    for a in A[i+1:]:
        if c * (c+a) >= 0:
            count += abs(a+c) + 1
            c = -1 * c//abs(c);
        else:
            c += a

print(count)

Submission status: Wrong Answer

Solution code:
n=int(input());A=list(map(int,input().split()))
even=sum([A[i] for i in range(len(A)) if i%2==0])
odd=sum([A[i] for i in range(len(A)) if i%2!=0])

if A[0]*(even-odd)>0:
    s=A[0];c=0
else:
    s=[-1,1][even>odd];c=1

for a in A[1:]:
    if s*(s+a)>=0:
        c+=abs(s+a)+1
        s=[-1,1][s<0]
        continue
    s+=a
print(c)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

n = int(input())
A = list(map(int,input().split()))
count = 0
i = 0

while i < n and A[i] == 0:
    if i == 0:
        count += 1
    else:
        count += 2
    i += 1

if i != n:
    if i != 0:
        c = -1 * A[i]//A[i]
        i -=1
    else:
        c = A[0]

    for a in A[i+1:]:
        if c * (c+a) >= 0:
            count += abs(a+c) + 1
            c = -1 * c//c;
        else:
            c += a

print(count)

Submission status: Wrong Answer

Solution code:
n=int(raw_input())
a=map(int,raw_input().split())
ans1=0
ans2=0
su1=a[0]
su2=a[0]
if a[0]==0:
    su1+=1
    su2-=1
    ans1=1
    ans2=1
for i in xrange(1,n):
    tmp=su1
    su1+=a[i]
    if su1*tmp<0:
        continue
    else:
        if su1==0 and tmp<0:
            ans1+=1
            su1+=1
        elif su1==0 and tmp>0:
            ans1+=1
            su1-=1
        elif tmp<0 and su1<0:
            ans1+=-su1+1
            su1=1
        elif tmp>0 and su1>0:
            ans1+=su1+1
            su1=-1
for i in xrange(1,n):
    tmp=su2
    su2+=a[i]
    if su2*tmp<0:
        continue
    else:
        if su2==0 and tmp<0:
            ans2+=1
            su2+=1
        elif su2==0 and tmp>0:
            ans2+=1
            su2-=1
        elif tmp<0 and su2<0:
            ans2+=-su2+1
            su2=1
        elif tmp>0 and su2>0:
            ans2+=su2+1
            su2=-1
print min(ans1,ans2)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

maenogoukei = a[0]
goukei = a[0]
kaisuu = 0

if goukei == 0:
	kaisuu += 1
	maenogoukei = -1 
	goukei = maenogoukei



for a_i in a[1:]:
	goukei += a_i
	if maenogoukei > 0 and goukei >= 0:
		kaisuu += goukei + 1
		goukei = -1
		maenogoukei = -1
	elif maenogoukei < 0 and goukei <= 0:
		kaisuu += -goukei + 1
		goukei = 1
		maenogoukei = 1
	else:
		maenogoukei += a_i




maenogoukei2 = a[0]
goukei2 = a[0]
kaisuu2 = 0

if goukei2 == 0:
	kaisuu2 += 1
	maenogoukei2 = 1 
	goukei2 = 1
else:
	kaisuu2 += goukei2 + 1
	maenogoukei2 = - int(np.sign(a[0]))
	goukei2 = - int(np.sign(a[0]))


for a_i2 in a[1:]:
	goukei2 += a_i2
	if maenogoukei2 > 0 and goukei2 >= 0:
		kaisuu2 += goukei2 + 1
		goukei2 = -1
		maenogoukei2 = -1
	elif maenogoukei2 < 0 and goukei2 <= 0:
		kaisuu2 += -goukei2 + 1
		goukei2 = 1
		maenogoukei2 = 1
	else:
		maenogoukei2 += a_i2

print( min([kaisuu,kaisuu2]) )

Submission status: Wrong Answer

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

sum = [0]
for i, a in enumerate(A):
	sum.append(a+sum[i])

num_op = 0
sum_shift = 0
prev_sign = '.'

if(sum[1] >= 0):
	prev_sign = '+'
else:
	prev_sign = '-'

for a in sum[2:]:
	a += sum_shift
	if(prev_sign == '+'):
		if (a >= 0):
			num_op += (a+1)
			sum_shift -= (a+1)
		prev_sign = '-'
	else:
		if(a <= 0):
			num_op += (abs(a)+1)
			sum_shift += (abs(a)+1)
		prev_sign = '+'

print(num_op)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = [A[0]]

for i in range(1, N):
    B.append(B[i - 1] + A[i])

BB = list(B)

# +, -
ans1 = 0
tmp1 = 0
if B[0] == 0:
    B[0] = 1
    ans1 += 1
    tmp1 += 1

for i in range(1, N):
    B[i] += tmp1
    # +
    if i % 2 == 0:
        if B[i] <= 0:
            ans1 += (1 - B[i])
            tmp1 += (1 - B[i])
            B[i] = 1
    # -
    else:
        if B[i] >= 0:
            ans1 += (B[i] + 1)
            tmp1 -= (B[i] + 1)
            B[i] = -1


# -, +
ans2 = 0
tmp2 = 0
if BB[0] == 0:
    BB[0] = -1
    ans2 += 1
    tmp2 -= 1

for i in range(1, N):
    BB[i] += tmp2
    # -
    if i % 2 == 0:
        if BB[i] >= 0:
            ans2 += (BB[i] + 1)
            tmp2 -= (BB[i] + 1)
            BB[i] = -1
    # +
    else:
        if BB[i] <= 0:
            ans2 += (1 - BB[i])
            tmp2 += (1 - BB[i])
            BB[i] = 1

"""
print(ans1, ans2)
print(B)
print(BB)
"""
print(min(ans1, ans2))

Submission status: Wrong Answer

Solution code:
n=int(raw_input())
a=map(int,raw_input().split())
ans1=0
ans2=0
su1=a[0]
su2=a[0]
if a[0]==0:
    su1+=1
    su2-=1
for i in xrange(1,n):
    tmp=su1
    su1+=a[i]
    if su1*tmp<0:
        continue
    else:
        if su1==0 and tmp<0:
            ans1+=1
            su1+=1
        elif su1==0 and tmp>0:
            ans1+=1
            su1-=1
        elif tmp<0 and su1<0:
            ans1+=-su1+1
            su1=1
        elif tmp>0 and su1>0:
            ans1+=su1+1
            su1=-1
for i in xrange(1,n):
    tmp=su2
    su2+=a[i]
    if su2*tmp<0:
        continue
    else:
        if su2==0 and tmp<0:
            ans2+=1
            su2+=1
        elif su2==0 and tmp>0:
            ans2+=1
            su2-=1
        elif tmp<0 and su2<0:
            ans2+=-su2+1
            su2=1
        elif tmp>0 and su2>0:
            ans2+=su2+1
            su2=-1
print min(ans1,ans2)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

maenogoukei = a[0]
goukei = a[0]
kaisuu = 0

if goukei == 0:
	kaisuu += 1
	maenogoukei = -1 
	goukei = maenogoukei



for a_i in a[1:]:
	goukei += a_i
	if maenogoukei > 0 and goukei >= 0:
		kaisuu += goukei + 1
		goukei = -1
		maenogoukei = -1
	elif maenogoukei < 0 and goukei <= 0:
		kaisuu += -goukei + 1
		goukei = 1
		maenogoukei = 1
	else:
		maenogoukei += a_i




maenogoukei2 = a[0]
goukei2 = a[0]
kaisuu2 = 0

if goukei2 == 0:
	kaisuu2 += 1
	maenogoukei2 = 1 
	goukei2 = maenogoukei2
else:
	kaisuu2 += goukei2 + 1
	maenogoukei2 = - np.sign(a[0])
	goukei2 = maenogoukei2


for a_i2 in a[1:]:
	goukei2 += a_i2
	if maenogoukei2 > 0 and goukei2 >= 0:
		kaisuu2 += goukei2 + 1
		goukei2 = -1
		maenogoukei2 = -1
	elif maenogoukei2 < 0 and goukei2 <= 0:
		kaisuu2 += -goukei2 + 1
		goukei2 = 1
		maenogoukei2 = 1
	else:
		maenogoukei2 += a_i2

print( min([kaisuu,kaisuu2]) )

Submission status: Wrong Answer

Solution code:
n=int(input());A=list(map(int,input().split()))
s=A[0]
c=1 if s==0 else 0
for a in A[1:]:
    if s*(s+a)>=0:
        c+=abs(s+a)+1
        s=1 if s+a>0 else -1
        continue
    s+=a
print(c)

Submission status: Wrong Answer

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

import math


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


def compute(n, ys):
    xs = ys[:]
    if xs[0] == 0:
        return min(compute(n, [1] + xs[1:]), compute(n, [-1] + xs[1:])) + 1
    ss = [None for _ in range(n)]
    ss[0] = xs[0]
    res = 0
    for i in range(n - 1):
        ss[i + 1] = ss[i] + xs[i + 1]
        dx = 0
        if ss[i + 1] == 0:
            dx = -sign(ss[i])
        elif sign(ss[i]) == sign(ss[i + 1]):
            dx = -sign(ss[i]) * (abs(ss[i + 1]) + 1)
        xs[i + 1] += dx
        ss[i + 1] += dx
        res += abs(dx)
    return res


def main():
    n = int(input())
    ys = [int(y) for y in input().split()]
    print(compute(n, ys))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n=int(input());A=list(map(int,input().split()))
c=0;s=A[0]
for a in A[1:]:
    if s*(s+a)>=0:
        c+=abs(s+a)+1
        if s==0:
            s=1 if s+a>0 else -1
            continue
        s=1 if s<0 else -1
        continue
    s+=a
print(c)

Submission status: Wrong Answer

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

import sys
import os

n = int(input())

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

A = BASE[:]
# assume plus start
sum_value = 0
add_num = 0
#print('before', A)
for i, a in enumerate(A):
    sum_value += a
    # 偶数項までの和は正
    if i % 2 == 0:
        if sum_value <= 0:
            A[i] = A[i] - sum_value + 1
            add_num += (-sum_value + 1)
            sum_value = 1
    # odd
    else:
        if sum_value > 0:
            A[i] = A[i] - sum_value - 1
            add_num += (sum_value + 1)
            sum_value = -1
#print(add_num)
ans0 = add_num

A = BASE[:]
# assume minus start
sum_value = 0
add_num = 0
for i, a in enumerate(A):
    sum_value += a
    # 偶数項までの和は負
    if i % 2 == 0:
        if sum_value > 0:
            A[i] = A[i] - sum_value - 1
            add_num += (sum_value + 1)
            sum_value = -1

    # odd
    else:
        if sum_value <= 0:
            A[i] = A[i] - sum_value + 1
            add_num += (-sum_value + 1)
            sum_value = 1
#print(add_num)
ans1 = add_num

print(min(ans0, ans1))

Submission status: Wrong Answer

Solution code:
n=int(input());A=list(map(int,input().split()))
c=0;s=A[0]
for a in A[1:]:
    if s*(s+a)>=0:
        c+=abs(s+a)+1
        s=-1 if s>0 else 1
        continue
    s+=a
print(c)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

maenogoukei = a[0]
goukei = a[0]
kaisuu = 0

def zerodenai(hairetsu):
	if hairetsu[0] != 0:
		return hairetsu[0]
	else:
		return zerodenai(hairetsu[1:])

if goukei == 0:
	kaisuu += 1
	if zerodenai(a) >= 0:
		maenogoukei = -1 
		goukei = maenogoukei
	else:
		maenogoukei = 1
		goukei = maenogoukei


for a_i in a[1:]:
	goukei += a_i
	if maenogoukei > 0 and goukei >= 0:
		kaisuu += goukei + 1
		goukei = -1
		maenogoukei = -1
	elif maenogoukei < 0 and goukei <= 0:
		kaisuu += -goukei + 1
		goukei = 1
		maenogoukei = 1
	else:
		maenogoukei += a_i

print (kaisuu)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = [A[0]]

for i in range(1, N):
    B.append(B[i - 1] + A[i])

BB = list(B)

# +, -
ans1 = 0
tmp1 = 0
if B[0] == 0:
    B[0] = 1
    ans1 += 1
    tmp1 += 1

for i in range(1, N):
    B[i] += tmp1
    # +
    if i % 2 == 0:
        if B[i] <= 0:
            ans1 += (1 - B[i])
            tmp1 += (1 - B[i])
            B[i] = 1
    # -
    else:
        if B[i] >= 0:
            ans1 += (B[i] + 1)
            tmp1 -= (B[i] + 1)
            B[i] = -1


# -, +
ans2 = 0
tmp2 = 0
if BB[0] == 0:
    BB[0] = -1
    ans2 += 1
    tmp2 += 1

for i in range(1, N):
    BB[i] += tmp2
    # -
    if i % 2 == 0:
        if BB[i] >= 0:
            ans2 += (BB[i] + 1)
            tmp2 -= (BB[i] + 1)
            BB[i] = -1
    # +
    else:
        if BB[i] <= 0:
            ans2 += (1 - BB[i])
            tmp2 += (1 - BB[i])
            BB[i] = 1

# print(ans1, ans2)
print(min(ans1, ans2))

Submission status: Wrong Answer

Solution code:
import numpy as np

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

maenogoukei = a[0]
goukei = a[0]
kaisuu = 0

if goukei == 0:
	kaisuu += 1
	if a[1] >= 0:
		maenogoukei = -1 
		goukei = maenogoukei
	else:
		maenogoukei = 1
		goukei = maenogoukei

for a_i in a[1:]:
	goukei += a_i
	if maenogoukei > 0 and goukei >= 0:
		kaisuu += goukei + 1
		goukei = -1
		maenogoukei = -1
	elif maenogoukei < 0 and goukei <= 0:
		kaisuu += -goukei + 1
		goukei = 1
		maenogoukei = 1
	else:
		maenogoukei += a_i

print (kaisuu)

Submission status: Wrong Answer

Solution code:
import sys

def solve():
    n = int(input())
    a = [int(i) for i in input().split()]

    if a[0] != 0:
        ans = 0

        v = a[0]

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

        print(ans)
    else:
        v = 1
        ans1 = 1

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

        v = -1
        ans2 = 1

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

        ans = min(ans1, ans2)

        print(ans)

    pass

if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
def solve(a):
    s, res = 0, 0
    for i, ai in enumerate(a):
        ss = s
        ss += ai
        if ss == 0:
            ss = 1 if s < 0 else -1
            res += 1
        else:
            if s < 0 and ss < 0:
                res += abs(ss) + 1
                ss = 1
            if s > 0 and ss > 0:
                res += abs(ss) + 1
                ss = -1
        s = ss
    return res

n = int(raw_input())
a = map(int, raw_input().split()) 

if a[0] == 0:
    a[0] = 1
    ans = solve(a) + 1
    a[0] = -1
    ans = min(ans, solve(a) + 1)
else:
    ans = solve(a)
    a[0] = 1 if a[0] < 0 else -1
    ans = min(ans, solve(a) + abs(a[0]) + 1)
print ans

Submission status: Wrong Answer

Solution code:
import numpy as np

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

maenogoukei = a[0]
goukei = a[0]
kaisuu = 0

if goukei == 0:
	kaisuu += 1
	maenogoukei += - np.sign(a[1])
	goukei += maenogoukei

for a_i in a[1:]:
	goukei += a_i
	if maenogoukei > 0 and goukei >= 0:
		kaisuu += goukei +1
		goukei = -1
		maenogoukei = -1
	elif maenogoukei < 0 and goukei <= 0:
		kaisuu += -goukei +1
		goukei = 1
		maenogoukei = 1
	else:
		maenogoukei += a_i

print (kaisuu)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import array, bisect, collections, heapq, itertools, math, random, re, string, sys, time
sys.setrecursionlimit(10 ** 7)
INF = 10 ** 20
MOD = 10 ** 9 + 7
 
 
def II(): return int(input())
def ILI(): return list(map(int, input().split()))
def IAI(LINE): return [ILI() for __ in range(LINE)]
def IDI(): return {key: value for key, value in ILI()}
 
 
def solve(n, a):
    ans = 0
    sum = a[0]
    for i in range(1, n):
        now = a[i]
        next_sum = sum + now
        if sum > 0:
            if next_sum > 0:
                ans += (next_sum + 1)
                next_sum = -1
                sum = next_sum
                continue
            elif next_sum < 0:
                sum = next_sum
                continue
            elif next_sum == 0:
                next_sum = -1
                sum = next_sum
                ans += 1
                continue
        elif sum < 0:
            if next_sum > 0:
                sum = next_sum
                continue
            elif next_sum < 0:
                ans += (abs(next_sum) + 1)
                next_sum = 1
                sum = next_sum
                continue
            elif next_sum == 0:
                next_sum = 1
                sum = next_sum
                ans += 1
                continue
        elif sum == 0:
            sum = next_sum
            continue
    
    return ans
            
 
def main():
    n = II()
    a = ILI()
    print(solve(n, a))
 
 
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n=int(raw_input())
a=map(int,raw_input().split())
ans1=0
ans2=0
su=a[0]
for i in xrange(1,n):
    tmp=su
    su+=a[i]
    if su*tmp<0:
        continue
    else:
        if su==0 and tmp==0:
            su-=1
            ans1+=1
        elif su==0 and tmp<0:
            ans1+=1
            su+=1
        elif su==0 and tmp>0:
            ans1+=1
            su-=1
        elif tmp<0 and su<0:
            ans1+=-su+1
            su=1
        elif tmp>0 and su>0:
            ans1+=su+1
            su=-1
su=a[0]
for i in xrange(1,n):
    tmp=su
    su+=a[i]
    if su*tmp<0:
        continue
    else:
        if su==0 and tmp==0:
            su+=1
            ans2+=1
        elif su==0 and tmp<0:
            ans2+=1
            su+=1
        elif su==0 and tmp>0:
            ans2+=1
            su-=1
        elif tmp<0 and su<0:
            ans2+=-su+1
            su=1
        elif tmp>0 and su>0:
            ans2+=su+1
            su=-1
print min(ans1,ans2)

Submission status: Wrong Answer

Solution code:
n = int(raw_input())
a = [int(_) for _ in raw_input().split()]
ans = 0
t = a[0]
if t == 0:
    if a[1] < 0:
        t = 1
    else:
        t = -1
    ans += 1
for i in xrange(1, n):
    b = cmp(t, 0)
    t += a[i]
    c = cmp(t, 0)
    d = 0
    if b == c or c == 0:
        d = -t - 1 if b == 1 else -t + 1
        ans += abs(d)
        t += d

print ans

Submission status: Wrong Answer

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

maenogoukei = a[0]
goukei = a[0]
kaisuu = 0
for a_i in a[1:]:
	goukei += a_i
	if maenogoukei > 0 and goukei >= 0:
		kaisuu += goukei +1
		goukei = -1
		maenogoukei = -1
	elif maenogoukei < 0 and goukei <= 0:
		kaisuu += -goukei +1
		goukei = 1
		maenogoukei = 1
	else:
		maenogoukei += a_i

print (kaisuu)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
def solve(a):
    s, res = 0, 0
    for i, ai in enumerate(a):
        ss = s
        ss += ai
        if ss == 0:
            ss = 1 if s < 0 else -1
            res += 1
        else:
            if s < 0 and ss < 0:
                res += abs(ss) + 1
                ss = 1
            if s > 0 and ss > 0:
                res += abs(ss) + 1
                ss = -1
        s = ss
    return res

n = int(raw_input())
a = map(int, raw_input().split()) 

if a[0] == 0:
    a[0] = 1
    ans = solve(a) + 1
    a[0] = -1
    ans = min(ans, solve(a) + 1)
else:
    ans = solve(a)

print ans