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()))
import sys
sum=a[0]
cnt=0
if a[0]==0:
    sum+=1
    cnt+=1
    for i in range(1,n):
        if sum<0:
            z=sum+a[i]
            if z>0:
                sum=z
            elif z<0:
                cnt+=(1-z)
                sum=1
            else:
                if a[i-1]>0:
                    sum=-1
                    cnt+=1
                elif a[i-1]<0:
                    sum=1
                    cnt+=1
        elif sum>0:
            z=sum+a[i]
            if z>=0:
                cnt+=(z+1)
                sum=-1
            elif z<0:
                sum=z
    cnt_plus=cnt
    sum=-1
    cnt=1
    for i in range(1,n):
        if sum<0:
            z=sum+a[i]
            if z>0:
                sum=z
            elif z<0:
                cnt+=(1-z)
                sum=1
            else:
                if a[i-1]>0:
                    sum=-1
                    cnt+=1
                elif a[i-1]<0:
                    sum=1
                    cnt+=1
        elif sum>0:
            z=sum+a[i]
            if z>=0:
                cnt+=(z+1)
                sum=-1
            elif z<0:
                sum=z
    cnt_sbst=cnt
    print(min(cnt_plus,cnt_sbst))
    sys.exit()
for i in range(1,n):
    if sum<0:
        z=sum+a[i]
        if z>0:
            sum=z
        elif z<0:
            cnt+=(1-z)
            sum=1
        else:
            if a[i-1]>0:
                sum=-1
                cnt+=1
            elif a[i-1]<0:
                sum=1
                cnt+=1
    elif sum>0:
        z=sum+a[i]
        if z>=0:
            cnt+=(z+1)
            sum=-1
        elif z<0:
            sum=z
print(cnt)

# 6
# 0 0 -1 3 5 0

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
import sys
sum=a[0]
cnt=0
if a[0]==0:
    sum+=1
    cnt+=1
    for i in range(1,n):
        if sum<0:
            z=sum+a[i]
            if z>0:
                sum=z
            elif z<0:
                cnt+=(1-z)
                sum=1
            else:
                if a[i-1]>0:
                    sum=-1
                    cnt+=1
                elif a[i-1]<0:
                    sum=1
                    cnt+=1
        elif sum>0:
            z=sum+a[i]
            if z>=0:
                cnt+=(z+1)
                sum=-1
            elif z<0:
                sum=z
    cnt_plus=cnt
    for i in range(1,n):
        if sum<0:
            z=sum+a[i]
            if z>0:
                sum=z
            elif z<0:
                cnt+=(1-z)
                sum=1
            else:
                if a[i-1]>0:
                    sum=-1
                    cnt+=1
                elif a[i-1]<0:
                    sum=1
                    cnt+=1
        elif sum>0:
            z=sum+a[i]
            if z>=0:
                cnt+=(z+1)
                sum=-1
            elif z<0:
                sum=z
    cnt_sbst=cnt
    print(min(cnt_plus,cnt_sbst))
    sys.exit()
for i in range(1,n):
    if sum<0:
        z=sum+a[i]
        if z>0:
            sum=z
        elif z<0:
            cnt+=(1-z)
            sum=1
        else:
            if a[i-1]>0:
                sum=-1
                cnt+=1
            elif a[i-1]<0:
                sum=1
                cnt+=1
    elif sum>0:
        z=sum+a[i]
        if z>=0:
            cnt+=(z+1)
            sum=-1
        elif z<0:
            sum=z
print(cnt)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
sum=a[0]
cnt=0
if a[0]==0:
    sum+=1
    cnt+=1
for i in range(1,n):
    if sum<0:
        z=sum+a[i]
        if z>0:
            sum=z
        elif z<=0:
            cnt+=(1-z)
            sum=1
    elif sum>0:
        z=sum+a[i]
        if z>=0:
            cnt+=(z+1)
            sum=-1
        elif z<0:
            sum=z
print(cnt)

Submission status: Wrong Answer

Solution code:
import sys

n, *a = map(int, sys.stdin.read().split())

def main():
    c1 = c2 = s1 = s2 = 0
    for i in range(n):
        s1 += a[i]
        s2 += a[i]
        if i & 1:
            if s1 >= 0:
                c1 += s1 + 1
                s1 = -1
            if s2 <= 0:
                c2 += 1 - s2
                s2 = 1
        else:
            if s1 <= 0:
                c1 += 1 - s2
                s1 = 1
            if s2 >= 0:
                c2 += s2 + 1
                s2 = -1
    print(min(c1, c2))

if __name__ ==  '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#
# Written by NoKnowledgeGG @YlePhan
# ('ω')
#
#import math
#mod = 10**9+7
#import itertools
#import fractions
#import numpy as np
#mod = 10**4 + 7
"""def kiri(n,m):
  r_ = n / m
  if (r_ - (n // m)) > 0:
    return (n//m) + 1
  else:
    return (n//m)"""

