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:
import numpy
n=int(input())
a=list(map(int,input().split()))
sum=a[0]
ans=0
z=1 if(sum<0) else -1
for i in range(1,n):
        if(numpy.sign(sum+a[i])==numpy.sign(sum) or sum+a[i]==0):
                tmp=1*z-sum
                ans+=abs(tmp-a[i])
                a[i]=tmp
        sum+=a[i]
        z*=-1
print(ans)

Submission status: Wrong Answer

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

ans = 0

flag = 0
if A[0] > 0:
    flag = 1
elif A[0] == 0:
    flag = 1
    ans += 1
else:
    flag = 0

acc = A[0]
for i in range(1, N):
    acc = acc+A[i]
    if i % 2 == flag:
        if acc >= 0:
            A[i] = A[i] - (1 + acc)
            ans += 1+acc
            acc = -1
    else:
        if acc <= 0:
            A[i] = A[i]+(1-acc)
            ans += 1-acc
            acc = 1

print(ans)

Submission status: Wrong Answer

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

ans = 0

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

print(ans)

Submission status: Wrong Answer

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

sum1 = A[0]
ans = 0
for i in range(1,N) :
    sum2 = sum1 + A[i]
    if sum2 * sum1 > 0 :
        if sum1 > 0 :
            ans += sum2 + 1
            sum2 = -1
        else :
            ans += -(sum2 - 1)
            sum2 = 1
    elif sum2 == 0 :
        if sum1 > 0 :
            sum2 -= 1
        else :
            sum2 += 1
        ans += 1
    sum1 = sum2
    #print(ans,sum1)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
ans1 = abs(a[0]-1)
ans2 = abs(a[0]+1)
ans3 = 0
def f(x,y):
  for i in range(1,n):
    if y > 0:
      y += a[i]
      if y < 0:
        continue
      else:
        x += abs(y+1)
        y = -1
    else:
      y += a[i]
      if y > 0:
        continue
      else:
        x += abs(y-1)
        y = 1
  return x
if a[0] == 0:
  ans3 = float("inf")
else:
  ans3 = f(ans3,a[0])
print(min(ans3,f(ans1,-1),f(ans2,1)))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
if a[0] == 0:
  ans = 1
  SUM = 1
  for i in range(1,n):
    if SUM > 0:
      SUM += a[i]
      if SUM < 0:
        continue
      else:
        ans += (SUM+1)
        SUM = -1
    else:
      SUM += a[i]
      if SUM >0:
        continue
      else:
        ans += -SUM+1
        SUM = 1
  ans2 = 1
  SUM = -1
  for i in range(1,n):
    if SUM > 0:
      SUM += a[i]
      if SUM < 0:
        continue
      else:
        ans2 += (SUM+1)
        SUM = -1
    else:
      SUM += a[i]
      if SUM >0:
        continue
      else:
        ans2 += -SUM+1
        SUM = 1
  print(min(ans,ans2))
  exit()
ans = 0
SUM = a[0]
for i in range(1,n):
  if SUM > 0:
    SUM += a[i]
    if SUM < 0:
      continue
    else:
      ans += (SUM+1)
      SUM = -1
  else:
    SUM += a[i]
    if SUM >0:
      continue
    else:
      ans += abs(SUM)+1
      SUM = 1
