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: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

def chk(a,first):
    count = 0
    x = 0 # sum
    t = first # True : plus, False : minus
    for item in a:
        x += item
        if t == True and x<1:
            count += 1-x
            x = 1
        elif t == False and x>-1:
            count += x+1
            x = -1
        t = not t
    return count

print(min(chk(a,True),chk(a,False)))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
import numpy as np
na1 = np.array(a).cumsum()
na2 = np.array(a).cumsum()
cnt1 = 0
hoge1 = 0
cnt2 = 0
hoge2 = 0
delta1 = 0
delta2 = 0
for i in range(n):
  na1[i] += hoge1
  na2[i] += hoge2
  if(i % 2 == 0 and na1[i] <= 0):
    delta1 = abs(na1[i]) + 1
    cnt1 = cnt1 + delta1
    hoge1 += delta1
    na1[i] += delta1
  elif(i % 2 == 1 and na1[i] >= 0):
    delta1 = abs(na1[i]) + 1 
    cnt1 = cnt1 + delta1
    hoge1 -= delta1
    na1[i] -= delta1    
  if(i % 2 == 1 and na2[i] <= 0):
    delta2 = abs(na2[i]) + 1
    cnt2 = cnt2 + delta2
    hoge2 += delta2
    na2[i] += delta2  
  elif(i % 2 == 0 and na2[i] >= 0):
    delta2 = abs(na2[i]) + 1
    cnt2 = cnt2 + delta2
    hoge2 -= delta2
    na2[i] -= delta2
  else:
    na1[i]
print(min([abs(cnt1),abs(cnt2)]))

Submission status: Accepted

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

    def solver(sign):
        ret, total = 0, 0
        for a in A:
            total += a
            if sign * total <= 0:
                ret += abs(sign - total)  # 総和がsignになるまでaを変化させる
                total = sign
            sign *= -1
        return ret
    # 1: 奇数番目を正、偶数番目を負にする場合。 -1: その逆
    return min(solver(1), solver(-1))

print(c_sequence())

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
ans = {True:0, False:0}
for s in [True, False]:
    sign = s
    total = 0
    for i in range(len(A)):
        if total + A[i] == 0 or sign == (total + A[i] > 0):
            ans[s] += abs(total + A[i]) + 1
            total = -1 if sign else 1
        else:
            total += A[i]
        sign = total > 0
print(min(ans.values()))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
A = list(map(int,input().split()))
sumA = [max(A[0],1)]
ans1 = abs(sumA[0]-A[0])
for i in range(1,N):
    sumA.append(sumA[-1]+A[i])
    if np.sign(sumA[-2]) == np.sign(sumA[-1]) or np.sign(sumA[-1]) == 0:
        ans1 += abs(sumA[-1] + np.sign(sumA[-2]))
        sumA[-1] = -np.sign(sumA[-2])
sumA = [min(A[0],-1)]
ans2 = abs(sumA[0]-A[0])
for i in range(1,N):
    sumA.append(sumA[-1]+A[i])
    if np.sign(sumA[-2]) == np.sign(sumA[-1]) or np.sign(sumA[-1]) == 0:
        ans2 += abs(sumA[-1] + np.sign(sumA[-2]))
        sumA[-1] = -np.sign(sumA[-2])
print(min(ans1,ans2))

Submission status: Accepted

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

if a[0] > 0:
  A = 0
  t = a[0]
else:
  A = -a[0] + 1
  t = 1
for i in range(1, n):
  if i % 2 == 1:
    if t + a[i] >= 0:
      A += t + a[i] + 1
      t = -1
    else:
      t = t + a[i]
  else:
    if t + a[i] <= 0:
      A += -(t + a[i]) + 1
      t = 1
    else:
      t = t + a[i]

if a[0] < 0:
  B = 0
  t = a[0]
else:
  B = a[0] + 1
  t = -1
for i in range(1, n):
  if i % 2 == 0:
    if t + a[i] >= 0:
      B += t + a[i] + 1
      t = -1
    else:
      t = t + a[i]
  else:
    if t + a[i] <= 0:
      B += -(t + a[i]) + 1
      t = 1
    else:
      t = t + a[i]

print(min(A, B))

Submission status: Accepted

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

ans = 10**10