""" n! mod m 階乗
mod = 1e9 + 7
N = 10000000
fac = [0] * N
def ini():
  fac[0] = 1 % mod
  for i in range(1,N):
    fac[i] = fac[i-1] * i % mod"""

"""mod = 1e9+7
N = 10000000
pw = [0] * N
def ini(c):
  pw[0] = 1 % mod
  for i in range(1,N):
    pw[i] = pw[i-1] * c % mod"""

"""
def YEILD():
  yield 'one'
  yield 'two'
  yield 'three'
generator = YEILD()
print(next(generator))
print(next(generator))
print(next(generator))
"""
"""def gcd_(a,b):
  if b == 0:#結局はc,0の最大公約数はcなのに
    return a
  return gcd_(a,a % b) # a = p * b + q"""
"""def extgcd(a,b,x,y):
  d = a
  if b!=0:
    d = extgcd(b,a%b,y,x)
    y -= (a//b) * x
    print(x,y)
  else:
    x = 1
    y = 0
  return d"""


def readInts():
  return list(map(int,input().split()))
mod = 10**9 + 7
def main():
  n = int(input())
  A = readInts()
  
  cur = 0
  ans1 = 0
  for i in range(n):
    cur += A[i]
    if i % 2:
      if cur <= 0:
        ans1 += -cur+1
        cur = 1
    else:
      if cur >= 0:
        ans1 += cur + 1
        cur = -1
  cur = 0
  ans2 = 0
  for i in range(n):
    cur += A[i]
    if i % 2:
      if cur >= 0:
        ans2 += cur + 1
        cur = -1
      else:
        if cur <= 0:
          ans2 += -cur + 1
          cur = 1
  print(min(ans1,ans2))
if __name__ == '__main__':
  main()

Submission status: Wrong Answer

Solution code:
#
# Written by NoKnowledgeGG @YlePhan
# ('ω')
#
#import math
#mod = 10**9+7
#import itertools
#import fractions
#import numpy as np
#mod = 10**4 + 7
"""def kiri(n,m):
  r_ = n / m
  if (r_ - (n // m)) > 0:
    return (n//m) + 1
  else:
    return (n//m)"""

""" n! mod m 階乗
mod = 1e9 + 7
N = 10000000
fac = [0] * N
def ini():
  fac[0] = 1 % mod
  for i in range(1,N):
    fac[i] = fac[i-1] * i % mod"""

"""mod = 1e9+7
N = 10000000
pw = [0] * N
def ini(c):
  pw[0] = 1 % mod
  for i in range(1,N):
    pw[i] = pw[i-1] * c % mod"""

"""
def YEILD():
  yield 'one'
  yield 'two'
  yield 'three'
generator = YEILD()
print(next(generator))
print(next(generator))
print(next(generator))
"""
"""def gcd_(a,b):
  if b == 0:#結局はc,0の最大公約数はcなのに
    return a
  return gcd_(a,a % b) # a = p * b + q"""
"""def extgcd(a,b,x,y):
  d = a
  if b!=0:
    d = extgcd(b,a%b,y,x)
    y -= (a//b) * x
    print(x,y)
  else:
    x = 1
    y = 0
  return d"""


def readInts():
  return list(map(int,input().split()))
mod = 10**9 + 7
def main():
  n = int(input())
  A = readInts()
  Cost = 0
  # 符号 positive?
  #po_ = True 
  # 変わったか変わってないか
  ANS = [0] * (n)
  if A[0] > 0:
    pri_po = True
  elif A[0] == 0:
    pri_po = True
    A[0] += 1
    Cost += 1
  else:
    pri_po = False
  
  ANS[0] = A[0]
  
  for i in range(1,n):
    #print(ANS[i-1],pri_po,Cost)
    if ANS[i-1] + A[i] > 0 and pri_po == True:
      pri_po = False
      Cost += abs(-1 - (ANS[i-1] + A[i]))
      #print(abs(-1 - (ANS[i-1] + A[i])))
      A[i] -= abs(-1 - (ANS[i-1] + A[i]))
      #print('Hi^^',A[i])
      ANS[i] = ANS[i-1] + A[i]
    elif ANS[i-1] + A[i] == 0 and pri_po == False:
      pri_po = True
      Cost += 1
      A[i] += 1
      ANS[i] = ANS[i-1] + A[i]
    elif ANS[i-1] + A[i] == 0 and pri_po == True:
      pri_po = False
      Cost += 1
      A[i] -= 1
      ANS[i] = ANS[i-1] + A[i]
    elif ANS[i-1] + A[i] > 0 and pri_po == False:
      pri_po = True
      ANS[i] = ANS[i-1] + A[i]
    elif ANS[i-1] + A[i] < 0 and pri_po == True:
      pri_po = False
      ANS[i] = ANS[i-1] + A[i]
    elif ANS[i-1] + A[i] < 0 and pri_po == False:
      pri_po = True
      Cost += 1 - (ANS[i-1] + A[i])
      A[i] += 1 - (ANS[i-1] + A[i])
      ANS[i] = ANS[i-1] + A[i]
    else:
      pass
  print(Cost)
