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:
def solve():
  ans = float('inf')
  N = int(input())
  A = list(map(int, input().split()))
  if A[0]==0:
    lis = [1,-1]
  else:
    lis = [A[0],-A[0]//abs(A[0])]
  for l in lis:
    cnt = abs(A[0]-l)
    total = l
    for i in range(1,N):
      new_total = total+A[i]
      if total*(new_total)>=0:
        new_total = (-1)*total//abs(total)
        cnt += abs(new_total-(total+A[i]))
      total = new_total
    ans = min(ans,cnt)
  return ans
print(solve())

Submission status: Accepted

Solution code:
n = int(input())

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

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

temp1 = 0
temp2 = 0
res = 0
for i in range(n):
    res += al[i]
    if i % 2 ==0:
        if res >0:
            pass
        else:
            y = res*(-1)+1
            temp1 += y
            res =1
    else:
        if res <0:
            pass
        else:
            y = res+1
            temp1 += y
            res = -1
    
res= 0

for j in range(n):
    res += al[j]
    if j % 2 ==0:
        if res <0:
            pass
        else:
            y = res+1
            temp2 += y
            res = -1
    else:
        if res >0:
            pass
        else:
            y = res*(-1)+1
            temp2 += y
            res = 1

print(min(temp1,temp2))

Submission status: Accepted

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

#正/負いずれかから始める
cost1=0
cost2=0
sum1=0
sum2=0
for i in range(n):
  sum1=sum1+a[i]
  sum2=sum2+a[i]
  if i%2==0:
    if sum1<1:
      cost1+=1-sum1
      sum1=1
    
    if sum2>-1:
      cost2+=1+sum2
      sum2=-1
  else:
    if sum1>-1:
      cost1+=1+sum1
      sum1=-1
    
    if sum2<1:
      cost2+=1-sum2
      sum2=1
print(min(cost1,cost2))

Submission status: Accepted

Solution code:
import copy
n=int(input())
a=[0]+list(map(int,input().split()))
b=copy.copy(a)
psum=0
nsum=0
sum1=0
sum2=0
for i in range(1,n+1):
    if i%2!=0:
        psum+=max([0,1-sum1-a[i]])
        a[i]+=max([0,1-sum1-a[i]])
        sum1+=a[i]
    else:
        psum+=max([0,1+sum1+a[i]])
        a[i]-=max([0,1+sum1+a[i]])
        sum1+=a[i]
for i in range(1,n+1):
    if i%2!=0:
        nsum+=max([0,1+sum2+b[i]])
        b[i]-=max([0,1+sum2+b[i]])
        sum2+=b[i]
    else:
        nsum+=max([0,1-sum2-b[i]])
        b[i]+=max([0,1-sum2-b[i]])
        sum2+=b[i]

print(min([psum,nsum]))

Submission status: Accepted

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


def solve(start):
    acc = [0] * n
    acc[0] = A[0]
    for i in range(1, n):
        acc[i] = acc[i - 1] + A[i]

    cur = start
    ans = 0
    x = 0
    for i in range(n):
        acc[i] += x
        if cur > 0:
            if acc[i] >= 0:
                ans += acc[i] + 1
                x -= acc[i] + 1
                acc[i] = -1
        else:
            if acc[i] <= 0:
                ans += abs(acc[i]) + 1
                x += abs(acc[i]) + 1
                acc[i] = 1
        cur = acc[i]
    return ans

ans = min(solve(1), solve(-1))
print(ans)

Submission status: Accepted

Solution code:
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  #空白あり

n = I()
a = LI()

# a[0] の符号を決めて貪欲に

# a[0] > 0 にする

s = 0  # 累積和
b = 0  # 操作回数
for i in range(n):
    if i % 2 == 0:
        if s+a[i] > 0:
            s += a[i]
        else:
            b += -(s+a[i])+1
            s = 1
    else:
        if s+a[i] < 0:
            s += a[i]
        else:
            b += s+a[i]+1
            s = -1

# a[i] < 0 にする

s = 0  # 累積和
c = 0  # 操作回数
for i in range(n):
    if i % 2 == 1:
        if s+a[i] > 0:
            s += a[i]
        else:
            c += -(s+a[i])+1
            s = 1
    else:
        if s+a[i] < 0:
            s += a[i]
        else:
            c += s+a[i]+1
            s = -1

print(min(b,c))

Submission status: Accepted

Solution code:
n = int(input())
arr = list(map(int , input().split()))
cur = 0
a = 0
b = 0
prea=0
preb=0
resa = 0
resb =0
# check independently even prefix having negative sum and odd prefix having positive sum
for i in range(0 , n):
	prea = prea+arr[i]
	if (i% 2 ==0 and prea<=0):
		resa = resa-prea+1
		prea=1
	if i%2 ==1 and prea>=0:
		resa = resa+prea+1
		prea=-1
# check independently even prefix having positive sum and odd prefix having negative sum
for i in range(0 , n):
	preb = preb+arr[i]
	if (i%2==1 and preb<=0):
		resb = resb+(-preb+1)
		preb=1
	if i%2 ==0 and preb>=0:
		resb =resb+ preb+1
		preb=-1
# find minimum of the two
print(min(resa , resb))

Submission status: Accepted

Solution code:
n = int(input())
arr = list(map(int , input().split()))
cur = 0
a = 0
b = 0
prea=0
preb=0
resa = 0
resb =0
for i in range(0 , n):
	prea = prea+arr[i]
	if (i% 2 ==0 and prea<=0):
		resa = resa-prea+1
		prea=1
	if i%2 ==1 and prea>=0:
		resa = resa+prea+1
		prea=-1
for i in range(0 , n):
	preb = preb+arr[i]
	if (i%2==1 and preb<=0):
		resb = resb+(-preb+1)
		preb=1
	if i%2 ==0 and preb>=0:
		resb =resb+ preb+1
		preb=-1
print(min(resa , resb))

Submission status: Accepted

Solution code:
'''
研究室PCでの解答
'''
import math
#import numpy as np
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)
mod = 10**9+7

