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:
# sys.stdin.readline
import sys

input = sys.stdin.readline


class AtCoder:
    def main(self):
        n = int(input())
        a = list(map(int, input().split()))

        even_plus_ans = 0
        before = a[0]
        if a[0] < 0:
            even_plus_ans -= a[0] - 1
            before = 1

        for i in range(1, n):
            if i % 2 == 0:
                v = a[i] + before
                if v > 0:
                    before = v
                elif v <= 0:
                    before = 1
                    even_plus_ans -= v - 1
            else:
                v = a[i] + before
                if v < 0:
                    before = v
                elif v >= 0:
                    before = -1
                    even_plus_ans += v + 1

        odd_plus_ans = 0
        before = a[0]
        if a[0] >= 0:
            odd_plus_ans += a[0] + 1
            before = -1

        for i in range(1, n):
            if i % 2 != 0:
                v = a[i] + before
                if v > 0:
                    before = v
                elif v <= 0:
                    before = 1
                    odd_plus_ans -= v - 1
            else:
                v = a[i] + before
                if v < 0:
                    before = v
                elif v >= 0:
                    before = -1
                    odd_plus_ans += v + 1

        print(min(even_plus_ans, odd_plus_ans))


# Run main
if __name__ == '__main__':
    AtCoder().main()

Submission status: Wrong Answer

Solution code:
# sys.stdin.readline
import sys

input = sys.stdin.readline


class AtCoder:
    def main(self):
        n = int(input())
        a = list(map(int, input().split()))

        ans = 0

        if a[0] == 0:
            if a[1] < 0:
                a[0] = 1
            else:
                a[0] = -1
            ans += 1

        for i in range(1, n):
            a[i] = a[i] + a[i - 1]
            if a[i - 1] > 0 and a[i] >= 0:
                ans += a[i] + 1
                a[i] = - 1
            elif a[i - 1] < 0 and a[i] < 0:
                ans += -1 * a[i] + 1
                a[i] = 1
            elif a[i] == 0:
                a[i] = 1
                ans += 1

        print(ans)


# Run main
if __name__ == '__main__':
    AtCoder().main()

Submission status: Wrong Answer

Solution code:
# sys.stdin.readline
import sys

input = sys.stdin.readline


class AtCoder:
    def main(self):
        n = int(input())
        a = list(map(int, input().split()))

        ans = 0

        if a[0] == 0:
            a[0] = 1
            ans += 1

        for i in range(1, n):
            a[i] = a[i] + a[i - 1]
            if a[i - 1] > 0 and a[i] >= 0:
                ans += a[i] + 1
                a[i] = - 1
            elif a[i - 1] < 0 and a[i] < 0:
                ans += -1 * a[i] + 1
                a[i] = 1
            elif a[i] == 0:
                a[i] = 1
                ans += 1

        print(ans)


# Run main
if __name__ == '__main__':
    AtCoder().main()

Submission status: Wrong Answer

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

def calc(A):
  ans = 0
  s = A[0]
  if s > 0:
    flag = 1
  elif s < 0:
    flag = -1
  for i in range(1,N):
    s += A[i]
    if flag == 1 and s >= 0:
      ans += s + 1
      s = -1
    elif flag == -1 and s <= 0:
      ans += 1 - s
      s = 1
    flag *= -1
  return ans

if A[0] != 0:
  print(calc(A))
else:
  print(min(calc([1]+A[1:]),calc([-1]+A[1:]))+1)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
n=int(input())
l=list(map(int,input().split()))
nsum=l[0]
op=0
for i in range(1,n):
    if nsum>0:
        if l[i]>-nsum-1:
            op+=abs(l[i]+nsum+1)
            l[i]=-nsum-1
            nsum=-1
        else:
            nsum+=l[i]
    elif nsum<0:
        if l[i]<-nsum+1:
            op+=abs(l[i]+nsum-1)
            l[i]=-nsum+1
            nsum=1
        else:
            nsum+=l[i]
print(op)

Submission status: Wrong Answer

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

ans = 0
acum = A[0]

for i in range(1, N):
    if (acum >= 0 and acum + A[i] >= 0) or (acum < 0 and acum + A[i] < 0) or (acum + A[i] == 0):
        # same signs
        ans += abs(acum + A[i]) + 1
        acum = 1 if acum < 0 else -1
        # print("a", acum)

    else:
        # different signs
        acum += A[i]
        # print("b", acum)

