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()))
suma = A[0]
count = 0
for i in range(1,N):
    if suma*(suma+A[i]) < 0:
        suma += A[i]
        continue
    else:
        if A[i] == 0:
            if suma > 0:
                count += suma+1
                suma = -1
            else:
                count += -suma+1
                suma = 1
        elif suma > 0:
            count += (suma+1)+A[i]
            suma = -1
        elif suma < 0:
            count += (-suma+1)-A[i]
            suma = 1
print(count)

Submission status: Wrong Answer

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

is_plus = False if a[0] > 0 else True

s = a[0]
ans = 0
for i in range(1, n):
    t = 0

    if is_plus and s+a[i] < 0:
        t = abs(s-a[i]) - 1
        a[i] += t
    elif not is_plus and s+a[i] > 0:
        t = abs(s+a[i]) + 1
        a[i] -= t

    s += a[i]

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

    is_plus = not is_plus

print(ans)

Submission status: Wrong Answer

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

is_plus = False if a[0] > 0 else True

s = a[0]
ans = 0
for i in range(1, n):
    t = 0

    if is_plus and s+a[i] < 0:
        t = abs(s-a[i]) - 1
        a[i] += t
    elif not is_plus and s+a[i] > 0:
        t = abs(s+a[i]) + 1
        a[i] -= t

    s += a[i]
    ans += t
    is_plus = not is_plus

if s != 0:
    print(ans)
else:
    print(ans-1 if a[-1] < 0 else ans+1)

Submission status: Wrong Answer

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

# a_1 > 0 の場合を試す
res, sum = 0, 0
if 0 < A[0]: sum = A[0]
else: res, sum = -A[0] + 1, 1

for i in range(1, N):
    if 0 < sum:
        if -1 < sum + A[i]: # a_i を足して、符号が変わらない
            res += abs((sum + 1) - A[i])
            sum = -1
        else: # a_i を足して符号が変わる
            sum += A[i]
    else:
        if sum + A[i] < 1:
            res += abs((sum + 1) - A[i])
            sum = 1
        else:
            sum += A[i]

ans = res

# a_1 < 0 の場合を試す
res, sum = 0, 0
if A[0] < 0: sum = A[0]
else: res, sum = A[0] + 1, -1

for i in range(1, N):
    if 0 < sum:
        if -1 < sum + A[i]: # a_i を足して、符号が変わらない
            res += abs((sum + 1) - A[i])
            sum = -1
        else: # a_i を足して符号が変わる
            sum += A[i]
    else:
        if sum + A[i] < 1:
            res += abs((sum + 1) - A[i])
            sum = 1
        else:
            sum += A[i]

ans = min(ans, res)
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[int(i) for i in input().split()]
for i in range(1,n):
    a[i]+=a[i-1]
if a[0]>0:
    pm=0
    c=0
    for i in range(1,n):
        a[i]+=pm
        if i%2==1:
            if a[i]>=0:
                pm-=(a[i]+1)
                c+=(a[i]+1)
        else:
            if a[i]<=0:
                pm+=(-a[i]+1)
                c+=(-a[i]+1)
    print(c)
elif a[0]<0:
    pm=0
    c=0
    for i in range(1,n):
        a[i]+=pm
        if i%2==0:
            if a[i]>=0:
                pm-=(a[i]+1)
                c+=(a[i]+1)
        else:
            if a[i]<=0:
                pm+=(-a[i]+1)
                c+=(-a[i]+1)
    print(c)
else:
    pm1=1
    c1=1
    for i in range(1,n):
        a[i]+=pm1
        if i%2==1:
            if a[i]>=0:
                pm1-=(a[i]+1)
                c1+=(a[i]+1)
        else:
            if a[i]<=0:
                pm1+=(-a[i]+1)
                c1+=(-a[i]+1)
    pm2=-1
    c2=1
    for i in range(1,n):
        a[i]+=pm2
        if i%2==0:
            if a[i]>=0:
                pm2-=(a[i]+1)
                c2+=(a[i]+1)
        else:
            if a[i]<=0:
                pm2+=(-a[i]+1)
                c2+=(-a[i]+1)
    print(min(c1,c2))

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().split()))
ind=1
res=10**10+1
for isplus in (True,False):
  cur=0
  ans=0
  for i in range(N):
    cur+=A[i]
    if isplus:
      if cur<=0:
        ans+=abs(cur-1)
        cur=1
      isplus=False
    else:
      if cur>=0:
        ans+=abs(cur-(-1))
        cur=-1
      isplus=True
  if res>ans:
    res=ans