def main():
    n = int(ipt())
    a = [int(i) for i in ipt().split()]
    nm = 0
    sm = 0
    p = 1
    for i in a:
        ns = sm+i
        if p:
            if ns <= 0:
                nm += 1-ns
                sm = 1
            else:
                sm = ns
        else:
            if ns >= 0:
                nm += 1+ns
                sm = -1
            else:
                sm = ns
        p = 1-p
    mi = nm
    nm = 0
    sm = 0
    p = 0
    for i in a:
        ns = sm+i
        if p:
            if ns <= 0:
                nm += 1-ns
                sm = 1
            else:
                sm = ns
        else:
            if ns >= 0:
                nm += 1+ns
                sm = -1
            else:
                sm = ns
        p = 1-p

    print(min(nm,mi))

    return None

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from itertools import accumulate

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

s=list(accumulate(a))

chg=0
sign=1
op1=0

for i in range(n):
    curs=s[i]+chg

    if sign*curs>0:
        sign*=-1
        continue

    chg+=sign-curs
    a[i]+=sign-curs
    op1+=abs(sign-curs)

    sign*=-1

chg=0
sign=-1
op2=0

for i in range(n):
    curs=s[i]+chg

    if sign*curs>0:
        sign*=-1
        continue

    chg+=sign-curs
    a[i]+=sign-curs
    op2+=abs(sign-curs)

    sign*=-1

print(min(op1,op2))

Submission status: Accepted

Solution code:
from itertools import accumulate
n = int(input())
a = list(map(int,input().split()))
p = list(accumulate(a))
plan = 0
cou = 0
for i in range(n):
    if i % 2 == 0:
        if p[i] + cou > 0:
            continue
        elif p[i] + cou <= 0:
            plan += abs(p[i]+cou) + 1
            cou = 1-p[i]
    elif i % 2 == 1:
        if p[i] + cou < 0:
            continue
        elif p[i] + cou >= 0:
            plan += p[i] + cou + 1
            cou = -1-p[i]
mian = 0
cou = 0
for i in range(n):
    if i % 2 == 1:
        if p[i] + cou > 0:
            continue
        elif p[i] + cou <= 0:
            mian += abs(p[i]+cou) + 1
            cou = 1-p[i]
    elif i % 2 == 0:
        if p[i] + cou < 0:
            continue
        elif p[i] + cou >= 0:
            mian += p[i] + cou + 1
            cou = -1-p[i]