print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
a= (list(map(int,input().split())))
sm1=sm2=0
cnt1=cnt2=0
for i ,num in enumerate(a):
	sm1+=num
	if sm1<=0 and i%2==0:
		cnt1+=1-sm1
		sm1=1
	elif sm1>=0 and i%2!=0:
		cnt1+=1+sm1
		sm1=-1
		
	sm2+=num
	if sm2<=0 and i%2!=0:
		cnt2+=1-sm2
		sm2=1
	elif sm2>=0 and i%2==0:
		cnt2+=1+sm2
		sm2=-1
	print(min(cnt1,cnt2))

Submission status: Wrong Answer

Solution code:
n=int(input())
a= (list(map(int,input().split())))
sm1=sm2=0
cnt1=cnt2=0
for i ,num in enumerate(a,1):
	sm1+=num
	if sm1<1 and i%2!=0:
		cnt1+=1-sm1
		sm=1
	elif sm1>-1 and i%2==0:
		cnt1+=1+sm1
		sm=-1
		
	sm2+=num
	if sm2<1 and i%2==0:
		cnt2+=1-sm2
		sm=1
	elif sm2>-1 and i%2==1:
		cnt2+=1+sm2
		sm2=-1
print(min(cnt1,cnt2))

Submission status: Wrong Answer

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

cnt1=cnt2=sm1=sm2=0
for x,y in enumerate(a):
	sm1+=y
	if x%2 ==1:
		if sm1<1:
			cnt1 += 1-sm1
			sm1=1
		elif sm1>-1:
			cnt1+= 1+sm1
			sm1=-1
	
for x,y in enumerate(a):
	sm2+=y
	if x%2 ==0:
		if sm2<1:
			cnt2 += 1-sm2 
			sm2=1
		elif sm1>-1:
			cnt2 += 1+sm2
			sm2=-1
print(min(cnt1,cnt2))

Submission status: Wrong Answer

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

cnt1=cnt2=sm1=sm2=0
for x,y in enumerate(a):
	sm1+=y
	if x%2 ==1:
		if sm1<1:
			cnt1 += 1-sm1
			sm1=1
		elif sm1>-1:
			cnt2+= 1+sm2
			sm2=-1
	
for x,y in enumerate(a):
	sm2+=y
	if x%2 ==0:
		if sm2<1:
			cnt2 += 1-sm2 
			sm2=1
		elif sm1>-1:
			cnt1 += 1+sm1
			sm1=-1
print(min(cnt1,cnt2))

Submission status: Wrong Answer

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

cnt1,cnt2,sm1,sm2=0,0,0,0
for i,val in enumerate(a):#+
	sm1+=val
	if i%2 ==0:
		if sm1<1:
			cnt1+=1-sm1
			sm1=1
	else:
		if sm1>-1:
			cnt1+=1+sm1
			sm1=-1

for i,val in enumerate(a):#-
	sm2+=val
	if i%2 ==0:
		if sm2>-1:
			cnt2+=1+sm2
			sm2=-1
		else:
			if sm2<1:
				cnt2+=1-sm2
				sm2=1
				
print(min(cnt1,cnt2))

Submission status: Wrong Answer

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

cnt1,cnt2,sm1,sm2=0,0,0,0
for i in a:#+
	sm1+=i
	if a.index(i)%2 ==0:
		if sm1<1:
			cnt1+=1-sm1
			sm1=1
	else:
		if sm1>-1:
			cnt1+=1+sm1
			sm1=-1

for i in a:#-
	sm2=i
	if a.index(i)%2 ==0:
		if sm2>-1:
			cnt2+=1+sm2
			sm2=-1
		else:
			if sm2<1:
				cnt2+=1-sm2
				sm2=1
				
print(min(cnt1,cnt2))

Submission status: Wrong Answer

Solution code:
import sys
import copy
import  math
from _bisect import *
from collections import *
from operator import itemgetter
from math import factorial
"""
from fractions import gcd
def lcm(x, y):
    return (x * y) // gcd(x, y)
"""
stdin = sys.stdin
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()

n = ni()
li = na()
ans = [0, 0]
s = li[0]
for j in range(2):
    code = j
    for i in range(n - 1):
        code = 1 - code
        if code:
            if s + li[i + 1] > 0:
                s += li[i + 1]
            else:
                ans[j] += abs(1 - s)
                s = 1
        else:
            if s + li[i + 1] < 0:
                s += li[i + 1]
            else:
                ans[j] += abs(- 1 - s)
                s = -1