if __name__ == '__main__':
  main()

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
ans = 0
cnt = [a[0]]
for i in range(1,n):
  cnt.append(cnt[-1]+a[i])
cnt_o = cnt[::2]
cnt_e = cnt[1::2]
o = sum(cnt_o)
e = sum(cnt_e)
ans = 0
c = 0
if o >= e:
  for i in range(n):
    cnt[i] += c
    if (i+1) % 2 != 0:
      if cnt[i] > 0:
        pass
      else:
        ans += abs(cnt[i])+1
        c += abs(cnt[i])+1
    else:
      if cnt[i] < 0:
        pass
      else:
        ans += cnt[i]+1
        c -= a[i]+1
else:
  for i in range(n):
    cnt[i] += c
    if (i+1) % 2 != 0:
      if cnt[i] < 0:
        pass
      else:
        ans += cnt[i]+1
        c -= cnt[i]+1
    else:
      if cnt[i] > 0:
        pass
      else:
        ans += abs(cnt[i])+1
        c += abs(a[i])+1
if sum(cnt) == 0:
  print(ans+1)
else:
  print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
ans = 0
cnt = [a[0]]
for i in range(1,n):
  cnt.append(cnt[-1]+a[i])
cnt_o = cnt[::2]
cnt_e = cnt[1::2]
o = sum(cnt_o)
e = sum(cnt_e)
ans = 0
c = 0
if o >= e:
  for i in range(n):
    cnt[i] += c
    if (i+1) % 2 != 0:
      if cnt[i] > 0:
        pass
      else:
        ans += abs(cnt[i])+1
        c += abs(cnt[i])+1
    else:
      if cnt[i] < 0:
        pass
      else:
        ans += cnt[i]+1
        c -= a[i]+1
else:
  for i in range(n):
    cnt[i] += c
    if (i+1) % 2 != 0:
      if cnt[i] < 0:
        pass
      else:
        ans += cnt[i]+1
        c -= cnt[i]+1
    else:
      if cnt[i] > 0:
        pass
      else:
        ans += abs(cnt[i])+1
        c += abs(a[i])+1
print(ans)

Submission status: Wrong Answer

Solution code:
#
# Written by NoKnowledgeGG @YlePhan
# ('ω')
#
#import math
#mod = 10**9+7
#import itertools
#import fractions
#import numpy as np
#mod = 10**4 + 7
"""def kiri(n,m):
  r_ = n / m
  if (r_ - (n // m)) > 0:
    return (n//m) + 1
  else:
    return (n//m)"""

""" n! mod m 階乗
mod = 1e9 + 7
N = 10000000
fac = [0] * N
def ini():
  fac[0] = 1 % mod
  for i in range(1,N):
    fac[i] = fac[i-1] * i % mod"""

"""mod = 1e9+7
N = 10000000
pw = [0] * N
def ini(c):
  pw[0] = 1 % mod
  for i in range(1,N):
    pw[i] = pw[i-1] * c % mod"""

"""
def YEILD():
  yield 'one'
  yield 'two'
  yield 'three'
generator = YEILD()
print(next(generator))
print(next(generator))
print(next(generator))
"""
"""def gcd_(a,b):
  if b == 0:#結局はc,0の最大公約数はcなのに
    return a
  return gcd_(a,a % b) # a = p * b + q"""
"""def extgcd(a,b,x,y):
  d = a
  if b!=0:
    d = extgcd(b,a%b,y,x)
    y -= (a//b) * x
    print(x,y)
  else:
    x = 1
    y = 0
  return d"""


def readInts():
  return list(map(int,input().split()))