print(min(mian,plan))

Submission status: Accepted

Solution code:
def solve(x):
    if x == 0:
        return INF

    sgn = (-1) ** (x < 0)
    ret = abs(a[0] - x)
    sm = x
    for e in a[1:]:
        sgn *= -1

        if sgn < 0:
            if sm + e < 0:
                sm += e
            else:
                ret += e + 1 + sm
                sm = -1

        else:
            if sm + e > 0:
                sm += e
            else:
                ret -= e - 1 + sm
                sm = 1

    return ret


n = int(input())
a = list(map(int, input().split()))
INF = 10 ** 18

ans = min(solve(1), solve(-1), solve(a[0]))
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(_) for _ in input().split()]
now = 0
cnt = 0
for i, a in enumerate(A):
    b = (-1)**i
    now += a
    if now * b <= 0:
        cnt += abs(now - b)
        now = b
ans = cnt
now = 0
cnt = 0
for i, a in enumerate(A):
    b = (-1)**(i + 1)
    now += a
    if now * b <= 0:
        cnt += abs(now - b)
        now = b
ans = min(ans, cnt)
print(ans)

Submission status: Accepted

Solution code:
import sys
from itertools import accumulate

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def solve(A):
    ans = 0
    s = A[0]
    for a in A[1:]:
        prev, s = s, s + a
        if prev > 0 and s >= 0:
            ans += s + 1
            s = -1
        if prev < 0 and s <= 0:
            ans += -s + 1
            s = 1

    return ans


def main():
    N, *A = map(int, read().split())

    a0 = A[0]

    ans1 = 0
    if A[0] <= 0:
        ans1 = -A[0] + 1
        A[0] = 1
    ans1 += solve(A)

    A[0] = a0

    ans2 = 0
    if A[0] >= 0:
        ans2 = A[0] + 1
        A[0] = -1
    ans2 += solve(A)

    print(min(ans1, ans2))

    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
from itertools import accumulate

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def solve(A):
    ans = 0
    s = A[0]
    for a in A[1:]:
        prev, s = s, s + a
        if prev > 0 and s >= 0:
            ans += s + 1
            s = -1
        if prev < 0 and s <= 0:
            ans += -s + 1
            s = 1

    return ans


def main():
    N, *A = map(int, read().split())

    if A[0] > 0:
        ans1 = solve(A)
        ans2 = A[0] + 1
        A[0] = -1
        ans2 += solve(A)
        ans = min(ans1, ans2)
    elif A[0] < 0:
        ans1 = solve(A)
        ans2 = -A[0] + 1
        A[0] = 1
        ans2 += solve(A)
        ans = min(ans1, ans2)
    else:
        A[0] = 1
        ans1 = solve(A) + 1
        A[0] = -1
        ans2 = solve(A) + 1
        ans = min(ans1, ans2)

    print(ans)

    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from sys import stdin

def main():
    
    n=int(stdin.readline().strip())
    a=list(map(int,stdin.readline().strip().split()))
    # dp=[[0,0]*4 for _ in range(n)] #0:<1, 1:-1, 2:1, 3:>1
    # dp[0][0]=[[0,inf][a[0]>=0],[a[0],0][a[0]>=0]]
    # dp[0][1]=[abs(a[0]+1),-1]
    # dp[0][2]=[abs(a[0]-1),1]
    # dp[0][3]=[[0,inf][a[0]<=0],[a[0],0][a[0]<=0]]
    dp=[[[0,0]]*2 for _ in range(n)]
    dp[0][0]=[max(1,a[0])-a[0],[1,a[0]][a[0]>0]]
    dp[0][1]=[a[0]-min(-1,a[0]),[-1,a[0]][a[0]<0]]
    for i in range(1,n):
        n0=[0,1-a[i]-dp[i-1][1][1]][a[i]+dp[i-1][1][1]<=0]
        n1=[0,1+a[i]+dp[i-1][0][1]][a[i]+dp[i-1][0][1]>=0]
        dp[i][0]=[dp[i-1][1][0]+n0,max(1,a[i]+dp[i-1][1][1])]
        dp[i][1]=[dp[i-1][0][0]+n1,min(-1,a[i]+dp[i-1][0][1])]
    print(min(dp[n-1][0][0],dp[n-1][1][0]))