# 1 -1 1 -1...
cost1 = 0
s = 0
for i in range(n):
    t = 1 if i % 2 == 0 else -1
    c = 0
    if i % 2 == 0:
        if s + a[i] > 0:
            s += a[i]
        else:
            c = abs(s - t)
            s += c
            cost1 += abs(c - a[i])
    else:
        if s + a[i] < 0:
            s += a[i]
        else:
            c = abs(s - t)
            s -= c
            cost1 += abs(- c - a[i])

cost2 = 0
s = 0
for i in range(n):
    t = -1 if i % 2 == 0 else 1
    c = 0
    if i % 2 == 1:
        if s + a[i] > 0:
            s += a[i]
        else:
            c = abs(s - t)
            s += c
            cost2 += abs(c - a[i])
    else:
        if s + a[i] < 0:
            s += a[i]
        else:
            c = abs(s - t)
            s -= c
            cost2 += abs(- c - a[i])

print(min(cost1, cost2))
# -1 1 -1 1..

Submission status: Accepted

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

def sign(n):
  if n > 0:
    return 'pos'
  elif n < 0:
    return 'neg'
  else:
    return 'zero'

# +-+-+-+-...と-+-+-+-+を両方して操作回数が少ない方
# 1イテレーションで2パターン同時にできそう
result_pos = 0
result_neg = 0
now_sum_pos = 0
now_sum_neg = 0
for i,a in enumerate(A):
  now_sum_pos += a
  now_sum_neg += a
  if i%2 == 0:
    if not sign(now_sum_pos) == 'pos':
      result_pos += 1+abs(now_sum_pos)
      now_sum_pos = 1
    if not sign(now_sum_neg) == 'neg':
      result_neg += 1+abs(now_sum_neg)
      now_sum_neg = -1
  elif i%2 == 1:
    if not sign(now_sum_pos) == 'neg':
      result_pos += 1+abs(now_sum_pos)
      now_sum_pos = -1
    if not sign(now_sum_neg) == 'pos':
      result_neg += 1+abs(now_sum_neg)
      now_sum_neg = 1
print(min(result_pos, result_neg))

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    A = [int(a) for a in input().split()]
    min_count = float('inf')
    if A[0] != 0:
        tot = A[0]
        count = 0
        for a in A[1:]:
            if tot > 0 and tot + a >= 0:
                count += tot+a+1
                tot = -1
            elif tot < 0 and tot + a <= 0:
                count += -(tot+a)+1
                tot = 1
            else:
                tot += a
        min_count = min(min_count, count)
    for i in [1, -1]:
        count = abs(i - A[0])
        tot = i
        for a in A[1:]:
            if tot > 0 and tot + a >= 0:
                count += tot+a+1
                tot = -1
            elif tot < 0 and tot + a <= 0:
                count += -(tot+a)+1
                tot = 1
            else:
                tot += a
        min_count = min(min_count, count)
    return min_count

if __name__ == '__main__':
    print(main())

Submission status: Accepted

Solution code:
n = int(input())
f,*A,= map(int,input().split())
pcnt = 0 if f>0 else 1-f
ncnt = 0 if f<0 else f+1
pcur = max(1,f)
ncur = min(-1,f)
for i,a in enumerate(A):
	if pcur*(pcur+a)<0:
		pcur += a
	else:
		pcnt += abs((-1)**(i+1)-(pcur+a))
		pcur = (-1)**(i+1)
	if ncur*(ncur+a)<0:
		ncur += a
	else:
		ncnt += abs((-1)**i-(ncur+a))
		ncur = (-1)**i
print(min(pcnt,ncnt))

Submission status: Accepted

Solution code:

N = int(input())

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


#奇数番目を負にする場合
odd = 0
total = 0
for i in range(N):
    total += A[i]
    if i % 2 == 0: #奇数番目の場合
        if total > -1:
            odd += 1 + total
            total = -1
    else:
        if total < 1:
            odd += 1 - total
            total = 1

#偶数番目を負にする場合
even = 0
total = 0
for i in range(N):
    total += A[i]
    if i % 2 == 0: #奇数番目の場合
        if total < 1:
            even += 1 - total
            total = 1
    else:
        if total > -1:
            even += 1 + total
            total = -1