mod = 10**9 + 7
def main():
  n = int(input())
  A = readInts()
  Cost = 0
  # 符号 positive?
  #po_ = True 
  # 変わったか変わってないか
  if A[0] > 0: # if positive
    po_ = True
  elif A[0] == 0: # negative
    po_ = True
    A[0] += 1
    Cost += 1
  else:
    po_ = False
  ANS = [0] * (n+1)
  ANS[0] = A[0]
  
  for i in range(1,n):
    #print(ANS[i-1],po_,ANS[i-1] + A[i],Cost)
    if ANS[i-1]+A[i] > 0 and not po_: # sumがpositiveで前がnegativeだった
      po_ = True 
      ANS[i] = ANS[i-1] + A[i]
      # これで終わり
    elif ANS[i-1]+A[i] > 0 and po_: # posi : posi ?
      # 負にしなければならない
      Cost += abs(-1 - (ANS[i-1]+A[i])) # 先にこれやれ
      A[i] += -1 - (ANS[i-1] + A[i])
      # -4
      ANS[i] = ANS[i-1] + A[i]
      po_ = False
    elif ANS[i-1]+A[i] < 0 and not po_: #nega : nega
      # -1 はここ
      # print(A[i])
      Cost += abs(1 - (ANS[i-1]+A[i])) # 先にこれやれ
      A[i] += 1 - (ANS[i-1] + A[i])
      ANS[i] = ANS[i-1] + A[i]
      
      po_ = True
    elif ANS[i-1]+A[i] == 0 and po_: # nega: pos
      po_ = False
      A[i] -= 1
      Cost += 1
      ANS[i] = ANS[i-1] + A[i]
    elif ANS[i-1]+A[i] < 0 and po_:
      po_ = False
      ANS[i] = ANS[i-1] + A[i]
    elif ANS[i-1]+A[i] == 0 and not po_:
      po_ = True
      A[i] += 1
      Cost += 1
      ANS[i] = ANS[i-1] + A[i]
  print(Cost)
if __name__ == '__main__':
  main()

Submission status: Wrong Answer

Solution code:
#
# Written by NoKnowledgeGG @YlePhan
# ('ω')
#
#import math
#mod = 10**9+7
#import itertools
#import fractions
#import numpy as np
#mod = 10**4 + 7
"""def kiri(n,m):
  r_ = n / m
  if (r_ - (n // m)) > 0:
    return (n//m) + 1
  else:
    return (n//m)"""

""" n! mod m 階乗
mod = 1e9 + 7
N = 10000000
fac = [0] * N
def ini():
  fac[0] = 1 % mod
  for i in range(1,N):
    fac[i] = fac[i-1] * i % mod"""

"""mod = 1e9+7
N = 10000000
pw = [0] * N
def ini(c):
  pw[0] = 1 % mod
  for i in range(1,N):
    pw[i] = pw[i-1] * c % mod"""

"""
def YEILD():
  yield 'one'
  yield 'two'
  yield 'three'
generator = YEILD()
print(next(generator))
print(next(generator))
print(next(generator))
"""
"""def gcd_(a,b):
  if b == 0:#結局はc,0の最大公約数はcなのに
    return a
  return gcd_(a,a % b) # a = p * b + q"""
"""def extgcd(a,b,x,y):
  d = a
  if b!=0:
    d = extgcd(b,a%b,y,x)
    y -= (a//b) * x
    print(x,y)
  else:
    x = 1
    y = 0
  return d"""


def readInts():
  return list(map(int,input().split()))
mod = 10**9 + 7
def main():
  n = int(input())
  A = readInts()
  # 符号 positive?
  #po_ = True 
  # 変わったか変わってないか
  if A[0] >= 0: # if positive
    po_ = True
  else: # negative
    po_ = False
  Cost = 0
  ANS = [0] * (n+1)
  ANS[0] = A[0]
  for i in range(1,n):
    #print(ANS[i-1],po_,ANS[i-1] + A[i],Cost)
    if ANS[i-1]+A[i] > 0 and not po_: # sumがpositiveで前がnegativeだった
      po_ = True 
      ANS[i] = ANS[i-1] + A[i]
      # これで終わり
    elif ANS[i-1]+A[i] > 0 and po_: # posi : posi ?
      # 負にしなければならない
      Cost += abs(-1 - (ANS[i-1]+A[i])) # 先にこれやれ
      A[i] += -1 - (ANS[i-1] + A[i])
      # -4
      ANS[i] = ANS[i-1] + A[i]
      po_ = False
    elif ANS[i-1]+A[i] < 0 and not po_: #nega : nega
      # -1 はここ
      # print(A[i])
      Cost += abs(1 - (ANS[i-1]+A[i])) # 先にこれやれ
      A[i] += 1 - (ANS[i-1] + A[i])
      ANS[i] = ANS[i-1] + A[i]
      
      po_ = True
    elif ANS[i-1]+A[i] == 0 and po_: # nega: pos
      po_ = False
      A[i] -= 1
      Cost += 1
      ANS[i] = ANS[i-1] + A[i]
    elif ANS[i-1]+A[i] < 0 and po_:
      po_ = False
      ANS[i] = ANS[i-1] + A[i]
    elif ANS[i-1]+A[i] == 0 and not po_:
      po_ = True
      A[i] += 1
      Cost += 1
      ANS[i] = ANS[i-1] + A[i]
  print(Cost)
if __name__ == '__main__':
  main()

Submission status: Wrong Answer

Solution code:
#
# Written by NoKnowledgeGG @YlePhan
# ('ω')
#
#import math
#mod = 10**9+7
#import itertools
#import fractions
#import numpy as np
#mod = 10**4 + 7
"""def kiri(n,m):
  r_ = n / m
  if (r_ - (n // m)) > 0:
    return (n//m) + 1
  else:
    return (n//m)"""