if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
arr1=list(map(int,input().split()))
arr2=arr1[:]
ans1=0
ans2=0

if arr1[0]<=0:
    ans1+=abs(arr1[0])+1
    arr1[0]=1
if arr2[0]>=0:
    ans2+=arr2[0]+1
    arr2[0]=-1

sum1=arr1[0]
for i in range(1,n):
    tmp=sum1+arr1[i]
    if i%2==1:
        if tmp>=0:
            ans1+=tmp+1
            sum1=-1
        else:
            sum1=tmp
    else:
        if tmp<=0:
            ans1+=abs(tmp)+1
            sum1=1
        else:
            sum1=tmp
sum2=arr2[0]
for i in range(1,n):
    tmp=sum2+arr2[i]
    if i%2==0:
        if tmp>=0:
            ans2+=tmp+1
            sum2=-1
        else:
            sum2=tmp
    else:
        if tmp<=0:
            ans2+=abs(tmp)+1
            sum2=1
        else:
            sum2=tmp
print(min(ans1,ans2))

Submission status: Accepted

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

ans1 = 0
ans2 = 0
tmp = 0

for i,e in enumerate(a):
    tmp += e
    if i % 2 == 0:
        if tmp > 0:
            pass
        else:
            ans1 += 1 - tmp
            tmp = 1
    else:
        if tmp < 0:
            pass
        else:
            ans1 += tmp + 1
            tmp = -1

tmp = 0

for i,e in enumerate(a):
    tmp += e
    if i % 2 == 1:
        if tmp > 0:
            pass
        else:
            ans2 += 1 - tmp
            tmp = 1
    else:
        if tmp < 0:
            pass
        else:
            ans2 += tmp + 1
            tmp = -1

print(min(ans1, ans2))

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
def main():
    n, *a = map(int, read().split())

    # 第一項をプラスにする場合
    r1 = 0
    csum = 0
    parity = True # 「次の項目はプラスであるべき」のTrue/Falseを示す
    for ae in a:
        csum += ae
        if parity:
            if csum <= 0:
                plus = 1 - csum
                r1 += plus
                csum += plus
        else:
            if csum >= 0:
                minus = csum + 1
                r1 += minus
                csum -= minus
        parity ^= 1
    # 第一項をマイナスにする場合
    r2 = 0
    csum = 0
    parity = False
    for ae in a:
        csum += ae
        if parity:
            if csum <= 0:
                plus = 1 - csum
                r2 += plus
                csum += plus
        else:
            if csum >= 0:
                minus = csum + 1
                r2 += minus
                csum -= minus
        parity ^= 1
    print(min(r1, r2))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import math
from collections import deque

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()


def make_grid(h, w, num): return [[int(num)] * w for _ in range(h)]


# 配列から累積和を返す
def make_cumulative(A):
    C = [0] * (len(A) + 1)
    for i, a in enumerate(A):
        i += 1
        C[i] = C[i - 1] + a
    return C


def main():
    N = NI()
    A = NLI()
    C = make_cumulative(A)[1:]
    PM = []
    MP = []
    for i in range(len(A)):
        PM.append((-1) ** i)
        MP.append((-1) ** (i+1))
    diff = 0
    ans = 0
    for c, sign in zip(C, PM):
        if (c + diff) * sign > 0:
            pass
        else:
            if c + diff > 0:
                ans += c + diff + 1
                diff = - c - 1
            elif c + diff < 0:
                ans += 1 - c - diff
                diff = 1 - c
            else:
                ans += 1
                diff += sign

    diff = 0
    ans2 = 0
    for c, sign in zip(C, MP):
        if (c + diff) * sign > 0:
            pass
        else:
            if c + diff > 0:
                ans2 += c + diff + 1
                diff = - c - 1
            elif c + diff < 0:
                ans2 += 1 - c - diff
                diff = 1 - c
            else:
                ans2 += 1
                diff += sign

    print(min(ans, ans2))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
A2 = A[:]
RAW = A[:]