print(res)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().split()))
cur=A[0]
ans=0
isplus=True
ind=1
if cur<0:
  isplus=False
if cur==0:
  # すべて0の場合
  allzero=True
  firstNotZero=0
  firstNotZeroInd=0
  for i in range(N):
    if A[i]!=0:
      firstNotZero=A[i]
      firstNotZeroInd=i
      allzero=False
      break
  if allzero:
    print(2*N-1)
    exit(0)
  # 0以外が出てくる場合
  ans+=(firstNotZeroInd)*2-1
  if firstNotZero>0:
    cur=-1
    ind=firstNotZeroInd
    isplus=False
  else:
    cur=1
    ind=firstNotZeroInd
    isplus=True

for i in range(ind,N):
  if isplus:
    if cur+A[i]>=0:
      diff=abs((cur+A[i])-(-1))
      ans+=diff
      cur=-1
    else:
      cur+=A[i]
    isplus=False
  else:
    if cur+A[i]<=0:
      diff=abs((cur+A[i])-1)
      ans+=diff
      cur=1
    else:
      cur+=A[i]
    isplus=True
  
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().split()))
cur=A[0]
ans=0
isplus=True
ind=1
if cur<0:
  isplus=False
if cur==0:
  # すべて0の場合
  allzero=True
  firstNotZero=0
  firstNotZeroInd=0
  for i in range(N):
    if A[i]!=0:
      firstNotZero=A[i]
      firstNotZeroInd=i
      allzero=False
      break
  if allzero:
    print(N)
    exit(0)
  # 0以外が出てくる場合
  if firstNotZero>0:
    cur=-1
    ind=firstNotZeroInd
    isplus=False
  else:
    cur=1
    ind=firstNotZeroInd
    isplus=True

for i in range(ind,N):
  if isplus:
    if cur+A[i]>=0:
      diff=abs((cur+A[i])-(-1))
      ans+=diff
      cur=-1
    else:
      cur+=A[i]
    isplus=False
  else:
    if cur+A[i]<=0:
      diff=abs((cur+A[i])-1)
      ans+=diff
      cur=1
    else:
      cur+=A[i]
    isplus=True
  
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().split()))
cur=A[0]
ans=0
isplus=True
ind=1
if cur<0:
  isplus=False
if cur==0:
  # すべて0の場合
  allzero=True
  firstNotZero=0
  firstNotZeroInd=0
  for i in range(N):
    if A[i]!=0:
      firstNotZero=A[i]
      firstNotZeroInd=i
      allzero=False
      break
  if allzero:
    print(N)
    exit(0)
  # 0以外が出てくる場合
  if firstNotZero>0:
    cur=-1
    ind=firstNotZeroInd
    isplus=False
    print("ind",ind,"isplus",isplus)
  else:
    cur=1
    ind=firstNotZeroInd
    isplus=True
    print("ind",ind,"isplus",isplus)

for i in range(ind,N):
  print("i",i,"cur",cur,"A[i]",A[i])
  if isplus:
    if cur+A[i]>=0:
      diff=abs((cur+A[i])-(-1))
      print("cur",cur,"cur+A[i]",cur+A[i]," -> -1 diff",diff)
      ans+=diff
      print("ans",ans)
      cur=-1
    else:
      print("no problem")
      cur+=A[i]
    isplus=False
  else:
    if cur+A[i]<=0:
      diff=abs((cur+A[i])-1)
      print("cur",cur,"cur+A[i]",cur+A[i]," -> 1 diff",diff)
      ans+=diff
      print("ans",ans)
      cur=1
    else:
      print("no problem")
      cur+=A[i]
    isplus=True
  
print(ans)

Submission status: Wrong Answer

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

acc = 0
cnt = 0
flag = 1 if a[0] > 0 else -1