print(min(ans))

Submission status: Wrong Answer

Solution code:
import sys
import copy
import  math
from _bisect import *
from collections import *
from operator import itemgetter
from math import factorial
"""
from fractions import gcd
def lcm(x, y):
    return (x * y) // gcd(x, y)
"""
stdin = sys.stdin
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()

n = ni()
li = na()
ans = [0, 0]
s = li[0]
for j in range(2):
    code = j
    for i in range(n - 1):
        code = 1 - code
        if code:
            if s + li[i + 1] > 0:
                s += li[i + 1]
            else:
                ans[j] += abs(s * (-1) + 1 - li[i + 1])
                s = 1
        else:
            if s + li[i + 1] < 0:
                s += li[i + 1]
            else:
                ans[j] += abs(s * (-1) - 1 - li[i + 1])
                s = -1
print(min(ans))

Submission status: Wrong Answer

Solution code:
import sys
import copy
import  math
from _bisect import *
from collections import *
from operator import itemgetter
from math import factorial
"""
from fractions import gcd
def lcm(x, y):
    return (x * y) // gcd(x, y)
"""
stdin = sys.stdin
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()

n = ni()
li = na()
ans = 0
if li[0] > 0:
    code = 1
else:
    code = 0
s = li[0]
for i in range(n - 1):
    code = 1 - code
    if code:
        if s + li[i + 1] > 0:
            s += li[i + 1]
            continue
        else:
            ans += abs(s * (-1) + 1 - li[i + 1])
            s = 1
    else:
        if s + li[i + 1] < 0:
            s += li[i + 1]
            continue
        else:
            ans += abs(s * (-1) - 1 - li[i + 1])
            s = -1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())

a = [int(i) for i in input().split()]

ans = 0

tmp = a[0]

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

#print(ans)


ans2 = 0

if a[0] > 0:
    ans2 += a[0]+1
    tmp = -1
elif a[0] < 0:
    ans2 += -a[0]+1
else:
    tmp = -1
    ans2 += 1

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

print(min(ans,ans2))
exit()

ans2 = 0

tmp = -a[0]

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

print(min(ans,ans2))

Submission status: Wrong Answer

Solution code:
n = int(input())

a = [int(i) for i in input().split()]

ans = 0

tmp = a[0]

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

#print(ans)


ans2 = 0

if a[0] > 0:
    ans2 += a[0]+1
    tmp = -1
elif a[0] < 0:
    ans2 += -a[0]+1
else:
    tmp = 1
    ans2 += 1

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

print(min(ans,ans2))
exit()

ans2 = 0

tmp = -a[0]

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

print(min(ans,ans2))

Submission status: Wrong Answer

Solution code:
n = int(input())

a = [int(i) for i in input().split()]

ans = 0

tmp = a[0]

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

#print(ans)


ans2 = 0

if a[0] > 0:
    ans2 += a[0]+1
    tmp = 1
elif a[0] < 0:
    ans2 += -a[0]+1
else:
    tmp = -1
    ans2 += 1

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

print(min(ans,ans2))
exit()

ans2 = 0

tmp = -a[0]

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

print(min(ans,ans2))

Submission status: Wrong Answer

Solution code:
n = int(input())

a = [int(i) for i in input().split()]

ans = 0

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

#print(ans)



ans2 = 0

tmp = -a[0]

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

print(min(ans,ans2))

Submission status: Wrong Answer

Solution code:
n = int(input())

a = [int(i) for i in input().split()]

ans = 0

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

print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
import copy

n = int(input())

a = list(map(int,input().split()))
b=copy.deepcopy(a)

cntm=0
cntp=0
sum_a=0
sum_b=0

if a[0]<=0:
    cntm+=-a[0]+1
    a[0]=1
if b[0]>=0:
    cntp+=b[0]+1
    b[0]=-1
for i in range(n-1):
    sum_a += a[i]
    sum_b += b[i]
    if abs(sum_a) >= abs(a[i+1]) or sum_a*a[i+1]>=0:
            cntp += abs(-sum_a-np.sign(sum_a) -a[i+1])
            a[i+1]=-sum_a-np.sign(sum_a)
    if abs(sum_b) >= abs(b[i+1]) or sum_b*b[i+1]>=0:
            cntm += abs(-sum_b-np.sign(sum_b) -b[i+1])
            b[i+1]=-sum_b-np.sign(sum_b)