# +-にするとき
su = 0
for i, a in enumerate(A):
    su += a
    if i % 2 == 0:
        if su <= 0:
            # a[i] を増やす
            t = (-su + 1)
            A[i] += t
            su += t
    else:
        if su >= 0:
            # a[i] をへらす
            t = (su + 1)
            A[i] -= t
            su -= t

# -+にするとき
su = 0
for i, a in enumerate(A2):
    su += a
    t = 0
    if i % 2 ==0:
        if su >= 0:
            # a[i]をへらす
            t = (su + 1)
            A2[i] -= t
            su -= t
    else:
        if su <= 0:
            # a[i]をふやす
            t = (-su + 1)
            A2[i] += t
            su += t
# print(A)
# print(A2)

ans1 = sum([abs(a-b) for a,b in zip(A, RAW)])
ans2 = sum([abs(a-b) for a,b in zip(A2, RAW)])
print(min(ans1, ans2))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
ans = 10**15

for s in [1,-1]:
  cos = 0
  tot = 0
  
  for a in A:
    tot+=a
    if s*tot<=0:
      cos+=abs(tot-s)
      tot=s
    s*=-1
  
  ans=min(ans,cos)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().rstrip().split(" ")))
summ = 0
ans_when_odd_indexes_plus = 0
ans_when_odd_indexes_minus = 0
for i in range(n):
    summ += a[i]
    if i % 2 == 0:
        if summ <= 0:
            ans_when_odd_indexes_plus += 1 - summ
            summ = 1
    else:
        if summ >= 0:
            ans_when_odd_indexes_plus += 1 + summ
            summ = -1

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

print(min(ans_when_odd_indexes_plus,ans_when_odd_indexes_minus))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import copy

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

now=0
ans1=0
for i in range(n):
  now+=a[i]
  if i%2==0:
    if now<=0:
      ans1+=abs(now)+1
      now=1
  else:
    if 0<=now:
      ans1+=abs(now)+1
      now=-1

now=0
ans2=0
for i in range(n):
  now+=a[i]
  if i%2==0:
    if 0<=now:
      ans2+=abs(now)+1
      now=-1
  else:
    if now<=0:
      ans2+=abs(now)+1
      now=1
print(min(ans1,ans2))

Submission status: Accepted

Solution code:
import copy

n=int(input())
a=[int(x) for x in input().rstrip().split()]
ac=copy.deepcopy(a)

now=0
ans1=0
for i in range(n):
  now+=a[i]
  if i%2==0:
    if now<=0:
      ans1+=abs(now)+1
      now=1
  else:
    if 0<=now:
      ans1+=abs(now)+1
      now=-1

now=0
ans2=0
for i in range(n):
  now+=a[i]
  if i%2==0:
    if 0<=now:
      ans2+=abs(now)+1
      now=-1
  else:
    if now<=0:
      ans2+=abs(now)+1
      now=1
print(min(ans1,ans2))

Submission status: Accepted

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

# 最左をプラスとする場合
sa = A[0]
pc = 0
if sa <= 0:
    pc = 1 - sa
    sa = 1
plus = -1
for a in A[1:]:
    sa += a
    if plus > 0 and sa <= 0:
        pc += 1 - sa
        sa = 1
    elif plus < 0 and sa >= 0:
        pc += 1 + sa
        sa = -1
    plus *= -1

# 最左がマイナスの場合
sa = A[0]
mc = 0
if sa >= 0:
    mc = 1 + sa
    sa = -1
plus = 1
for a in A[1:]:
    sa += a
    if plus > 0 and sa <= 0:
        mc += 1 - sa
        sa = 1
    elif plus < 0 and sa >= 0:
        mc += 1 + sa
        sa = -1
    plus *= -1

print(min(pc, mc))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import copy

n = int(input())
am = list(map(int, input().split()))

pcnt = 0
mcnt = 0

f = 0  #前の項の符号（1:+、2:-）
wa = 0

#最初の数字を正とするとき
a = copy.copy(am)
f = 2

for i in range(n):
    wa += a[i]

    if f == 1:#-にする必要あり
        if wa > -1:
            pcnt += wa + 1
            wa = -1
        
        f = 2

    elif f == 2:  #+にする必要あり

        if wa < 1:
            pcnt += -wa + 1
            wa = 1

        f = 1

#最初の数字を負とするとき
a = copy.copy(am)
wa = 0
f = 1