""" n! mod m 階乗
mod = 1e9 + 7
N = 10000000
fac = [0] * N
def ini():
  fac[0] = 1 % mod
  for i in range(1,N):
    fac[i] = fac[i-1] * i % mod"""

"""mod = 1e9+7
N = 10000000
pw = [0] * N
def ini(c):
  pw[0] = 1 % mod
  for i in range(1,N):
    pw[i] = pw[i-1] * c % mod"""

"""
def YEILD():
  yield 'one'
  yield 'two'
  yield 'three'
generator = YEILD()
print(next(generator))
print(next(generator))
print(next(generator))
"""
"""def gcd_(a,b):
  if b == 0:#結局はc,0の最大公約数はcなのに
    return a
  return gcd_(a,a % b) # a = p * b + q"""
"""def extgcd(a,b,x,y):
  d = a
  if b!=0:
    d = extgcd(b,a%b,y,x)
    y -= (a//b) * x
    print(x,y)
  else:
    x = 1
    y = 0
  return d"""


def readInts():
  return list(map(int,input().split()))
mod = 10**9 + 7
def main():
  n = int(input())
  A = readInts()
  # 符号 positive?
  #po_ = True 
  # 変わったか変わってないか
  if A[0] >= 0: # if positive
    po_ = True
  else: # negative
    po_ = False
  Cost = 0
  ANS = [0] * (n+1)
  ANS[0] = A[0]
  for i in range(1,n):
    #print(ANS[i-1],po_,ANS[i-1] + A[i],Cost)
    if ANS[i-1]+A[i] > 0 and not po_: # sumがpositiveで前がnegativeだった
      po_ = True 
      ANS[i] = ANS[i-1] + A[i]
      # これで終わり
    elif ANS[i-1]+A[i] > 0 and po_: # posi : posi ?
      # 負にしなければならない
      Cost += abs(-1 - (ANS[i-1]+A[i])) # 先にこれやれ
      A[i] += -1 - (ANS[i-1] + A[i])
      # -4
      ANS[i] = ANS[i-1] + A[i]
      po_ = False
    elif ANS[i-1]+A[i] < 0 and not po_: #nega : nega
      # -1 はここ
      # print(A[i])
      Cost += abs(1 - (ANS[i-1]+A[i])) # 先にこれやれ
      A[i] += 1 - (ANS[i-1] + A[i])
      ANS[i] = ANS[i-1] + A[i]
      
      po_ = True
    elif ANS[i-1]+A[i] == 0 and po_: # nega: pos
      po_ = False
      A[i] += 1
      Cost += 1
      ANS[i] = ANS[i-1] + A[i]
    elif ANS[i-1]+A[i] < 0 and po_:
      po_ = False
      ANS[i] = ANS[i-1] + A[i]
    elif ANS[i-1]+A[i] == 0 and not po_:
      po_ = True
      A[i] += 1
      Cost += 1
      ANS[i] = ANS[i-1] + A[i]
  print(Cost)
if __name__ == '__main__':
  main()

Submission status: Wrong Answer

Solution code:
#
# Written by NoKnowledgeGG @YlePhan
# ('ω')
#
#import math
#mod = 10**9+7
#import itertools
#import fractions
#import numpy as np
#mod = 10**4 + 7
"""def kiri(n,m):
  r_ = n / m
  if (r_ - (n // m)) > 0:
    return (n//m) + 1
  else:
    return (n//m)"""

""" n! mod m 階乗
mod = 1e9 + 7
N = 10000000
fac = [0] * N
def ini():
  fac[0] = 1 % mod
  for i in range(1,N):
    fac[i] = fac[i-1] * i % mod"""

"""mod = 1e9+7
N = 10000000
pw = [0] * N
def ini(c):
  pw[0] = 1 % mod
  for i in range(1,N):
    pw[i] = pw[i-1] * c % mod"""

"""
def YEILD():
  yield 'one'
  yield 'two'
  yield 'three'
generator = YEILD()
print(next(generator))
print(next(generator))
print(next(generator))
"""
"""def gcd_(a,b):
  if b == 0:#結局はc,0の最大公約数はcなのに
    return a
  return gcd_(a,a % b) # a = p * b + q"""
"""def extgcd(a,b,x,y):
  d = a
  if b!=0:
    d = extgcd(b,a%b,y,x)
    y -= (a//b) * x
    print(x,y)
  else:
    x = 1
    y = 0
  return d"""


def readInts():
  return list(map(int,input().split()))