ans2 = abs(a[0])+1
SUM = -(a[0]//a[0])
for i in range(1,n):
  if SUM > 0:
    SUM += a[i]
    if SUM < 0:
      continue
    else:
      ans2 += (SUM+1)
      SUM = -1
  else:
    SUM += a[i]
    if SUM >0:
      continue
    else:
      ans2 += abs(SUM)+1
      SUM = 1
ans3 = abs(a[0])-1
SUM = (a[0]//a[0])
for i in range(1,n):
  if SUM > 0:
    SUM += a[i]
    if SUM < 0:
      continue
    else:
      ans3 += (SUM+1)
      SUM = -1
  else:
    SUM += a[i]
    if SUM >0:
      continue
    else:
      ans3 += abs(SUM)+1
      SUM = 1
print(min(ans2,ans,ans3))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
if a[0] == 0:
  ans = 1
  SUM = 1
  for i in range(1,n):
    if SUM > 0:
      SUM += a[i]
      if SUM < 0:
        continue
      else:
        ans += (SUM+1)
        SUM = -1
    else:
      SUM += a[i]
      if SUM >0:
        continue
      else:
        ans += -SUM+1
        SUM = 1
  ans2 = 1
  SUM = -1
  for i in range(1,n):
    if SUM > 0:
      SUM += a[i]
      if SUM < 0:
        continue
      else:
        ans2 += (SUM+1)
        SUM = -1
    else:
      SUM += a[i]
      if SUM >0:
        continue
      else:
        ans2 += -SUM+1
        SUM = 1
  print(min(ans,ans2))
  exit()
ans = 0
SUM = a[0]
for i in range(1,n):
  if SUM > 0:
    SUM += a[i]
    if SUM < 0:
      continue
    else:
      ans += (SUM+1)
      SUM = -1
  else:
    SUM += a[i]
    if SUM >0:
      continue
    else:
      ans += abs(SUM)+1
      SUM = 1
ans2 = abs(a[0])+1
SUM = -(a[0]//a[0])
for i in range(1,n):
  if SUM > 0:
    SUM += a[i]
    if SUM < 0:
      continue
    else:
      ans2 += (SUM+1)
      SUM = -1
  else:
    SUM += a[i]
    if SUM >0:
      continue
    else:
      ans2 += abs(SUM)+1
      SUM = 1
print(min(ans2,ans))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
if a[0] == 0:
  ans = 1
  SUM = 1
  for i in range(1,n):
    if SUM > 0:
      SUM += a[i]
      if SUM < 0:
        continue
      else:
        ans += (SUM+1)
        SUM = -1
    else:
      SUM += a[i]
      if SUM >0:
        continue
      else:
        ans += -SUM+1
        SUM = 1
  ans2 = 1
  SUM = -1
  for i in range(1,n):
    if SUM > 0:
      SUM += a[i]
      if SUM < 0:
        continue
      else:
        ans2 += (SUM+1)
        SUM = -1
    else:
      SUM += a[i]
      if SUM >0:
        continue
      else:
        ans2 += -SUM+1
        SUM = 1
  print(min(ans,ans2))
  exit()
ans = 0
SUM = a[0]
for i in range(1,n):
  if SUM > 0:
    SUM += a[i]
    if SUM < 0:
      continue
    else:
      ans += (SUM+1)
      SUM = -1
  else:
    SUM += a[i]
    if SUM >0:
      continue
    else:
      ans += -SUM+1
      SUM = 1
ans2 = abs(a[0])+1
SUM = -a[0]//a[0]
for i in range(1,n):
  if SUM > 0:
    SUM += a[i]
    if SUM < 0:
      continue
    else:
      ans2 += (SUM+1)
      SUM = -1
  else:
    SUM += a[i]
    if SUM >0:
      continue
    else:
      ans2 += -SUM+1
      SUM = 1
print(min(ans2,ans))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
if a[0] == 0:
  ans = 1
  SUM = 1
  for i in range(1,n):
    if SUM > 0:
      SUM += a[i]
      if SUM < 0:
        continue
      else:
        ans += (SUM+1)
        SUM = -1
    else:
      SUM += a[i]
      if SUM >0:
        continue
      else:
        ans += -SUM+1
        SUM = 1
  ans2 = 1
  SUM = -1
  for i in range(1,n):
    if SUM > 0:
      SUM += a[i]
      if SUM < 0:
        continue
      else:
        ans2 += (SUM+1)
        SUM = -1
    else:
      SUM += a[i]
      if SUM >0:
        continue
      else:
        ans2 += -SUM+1
        SUM = 1
  print(min(ans,ans2))
  exit()
ans = 0
SUM = a[0]
for i in range(1,n):
  if SUM > 0:
    SUM += a[i]
    if SUM < 0:
      continue
    else:
      ans += (SUM+1)
      SUM = -1
  else:
    SUM += a[i]
    if SUM >0:
      continue
    else:
      ans += -SUM+1
      SUM = 1
print(ans)

Submission status: Wrong Answer

Solution code:
from operator import itemgetter
import numpy as np

N = int(input())
sum=0
sum2=0
count=0
count2=0


s = input().split()

for i in range(N):
    c = int(s[i])
    if i==0:
        sum=c
        count2=abs(c)+1
        sum2=np.sign(c)*(-1)
    else:
        if sum*(sum+c)>=0:
            count += abs(sum+c)+1
            sum = -1 * np.sign(sum)
        else:
            sum+=c

        if sum2 * (sum2 + c) >= 0:
            count2 += abs(sum2 + c) + 1
            sum2 = -1 * np.sign(sum2)
        else:
            sum2+=c

print(min(count,count2))

Submission status: Wrong Answer

Solution code:
from operator import itemgetter
import numpy as np

N = int(input())
sum=0
sum2=0
count=0
count2=0


s = input().split()

for i in range(N):
    c = int(s[i])
    if i==0:
        sum=c
        count2=abs(c)+1
        sum2=np.sign(c)*(-1)
    else:
        if sum*(sum+c)>=0:
            count += abs(sum+c)+1
            sum = -1 * np.sign(sum)
        else:
            sum=sum+c

        if sum2 * (sum2 + c) >= 0:
            count2 += abs(sum2 + c) + 1
            sum2 = -1 * np.sign(sum2)
        else:
            sum2 = sum2 + c

print(min(count,count2))

Submission status: Wrong Answer

Solution code:
from operator import itemgetter
import numpy as np

N = int(input())
sum=0
sum2=0
count=0
count2=0


s = input().split()

for i in range(N):
    c = int(s[i])
    if i==0:
        sum=c
        count2=abs(c+1)
        sum2=np.sign(c)*(-1)
    else:
        if sum*(sum+c)>=0:
            count += abs(sum+c)+1
            sum = -1 * np.sign(sum)
        else:
            sum=sum+c

        if sum2 * (sum2 + c) >= 0:
            count2 += abs(sum2 + c) + 1
            sum2 = -1 * np.sign(sum2)
        else:
            sum2 = sum2 + c

print(min(count,count2))

Submission status: Wrong Answer

Solution code:
from operator import itemgetter
import numpy as np

N = int(input())
sum=0
sum2=0
count=0
count2=0


s = input().split()

for i in range(N):
    c = int(s[i])
    if i==0:
        sum=c
        count2=np.abs(c+1)
        sum2=np.sign(c)*(-1)
    else:
        if sum*(sum+c)>=0:
            count += np.abs(sum+c)+1
            sum = -1 * np.sign(sum)
        else:
            sum=sum+c

        if sum2 * (sum2 + c) >= 0:
            count2 += np.abs(sum2 + c) + 1
            sum2 = -1 * np.sign(sum2)
        else:
            sum2 = sum2 + c

print(min(count,count2))

Submission status: Wrong Answer

Solution code:
from operator import itemgetter
import numpy as np

N = int(input())
sum=0
sum2=0
count=0
count2=0


s = input().split()

for i in range(N):
    c = int(s[i])
    if i==0:
        sum=c
        count2=np.abs(c+1)
        sum2=np.sign(c)*(-1)
    else:
        if np.sign(sum)*np.sign(sum+c)>=0:
            count += np.abs(sum+c)+1
            sum = -1 * np.sign(sum)
        else:
            sum=sum+c

        if np.sign(sum2) * np.sign(sum2 + c) >= 0:
            count2 += np.abs(sum2 + c) + 1
            sum2 = -1 * np.sign(sum2)
        else:
            sum2 = sum2 + c

print(min(count,count2))

Submission status: Wrong Answer

Solution code:
from operator import itemgetter
import numpy as np

N = int(input())
sum=0
count=0


s = input().split()

for i in range(N):
    c = int(s[i])
    if i==0:
        sum=c
    else:
        if np.sign(sum)*np.sign(sum+c)>=0:
            count += np.abs(sum+c)+1
            sum = -1 * np.sign(sum)
        else:
            sum=sum+c

print(count)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().split()))
s=0
t=0
a=0
b=0
for i in range(N):
    s+=A[i]
    if i%2==0:
        if s>=0:
            a+=s+1
            s=-1
    if i%2!=0:
        if s<=0:
            a+=-s+1
            s=1
for j in range(N):
    t+=A[i]
    if j%2!=0:
        if t>=0:
            b+=t+1
            t=-1
    if j%2==0:
        if t<=0:
            b+=-t+1
            t=1
print(min(a,b))

Submission status: Wrong Answer

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

def calc(tmp, cnt=0):
    for i in a[1:]:
        if tmp < 0:
            tmp = tmp + i
            if tmp <= 0:
                cnt += -tmp + 1
                tmp = 1
        elif tmp > 0:
            tmp = tmp + i
            if tmp >= 0:
                cnt += tmp + 1
                tmp = -1
    return cnt

if a[0]:
    hugou = 1 if a[0] > 0 else -1
    min(calc(a[0]), calc(-hugou, abs(a[0]) + 1))
else:
    print(min(calc(1, 1), calc(-1, 1)))

Submission status: Wrong Answer

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

def calc(tmp, cnt=0):
    for i in a[1:]:
        if tmp < 0:
            tmp = tmp + i
            if tmp <= 0:
                cnt += -tmp + 1
                tmp = 1
        elif tmp > 0:
            tmp = tmp + i
            if tmp >= 0:
                cnt += tmp + 1
                tmp = -1
    return cnt

if a[0]:
    print(min(calc(a[0]),calc(int(-1*abs(a[0])/a[0]),abs(a[0]+1))))
else:
    print(min(calc(1, 1), calc(-1, 1)))

Submission status: Wrong Answer

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

def calc(tmp, cnt=0):
    for i in a[1:]:
        if tmp < 0:
            tmp = tmp + i
            if tmp <= 0:
                cnt += -tmp + 1
                tmp = 1
        elif tmp > 0:
            tmp = tmp + i
            if tmp >= 0:
                cnt += tmp + 1
                tmp = -1
    return cnt

if a[0]:
    print(calc(a[0]))
else:
    print(min(calc(1, 1), calc(-1, 1)))

Submission status: Wrong Answer

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

def calc(tmp, cnt=0):
    for i in a[1:]:
        if tmp < 0:
            tmp = tmp + i
            if tmp <= 0:
                cnt += -tmp + 1
                tmp = 1
        elif tmp > 0:
            tmp = tmp + i
            if tmp >= 0:
                cnt += tmp + 1
                tmp = -1
    return cnt

if a[0]:
    print(calc(a[0]))
else:
    print(min(calc(1, -1), calc(1, 1)))

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int, input().split()))
h=0
nw=a[0]
ans=0
for i in range(1,n):
    if nw>0:
        h=min(0,-1-nw-a[i])
        ans+=abs(h)
        nw+=a[i]+h
    else:
        h=max(0,1-nw-a[i])
        ans+=abs(h)
        nw+=a[i]+h