print(min(odd, even))

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a = [int(i) for i in input().split()]
    i = 0
    # 偶数番目が正のとき
    s = 0
    tmp1 = 0
    if a[i] > 0:
        s = a[i]
    else:
        tmp1 = abs(a[i])+1
        s = 1
    i = 1
    while i < n:
        if not s * (s+a[i]) < 0:
            tmp1 += abs(s+a[i])+1
            if i % 2 == 0:
                s = 1
            else:
                s = -1
        else:
            s += a[i]
        i += 1
    # 奇数番目が正のとき
    s = 0
    tmp2 = 0
    i = 0
    if a[i] < 0:
        s = a[i]
    else:
        tmp2 = abs(a[i])+1
        s = -1
    i = 1
    while i < n:
        if not s * (s+a[i]) < 0:
            tmp2 += abs(s+a[i])+1
            if i % 2 == 1:
                s = 1
            else:
                s = -1
        else:
            s += a[i]
        i += 1
    print(min(tmp1, tmp2))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
nums = int(input())
ary = list(map(int,input().split()))

#puls no toki
count = 0
sum = ary[0]
if ary[0] < 0 :
    count += abs(ary[0])+1
    sum = 1
elif ary[0] == 0:
    sum = 1
    count += 1

for i in range (1,nums):
    sum += ary[i]

    if i % 2 == 1:
        if sum > 0 :
            count += abs(sum)+1
            sum = -1
        elif sum == 0:
            sum = -1
            count += 1
    elif i % 2 == 0:
        if sum < 0:
            count += abs(sum)+1
            sum  = 1
        elif sum == 0:
            sum = 1
            count += 1

result_plus = count

#minus no toki
count = 0
sum = ary[0]
if ary[0] > 0:
    count += abs(ary[0]) +1
    sum = -1
elif ary[0] == 0:
    sum = -1
    count += 1

for i in range(1,nums):
    sum += ary[i]
    if i % 2 == 1:
        if sum < 0:
            count += abs(sum)+1
            sum = 1
        elif sum == 0:
            count += 1
            sum = 1
    if i % 2 == 0:
        if sum > 0:
            count += abs(sum)+1
            sum = -1
        elif sum == 0:
            count += 1
            sum = -1

result_minus = count

if result_plus > result_minus:
    print(result_minus)
else:
    print(result_plus)

Submission status: Accepted

Solution code:
from copy import copy
n = int(input())
a = [int(x) for x in input().split()]
ans1=[(-1)**i for i in range(n)]

b=copy(a)
res_b=0
sb=0
c=copy(a)
res_c=0
sc=0
for i in range(n):
  sb+=a[i]
  if ans1[i]*sb>0:
    pass
  else:
    b[i]=ans1[i]-(sb-b[i])
    sb=sb-a[i]+b[i]
    res_b+=abs(b[i]-a[i])

  sc+=a[i]
  if -1*ans1[i]*sc>0:
    pass
  else:
    c[i]=-1*ans1[i]-(sc-c[i])
    sc=sc-a[i]+c[i]
    res_c+=abs(c[i]-a[i])
  
print(min(res_b,res_c))

Submission status: Accepted

Solution code:
"""
# 標準入力取得
## 文字列
 = sys.stdin.readline().rstrip()
 = list(sys.stdin.readline().rstrip())

## 数値
 = int(sys.stdin.readline())
 = map(int, sys.stdin.readline().split())
 = list(map(int, sys.stdin.readline().split()))
 = [list(map(int,list(sys.stdin.readline().split()))) for i in range(h)]  # 二次元配列入力　二次元マップみたいな入力のとき

# 0埋め, 小数点出力桁指定時のときの出力
a = 100
b = 0.987654321
print("{0:06d}-{1:6f}".format(a,b))
000100-0.987654

# 文字列をリストに格納
char_list = list("abcd") # ["a","b","c","d"]
"""

import math
import sys
import itertools
import queue
from fractions import gcd

def lcm(a, b):
    return a * b // gcd(a, b)

mod = 1000000007

if __name__ == "__main__":
    n = int(sys.stdin.readline())
    a = list(map(int, sys.stdin.readline().split()))
    def sequence(a,t):
        ans = 0
        x = 0
        for e in a:
            x += e
            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
        return ans
    print(min(sequence(a,True),sequence(a,False)))

Submission status: Accepted