mod = 10**9 + 7
def main():
  n = int(input())
  A = readInts()
  # 符号 positive?
  #po_ = True 
  # 変わったか変わってないか
  if A[0] >= 0: # if positive
    po_ = True
  else: # negative
    po_ = False
  Cost = 0
  ANS = [0] * (n+1)
  ANS[0] = A[0]
  for i in range(1,n):
    #print(ANS[i-1],po_,ANS[i-1] + A[i],Cost)
    if ANS[i-1]+A[i] > 0 and not po_: # sumがpositiveで前がnegativeだった
      po_ = True 
      ANS[i] = ANS[i-1] + A[i]
      # これで終わり
    elif ANS[i-1]+A[i] > 0 and po_: # posi : posi ?
      # 負にしなければならない
      Cost += abs(-1 - (ANS[i-1]+A[i])) # 先にこれやれ
      A[i] += -1 - (ANS[i-1] + A[i])
      # -4
      ANS[i] = ANS[i-1] + A[i]
      po_ = False
    elif ANS[i-1]+A[i] < 0 and not po_: #nega : nega
      # -1 はここ
      # print(A[i])
      Cost += abs(1 - (ANS[i-1]+A[i])) # 先にこれやれ
      A[i] += 1 - (ANS[i-1] + A[i])
      ANS[i] = ANS[i-1] + A[i]
      
      po_ = True
    elif ANS[i-1]+A[i] == 0 and po_: # nega: pos
      po_ = False
      A[i] += 1
      Cost += 1
      ANS[i] = ANS[i-1] + A[i]
    elif ANS[i-1]+A[i] < 0 and po_:
      po_ = False
      ANS[i] = ANS[i-1] + A[i]
      
  print(Cost)
if __name__ == '__main__':
  main()

Submission status: Wrong Answer

Solution code:
#
# Written by NoKnowledgeGG @YlePhan
# ('ω')
#
#import math
#mod = 10**9+7
#import itertools
#import fractions
#import numpy as np
#mod = 10**4 + 7
"""def kiri(n,m):
  r_ = n / m
  if (r_ - (n // m)) > 0:
    return (n//m) + 1
  else:
    return (n//m)"""

""" n! mod m 階乗
mod = 1e9 + 7
N = 10000000
fac = [0] * N
def ini():
  fac[0] = 1 % mod
  for i in range(1,N):
    fac[i] = fac[i-1] * i % mod"""

"""mod = 1e9+7
N = 10000000
pw = [0] * N
def ini(c):
  pw[0] = 1 % mod
  for i in range(1,N):
    pw[i] = pw[i-1] * c % mod"""

"""
def YEILD():
  yield 'one'
  yield 'two'
  yield 'three'
generator = YEILD()
print(next(generator))
print(next(generator))
print(next(generator))
"""
"""def gcd_(a,b):
  if b == 0:#結局はc,0の最大公約数はcなのに
    return a
  return gcd_(a,a % b) # a = p * b + q"""
"""def extgcd(a,b,x,y):
  d = a
  if b!=0:
    d = extgcd(b,a%b,y,x)
    y -= (a//b) * x
    print(x,y)
  else:
    x = 1
    y = 0
  return d"""


def readInts():
  return list(map(int,input().split()))
mod = 10**9 + 7
def main():
  n = int(input())
  A = readInts()
  # 符号 positive?
  #po_ = True 
  # 変わったか変わってないか
  if A[0] >= 0: # if positive
    po_ = True
  else: # negative
    po_ = False
  Cost = 0
  ANS = [0] * (n+1)
  ANS[0] = A[0]
  for i in range(1,n):
    #print(ANS[i-1],po_,ANS[i-1] + A[i])
    if ANS[i-1]+A[i] >= 0 and not po_: # sumがpositiveで前がnegativeだった
      po_ = True 
      if ANS[i-1]+A[i] == 0:
        A[i] -= 1
        ANS[i] = ANS[i-1] + A[i]
        Cost += 1
      
      ANS[i] = ANS[i-1] + A[i]
      # これで終わり
    elif ANS[i-1]+A[i] >= 0 and po_: # posi : posi ?
      # 負にしなければならない
      Cost += abs(-1 - (ANS[i-1]+A[i])) # 先にこれやれ
      A[i] += -1 - (ANS[i-1] + A[i])
      # -4
      ANS[i] = ANS[i-1] + A[i]
      po_ = False
    elif ANS[i-1]+A[i] < 0 and not po_: #nega : nega
      # -1 はここ
      # print(A[i])
      Cost += abs(1 - (ANS[i-1]+A[i])) # 先にこれやれ
      A[i] += 1 - (ANS[i-1] + A[i])
      ANS[i] = ANS[i-1] + A[i]
      
      po_ = True
    elif ANS[i-1]+A[i] < 0 and po_: # nega: pos
      po_ = False
      ANS[i] = ANS[i-1] + A[i]
  print(Cost)
if __name__ == '__main__':
  main()

Submission status: Wrong Answer