print(ans)

Submission status: Wrong Answer

Solution code:
def osilate(seq):
    flag = 1
    acc = 0
    ans = 0
    while len(seq) > 0:
        #print(seq,acc,ans)
        a = seq[0]
        acc += a
        if flag > 0:
            if acc <= 0:
                ans += 1-acc
                acc = 1
        if flag < 0:
            if a >= 0:
                ans += 1+acc
                acc = -1
        seq.pop(0)
        flag = flag * (-1)
    return ans

n = int(input())
seq = [int(i) for i in input().split()] + [0]
acc = []
tmp = 0

seq_rev = [-i for i in seq]
print(min(osilate(seq),osilate(seq_rev)))

Submission status: Wrong Answer

Solution code:
n = int(input())
lis = list(map(int,input().split()))
cou = 0
co = 0
ans1 = 0
ans2 = 0
for i in range(n):
  cou += lis[i]
  co += lis[i]
  if cou < 0 and i % 2 == 0:
    ans1 += (1 -cou)
    cou = 1
  if cou > 0 and i % 2 == 1:
    ans1 += (cou +1)
    cou = -1
  if co > 0 and i % 2 == 0:
    ans2 += (co +1)
    co = -1
  if co < 0 and i % 2 == 1:
    ans2 += (1 -co)
    co = 1