for i in a:
    acc += i
    if (flag == 1 and flag > acc) or (flag == -1 and flag < acc) or i == 0:
        cnt += abs(flag - acc)
        acc = flag
    flag *= -1
print(cnt)

Submission status: Wrong Answer

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

def f(presum, x, fugo):
  if (presum+x)*fugo<=0:
    sa=fugo-presum-x
    prosum=fugo
  else:
    sa=0
    prosum=presum+x
  return sa, prosum

out1=0
y=a[0]
for i in range(1, n):
  x, y=f(y, a[i], (-1)**i)
  out1+=abs(x)
  
out2=0
y=a[0]
for i in range(1, n):
  x, y=f(y, a[i], -(-1)**i)
  out2+=abs(x)
  
print(min(out1, out2))

Submission status: Wrong Answer

Solution code:
n = int(input())
A = [int(i) for i in input().split()]

c = 10**15
for i in range(2):
    A = [-a for a in A]
    if A[0] != 0:
        ans = 0
        S = A[0]
        f = A[0]//abs(A[0])
    else:
        ans = 1
        S = 1
        f = 1

    for a in A[1:]:
        S += a
        if S == 0:
            ans += 1
            S = -f
        else:
            if S//abs(S) != f*(-1):
                ans += abs(S)+1
                S = -f
        f *= -1
    c = min(ans, c)
print(c)

Submission status: Wrong Answer

Solution code:
n = int(input())
A = [int(i) for i in input().split()]

c = 10**15
for i in range(2):
    A = [-a for a in A]
    if A[0] != 0:
        ans = 0
        S = A[0]
        f = A[0]//abs(A[0])
    else:
        ans = 1
        S = 1
        f = 1

    for a in A[1:]:
        S += a
        if S == 0:
            ans += 1
            S = -f
        else:
            if S/abs(S) != f*(-1):
                ans += abs(S)+1
                S = -f
        f *= -1
    c = min(ans, c)
print(c)

Submission status: Wrong Answer

Solution code:
n = int(input())
A = [int(i) for i in input().split()]

ff = -1
for i in range(n):
    if A[i] > 0:
        ff = i
        break
    elif A[i] < 0:
        ff = i+1
        break

if A[0] != 0:
    ans = 0
    S = A[0]
    f = A[0]//abs(A[0])
else:
    if ff == -1:
        ans = 1
        S = 1
        f = 1
    else:
        if ff % 2 == 0:
            ans = 1
            S = 1
            f = 1
        else:
            ans = 1
            S = -1
            f = -1

for a in A[1:]:
    S += a
    if S == 0:
        ans += 1
        S = -f
    else:
        if S/abs(S) != f*(-1):
            ans += abs(S)+1
            S = -f
    f *= -1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
A = [int(i) for i in input().split()]

ff = -1
for i in range(n):
    if A[i] != 0:
        ff = i
        break

if A[0] != 0:
    ans = 0
    S = A[0]
    f = A[0]//abs(A[0])
else:
    if ff == -1:
        ans = 1
        S = 1
        f = 1
    else:
        if ff % 2 == 0:
            ans = 1
            S = 1
            f = 1
        else:
            ans = 1
            S = -1
            f = -1

for a in A[1:]:
    S += a
    if S == 0:
        ans += 1
        S = -f
    else:
        if S/abs(S) != f*(-1):
            ans += abs(S)+1
            S = -f
    f *= -1
print(ans)

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n = I()
    a = LI()
    ans = 0
    s = a[0]
    if s <= 0:
        ans += 1-a[0]
        s = 1
    f = 1
    for i in a[1:]:
        s += i
        if f and (s >= 0):
            ans += s+1
            s = -1
        if not f and (s <= 0):
            ans += 1-s
            s = 1
        f ^= 1
    m = ans
    ans = 0
    s = a[0]
    if s >= 0:
        ans += a[0]+1
        s = 1
    f = 0
    for i in a[1:]:
        s += i
        if f and (s >= 0):
            ans += s+1
            s = -1
        if not f and (s <= 0):
            ans += 1-s
            s = 1
        f ^= 1
    print(min(m,ans))
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
import numpy as np
n = int(input())
L = np.array([int(i) for i in input().split()])