Solution code:
#
# Written by NoKnowledgeGG @YlePhan
# ('ω')
#
#import math
#mod = 10**9+7
#import itertools
#import fractions
#import numpy as np
#mod = 10**4 + 7
"""def kiri(n,m):
  r_ = n / m
  if (r_ - (n // m)) > 0:
    return (n//m) + 1
  else:
    return (n//m)"""

""" n! mod m 階乗
mod = 1e9 + 7
N = 10000000
fac = [0] * N
def ini():
  fac[0] = 1 % mod
  for i in range(1,N):
    fac[i] = fac[i-1] * i % mod"""

"""mod = 1e9+7
N = 10000000
pw = [0] * N
def ini(c):
  pw[0] = 1 % mod
  for i in range(1,N):
    pw[i] = pw[i-1] * c % mod"""

"""
def YEILD():
  yield 'one'
  yield 'two'
  yield 'three'
generator = YEILD()
print(next(generator))
print(next(generator))
print(next(generator))
"""
"""def gcd_(a,b):
  if b == 0:#結局はc,0の最大公約数はcなのに
    return a
  return gcd_(a,a % b) # a = p * b + q"""
"""def extgcd(a,b,x,y):
  d = a
  if b!=0:
    d = extgcd(b,a%b,y,x)
    y -= (a//b) * x
    print(x,y)
  else:
    x = 1
    y = 0
  return d"""


def readInts():
  return list(map(int,input().split()))
mod = 10**9 + 7
def main():
  n = int(input())
  A = readInts()
  # 符号 positive?
  #po_ = True 
  # 変わったか変わってないか
  if A[0] >= 0: # if positive
    po_ = True
  else: # negative
    po_ = False
  Cost = 0
  ANS = [0] * (n+1)
  ANS[0] = A[0]
  #print(ANS[0])
  for i in range(1,n):
    #print(sum(A[:i+1]),A[i],po_)
    if ANS[i-1]+A[i] >= 0 and not po_: # sumがpositiveで前がnegativeだった
      po_ = True # positiveに
      ANS[i] = ANS[i-1] + A[i]
      # 3
      if ANS[i-1]+A[i] == 0:
        ANS[i] = 1
        Cost += 1
      # これで終わり
    elif ANS[i-1]+A[i] >= 0 and po_: # posi : posi ?
      # 負にしなければならない
      Cost += abs(-1 - (ANS[i-1]+A[i])) # 先にこれやれ
      A[i] += -1 - (ANS[i-1] + A[i])
      # -4
      ANS[i] = ANS[i-1] + A[i]
      po_ = False
    elif ANS[i-1]+A[i] < 0 and not po_: #nega : nega
      # -1 はここ
      # print(A[i])
      Cost += abs(1 - (ANS[i-1]+A[i])) # 先にこれやれ
      A[i] += 1 - (ANS[i-1] + A[i])
      ANS[i] = ANS[i-1] + A[i]
      
      po_ = True
    else: # nega: pos
      po_ = False
      ANS[i] = ANS[i-1] + A[i]
      if ANS[i-1]+A[i] == 0:
        ANS[i] = -1
        Cost +=1
    #print(A[i])
    #print(ANS[i])
  print(Cost)
if __name__ == '__main__':
  main()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
n=int(input())
A=list(map(int,input().split()))

S1=0
cost1=0
for i in range(n):
    if i%2==0:
        if S1+A[i]>0:
            S1+=A[i]
        else:
            cost1+=abs(S1+A[i])+1
            S1=1
    else:
        if S1+A[i]<0:
            S1+=A[i]
        else:
            cost1+=abs(S1+A[i])+1
            S1=-1

S2=0
cost2=0
for i in range(n):
    if i%2==0:
        if S2+A[i]<0:
            S2+=A[i]
        else:
            cost2+=abs(S2+A[i])+1
            S2=-1
    else:
        if S2+A[i]>0:
            S2+=A[i]
        else:
            cost2+=abs(S2+A[i])+1
            S=1
    
print(min(cost1,cost2))

Submission status: Wrong Answer

Solution code:
n=int(input())

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

kp=0
asum=0
flag=-1
for i in range(n):
    asum=asum+a[i]
    if flag==-1:
        if asum>=0:
            kp=kp+asum+1
            asum=-1
    else:
        if asum<=0:
            kp=kp+1-asum
            asum=1
    flag=-flag
    print(asum)
print("-----",kp)            
km=0
asum=0
flag=1
for i in range(n):
    asum=asum+a[i]
    if flag==-1:
        if asum>=0:
            km=km+asum+1
            asum=-1
    else:
        if asum<=0:
            km=km+1-asum
            asum=1
    flag=-flag
    print(asum)
print("-----",km)            

print(min(kp,km))

Submission status: Wrong Answer

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

cumulative=[0]
for i in range(n):
    cumulative.append(cumulative[i] + a[i])
cumulative.pop(0)
#print(cumulative)