print(min(ans1,ans2))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

ret = [0,0]
sum = [0,0]

if a[0]==0:
    sum[0] = 1
    ret[0] = 1
    sum[1] = -1
    ret[1] = 1
elif a[0]>0:
    sum[0] = a[0]
    sum[1] = -1*a[0]
    ret[1] = a[0]+1
else:
    sum[0] = -1*a[0]
    ret[0] = -1*a[0]+1
    sum[1] = a[0]

for i in range(1,n):
    for j in range(2):
        if sum[j]*(sum[j]+a[i])<0:
            sum[j] += a[i]
        elif sum[j]+a[i]==0:
            if sum[j]>0:
                sum[j] = -1
            else:
                sum[j] = 1
            ret[j] += 1
        elif sum[j]+a[i]>0:
            ret[j] += sum[j]+a[i]+1
            sum[j] = -1
        else:
            ret[j] += -1*(sum[j]+a[i])+1
            sum[j] = 1

print(min(ret[0],ret[1]))

Submission status: Wrong Answer

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

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


ret = 0
sum = a[0]
for i in range(1,n):
    if sum*(sum+a[i])<0:
        sum += a[i]
        continue
    if sum+a[i]==0:
        if sum>0:
            sum = sum+a[i]-1
        else:
            sum = sum+a[i]+1
        ret += 1
        continue
    if sum+a[i]>0:
        ret += sum+a[i]+1
        sum = -1
    else:
        ret += -1*(sum+a[i])+1
        sum = 1