for i in range(n):
    wa += a[i]

    if f == 1:  # -にする必要あり
        if wa > -1:
            mcnt += wa + 1
            wa = -1

        f = 2

    elif f == 2:  # +にする必要あり

        if wa < 1:
            mcnt += -wa + 1
            wa = 1

        f = 1

print(min(pcnt, mcnt))

Submission status: Accepted

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

s = 0
ans1 = 0
ans2 = 0

for i in range(len(a)):
    s += a[i]
    if(i%2 == 0):
        if(np.sign(s) != 1):
            ans1 += abs(1 - s)
            s = 1
    else:
        if(np.sign(s) != -1):
            ans1 += abs(-1-s)
            s = -1
s = 0
for i in range(len(a)):
    s += a[i]
    if(i%2==0):
        if(np.sign(s) != -1):
            ans2 += abs(-1-s)
            s = -1
    else:
        if(np.sign(s) != 1):
            ans2 += abs(1-s)
            s = 1
print(min(ans1, ans2))

Submission status: Accepted

Solution code:
import copy 

def resolve(List):
    # L[0]!=0を起点とする
    L = copy.deepcopy(List)
    cnt = 0
    s = L[0]
    for i in range(1,len(L)):
        a = L[i]
        if(s>0 and s+a>=0):
            L[i] = -s-1
            cnt += (s+a+1)
            s = -1
        elif(s<0 and s+a<=0):
            L[i] = -s+1
            cnt += (-s-a+1)
            s = 1
        else:
            s += a
    return cnt      

def ans(List):
    L = copy.deepcopy(List)
    a = L[0]
    c0,c1=0,0

    if (a>0):
        c0 = resolve(L)
        c1 = (a+1) + resolve([-1]+L[1:])
    elif (a<0):
        c0 = resolve(L)
        c1 = (-a+1) + resolve([1]+L[1:])
    else:
        c0 = 1 + resolve([1]+L[1:])
        c1 = 1 + resolve([-1]+L[1:])
    
    return(min(c0,c1))

N = int(input())
List = [int(x) for x in input().split(' ')]
print(ans(List))

Submission status: Accepted

Solution code:
import copy 

def resolve(List):
    # L[0]!=0を起点とする
    L = copy.deepcopy(List)
    cnt = 0
    s = L[0]
    for i in range(1,len(L)):
        a = L[i]
        if(s>0 and s+a>=0):
            L[i] = -s-1
            cnt += (s+a+1)
            s = -1
        elif(s<0 and s+a<=0):
            L[i] = -s+1
            cnt += (-s-a+1)
            s = 1
        else:
            s += a
    return cnt      

def ans(List):
    L = copy.deepcopy(List)
    a = L[0]
    c0,c1=0,0

    if (a>0):
        c0 = resolve(L)
        c1 = (a+1) + resolve([-1]+L[1:])
    elif (a<0):
        c0 = resolve(L)
        c1 = (-a+1) + resolve([1]+L[1:])
    else:
        c0 = 1 + resolve([1]+L[1:])
        c1 = 1 + resolve([-1]+L[1:])
    
    return(min(c0,c1))

def main():
    N = int(input())
    List = [int(x) for x in input().split(' ')]
    print(ans(List))

main()

Submission status: Accepted

Solution code:
import sys, os, math, bisect, itertools, collections, heapq, queue
# from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
from decimal import Decimal
from collections import defaultdict, deque

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    A = il()

    cnt, q1 = 0, 0
    ism = False
    for a in A:
        cnt += a
        if ism and cnt >= 0:
            q1 += abs(cnt) + 1
            cnt = -1
        elif not ism and cnt <= 0:
            q1 += abs(cnt) + 1
            cnt = 1
        if ism:
            ism = False
        else:
            ism = True

    cnt , q2 = 0, 0
    ism = True
    for a in A:
        cnt += a
        if ism and cnt >= 0:
            q2 += abs(cnt) + 1
            cnt = -1
        elif not ism and cnt <= 0:
            q2 += abs(cnt) + 1
            cnt = 1
        if ism:
            ism = False
        else:
            ism = True
    print(min(q1,q2))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