count = 0
s = L[0]
  
# print(L)
  
loopnum = n//2
if n%2 == 0:
  loopnum -= 1

#+-+-...

for i in range(loopnum):
  s = s + L[2*i+1]
  if s >= 0:
    subt = s + 1
    count += subt
    s = s - subt
  s = s + L[2*i+2]
  if s < 0:
    subt = s - 1 
    count -= subt
    s = s - subt
    
if n%2 == 0:
  s = s + L[-1]
  if s >= 0:
    subt = s + 1
    count += subt

cand1 = count

count = 0
s = L[0]

#-+-+...

for i in range(loopnum):
  s = s + L[2*i+1]
  if s <= 0:
    subt = s - 1
    count -= subt
    s = s - subt
  s = s + L[2*i+2]
  if s > 0:
    subt = s + 1 
    count += subt
    s = s - subt
    
if n%2 == 0:
  s = s + L[-1]
  if s <= 0:
    subt = s - 1
    count -= subt

cand2 = count


#print(cand1)
#print(cand2)
print(min(cand1,cand2))

Submission status: Wrong Answer

Solution code:
import numpy as np
n = int(input())
L = np.array([int(i) for i in input().split()])

count = 0
s = L[0]
if L[0] == 0:
  if L[1] > 0:
    L[0] = -1
  else:
    L[0] = 1
  count += 1
  
# print(L)
  
loopnum = n//2
if n%2 == 0:
  loopnum -= 1

#+-+-...

for i in range(loopnum):
  s = s + L[2*i+1]
  if s >= 0:
    subt = s + 1
    count += subt
    s = s - subt
  s = s + L[2*i+2]
  if s <= 0:
    subt = s - 1 
    count -= subt
    s = s - subt
    
if n%2 == 0:
  s = s + L[-1]
  if s >= 0:
    subt = s + 1
    count += subt

cand1 = count

count = 0
s = L[0]

#-+-+...

for i in range(loopnum):
  s = s + L[2*i+1]
  if s <= 0:
    subt = s - 1
    count -= subt
    s = s - subt
  s = s + L[2*i+2]
  if s >= 0:
    subt = s + 1 
    count += subt
    s = s - subt
    
if n%2 == 0:
  s = s + L[-1]
  if s <= 0:
    subt = s - 1
    count -= subt

cand2 = count


#print(cand1)
#print(cand2)
print(min(cand1,cand2))

Submission status: Wrong Answer

Solution code:
import numpy as np
n = int(input())
L = np.array([int(i) for i in input().split()])

count = 0
s = L[0]
#if L[0] == 0:
#  if L[1] > 0:
#    L[0] = -1
#  else:
#    L[0] = 1
#  count += 1
  
# print(L)
  
loopnum = n//2
if n%2 == 0:
  loopnum -= 1

#+-+-...

for i in range(loopnum):
  s = s + L[2*i+1]
  if s >= 0:
    subt = s + 1
    count += subt
    s = s - subt
  s = s + L[2*i+2]
  if s <= 0:
    subt = s - 1 
    count -= subt
    s = s - subt
    
if n%2 == 0:
  s = s + L[-1]
  if s >= 0:
    subt = s + 1
    count += subt

cand1 = count

count = 0
s = L[0]

#-+-+...

for i in range(loopnum):
  s = s + L[2*i+1]
  if s <= 0:
    subt = s - 1
    count -= subt
    s = s - subt
  s = s + L[2*i+2]
  if s >= 0:
    subt = s + 1 
    count += subt
    s = s - subt
    
if n%2 == 0:
  s = s + L[-1]
  if s <= 0:
    subt = s - 1
    count -= subt

cand2 = count


#print(cand1)
#print(cand2)
print(min(cand1,cand2))

Submission status: Wrong Answer

Solution code:
import numpy as np
n = int(input())
L = np.array([int(i) for i in input().split()])

count = 0
s = L[0]
#if L[0] == 0:
#  if L[1] > 0:
#    L[0] = -1
#  else:
#    L[0] = 1
#  count += 1
  
print(L)
  
loopnum = n//2
if n%2 == 0:
  loopnum -= 1

#+-+-...