Solution code:
n = int(input())
As = list(map(int, input().split()))
rs = []
for m in {0, 1}:
  r = 0
  s = 0
  for i in range(n):
    A = As[i]
    if i%2 == m:
      if s+A >= 0:
        r += s+A+1
        s = -1
      else:
        s += A
    else:
      if s+A <= 0:
        r += -(s+A)+1
        s = +1
      else:
        s += A
  #print(m, r)
  rs.append(r)
r = min(rs)
print(r)

Submission status: Accepted

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

def f(a,t):
  ans=0
  cur=0
  for i in a:
    cur+=i
    if t==True and cur<1:
      ans+=(1-cur)
      cur=1
    elif t==False and cur>-1:
      ans+=(1+cur)
      cur=-1
    t=not t
  return ans
print(min(f(a,True),f(a,False)))

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
def chk(A,t):
  ans=0
  s=0
  for i in A:
    s+=i
    if t==True and s<1:
      ans+=1-s
      s=1
    elif t==False and s>-1:
      ans+=1+s
      s=-1
    t=not t
  return ans
print(min(chk(A,True),chk(A,False)))

Submission status: Accepted

Solution code:

import bisect
import collections
import itertools

def getint(): return int(input())
def getints(): return list(map(int, input().split()))
def getint2d(rows): return [getints() for _ in range(rows)]
def getgrid(rows): return [input() for _ in range(rows)]
def array1d(n, value): return [value for _ in range(n)]
def array2d(n, m, value): return [array1d(m, value) for _ in range(n)]

n = getint()
values = getints()

def solve(values, prev):
    res = 0
    for v in values:
        s = prev + v
        add = 0
        if prev > 0 and s >= 0:
            add = -s - 1
        if prev < 0 and s <= 0:
            add = -s + 1
        res += abs(add)
        prev = s + add
    return res

res1, res2 = 0, 0
if values[0] <= 0:
    add = abs(values[0]) + 1
    res1 = solve(values[1:], values[0] + add) + abs(add)
    add = -1 if values[0] == 0 else 0
    res2 = solve(values[1:], values[0] + add) + abs(add)
else:
    add = 1 if values[0] == 0 else 0
    res1 = solve(values[1:], values[0] + add) + abs(add)
    add = -(values[0] + 1)
    res2 = solve(values[1:], values[0] + add) + abs(add)

print(min(res1, res2))

Submission status: Accepted

Solution code:
#! /Library/Frameworks/Python.framework/Versions/3.7/bin/python3
# a[0] == 0 のケース、要検証

n = int(input())
a = list(map(int, input().split()))
count_p = 0  # 操作回数(正)
count_m = 0  # 操作回数(負)
s = 0  # 現時点の和


def counter(a, s):
    if s * (s + a) < 0:
        return 0
    else:
        if s < 0:
            return 1 - (s + a)
        else:
            return -1 - (s + a)


# a[0]を+にする場合
if a[0] <= 0:
    count_p = 1 - a[0]
    s = 1
else:
    s = a[0]

for i in range(n-1):
    tmp = counter(a[i + 1], s)
    s = s + a[i+1]+tmp
    count_p = count_p + abs(tmp)

# a[0]を-にする場合
if a[0] >= 0:
    count_m = 1 + a[0]
    s = -1
else:
    s = a[0]


for i in range(n-1):
    tmp = counter(a[i + 1], s)
    s = s + a[i+1]+tmp
    count_m = count_m + abs(tmp)

print(str(min(count_p, count_m)))

Submission status: Accepted

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

def j(B,sign):
    ans,cur = 0,[0]
    for i in range(n):
        cur.append(cur[i]+B[i])
        cs = np.sign(cur[i+1])
        if cs != sign:
            if sign == -1:
                ans += -(-cur[i]-1)+B[i]
                cur[i+1] = -1
            else:
                ans += -cur[i]+1-B[i]
                cur[i+1] = 1
        sign *= -1
    return ans

print(min(j(A,1),j(A,-1)))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
INF = float("inf")


def solve(n: int, a: "List[int]"):

    # 正スタート
    tot = 0
    countA = 0
    for i, x in enumerate(a):
        tot += x
        if i % 2 == 0:
            if tot <= 0:
                countA += -tot+1
                tot = 1
        else:
            if tot >= 0:
                countA += tot+1
                tot = -1

    tot = 0
    countB = 0
    for i, x in enumerate(a):
        tot += x
        if i % 2 == 1:
            if tot <= 0:
                countB += -tot+1
                tot = 1
        else:
            if tot >= 0:
                countB += tot+1
                tot = -1
    print(min(countA, countB))

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    n = int(next(tokens))  # type: int
    a = [int(next(tokens)) for _ in range(n)]  # type: "List[int]"
    solve(n, a)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