print(ret)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(_) for _ in input().split()]

def calc(A, y):
    result = abs(A[0] - y)
    t = y
    if t == 0:
        return 10**30
    for a in A[1:N]:
        tt = t + a
        if t * tt >= 0:
            m =  -t // abs(t)
            result += abs(m - tt)
            tt = m
        t = tt
    return result

result = min(calc(A, A[0]), calc(A, -A[1]-1),  calc(A, -A[1]+1))

print(result)

Submission status: Wrong Answer

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

count0 = 0
prevSum = a[0]
if prevSum == 0:
    count0 = 1
    prevSum = 1
for i in range(1, len(a)) :
    nextSum = prevSum + a[i]

    addvalue = 0
    if prevSum > 0 and nextSum >= 0:
        addvalue = - nextSum - 1
    elif prevSum < 0 and nextSum <= 0:
        addvalue = - nextSum + 1
    
    prevSum = nextSum + addvalue
    count0 += abs(addvalue)

count1 = 0
prevSum = a[0]
if prevSum == 0:
    count1 = 1
    prevSum = -1
for i in range(1, len(a)) :
    nextSum = prevSum + a[i]

    addvalue = 0
    if prevSum > 0 and nextSum >= 0:
        addvalue = - nextSum - 1
    elif prevSum < 0 and nextSum <= 0:
        addvalue = - nextSum + 1
    
    prevSum = nextSum + addvalue
    count1 += abs(addvalue)

count2 = 0
prevSum = a[0]
if prevSum == 0:
    count2 = 1
    prevSum = 1
for i in range(1, len(a)) :
    nextSum = prevSum + a[i]

    addvalue = 0
    if prevSum > 0 and nextSum >= 0:
        addvalue = - nextSum - 1
    elif prevSum < 0 and nextSum <= 0:
        addvalue = - nextSum + 1
    
    prevSum = nextSum
    count2 += abs(addvalue)

count3 = 0
prevSum = a[0]
if prevSum == 0:
    count3 = 1
    prevSum = -1
for i in range(1, len(a)) :
    nextSum = prevSum + a[i]

    addvalue = 0
    if prevSum > 0 and nextSum >= 0:
        addvalue = - nextSum - 1
    elif prevSum < 0 and nextSum <= 0:
        addvalue = - nextSum + 1
    
    prevSum = nextSum
    count3 += abs(addvalue)

print(min(count0, count1, count2, count3))

Submission status: Wrong Answer

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

count0 = 0
prevSum = a[0]
"""if prevSum == 0:
    count0 = 1
    prevSum = 1"""
for i in range(1, len(a)) :
    nextSum = prevSum + a[i]

    addvalue = 0
    if prevSum > 0 and nextSum >= 0:
        addvalue = - nextSum - 1
    elif prevSum < 0 and nextSum <= 0:
        addvalue = - nextSum + 1
    
    prevSum = nextSum + addvalue
    count0 += abs(addvalue)

count1 = 0
prevSum = a[0]
"""
if prevSum == 0:
    count1 = 1
    prevSum = -1"""
for i in range(1, len(a)) :
    nextSum = prevSum + a[i]

    addvalue = 0
    if prevSum > 0 and nextSum >= 0:
        addvalue = - nextSum - 1
    elif prevSum < 0 and nextSum <= 0:
        addvalue = - nextSum + 1
    
    prevSum = nextSum + addvalue
    count1 += abs(addvalue)