def plus():
    change = 0
    cnt = 0
    for i, val in enumerate(cumulative):
        #print(i, val+change, change)
        if i % 2 == 0:
            if val + change > 0:
                pass
            else:
                temp = change
                change += 1 - (val + change)
                cnt += abs(1 - (val + temp))
        else:
            if val + change < 0:
                pass
            else:
                temp = change
                change += -1 - (val + change)
                cnt += abs(-1 - (val + temp))
    #print(cnt)
    #print(change)
    return cnt


def minus():
    change = 0
    cnt = 0
    for i, val in enumerate(cumulative):
        #print(i, val+change, change)
        if i % 2 == 0:
            if val + change < 0:
                pass
            else:
                temp = change
                change += -1 - (val + change)
                cnt += abs(-1 - (val + temp))
                #print('+', abs(-1 - (val + temp)))
        else:
            if val + change > 0:
                pass
            else:
                temp = change
                change += 1 - (val + change)
                cnt += abs(1 - (val + temp))
                #print('+', abs(1 - (val + temp)))
    #print(cnt)
    #print(change)
    return cnt

if a[0] > 0:
    print(plus())
elif a[0] < 0:
    print(minus())
else:
    print(min(plus(), minus()))

Submission status: Wrong Answer

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

x=0#pmpm
y=0
sum1=0
sum2=0

for i in range(n):
  sum1+=a[i]
  sum2+=b[i]
  if(i%2==0):
    if(sum1<=0):
      x+=1-sum1
      a[i]+=1-sum1
    if(sum2>=0):
      y+=sum2+1
      b[i]-=sum2+1
  else:
    if(sum1>=0):
      x+=sum1+1
      a[i]-=1+sum1
    if(sum2<=0):
      y+=1-sum2
      b[i]+=1-sum2
print(min(x,y))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
count = 0
sum_ = 0
for i in range(n):
    if sum_ * (sum_+a[i]) >=0 and i!=0:
        if sum_ > 0:
            count += sum_+a[i]+1
            a[i] = -sum_-1
        elif sum_ < 0:
            count += abs(sum_)-a[i]+1
            a[i] = -sum_+1
    sum_ += a[i]
print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
count = 0
sum_ = 0
for i in range(n):
    if sum_ * (sum_+a[i]) >=0 and i!=0:
        if sum_ > 0:
            count += sum_+a[i]+1
            a[i] = -sum_-1
        elif sum_ < 0:
            count += abs(sum_+a[i])+1
            a[i] = -sum_+1
    sum_ += a[i]
print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
count = 0
sum_ = 0
for i in range(n):
    if sum_ * (sum_+a[i]) >=0 and i!=0:
        if sum_ > 0:
            count += sum_+a[i]+1
            a[i] = -sum_-1
        if sum_ < 0:
            count += abs(sum_+a[i])+1
            a[i] = -sum_+1
    sum_ += a[i]
print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
count = 0
sum_ = 0
for i in range(n):
    if sum_ * (sum_+a[i]) >0 and i!=0:
        if sum_ > 0:
            count += sum_+a[i]+1
            a[i] = -sum_-1
        if sum_ < 0:
            count += abs(sum_+a[i])+1
            a[i] = -sum_+1
    sum_ += a[i]
print(count)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
count = 0
sum_ = 0
for i in range(n):
    if sum_ * (sum_+a[i]) <0 or i == 0:
        sum_ += a[i]
        continue
    elif sum_ > 0:
        count += sum_+a[i]+1
        a[i] = -sum_-1
    elif sum_ < 0:
        count += abs(sum_+a[i])+1
        a[i] = -sum_+1
    sum_ += a[i]
print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
count = 0
sum_ = 0
for i in range(n):
    if sum_ * (sum_+a[i]) <0 or i == 0:
        sum_ += a[i]
    elif sum_ >= 0:
        count += sum_+a[i]+1
        a[i] = -sum_-1
        sum_ += a[i]
    elif sum_ < 0:
        count += abs(sum_+a[i])+1
        a[i] = -sum_+1
        sum_ += a[i]

print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
count = 0
sum_ = 0
for i in range(n):
    if sum_ * (sum_+a[i]) <0 or i == 0:
        sum_ += a[i]
    elif sum_ > 0:
        count += sum_+a[i]+1
        a[i] = -sum_-1
        sum_ += a[i]
    elif sum_ < 0:
        count += abs(sum_+a[i])+1
        a[i] = -sum_+1
        sum_ += a[i]

print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
count = 0
sum_ = 0
for i in range(n):
    if sum_ * (sum_+a[i]) <0 or i == 0:
        sum_ += a[i]
    elif sum_ > 0:
        count += sum_+a[i]+1
        a[i] = -sum_-1
        sum_ += a[i]
    elif sum_ < 0:
        count += abs(sum_+a[i])+1
        a[i] = -sum_-1
        sum_ += a[i]

print(count)