tmp1 = 0
sum1 = 0
tmp2 = 0
sum2 = 0

for i in range(n):
    sum1 += a[i]
    if i % 2 == 0:
        if sum1  > 0 :
            pass
        else:
            tmp1 += 1 - sum1
            sum1 =  1
    else:
        if sum1  < 0 :
            pass
        else:
            tmp1 += sum1 - (-1)
            sum1 =  -1

for i in range(n):
    sum2 += a[i]
    if i % 2 == 1:
        if sum2  > 0 :
            pass
        else:
            tmp2 += 1 - sum2
            sum2 =  1
    else:
        if sum2  < 0 :
            pass
        else:
            tmp2 += sum2 - (-1)
            sum2 =  -1

ans = min(tmp1, tmp2)
print(ans)

Submission status: Accepted

Solution code:
n=input()
a=list(map(int,input().split()))
ans=mans=0
if a[0]==0:
    ans+=1
    mans+=1
    sim=1
    msim=-1
elif a[0]>0:
    ans=0
    sim=a[0]
    mans+=a[0]+1
    msim=-1
else:
    ans=-a[0]+1
    sim=1
    mans=0
    msim=a[0]
for i in a[1:]:
    if sim<0:
        sim+=i
        if sim<=0:
            ans+=1-sim
            sim=1
    else:
        sim+=i
        if sim>=0:
            ans+=sim+1
            sim=-1
    if msim<0:
        msim+=i
        if msim<=0:
            mans+=1-msim
            msim=1
    else:
        msim+=i
        if msim>=0:
            mans+=msim+1
            msim=-1
print(min(ans,mans))

Submission status: Accepted

Solution code:
n = int(input())
*l, = map(int,input().split())
def calc(l,even):
  ans = 0
  l1 = l[:]
  s = 0
  for i in range(n):
    s += l1[i] 
    if i%2 == even and s<=0: 
      ans += -s+1 
      s = 1
    elif i%2 != even and s>=0: 
      ans += s+1
      s = -1
  return ans
print(min([calc(l,0),calc(l,1)]))

Submission status: Accepted

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

def f(x):
    ans = 0
    count = 0
    for a in an:
        count += a
        if count * x >= 0:
            ans += abs(count)+1
            count = -x
        x *= -1
    return ans


print(min(f(1),f(-1)))

Submission status: Accepted

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


def chk(a, t):
    ans = 0
    x = 0
    for i in a:
        x += i
        if t == True and x < 1:
            ans += 1 - x
            x = 1
        if t == False and x > -1:
            ans += x + 1
            x = -1
        t = not t
    return ans


print(min(chk(a, True), chk(a, False)))

Submission status: Accepted

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

goukei=0
count=0

for i in range(n):
    number=lista[i]+goukei
    if i%2==0:
        if number>=0:
            count=count+number+1
            goukei=-1
        else:
            goukei=number
    else:
        if number<=0:
            count=count+abs(number)+1
            goukei=1
        else:
            goukei=number

count1=count

goukei=0
count=0

for i in range(n):
    number=lista[i]+goukei
    if i%2==1:
        if number>=0:
            count=count+number+1
            goukei=-1
        else:
            goukei=number
    else:
        if number<=0:
            count=count+abs(number)+1
            goukei=1
        else:
            goukei=number
print(min(count,count1))

Submission status: Accepted

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

pos = 0
left = A[0]
if left <= 0:
    pos += 1 - left
    left = 1
for a in A[1:]:
    if left * (left + a) < 0:
        left += a
    else:
        if left + a == 0:
            if left < 0:
                left += a + 1
            else:
                left += a - 1
            pos += 1
        else:
            pos += abs(left + a) + 1
            if left < 0:
                left = 1
            else:
                left = -1

neg = 0
left = A[0]
if left >= 0:
    neg += left + 1
    left = -1
for a in A[1:]:
    if left * (left + a) < 0:
        left += a
    else:
        if left + a == 0:
            if left < 0:
                left += a + 1
            else:
                left += a - 1
            neg += 1
        else:
            neg += abs(left + a) + 1
            if left < 0:
                left = 1
            else:
                left = -1