for i in range(loopnum):
  s = s + L[2*i+1]
  if s >= 0:
    subt = s + 1
    count += subt
    s = s - subt
  s = s + L[2*i+2]
  if s <= 0:
    subt = s - 1 
    count -= subt
    s = s - subt
    
if n%2 == 0:
  s = s + L[-1]
  if s >= 0:
    subt = s + 1
    count += subt

cand1 = count

count = 0
s = L[0]

#-+-+...

for i in range(loopnum):
  s = s + L[2*i+1]
  if s <= 0:
    subt = s - 1
    count -= subt
    s = s - subt
  s = s + L[2*i+2]
  if s >= 0:
    subt = s + 1 
    count += subt
    s = s - subt
    
if n%2 == 0:
  s = s + L[-1]
  if s <= 0:
    subt = s - 1
    count -= subt

cand2 = count


#print(cand1)
#print(cand2)
print(min(cand1,cand2))

Submission status: Wrong Answer

Solution code:
import numpy as np
n = int(input())
L = np.array([int(i) for i in input().split()])
if L[0] < 0:
  L = -L

count = 0
s = L[0]
if L[0] == 0:
  if L[1] > 0:
    L[0] = -1
  else:
    L[0] = 1
  count += 1

loopnum = n//2
if n%2 == 0:
  loopnum -= 1
for i in range(loopnum):
  s = s + L[2*i+1]
  if s >= 0:
    subt = s + 1
    count += subt
    s = s - subt
  #print("s, count = {0}, {1}".format(s, count))
  s = s + L[2*i+2]
  if s <= 0:
    subt = s - 1 
    count -= subt
    s = s - subt
  #print("s, count = {0}, {1}".format(s, count))
    
if n%2 == 0:
  s = s + L[-1]
  if s >= 0:
    subt = s + 1
    count += subt
print(count)

Submission status: Wrong Answer

Solution code:
import numpy as np
n = int(input())
L = np.array([int(i) for i in input().split()])
if L[0] < 0:
  L = -L

count = 0
s = L[0]
if L[0] == 0:
  if L[1] > 0:
    L[0] = -1
  else:
    L[0] = 1
  count += 1

loopnum = n//2
if n%2 == 0:
  loopnum -= 1
for i in range(loopnum):
  s = s + L[2*i+1]
  if s >= 0:
    subt = s + 1
    count += subt
    s = s - subt
  #print("s, count = {0}, {1}".format(s, count))
  s = s + L[2*i+2]
  if s <= 0:
    subt = s - 1 
    count -= subt
    s = s + subt
  #print("s, count = {0}, {1}".format(s, count))
    
if n%2 == 0:
  s = s + L[-1]
  if s >= 0:
    subt = s + 1
    count += subt
print(count)

Submission status: Wrong Answer

Solution code:
import numpy as np
n = int(input())
L = np.array([int(i) for i in input().split()])
if L[0] < 0:
  L = -L

count = 0
s = L[0]
if L[0] == 0:
  if L[1] > 0:
    L[0] = -1
  else:
    L[0] = 1
  count += 1