def pm(a, t):
    ans = 0
    s = 0
    for i in a:
        s += i
        if t == 1 and s<1:
            ans += -s+1
            s = 1
        elif t==-1 and s>-1:
            ans += s+1
            s = -1
        t *= -1
    return ans
  
print(min(pm(a, 1), pm(a, -1)))

Submission status: Accepted

Solution code:

import sys
import copy
input = sys.stdin.readline

n = int(input())
a = list(map(int,input().split()))
b = copy.copy(a)
sum_odd = sum(a[1::2])
sum_eve = sum(a[::2])

ans = 0
sum_a  = 0
for i in range(n):
        
    sum_a = sum_a + a[i]
        
    if sum_a *(-1)**(i+1) < 1:
        kari = 1-sum_a *(-1)**(i+1)
        a[i] += 1*(-1)**(i+1) *(kari)
        sum_a += 1*(-1)**(i+1) *(kari)
            
        ans = ans + abs(kari)
            
ans1 = 0
sum_b = 0
             
for i in range(n):
       
    sum_b = sum_b + b[i]
    if sum_b *(-1)**(i) < 1:
        kari = (1-sum_b *(-1)**(i))
        b[i] += 1*(-1)**(i) * kari
        sum_b += 1*(-1)**(i) * kari

        ans1 = ans1 + abs(kari)


print(min(ans,ans1))

Submission status: Accepted

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

def chk(a,t):
    ans=0
    x=0
    for i in a:
        x+=i
        if t and x<1:
            ans+=1-x
            x=1
        elif not t 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())
A = tuple(map(int, input().split(' ')))

if A[0] == 0:
    cs = 1
    ans1 = 1
else:
    cs = A[0]
    ans1 = 0

for na in A[1:]:
    if cs >= 0:
        cs += na
        if cs < 0:
            continue

        ans1 += abs(cs) + 1
        cs = -1
    else:
        cs += na
        if cs > 0:
            continue

        ans1 += abs(cs) + 1
        cs = 1

if A[0] == 0:
    cs = -1
    ans2 = 1
elif A[0] > 0:
    cs = -1
    ans2 = abs(A[0]) + 1
else:
    cs = 1
    ans2 = abs(A[0]) + 1

for na in A[1:]:
    if cs >= 0:
        cs += na
        if cs < 0:
            continue

        ans2 += abs(cs) + 1
        cs = -1
    else:
        cs += na
        if cs > 0:
            continue

        ans2 += abs(cs) + 1
        cs = 1

print(min(ans1, ans2))

Submission status: Accepted

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

cntA, sumA = 0, 0
for i in range(N):
    sumA += A[i]
    if i % 2 == 0:
        if sumA <= 0:
            cntA += abs(sumA) + 1
            sumA += abs(sumA) + 1
    else:
        if sumA >= 0:
            cntA += abs(sumA) + 1
            sumA -= abs(sumA) + 1
    
cntB, sumB = 0, 0
for i in range(N):
    sumB += A[i]
    if i % 2 != 0:
        if sumB <= 0:
            cntB += abs(sumB) + 1
            sumB += abs(sumB) + 1
    else:
        if sumB >= 0:
            cntB += abs(sumB) + 1
            sumB -= abs(sumB) + 1

print(min(cntA, cntB))

Submission status: Accepted

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

Submission status: Accepted

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

S = 0
C = 0
S = a[0]
if S > 0:
    pm = 1
elif S < 0:
    pm = 0
else:
    S += 1
    C += 1
    pm = 1
for i in range(1, n):
    S += a[i]
    if pm == 1 and S >= 0:
        C += S + 1
        S -= S + 1
    elif pm == 0 and S <= 0:
        C += -S + 1
        S += -S + 1
    pm = 1 - pm 

T = 0
D = 0
T = a[0]
if T > 0:
    D += T + 1
    T -= T + 1
    pm = 0
elif T < 0:
    D += -T + 1
    T += -T + 1
    pm = 1
else:
    T -= 1
    D += 1
    pm = 0
for i in range(1, n):
    T += a[i]
    if pm == 1 and T >= 0:
        D += T + 1
        T -= T + 1
    elif pm == 0 and T <= 0:
        D += -T + 1
        T += -T + 1
    pm = 1 - pm

print(min(C, D))