count2 = 0
prevSum = a[0]
"""
if prevSum == 0:
    count2 = 1
    prevSum = 1"""
for i in range(1, len(a)) :
    nextSum = prevSum + a[i]

    addvalue = 0
    if prevSum > 0 and nextSum >= 0:
        addvalue = - nextSum - 1
    elif prevSum < 0 and nextSum <= 0:
        addvalue = - nextSum + 1
    
    prevSum = nextSum
    count2 += abs(addvalue)

count3 = 0
prevSum = a[0]
"""if prevSum == 0:
    count3 = 1
    prevSum = -1"""
for i in range(1, len(a)) :
    nextSum = prevSum + a[i]

    addvalue = 0
    if prevSum > 0 and nextSum >= 0:
        addvalue = - nextSum - 1
    elif prevSum < 0 and nextSum <= 0:
        addvalue = - nextSum + 1
    
    prevSum = nextSum
    count3 += abs(addvalue)

print(min(count0, count1, count2, count3))

Submission status: Wrong Answer

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

count0 = 0
prevSum = a[0]
if prevSum == 0:
    count0 = 1
    prevSum = 1
for i in range(1, len(a)) :
    nextSum = prevSum + a[i]

    addvalue = 0
    if prevSum > 0 and nextSum >= 0:
        addvalue = - nextSum - 1
    elif prevSum < 0 and nextSum <= 0:
        addvalue = - nextSum + 1
    
    prevSum = nextSum + addvalue
    count0 += abs(addvalue)

count1 = 0
prevSum = a[0]
if prevSum == 0:
    count1 = 1
    prevSum = -1
for i in range(1, len(a)) :
    nextSum = prevSum + a[i]

    addvalue = 0
    if prevSum > 0 and nextSum >= 0:
        addvalue = - nextSum - 1
    elif prevSum < 0 and nextSum <= 0:
        addvalue = - nextSum + 1
    
    prevSum = nextSum + addvalue
    count1 += abs(addvalue)


count2 = 0
prevSum = a[0]
if prevSum == 0:
    count0 = 1
    prevSum = 1
for i in range(1, len(a)) :
    nextSum = prevSum + a[i]

    addvalue = 0
    if prevSum > 0 and nextSum >= 0:
        addvalue = - nextSum - 1
    elif prevSum < 0 and nextSum <= 0:
        addvalue = - nextSum + 1
    
    prevSum = nextSum
    count2 += abs(addvalue)

count3 = 0
prevSum = a[0]
if prevSum == 0:
    count1 = 1
    prevSum = -1
for i in range(1, len(a)) :
    nextSum = prevSum + a[i]

    addvalue = 0
    if prevSum > 0 and nextSum >= 0:
        addvalue = - nextSum - 1
    elif prevSum < 0 and nextSum <= 0:
        addvalue = - nextSum + 1
    
    prevSum = nextSum
    count3 += abs(addvalue)

print(min(count0, count1, count2, count3))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(s) for s in input().split(' ')]
#print(a)
count0 = 0
prevSum = a[0]
if prevSum == 0:
    count0 = 1
    prevSum = 1
#print(count0)
for i in range(1, len(a)) :
    nextSum = prevSum + a[i]

    addvalue = 0
    if prevSum > 0 and nextSum >= 0:
        addvalue = (0 - nextSum) - 1
    elif prevSum < 0 and nextSum <= 0:
        addvalue = (0 - nextSum) + 1
    
    prevSum = nextSum + addvalue
    count0 += abs(addvalue)

count1 = 0
prevSum = a[0]
if prevSum == 0:
    count1 = 1
    prevSum = -1

for i in range(1, len(a)) :
    nextSum = prevSum + a[i]

    addvalue = 0
    if prevSum > 0 and nextSum >= 0:
        addvalue = - nextSum - 1
    elif prevSum < 0 and nextSum <= 0:
        addvalue = - nextSum + 1
    
    prevSum = nextSum + addvalue
    count1 += abs(addvalue)

print(min(count0, count1))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(s) for s in input().split(' ')]
#print(a)
count0 = 0
prevSum = a[0]
if prevSum == 0:
    count0 = 1
    prevSum = 1