print(min(cntm, cntp))

Submission status: Wrong Answer

Solution code:
import numpy as np
import copy

n = int(input())

a = list(map(int,input().split()))
b=copy.deepcopy(a)

cntm=0
cntp=0
sum_a=0
sum_b=0

if a[0]==0:
    cntm+=1
    cntp+=1
    a[0]=1
    b[0]=-1
for i in range(n-1):
    sum_a += a[i]
    sum_b += b[i]
    if abs(sum_a) >= abs(a[i+1]) or sum_a*a[i+1]>=0:
            cntp += abs(-sum_a-np.sign(sum_a) -a[i+1])
            a[i+1]=-sum_a-np.sign(sum_a)
    if abs(sum_b) >= abs(b[i+1]) or sum_b*b[i+1]>=0:
            cntm += abs(-sum_b-np.sign(sum_b) -b[i+1])
            b[i+1]=-sum_b-np.sign(sum_b)
print(min(cntm, cntp))

Submission status: Wrong Answer

Solution code:
import numpy as np

n = int(input())

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

cnt=0
sum_a=0
if a[0]==0:
    cnt+=1
    a[0]=1
for i in range(n-1):
    sum_a += a[i]
    if abs(sum_a) >= abs(a[i+1]) or sum_a*a[i+1]>=0:
            cnt += abs(-sum_a-np.sign(sum_a) -a[i+1])
            a[i+1]=-sum_a-np.sign(sum_a)
print(cnt)

Submission status: Wrong Answer

Solution code:
import numpy as np

n = int(input())

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

cnt=0
sum_a=0

for i in range(n-1):
    sum_a += a[i]
    if abs(sum_a) >= abs(a[i+1]) or sum_a*a[i+1]>=0:
            cnt += abs(-sum_a-np.sign(sum_a) -a[i+1])
            a[i+1]=-sum_a-np.sign(sum_a)
print(cnt)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split(' ')))
result = 0
h = 0
if a[0] == 0:
    a[0] += 1
    result += 1
    h = 1
counter = a[0]

for i in range(1, n):
    if counter < 0:
        counter += a[i]
        if counter == 0:
            counter += 1
            result +=1
        elif counter > 0:
            continue
        else:
            result += 1-counter
            counter = 1
    else:
        counter += a[i]
        if counter == 0:
            counter -= 1
            result += 1
        elif counter < 0:
            continue
        else:
            result += counter+1
            counter = -1
out = []
out.append(result)
result = 0
if h == 1:
    result = 1
if a[0] > 0:
    result += a[0] +1
    counter = -1
else:
    result += 1-a[0]
    counter = 1

for i in range(1, n):
    if counter < 0:
        counter += a[i]
        if counter == 0:
            counter += 1
            result += 1
        elif counter > 0:
            continue
        else:
            result += 1-counter
            counter = 1
    else:
        counter += a[i]
        if counter == 0:
            counter -= 1
            result +=1
        elif counter < 0:
            continue
        else:
            result += counter+1
            counter = -1
out.append(result)
print(min(out))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split(' ')))
result = 0
h = 0
if a[0] == 0:
    a[0] += 1
    result += 1
    h = 1
counter = a[0]

for i in range(1, n):
    if counter < 0:
        counter += a[i]
        if counter > 0:
            continue
        else:
            result += 1-counter
            counter = 1
    else:
        counter += a[i]
        if counter < 0:
            continue
        else:
            result += counter+1
            counter = -1
out = []
out.append(result)
result = 0
if h == 1:
    result = 1
if a[0] > 0:
    result += a[0] +1
    counter = -1
else:
    result += 1-a[0]
    counter = 1

for i in range(1, n):
    if counter < 0:
        counter += a[i]
        if counter > 0:
            continue
        else:
            result += 1-counter
            counter = 1
    else:
        counter += a[i]
        if counter < 0:
            continue
        else:
            result += counter+1
            counter = -1
out.append(result)
print(min(out))

Submission status: Wrong Answer

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

sa = np.zeros(2)
count = np.zeros(2)