print(min(neg, pos))

Submission status: Accepted

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

a1 = list(a)
s1 = 0
for i in range(n):
    if i % 2 == 0:
        if s1 + a1[i] >= 0:
            a1[i] = -1 - s1
    else:
        if s1 + a1[i] <= 0:
            a1[i] =  1 - s1
    s1 += a1[i]


a2 = list(a)
s2 = 0
for i in range(n):
    if i % 2 == 0:
        if s2 + a2[i] <= 0:
            a2[i] =  1 - s2
    else:
        if s2 + a2[i] >= 0:
            a2[i] = -1 - s2
    s2 += a2[i]

t1 = sum(abs(x - y) for x, y in zip(a, a1))
t2 = sum(abs(x - y) for x, y in zip(a, a2))
t = min(t1, t2)
print(t)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import copy
import collections
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import math
import itertools
import random
 
# NO, PAY-PAY
#import numpy as np
#import statistics
#from statistics import mean, median,variance,stdev
 
INF = float('inf')
def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
 
def main():
    n = inputInt()
    a = inputList()
    aa = []
    for i,val in enumerate(a):
        if i == 0:
            aa.append(val)
        else:
            tmp = val + aa[i-1]
            aa.append(tmp)
            
    barans = 0
    ans = 0
    flg = True
    for i,val in enumerate(aa):
        if i == 0:
            if val <= 0:
                barans = 1 - val
                ans += barans
        else:
            val_sin = val + barans
            if flg == True:
                flg = False
                if val_sin >= 0:
                    tmp = 1 + val_sin
                    ans += tmp
                    barans -= tmp
            else:
                flg = True
                if val_sin <= 0:
                    tmp = 1 - val_sin
                    ans += tmp
                    barans += tmp
        
    barans = 0
    ansans = 0
    flg = False
    for i,val in enumerate(aa):
        if i == 0:
            if val >= 0:
                barans = 1 + val
                ansans += barans
                barans = -1 * barans
        else:
            val_sin = val + barans
            if flg == True:
                flg = False
                if val_sin >= 0:
                    tmp = 1 + val_sin
                    ansans += tmp
                    barans -= tmp
            else:
                flg = True
                if val_sin <= 0:
                    tmp = 1 - val_sin
                    ansans += tmp
                    barans += tmp
        
    #print("{} {}".format(ans, ansans))
    print(min(ans,ansans))
        
    
if __name__ == "__main__":
	main()

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
s = []
for i in range(n):
    if i == 0:
        s.append(a[0])
    else:
        s.append(a[i]+s[-1])

count = 0
ans1 = 0
for i in range(n):
    if i%2 == 0:
        if s[i]+count > 0:
            continue
        else:
            ans1 += 1-(s[i]+count)
            count += 1-(s[i]+count)
    else:
        if s[i]+count < 0:
            continue
        else:
            ans1 += (s[i]+count) + 1
            count -= (s[i]+count) + 1
            
count1 = 0
ans2 = 0
for i in range(n):
    if i%2 == 1:
        if s[i]+count1 > 0:
            continue
        else:
            ans2 += 1-(s[i]+count1)
            count1 += 1-(s[i]+count1)
    else:
        if s[i]+count1 < 0:
            continue
        else:
            ans2 += (s[i]+count1) + 1
            count1 -= (s[i]+count1) + 1
            
print(min(ans1,ans2))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
sun = 0
ans1 = 0
for i in range(n):
  if i == 0:
    SUN = 1
  else:
    SUN = sun
  now = a[i]
  if SUN * (sun+now) >= 0:
    ans1 += abs(sun+now) + 1
    if SUN > 0:
      sun = -1
    else:
      sun = 1
  else:
    sun += now
sun = 0
ans2 = 0
for i in range(n):
  now = a[i]
  if i == 0:
    SUN = -1
  else:
    SUN = sun
  if SUN * (sun+now) >= 0:
    ans2 += abs(sun+now) + 1
    if SUN > 0:
      sun = -1
    else:
      sun = 1
  else:
    sun += now
#print(ans1,ans2)
print(min(ans1,ans2))

Submission status: Accepted

Solution code:
n=int(input())
aaa=tuple(map(int, input().split()))