#print(count0)
for i in range(1, len(a)) :
    nextSum = prevSum + a[i]
    #print("{}, {}, {}".format(prevSum, a[i], nextSum))
    addvalue = 0
    if prevSum > 0 and nextSum >= 0:
        addvalue = - nextSum - 1
    
    elif prevSum < 0 and nextSum <= 0:
        addvalue = - nextSum + 1
    
    prevSum = nextSum
    count0 += abs(addvalue)

count1 = 0
prevSum = a[0]
if prevSum == 0:
    count1 = 1
    prevSum = -1

for i in range(1, len(a)) :
    nextSum = prevSum + a[i]

    addvalue = 0
    if prevSum > 0 and nextSum >= 0:
        addvalue = - nextSum - 1
    elif prevSum < 0 and nextSum <= 0:
        addvalue = - nextSum + 1
    
    prevSum = nextSum# + addvalue
    count1 += abs(addvalue)

print(min(count0, count1))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(s) for s in input().split(' ')]
#print(a)
count0 = 0
prevSum = a[0]
if prevSum == 0:
    count0 = 1
    prevSum = 1
#print(count0)
for i in range(1, len(a)) :
    nextSum = prevSum + a[i]
    #print("{}, {}, {}".format(prevSum, a[i], nextSum))
    addvalue = 0
    if prevSum > 0 and nextSum >= 0:
        addvalue = (0 - nextSum) - 1
        
    elif prevSum < 0 and nextSum <= 0:
        addvalue = (0 - nextSum) + 1
    
    prevSum = nextSum + addvalue
    count0 += abs(addvalue)

count1 = 0
prevSum = a[0]
if prevSum == 0:
    count1 = 1
    prevSum = -1

for i in range(1, len(a)) :
    nextSum = prevSum + a[i]

    addvalue = 0
    if prevSum > 0 and nextSum >= 0:
        addvalue = - nextSum - 1
    elif prevSum < 0 and nextSum <= 0:
        addvalue = - nextSum + 1
    
    prevSum = nextSum# + addvalue
    count1 += abs(addvalue)

print(min(count0, count1))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(s) for s in input().split(' ')]
#print(a)
count0 = 0
prevSum = a[0]
if prevSum == 0:
    count0 = 1
    prevSum = 1
#print(count0)
for i in range(1, len(a)) :
    nextSum = prevSum + a[i]
    #print("{}, {}, {}".format(prevSum, a[i], nextSum))
    addvalue = 0
    if prevSum > 0 and nextSum >= 0:
        addvalue = (0 - nextSum) - 1
        
    elif prevSum < 0 and nextSum <= 0:
        addvalue = (0 - nextSum) + 1
    
    prevSum = nextSum + addvalue
    count0 += abs(addvalue)

count1 = 0
prevSum = a[0]
if prevSum == 0:
    count1 = 1
    prevSum = -1

for i in range(1, len(a)) :
    nextSum = prevSum + a[i]
    #print("{}, {}, {}".format(prevSum, a[i], nextSum))
    addvalue = 0
    if prevSum > 0 and nextSum >= 0:
        addvalue = (0 - nextSum) - 1
        
    elif prevSum < 0 and nextSum <= 0:
        addvalue = (0 - nextSum) + 1
    
    prevSum = nextSum + addvalue
    count1 += abs(addvalue)

print(min(count0, count1))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(s) for s in input().split(' ')]
#print(a)
count = 0
prevSum = a[0]
if prevSum == 0:
    c = 0 - prevSum + 1
    count += abs(c)
    prevSum += c
#print(count)
for i in range(1, len(a)) :
    nextSum = prevSum + a[i]
    #print("{}, {}, {}".format(prevSum, a[i], nextSum))
    addvalue = 0
    if prevSum > 0 and nextSum >= 0:
        addvalue = (0 - nextSum) - 1
        
    elif prevSum < 0 and nextSum <= 0:
        addvalue = (0 - nextSum) + 1
    
    prevSum = nextSum + addvalue
    count += abs(addvalue)

print(count)

Submission status: Wrong Answer

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

ans=0
x=0
if sum(a[::2])>=sum(a[1::2]):
  t=True
else:
  t=False
  
for i in a:
  x+=i
  if t==True and x<1:
    ans+=1-x
    x=1
  elif t==False and x>-1:
    ans+=x+1
    x=-1
  t= not t
  
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

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