for i in range(n):
    sa += a[i]
    if sa[0] <= 0 and i%2 == 0:
        count[0] += 1 - sa[0]
        sa[0] = 1
    elif sa[0] >= 0 and i%2 == 1:
        count[0] += 1 + sa[0]
        sa[0] = -1

    if sa[1] <= 0 and i%2 == 1:
        count[1] += 1 - sa[1]
        sa[1] = 1
    elif sa[1] >= 0 and i%2 == 0:
        count[1] += 1 + sa[1]
        sa[1] = -1

print(min(count))

Submission status: Wrong Answer

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

sa = 0
count = [0, 0]

for i in range(n):
    now = a[i]
    sa += now
    if sa <= 0 and i%2 == 0:
        count[0] += 1 - sa
        sa = 1
    elif sa >= 0 and i%2 == 1:
        count[0] += 1 + sa
        sa = -1

sa = 0

for i in range(n):
    now = a[i]
    sa += now
    if sa <= 0 and i%2 == 1:
        count[1] += 1 - sa
        sa = 1
    elif sa >= 0 and i%2 == 0:
        count[1] += 1 + sa
        sa = -1

print(count)

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
#input = open(sys.argv[1], "r").readline

def main():
    N = int(input())
    A = list(map(int, input().split()))
    tmp = A.copy()
    mi = -1
    for j in range(2):
        A = tmp.copy()
        s = 0
        n = 0
        if j == 0: # 偶数番目までの和が正 ⇒ A[0] を負にする
            if A[0] > 0:
                n += abs(A[0] +1)
                A[0] = -1
        else:   # 奇数番目までの和が正 ⇒ A[0] を正にする
            if A[0] < 0:
                n += abs(A[0] -1)
                A[0] = 1
        s = A[0]
#        print(A)
        for i in range(1,N):
            if s * (s+A[i]) >= 0:
                if s < 0:
                    n += abs(-s+1 -A[i])
                    A[i] = -s+1
                else:
                    n += abs(-s-1 -A[i])
                    A[i] = -s-1
            s += A[i]
        if mi == -1:
            mi = n
        else:
            mi = min(mi, n)
    print(mi)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline

def main():
    N = int(input())
    A = list(map(int, input().split()))
    tmp = A.copy()
    mi = 10**9
    for j in range(2):
        A = tmp.copy()
        s = 0
        n = 0
        if j == 0: # 偶数番目までの和が正 ⇒ A[0] を負にする
            if A[0] > 0:
                n += abs(A[0] +1)
                A[0] = -1
        else:   # 奇数番目までの和が正 ⇒ A[0] を正にする
            if A[0] < 0:
                n += abs(A[0] -1)
                A[0] = 1
        s = A[0]
        for i in range(1,N):
            if s * (s+A[i]) >= 0:
                if s < 0:
                    n += abs(-s+1 -A[i])
                    A[i] = -s+1
                else:
                    n += abs(-s-1 -A[i])
                    A[i] = -s-1
            s += A[i]
        mi = min(mi, n)
    print(mi)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline

def main():
    N = int(input())
    A = list(map(int, input().split()))
    s = A[0]
    n = 0
    for i in range(1,N):
        if s * (s+A[i]) >= 0:
            if s < 0:
                n += abs(-s+1 -A[i])
                A[i] = -s+1
            else:
                n += abs(-s-1 -A[i])
                A[i] = -s-1
        s += A[i]
    print(n)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

for i in range(n-1):
  total = prv_total + a[i]
  nxt_total = total+a[i+1]
  
  if total == 0 and i != 0:
    if prv_total > 0:
      cnt += 1
      total -= 1
      nxt_total -= 1
    else:
      cnt += 1
      total += 1
      nxt_total += 1
  
  if total > 0 and nxt_total >= 0:
    a[i+1] -= nxt_total+1
    cnt += nxt_total+1
    nxt_total -= nxt_total+1
  elif total < 0 and nxt_total <=0:
    a[i+1] += abs(nxt_total)+1
    cnt += abs(nxt_total)+1
    nxt_total += abs(nxt_total)+1
  prv_total = total

total = prv_total + a[-1]
if total == 0:
  cnt += 1

print(cnt)

Submission status: Wrong Answer

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

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