ans_e=0
ans_o=0
even=0
odd=0
#even->+
for i, a in enumerate(aaa):
    even+=a
    if i%2==0:
        if even>0:
            continue
        else:
            tmp=1-even
            even=1
            ans_e+=tmp
    else:
        if even<0:
            continue
        else:
            tmp=even+1
            even=-1
            ans_e+=tmp

for i, a in enumerate(aaa):
    odd+=a
    if i%2==0:
        if odd<0:
            continue
        else:
            tmp=odd+1
            odd=-1
            ans_o+=tmp
    else:
        if odd>0:
            continue
        else:
            tmp=1-odd
            odd=1
            ans_o+=tmp
print(min(ans_e, ans_o))

Submission status: Accepted

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

total = 0
count = 0
pos_or_neg = True  # True = +, False = -

for a in a_list:
    total += a
    if pos_or_neg:
        if total <= 0:
            count += -total + 1
            total += -total + 1
        pos_or_neg = False

    else:
        if total >= 0:
            count += total + 1
            total -= total + 1
        pos_or_neg = True

first_count = count
count = 0
total = 0
pos_or_neg = False

for a in a_list:
    total += a
    if pos_or_neg:
        if total <= 0:
            count += -total + 1
            total += -total + 1
        pos_or_neg = False

    else:
        if total >= 0:
            count += total + 1
            total -= total + 1
        pos_or_neg = True

print(min(count, first_count))

Submission status: Accepted

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

def solve(isPositive):
    sum_a = 0
    ans = 0
    for a in A:
        sum_a += a
        
        # 一回前が正か？
        if isPositive:
            # 一回前が正の場合、今回は負にならないといけない
            # そのため、0以上ならば負になるように調整
            if sum_a>=0:
                ans += 1 + sum_a
                # 最小回数の操作なので、sum_aはマイナス1になる
                sum_a = -1
            isPositive = False
        else:
            if sum_a<=0:
                ans += 1 - sum_a
                sum_a = 1
            isPositive = True
            
    return ans

print(min(solve(False), solve(True)))

Submission status: Accepted

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

def calc(sign) :
    pre_sign = sign
    s = 0
    ret = 0
    for ai in a :
        if ai + s > 0 :
            cur_sign = 1
        elif ai + s < 0 :
            cur_sign = -1
        else :
            cur_sign = 0
        
        if pre_sign * cur_sign < 0 :
            s += ai
        else :
            ret += abs((pre_sign * -1) - (ai + s))
            s = pre_sign * -1
        pre_sign *= -1
        
    return ret
    
print(min(calc(1), calc(-1)))

Submission status: Accepted

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

cnt1 = 0
cnt2 = 0
acs = 0

for i in range(n):
    acs += lst[i]
    if acs == 0:
        if i%2==0:
            acs = 1
        else:
            acs = -1
        cnt1 += 1
    
    if i%2 == 0 and acs < 0: #偶数番目が正の場合
        cnt1 += 1 - acs
        acs = 1
        
    elif i%2 != 0 and acs > 0:
        cnt1 += abs(-1 - acs)
        acs = -1

acs = 0

for i in range(n):
    acs += lst[i]
    if acs == 0:
        if i%2==0:
            acs = -1
        else:
            acs = 1
        cnt2 += 1
    if i%2 != 0 and acs < 0: #奇数番目が正の場合
        cnt2 += 1 - acs
        acs = 1
        
    elif i%2 == 0 and acs > 0:
        cnt2 += abs(-1 - acs)
        acs = -1
        
print(min(cnt1,cnt2))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
l = len(a)
cnt = 0
a_ = 0
x = -1
# -から
for i in range(l):
    a_ += a[i]
    if x == -1:
        if a_ > x:
            cnt += a_ -x
            a_ = -1
    else:
        if a_ < x:
            cnt += x-a_
            
            a_ = 1

    x *= -1

cnt1 = 0
a_ = 0
x1 = 1
# +から
for i in range(l):
    a_ += a[i]
    if x1 == -1:
        if a_ > x1:
            cnt1 += a_ -x1
            a_ = -1
    else:
        if a_ < x1:
            cnt1 += x1-a_
            
            a_ = 1

    x1 *= -1

if cnt >= 0 and cnt1 >=0:
    print(min(cnt, cnt1))
elif cnt < 0:
    print(cnt1)
else:
    print(cnt)