b = A[0]
ans = 0
#print(b)
for a in A[1:]:
    #print("a+b"+str(a+b))
    if a+b==0:
        ans += 1
        b = (-1)*(b)//abs(b)
    elif b*(a+b)<0:
        b += a
    else:
        ans += abs(a+b)+1
        #print("ans"+str(ans))
        b = (-1)*(a+b)//abs(a+b)
    #print(b)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = 0
ans2 = 0
a_sum = 0
b_sum = 0

if a[0] < 0:
	a = [-a[i] for i in range(n)]

b = list.copy(a)

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

a_sum += a[0]

for i in range(1, n, 2):
	if a_sum + a[i] >= 0:
		ans += a_sum + a[i] + 1
		a[i] = -1 - a_sum
	a_sum += a[i]
	if i == n - 1:
		break
	if a_sum + a[i + 1] <= 0:
		ans += 1 - a_sum - a[i + 1]
		a[i + 1] = 1 - a_sum
	a_sum += a[i + 1]

if b[0] == 0:
	b[0] = -1
	ans2 += 1

b_sum += b[0]

for i in range(1, n, 2):
	if b_sum + b[i] <= 0:
		ans2 += 1 - b_sum - b[i]
		b[i] = 1 - b_sum
	b_sum += b[i]
	if i == n - 1:
		break
	if b_sum + b[i + 1] >= 0:
		ans2 += b_sum + b[i + 1] + 1
		b[i + 1] = -1 - b_sum
	b_sum += b[i + 1]


print(min(ans, ans2))

Submission status: Wrong Answer

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

ans = 0
a_sum = 0

if a[0] < 0:
	a = [-a[i] for i in range(n)]

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

a_sum += a[0]

for i in range(1, n, 2):
	if a_sum + a[i] >= 0:
		ans += a_sum + a[i] + 1
		a[i] = -1 - a_sum
	a_sum += a[i]
	if i == n - 1:
		break
	if a_sum + a[i + 1] <= 0:
		ans += 1 - a_sum - a[i + 1]
		a[i + 1] = 1 - a_sum
	a_sum += a[i + 1]

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
numbers = list(map(int,input().split()))
p = numbers[0]
a, b = 0, 0
if p > 0:
    q = -1
    b = p + 1
else:
    q = 1
    b = -p + 1
    
for i in range(1, n):
    if p > 0:
        p += numbers[i]
        if p >= 0:
            a += (p + 1)
            p -= (p + 1)
    else:
        p += numbers[i]
        if p <= 0:
            a -= (p - 1)
            p -= (p - 1)

for j in range(1, n):
    if q > 0:
        q += numbers[j]
        if q >= 0:
            b += (q + 1)
            q -= (q + 1)
    else:
        q += numbers[j]
        if q <= 0:
            b -= (q - 1)
            q -= (q - 1)


print(min(a, b))

Submission status: Wrong Answer

Solution code:
n = int(input())
numbers = list(map(int,input().split()))
p = numbers[0]
ans = 0

    
for i in range(1, n):
    if p > 0:
        p += numbers[i]
        if p >= 0:
            ans += (p + 1)
            p -= (p + 1)
    else:
        p += numbers[i]
        if p <= 0:
            ans -= (p - 1)
            p -= (p - 1)

print(ans)

Submission status: Wrong Answer

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

def func(A, plus):
  total = 0
  ans = 0
  if plus:
    if A[0] <= 0:
      ans = abs(A[0])+1
      total = 1
  else:
    if A[0] >= 0:
      ans += abs(A[0])+1
      total = -1
  for a in A[1:]:
    t = total + a
    if total * t < 0:
      total = t
      continue
    ans += abs(t)+1
    total = -1 if total > 0 else 1
  return ans

print(min(func(A, True), func(A, False)))

Submission status: Wrong Answer

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

def func(A, plus):
  total = 0
  ans = 0
  if A[0] == 0:
    total = 1 if plus else -1
    ans = 1
  for a in A[ans:]:
    t = total + a
    if t != 0 and total * t <= 0:
      total = t
      continue
    ans += abs(t)+1
    total = -1 if total > 0 else 1
  return ans

print(min(func(A, True), func(A, False)))

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
  total = -1 if total > 0 else 1

print(ans)