for i in range(n//2 -1):
  s = s + L[2*i+1]
  if s >= 0:
    subt = s + 1
    count += subt
    s = s - count
  s = s + L[2*i+2]
  if s <= 0:
    subt = s - 1 
    count -= subt
    s = s + count

if n%2 == 0:
  s = s + L[-1]
  if s >= 0:
    subt = s + 1
    count += subt
print(count)

Submission status: Wrong Answer

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

import itertools
al_ac = list(itertools.accumulate(al))

ans = 0
if al_ac[0] > 0:
    init = 1
else:
    init = -1

fix = 0
for a in al_ac:
    a += fix
    if init == 1:
        if a >= 1:
            init *= -1
            continue
        else:
            ans += abs(init - a)
            fix += init - a
            init *= -1
    else:
        if a <= -1:
            init *= -1
            continue
        else:
            ans += abs(init - a)
            fix += init - a
            init *= -1

print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Here your code

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

result_1 = 0
before_sum = a[0]

if a[0] <= 0:
    before_sum = 1
    result_1 += 1 + abs(a[0])

after_sum = before_sum

for i in range(1,N):
    before_sum = after_sum
    after_sum = before_sum + a[i]
    if i % 2 == 0:
        if after_sum <= 0:
            result_1 += 1 + abs(after_sum)
            after_sum = 1
    else:
        if after_sum >= 0:
            result_1 += 1 + abs(after_sum)
            after_sum = -1

result_2 = 0
before_sum = a[0]

if a[0] >= 0:
    before_sum = -1
    result_2 += 1 + abs(a[0])

after_sum = before_sum

for i in range(1,N):
    before_sum = after_sum
    after_sum = before_sum + a[i]
    if i % 2 == 1:
        if after_sum >= 0:
            result_2 += 1 + abs(after_sum)
            after_sum = -1
    else:
        if after_sum <= 0:
            result_2 += 1 + abs(after_sum)
            after_sum = 1

print(min(result_1,result_2))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Here your code



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

result_1 = 0
before_sum =a[0]
if a[0] == 0:
    before_sum = 1
    result_1 += 1
after_sum =before_sum

for i in range(1,N):
    before_sum = after_sum
    after_sum = before_sum + a[i]
    if before_sum * after_sum > 0:
        if after_sum < 0:
            result_1 += 1 - after_sum
            after_sum = 1
        elif after_sum > 0:
            result_1 += 1 + after_sum
            after_sum = -1
    elif before_sum * after_sum == 0:
        result_1 += abs(before_sum) + 1
        if before_sum < 0:
            after_sum = 1
        else:
            after_sum = -1

if a[0] < 0:
    before_sum = 1
elif a[0] >= 0:
    before_sum = -1
after_sum =before_sum
result_2 = 1 + abs(a[0])

for i in range(1,N):
    before_sum = after_sum
    after_sum = before_sum + a[i]
    if before_sum * after_sum > 0:
        if after_sum < 0:
            result_2 += 1 - after_sum
            after_sum = 1
        elif after_sum > 0:
            result_2 += 1 + after_sum
            after_sum = -1
    elif before_sum * after_sum == 0:
        result_2 += abs(before_sum) + 1
        if before_sum < 0:
            after_sum = 1
        else:
            after_sum = -1
    
print(min(result_1,result_2))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Here your code



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

result_1 = 0
before_sum =a[0]
if a[0] == 0:
    before_sum = 1
    result_1 += 1
after_sum =before_sum

for i in range(1,N):
    before_sum = after_sum
    after_sum = before_sum + a[i]
    if before_sum * after_sum > 0:
        if after_sum < 0:
            result_1 += 1 - after_sum
            after_sum = 1
        elif after_sum > 0:
            result_1 += 1 + after_sum
            after_sum = -1
    elif before_sum * after_sum == 0:
        result_1 += abs(before_sum) + 1
        if before_sum < 0:
            after_sum = 1
        else:
            after_sum = -1

if a[0] < 0:
    before_sum = 1
elif a[0] >= 0:
    before_sum = -1
after_sum =before_sum
result_2 = 1 + abs(before_sum)

for i in range(1,N):
    before_sum = after_sum
    after_sum = before_sum + a[i]
    if before_sum * after_sum > 0:
        if after_sum < 0:
            result_2 += 1 - after_sum
            after_sum = 1
        elif after_sum > 0:
            result_2 += 1 + after_sum
            after_sum = -1
    elif before_sum * after_sum == 0:
        result_2 += abs(before_sum) + 1
        if before_sum < 0:
            after_sum = 1
        else:
            after_sum = -1
    
print(min(result_1,result_2))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Here your code



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

result_1 = 0
before_sum =a[0]
if a[0] == 0:
    before_sum = 1
    result_1 += 1
after_sum =a[0]

for i in range(1,N):
    before_sum = after_sum
    after_sum = before_sum + a[i]
    if before_sum * after_sum > 0:
        if after_sum < 0:
            result_1 += 1 - after_sum
            after_sum = 1
        elif after_sum > 0:
            result_1 += 1 + after_sum
            after_sum = -1
    elif before_sum * after_sum == 0:
        result_1 += 2
        if before_sum < 0:
            after_sum = 1
        else:
            after_sum = -1

if a[0] < 0:
    before_sum = 1
elif a[0] >= 0:
    before_sum = -1
after_sum =before_sum
result_2 = 1 + abs(before_sum)

for i in range(1,N):
    before_sum = after_sum
    after_sum = before_sum + a[i]
    if before_sum * after_sum > 0:
        if after_sum < 0:
            result_2 += 1 - after_sum
            after_sum = 1
        elif after_sum > 0:
            result_2 += 1 + after_sum
            after_sum = -1
    elif before_sum * after_sum == 0:
        result_2 += 2
        if before_sum < 0:
            after_sum = 1
        else:
            after_sum = -1
    print(after_sum)
    
print(min(result_1,result_2))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Here your code



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

result_1 = 0
before_sum =a[0]
if a[0] == 0:
    before_sum = 1
    result_1 += 1
after_sum =a[0]

for i in range(1,N):
    before_sum = after_sum
    after_sum = before_sum + a[i]
    if before_sum * after_sum > 0:
        if after_sum < 0:
            result_1 += 1 - after_sum
            after_sum = 1
        elif after_sum > 0:
            result_1 += 1 + after_sum
            after_sum = -1
    elif before_sum * after_sum == 0:
        result_1 += 1
        if before_sum < 0:
            after_sum = 1
        else:
            after_sum = -1

if a[0] < 0:
    before_sum = 1
elif a[0] >= 0:
    before_sum = -1
after_sum =before_sum
result_2 = 1 + abs(before_sum)

for i in range(1,N):
    before_sum = after_sum
    after_sum = before_sum + a[i]

    if before_sum * after_sum > 0:
        if after_sum < 0:
            result_2 += 1 - after_sum
            after_sum = 1
        elif after_sum > 0:
            result_2 += 1 + after_sum
            after_sum = -1
    elif before_sum * after_sum == 0:
        result_2 += 1
        if before_sum < 0:
            after_sum = 1
        else:
            after_sum = -1

print(min(result_1,result_2))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Here your code



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

result_1 = 0
before_sum =a[0]
if a[0] == 0:
    before_sum = 1
    result_1 += 1
after_sum =a[0]

for i in range(1,N):
    before_sum = after_sum
    after_sum = before_sum + a[i]
    if before_sum * after_sum > 0:
        if after_sum < 0:
            result_1 += 1 - after_sum
            after_sum = 1
        elif after_sum > 0:
            result_1 += 1 + after_sum
            after_sum = -1
    elif before_sum * after_sum == 0:
        result_1 += 1
        if before_sum < 0:
            after_sum = 1
        else:
            after_sum = -1

if a[0] < 0:
    before_sum = 1
elif a[0] >= 0:
    before_sum = -1
after_sum =a[0]
result_2 = 1 + abs(before_sum)


for i in range(1,N):
    before_sum = after_sum
    after_sum = before_sum + a[i]
    if before_sum * after_sum > 0:
        if after_sum < 0:
            result_2 += 1 - after_sum
            after_sum = 1
        elif after_sum > 0:
            result_2 += 1 + after_sum
            after_sum = -1
    elif before_sum * after_sum == 0:
        result_2 += 1
        if before_sum < 0:
            after_sum = 1
        else:
            after_sum = -1

print(min(result_1,result_2))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Here your code



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

result_1 = 0
before_sum =a[0]
if a[0] == 0:
    before_sum = 1
after_sum =a[0]

for i in range(1,N):
    before_sum = after_sum
    after_sum = before_sum + a[i]
    if before_sum * after_sum > 0:
        if after_sum < 0:
            result_1 += 1 - after_sum
            after_sum = 1
        elif after_sum > 0:
            result_1 += 1 + after_sum
            after_sum = -1
    elif before_sum * after_sum == 0:
        result_1 += 1
        if before_sum < 0:
            after_sum = 1
        else:
            after_sum = -1

if a[0] < 0:
    before_sum = 1
elif a[0] >= 0:
    before_sum = -1
after_sum =a[0]
result_2 = 1 + abs(before_sum)


for i in range(1,N):
    before_sum = after_sum
    after_sum = before_sum + a[i]
    if before_sum * after_sum > 0:
        if after_sum < 0:
            result_2 += 1 - after_sum
            after_sum = 1
        elif after_sum > 0:
            result_2 += 1 + after_sum
            after_sum = -1
    elif before_sum * after_sum == 0:
        result_2 += 1
        if before_sum < 0:
            after_sum = 1
        else:
            after_sum = -1

print(min(result_1,result_2))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Here your code



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

result_1 = 0
before_sum =a[0]
after_sum =a[0]

for i in range(1,N):
    before_sum = after_sum
    after_sum = before_sum + a[i]
    if before_sum * after_sum > 0:
        if after_sum < 0:
            result_1 += 1 - after_sum
            after_sum = 1
        elif after_sum > 0:
            result_1 += 1 + after_sum
            after_sum = -1
    elif before_sum * after_sum == 0:
        result_1 += 1
        if before_sum < 0:
            after_sum = 1
        else:
            after_sum = -1

before_sum =-a[0]
after_sum =-a[0]
result_2 = 1 + abs(before_sum)


for i in range(1,N):
    before_sum = after_sum
    after_sum = before_sum + a[i]
    if before_sum * after_sum > 0:
        if after_sum < 0:
            result_2 += 1 - after_sum
            after_sum = 1
        elif after_sum > 0:
            result_2 += 1 + after_sum
            after_sum = -1
    elif before_sum * after_sum == 0:
        result_2 += 1
        if before_sum < 0:
            after_sum = 1
        else:
            after_sum = -1


print(min(result_1,result_2))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Here your code



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

result = 0
before_sum =a[0]
after_sum =a[0]

for i in range(1,N):
    before_sum = after_sum
    after_sum = before_sum + a[i]
    if before_sum * after_sum > 0:
        if after_sum < 0:
            result += 1 - after_sum
            after_sum = 1
        elif after_sum > 0:
            result += 1 + after_sum
            after_sum = -1
    elif before_sum * after_sum == 0:
        result += 1
        if before_sum < 0:
            after_sum = 1
        else:
            after_sum = -1

print(result)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
n = int(input())
a = list(map(int,input().split()))
def operate(ls,x):
    sm = 0
    ans = 0
    ans += abs(ls[0]-x)
    k = x
    for i in range(1,len(ls)):
        sm += k
        if sm > 0:
            if abs(ls[i]) <= sm or sm < ls[i]:
                ans += abs(ls[i]+sm+1)
                k = -sm-1
            else:
                k = ls[i]
        if sm < 0:
            if abs(ls[i]) <= abs(sm) or sm > ls[i]:
                ans += abs(-sm+1-ls[i])
                k = -sm+1
            else:
                k = ls[i]
    return ans

anstot = []
if a[0] > 0:
    inv = -1
if a[0] < 0:
    inv = 1
if a[0] != 0:
    anstot.append(operate(a,a[0]))
    anstot.append(operate(a,inv))
if a[0] == 0:
    a[0] = 1
    anstot.append(1+operate(a,1))
    anstot.append(1+operate(a,-1))    
print(min(anstot))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
n = int(input())
a = list(map(int,input().split()))
def operate(ls,x):
    sm = 0
    ans = 0
    ans += abs(ls[0]-x)
    k = x
    for i in range(1,len(ls)):
        sm += k
        if sm > 0:
            if abs(ls[i]) <= sm or sm < ls[i]:
                ans += abs(ls[i]+sm+1)
                k = -sm-1
            else:
                k = ls[i]
        if sm < 0:
            if abs(ls[i]) <= abs(sm) or sm > ls[i]:
                ans += abs(-sm+1-ls[i])
                k = -sm+1
            else:
                k = ls[i]
    return ans

anstot = []
if a[0] > 0:
    inv = -1
if a[0] < 0:
    inv = 1
if a[0] != 0:
    anstot.append(operate(a,a[0]))
    anstot.append(operate(a,inv))
if a[0] == 0:
    for i in range(n):
        if a[i] == 0:
            a[i] = -1**(i%2)
        elif a[i] != 0:
            r = i
            break
    anstot.append(r+operate(a,a[0]))
    for i in range(r):
        a[i] = -1**((1+i)%2)
    anstot.append(r+operate(a,a[0]))    
print(min(anstot))