for i in range(n-1):
  total = prv_total + a[i]
  nxt_total = total+a[i+1]
  
  if total == 0 and i != 0:
    if prv_total > 0:
      cnt += 1
      total -= 1
    else:
      cnt += 1
      total += 1
  
  if total > 0 and nxt_total >= 0:
    a[i+1] -= nxt_total+1
    cnt += nxt_total+1
    nxt_total -= nxt_total+1
  elif total < 0 and nxt_total <=0:
    a[i+1] += abs(nxt_total)+1
    cnt += abs(nxt_total)+1
    nxt_total += abs(nxt_total)+1
  prv_total = total

total = prv_total + a[-1]
if total == 0:
  cnt += 1

print(cnt)

Submission status: Wrong Answer

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

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

for i in range(n-1):
  total = prv_total + a[i]
  nxt_total = total+a[i+1] 
  if total > 0 and nxt_total >= 0:
    a[i+1] -= nxt_total+1
    cnt += nxt_total+1
    nxt_total -= nxt_total+1
  elif total < 0 and nxt_total <=0:
    a[i+1] += abs(nxt_total)+1
    cnt += abs(nxt_total)+1
    nxt_total += abs(nxt_total)+1
  prv_total = total

total = prv_total + a[-1]
if total == 0:
  cnt += 1

print(cnt)

Submission status: Wrong Answer

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

total = prv_total + a[-1]
if total == 0:
  cnt += 1
  
print(cnt)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
import numpy as np
na1 = np.array(a).cumsum()
na2 = np.array(a).cumsum()
cnt1 = 0
hoge1 = 0
cnt2 = 0
hoge2 = 0

for i in range(1, n):
  na1[i] += hoge1
  na2[i] += hoge2
  if(i % 2 == 0 and na1[i] <= 0):
    delta1 = abs(na1[i]) + 1
    cnt1 = cnt1 + delta1
    hoge1 += delta1
  elif(i % 2 == 1 and na1[i] >= 0):
    delta1 = abs(na1[i]) + 1 
    cnt1 = cnt1 + delta1
    hoge1 -= delta1
  if(i % 2 == 1 and na2[i] <= 0):
    delta2 = abs(na2[i]) + 1
    cnt2 = cnt2 + delta2
    hoge2 += delta2
  elif(i % 2 == 0 and na2[i] >= 0):
    delta2 = abs(na2[i]) + 1
    cnt2 = cnt2 + delta2
    hoge2 -= delta2
  else:
    na1[i]
print(min([cnt1,cnt2]))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
import numpy as np
na1 = np.array(a).cumsum()
na2 = np.array(a).cumsum()
cnt1 = 0
hoge1 = 0
cnt2 = 0
hoge2 = 0

for i in range(1, n):
  na1[i] += hoge1
  na2[i] += hoge2
  if(i % 2 == 0 and na1[i] <= 0):
    delta1 = abs(na1[i]) + 1
    delta2 = abs(na2[i]) + 1
    cnt1 = cnt1 + delta1
    hoge1 += delta1
  elif(i % 2 == 1 and na1[i] >= 0):
    delta1 = abs(na1[i]) + 1
    delta2 = abs(na2[i]) + 1  
    cnt1 = cnt1 + delta1
    hoge1 -= delta1
  if(i % 2 == 1 and na2[i] <= 0):
    delta1 = abs(na1[i]) + 1
    delta2 = abs(na2[i]) + 1
    cnt2 = cnt2 + delta2
    hoge2 += delta2
  elif(i % 2 == 0 and na2[i] >= 0):
    delta1 = abs(na1[i]) + 1
    delta2 = abs(na2[i]) + 1
    cnt2 = cnt2 + delta2
    hoge2 -= delta2
  else:
    na1[i]
print(min([cnt1,cnt2]))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
import numpy as np
na = np.array(a).cumsum()
cnt = 0
hoge = 0
if(na[0] > 0):
  for i in range(n):
    delta = abs(na[i]) + 1
    na[i] += hoge
    if(i % 2 == 0 and na[i] <= 0):
      cnt = cnt + delta
      hoge += delta
    elif(i % 2 == 1 and na[i] >= 0):
      cnt = cnt + delta
      hoge -= delta
    else:
      na[i]
else:
  for i in range(n):
    delta = abs(na[i]) + 1
    na[i] += hoge
    if(i % 2 == 1 and na[i] <= 0):
      cnt = cnt + delta
      hoge += delta
    elif(i % 2 == 0 and na[i] >= 0):
      cnt = cnt + delta
      hoge -= delta
    else:
      na[i]
print(cnt)

Submission status: Wrong Answer

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