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()))

ans=float('inf')
s=0
cnt=0

for i in range(n):
    s+=a[i]
    if i%2==0:
        if s>=0:
            cnt+=1+s
            s=-1
    else:
        if s<=0:
            cnt+=1-s
            s=1
ans=min(ans,cnt)
s=0
cnt=0
for i in range(n):
    s+=a[i]
    if i%2==1:
        if s>=0:
            cnt+=1+s
            s=-1
    else:
        if s<=0:
            cnt+=1-s
            s=1
ans=min(ans,cnt)

print(ans)

Submission status: Accepted

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

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

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

print(min(count1,count2))

Submission status: Accepted

Solution code:
from sys import maxsize

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

res = maxsize
for i in (0, 1):
    cumsum = cnt = 0
    for k in range(n):
        if k % 2 == i:
            if cumsum + a[k] <= 0:
                cnt += abs(1 - (cumsum + a[k]))
                cumsum = 1
            else:
                cumsum += a[k]
        else:
            if cumsum + a[k] >= 0:
                cnt += abs(-1 - (cumsum + a[k]))
                cumsum = -1
            else:
                cumsum += a[k]
    if cnt < res:
        res = cnt

print(res)

Submission status: Accepted

Solution code:
def f(a):
    mode=1
    s=0
    ans=0
    for i in range(n):
        s+=a[i]
        if mode*s<=0: ans+=abs(s)+1;s=mode
        mode*=-1
    return ans


n=int(input())
a=list(map(int,input().split()))
b=[-a[i] for i in range(n)]

mode=1
s=0
print(min(f(a),f(b)))

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc059/tasks/arc072_a
# 累積和を作っておいて、その累積和の符号が交互になるようにしていく
# 累積和のi以降の要素をすべて変更する代わりにいくつ足せばいいのか持っておく(imos法のイメージ)

import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


class cumsum1d:  # 一次元累積和クラス
    def __init__(self, ls: list):
        '''
        1次元リストを受け取る
        '''
        from itertools import accumulate
        self.ls_accum = [0] + list(accumulate(ls))

    def total(self, i, j):
        # もとの配列lsにおける[i,j)の中合計
        return self.ls_accum[j] - self.ls_accum[i]

    def __call__(self, i):
        # i番目までの合計
        return self.ls_accum[i + 1]


N = read_a_int()
A = read_ints()

A = cumsum1d(A)
# ただし第一項が0とか途中0になる場合がめんどくさい
# →二通りやって少ないほうでいいじゃん
ans1 = 0
bias = 0
pre_sign = 1  # 第一項が負と仮定
for i in range(N):
    now = A(i) + bias
    if pre_sign * now < 0:  # 異符号
        pass
    elif pre_sign * now >= 0:  # 同符号
        ans1 += abs(now) + 1
        bias += -pre_sign * (abs(now) + 1)
    pre_sign = 1 if pre_sign == -1 else -1

ans2 = 0
bias = 0
pre_sign = -1  # 第一項が正と仮定
for i in range(N):
    now = A(i) + bias
    if pre_sign * now < 0:  # 異符号
        pass
    elif pre_sign * now >= 0:  # 同符号
        ans2 += abs(now) + 1
        bias += -pre_sign * (abs(now) + 1)
    pre_sign = 1 if pre_sign == -1 else -1
print(min(ans1, ans2))

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc059/tasks/arc072_a
# 累積和を作っておいて、その累積和の符号が交互になるようにしていく

import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


class cumsum1d:  # 一次元累積和クラス
    def __init__(self, ls: list):
        '''
        1次元リストを受け取る
        '''
        from itertools import accumulate
        self.ls_accum = [0] + list(accumulate(ls))

    def total(self, i, j):
        # もとの配列lsにおける[i,j)の中合計
        return self.ls_accum[j] - self.ls_accum[i]

    def __call__(self, i):
        # i番目までの合計
        return self.ls_accum[i + 1]


N = read_a_int()
A = read_ints()

A = cumsum1d(A)
# ただし第一項が0とか途中0になる場合がめんどくさい
# →二通りやって少ないほうでいいじゃん
ans1 = 0
bias = 0
pre_sign = 1  # 第一項が負と仮定
for i in range(N):
    now = A(i) + bias
    if (-1, 1)[pre_sign] * now < 0:  # 異符号
        pass
    elif (-1, 1)[pre_sign] * now >= 0:  # 同符号
        ans1 += abs(now) + 1
        bias += (1, -1)[pre_sign] * (abs(now) + 1)
    pre_sign = 1 - pre_sign

ans2 = 0
bias = 0
pre_sign = 0  # 第一項が正と仮定
for i in range(N):
    now = A(i) + bias
    if (-1, 1)[pre_sign] * now < 0:  # 異符号
        pass
    elif (-1, 1)[pre_sign] * now >= 0:  # 同符号
        ans2 += abs(now) + 1
        bias += (1, -1)[pre_sign] * (abs(now) + 1)
    pre_sign = 1 - pre_sign
print(min(ans1, ans2))

Submission status: Accepted

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

def is_pos(x,i):
    if not x >= 1:
        cnt[i] += 1-x
        s[i] = 1
    return

def is_neg(x,i):
    if not x <= -1:
        cnt[i] += 1+x
        s[i] = -1
    return
    
for i in range(n):
    s[0] += a[i]
    s[1] += a[i]
    x = i%2
    y = (i+1)%2
    is_pos(s[x],x)
    is_neg(s[y],y)

ans = min(cnt)
print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate

N = int(input())
As = list(map(int, input().split()))
acc = list(accumulate(As))

# 偶数が正
ans = 0
cnt = 0 #累積カウント
parity = [1,-1]
for i,a in enumerate(acc):
    a += cnt
    p = parity[i%2]
    if p * a > 0:
        continue
    else:
        ans += abs(a)+1
        cnt += p*(abs(a)+1)

# 偶数が負
ans2 = 0
cnt = 0 #累積カウント
parity = [-1,1]
for i,a in enumerate(acc):
    a += cnt
    p = parity[i%2]
    if p * a > 0:
        continue
    else:
        ans2 += abs(a)+1
        cnt += p*(abs(a)+1)

print(min(ans,ans2))

Submission status: Accepted

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

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

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

print(min(ans1, ans2))

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
fg=[1,-1]
ret=[0]*2
tot=[0]*2
for i in range(n):
  for j in range(2):
    tot[j]+=a[i]
    if tot[j]*fg[j]<=0:
      ret[j]+=abs(fg[j]-tot[j])
      tot[j]+=fg[j]-tot[j]
    fg[j]*=-1
print(min(ret))

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
ans=10**15
for t in [1,-1]:
    r=0
    cnt=0
    T=t
    for i in range(N):
        r+=A[i]
        if(T>0 and r<=0):
            cnt+=1-r
            r+=1-r
        if(T<0 and r>=0):
            cnt+=r+1
            r-=r+1
        T*=-1
    ans=min(cnt,ans)
print(ans)

Submission status: Accepted

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


def solve(n: int, a: "List[int]"):
    def _solve(op):
        from itertools import cycle
        ab = 0
        for aa in map(lambda a_o: a_o[0]*a_o[1], zip(a, cycle(op))):
            ab -= aa
            if ab >= 0:
                yield ab + 1
                ab = -1
            ab = abs(ab)
    return min(sum(_solve([1, -1])), sum(_solve([-1, 1])))

# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
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]"
    print(solve(n, a))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
a1=0
a2=0
s1=0
s2=0
for i,j in enumerate(l):
    s1+=j
    s2+=j
    if i%2:
        if s1>=0:
            a1+=s1+1
            s1=-1
        if s2<=0:
            a2+=-s2+1
            s2=1
    else:
        if s2>=0:
            a2+=s2+1
            s2=-1
        if s1<=0:
            a1+=-s1+1
            s1=1
print(min(a1,a2))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
INF = 10**10
MOD = 10**9 + 7
sys.setrecursionlimit(100000000)
from functools import lru_cache
from copy import deepcopy 

def main():
    n = int(input())
    a = list(map(int,input().split()))
    b = deepcopy(a)

    ans1 = 0
    if a[0] <= 0:
        ans1 += 1 - a[0]
        a[0] = 1
    
    for i in range(1,n):
        a[i] += a[i - 1]
        if a[i - 1] < 0 and a[i] <= 0:
            ans1 += 1 - a[i]
            a[i] = 1
        elif a[i - 1] > 0 and a[i] >= 0:
            ans1 += a[i] + 1
            a[i] = -1
    
    ans2 = 0
    if b[0] >= 0:
        ans2 += b[0] + 1
        b[0] = -1

    for i in range(1,n):
        b[i] += b[i - 1]
        if b[i - 1] < 0 and b[i] <= 0:
            ans2 += 1 - b[i]
            b[i] = 1
        elif b[i - 1] > 0 and b[i] >= 0:
            ans2 += b[i] + 1
            b[i] = -1
    
    ans = min(ans1,ans2)
    print(ans)

if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
def resolve():
    n = int(input())
    C = [0, 0]
    S = list(map(int, input().split()))
    for i in range(2):
        tmp = 0
        for j in range(n):
            tmp += S[j]
            if (i+j)%2==0:
                if tmp <= 0:
                    C[i] += abs(tmp) + 1
                    tmp = 1
            else:
                if tmp >= 0:
                    C[i] += abs(tmp) + 1
                    tmp = -1

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

if __name__ == '__main__':
    resolve()

Submission status: Accepted

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

ans = 10 ** 18
for z in range(2):
    if z == 0:
        sign = 1
    else:
        sign = -1
    c = 0
    cost = 0
    for x in a:
        c += x
        if c * sign <= 0:
            cost += 1 - c * sign
            c = sign
        sign *= -1
    ans = min(ans, cost)
print(ans)

Submission status: Accepted

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

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

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

print(min(ans1,ans2))

Submission status: Accepted

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

res1 = 0
cum1 = 0

for i in range(N):
    cum1 += A[i]
    if i % 2 == 0 and cum1 <= 0:
        res1 += - cum1 + 1
        cum1 = 1
    if i % 2 != 0 and cum1 >= 0:
        res1 += cum1 + 1
        cum1 = -1

res2 = 0
cum2 = 0

for i in range(N):
    cum2 += A[i]
    if i % 2 != 0 and cum2 <= 0:
        res2 += - cum2 + 1
        cum2 = 1
    if i % 2 == 0 and cum2 >= 0:
        res2 += cum2 + 1
        cum2 = -1

print(min(res1, res2))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
cnt1 = 0
s = 0
swc = 1
for i in a:
	s += i
	if swc == 1:
		swc = 0
		if s > 0:
			continue
		else:
			cnt1 += 1-s
			s = 1
	else:
		swc = 1
		if s < 0:
			continue
		else:
			cnt1 += s+1
			s = -1
swc = 0
cnt2 = 0
s= 0
for i in a:
	s += i
	if swc == 1:
		swc = 0
		if s > 0:
			continue
		else:
			cnt2 += 1-s
			s = 1
	else:
		swc = 1
		if s < 0:
			continue
		else:
			cnt2 += s+1
			s = -1
print (min(cnt1, cnt2))

Submission status: Accepted

Solution code:
n, *A = map(int, open(0).read().split())
def sgn(n):
    return 0 if n==0 else 1 if n>0 else -1

a = A[0]
C = [0 if a>0 else -a+1, 0 if a<0 else a+1]
S = [max(1, a), min(-1, a)]

for a in A[1:]:
    for i, s in enumerate(S):
        sgn_sum = sgn(s)
        if sgn(s+a) == -sgn_sum:
            S[i] += a
        else:
            C[i] += abs(s+a+sgn_sum)
            S[i] = -sgn_sum

print(min(C))

Submission status: Accepted

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


def solve(tmp):
    ans = 0
    for i in range(1, n):
        if tmp * (tmp + a[i]) < 0:
            tmp += a[i]
        else:
            ans += abs(tmp + a[i]) + 1
            if tmp < 0:
                tmp = 1
            else:
                tmp = - 1
    return ans

minus = min(a[0], -1)
plus = max(a[0], 1)
ans = min(solve(plus) + abs(a[0] - plus), solve(minus) + abs(a[0] - minus))
print(ans)

Submission status: Accepted

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

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


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

print(min(count1,count2))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

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

# スタート＋
if a[0] <= 0:
    A1 = 1
    ans1 = abs(a[0]) + 1
else:
    A1 = a[0]
    ans1 = 0
for i in range(1, n):
    nextA = A1 + a[i]
    if (A1 > 0 and nextA < 0) or (A1 < 0 and nextA > 0):
        A1 = nextA
    elif nextA == 0 and A1 > 0:
        ans1 += 1
        A1 = -1
    elif nextA == 0 and A1 < 0:
        ans1 += 1
        A1 = 1
    elif A1 > 0:
        ans1 += abs(nextA) + 1
        A1 = -1
    else:
        ans1 += abs(nextA) + 1
        A1 = 1

# スタート-
if a[0] >= 0:
    ans2 = abs(a[0]) + 1
    A2 = -1
else:
    A2 = a[0]
    ans2 = 0
for i in range(1, n):
    nextA = A2 + a[i]
    if (A2 > 0 and nextA < 0) or (A2 < 0 and nextA > 0):
        A2 = nextA
    elif nextA == 0 and A2 > 0:
        ans2 += 1
        A2 = -1
    elif nextA == 0 and A2 < 0:
        ans2 += 1
        A2 = 1
    elif A2 > 0:
        ans2 += abs(nextA) + 1
        A2 = -1
    else:
        ans2 += abs(nextA) + 1
        A2 = 1

print(min(ans1, ans2))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N=I()
    a=LI()
    ans=10**30
    aaa=[-1,1]
    
    for j in range(2):
        pos=aaa[j]
        S=0
        temp=0
        for i in range(N):
            S+=a[i]
            if S>0:
                Sp=1
            elif S<0:
                Sp=-1
            else:
                Sp=0
            
            
            if Sp!=pos*(-1):
                target=-1*pos
                temp+=abs(target-S)
                S=target
            pos*=-1
        ans=min(ans,temp)
        
    print(ans)
        
        

main()

Submission status: Accepted

Solution code:
# 気づき
# ①+-+-+-..か-+-+-+...のふたつを試して小さい方
# ②満たさなくなったら帳尻を合わせる（いまみてるやつだけでいい)
# ③1<=i<=nに対して和が0でない -> a1≠0（これは①とかぶる）
# 累積和をとってインデックスの偶奇で判断

n = int(input())
arr = list(map(int, input().split()))
p, m = 0, 0  # 累積和
pn, mn = 0, 0  # （操作した回数の）カウント
for i in range(n):
    if i % 2 == 0:
        # pは正にしたい
        if p + arr[i] > 0:
            p += arr[i]
        else:
            # 少ない操作で和を大きくしたいので1との差を取る
            d = p + arr[i]  # 差
            pn += abs(1 - d)
            p = 1
    else:
        # pは負にしたい
        if p + arr[i] < 0:
            p += arr[i]
        else:
            # 少ない操作で和を小さくしたいので-1との差をとる
            d = p + arr[i]
            pn += abs(-1 - d)
            p = -1
for i in range(n):
    if i % 2 == 0:
        # mは負にしたい
        if m + arr[i] < 0:
            m += arr[i]
        else:
            # ry
            d = m + arr[i]
            mn += abs(-1 - d)
            m = -1
    else:
        # mは正にしたい
        if m + arr[i] > 0:
            m += arr[i]
        else:
            # ry
            d = m + arr[i]
            mn += abs(1 - d)
            m = 1

print(min(pn, mn))

Submission status: Accepted

Solution code:
import copy

n = int(input())
a = list(map(int, input().split(' ')))
sum_list = [0] * n
past = 0
for i in range(n):
    if i > 0:
        sum_list[i] = sum_list[i-1] + a[i]
    else:
        sum_list[0] = a[i]
sum1 = copy.copy(sum_list)
sum2 = copy.copy(sum_list)

ans = 0
count = 0
for i in range(n):
    # a[0]がプラス
    num = 0
    sum1[i] += past
    if i % 2 == 0 and sum1[i] <= 0:
        num = (abs(sum1[i]) + 1)
        sum1[i] += num
        past += num
    if i % 2 == 1 and sum1[i] >= 0:
        num = (abs(sum1[i]) + 1)
        sum1[i] -= num
        past -= num
    count += num

ans = count
count = 0
past = 0

for i in range(n):
    # a[0]がマイナス
    num = 0
    sum2[i] += past
    if i % 2 == 0 and sum2[i] >= 0:
        num = (abs(sum2[i]) + 1)
        sum2[i] -= num
        past -= num
    if i % 2 == 1 and sum2[i] <= 0:
        num = (abs(sum2[i]) + 1)
        sum2[i] += num
        past += num
    count += num

ans = min(ans, count)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
ret = 0
def count(nega):
  ret = 0
  s = 0
  for e in A:
    nega = not nega
    #print(e,ret,s,s+e)
    if nega and 0 <= s + e:
      ret += abs(s + e) + 1
      s = -1
    elif not nega and s + e <= 0:
      ret += abs(s + e) + 1
      s = 1
    else:
      s += e
  return ret
print(min(count(True),count(False)))

Submission status: Accepted

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

def test(total, res):
    for i in a[1:]:
        hugou = total > 0
        total += i
        if hugou:
            if total>=0:
                res += abs(total)+1
                total = -1
        else:
            if total<=0:
                res += abs(total)+1
                total = 1
    return res

    
res = 0
total = a[0]
if a[0]<=0:
    total = 1
    res += abs(a[0])+1
res1 = test(total, res)

res = 0
total = a[0]
if a[0]>=0:
    total = -1
    res += abs(a[0])+1
res2 = test(total, res)

print(min(res1, res2))

Submission status: Accepted

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

def search(x):#x=0,1
    tmp=0
    cnt=0
    for i in range(n):
        tmp+=a[i]
        if i%2==x:
            if tmp<=0:
                cnt+=1-tmp
                tmp=1              
        else:
            if tmp>=0:
                cnt+=tmp+1
                tmp=-1
    return cnt

print(min(search(0),search(1)))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
mans = 0
msum = 0
for i in range(N):
    if i%2:
        if msum + A[i] <= 0:
            mans += abs(msum + A[i])+1
            msum = 1
        else:
            msum += A[i]
    else:
        if msum + A[i] >= 0:
            mans += msum + A[i]+1
            msum = -1
        else:
            msum += A[i]
pans = 0
psum = 0
for i in range(N):
    if i%2:
        if psum + A[i] >= 0:
            pans += psum + A[i]+1
            psum = -1
        else:
            psum += A[i]
    else:
        if psum + A[i] <= 0:
            pans += abs(psum + A[i])+1
            psum = 1
        else:
            psum += A[i]

print(min(pans,mans))

Submission status: Accepted

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

if l[0] <= 0:
    x = 1
    ans1 = 1-l[0]
else:
    x = l[0]
    ans1 = 0
def check(l,x):
    ans=0
    for i in range(1,n):
        if x < 0:
            if x + l[i] <= 0:
                ans += 1-(x+l[i])
                x = 1
            else:
                x = x + l[i]
        else:
            if x + l[i] >= 0:
                ans += x + l[i] + 1
                x = -1
            else:
                x = x + l[i]
    return ans
ans1 += check(l,x)
if l[0] >= 0:
    x = -1
    ans2 = l[0]+1
else:
    ans2 = 0
    x = l[0]
ans2 += check(l,x)
print(min(ans1,ans2))

Submission status: Accepted

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

Submission status: Accepted

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


def calc(init):
    ans = 0
    sum = 0
    prev = init
    for i in range(n):
        sum += a[i]
        if sum == 0:
            ans += 1
            if prev < 0:
                sum = 1
            else:
                sum = -1
        else:
            if sum > 0 and prev > 0:
                ans += sum + 1
                sum = -1
            elif sum < 0 and prev < 0:
                ans += 1 - sum
                sum = 1
        prev = sum
    return ans


print(min(calc(1), calc(-1)))

Submission status: Accepted

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

def F(A, sgn):
  cnt = 0
  cum = 0
  for a in A:
    sgn *= -1
    cum += a
    if cum * sgn > 0:
      continue
    if sgn > 0:
      cnt += 1 - cum
      cum = 1
    else:
      cnt += cum + 1
      cum = -1
  return cnt

answer = min(F(A,1), F(A,-1))
print(answer)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
 
r_even = 0  # 偶数桁を＋にする場合の値
a_even = a[:]
sum_even = 0
for i in range(len(a)):
    sum_even += a_even[i]
    if i % 2 == 0:
        if sum_even <= 0:
            r_even += 1 - sum_even
            a_even[i] += 1 - sum_even
            sum_even += 1 - sum_even
    else:
        if sum_even >= 0:
            r_even += sum_even + 1
            a_even[i] -= sum_even + 1
            sum_even -= sum_even + 1

r_odd = 0   #奇数桁を＋にする場合の値
a_odd = a[:]
sum_odd = 0
for i in range(len(a)):
    sum_odd += a_odd[i]
    if i % 2 != 0:
        if sum_odd <= 0:
            r_odd += 1 - sum_odd
            a_odd[i] += 1 - sum_odd
            sum_odd += 1 - sum_odd
    else:
         if sum_odd >= 0:
            r_odd += sum_odd + 1
            a_odd[i] -= sum_odd + 1
            sum_odd -= sum_odd + 1

print(min(r_odd, r_even))

Submission status: Accepted

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

ans_even = 0
ans_odd = 0

sum = 0

for index, i in enumerate(A):
  sum += i

  if index % 2 == 0:
    if sum <= 0:
      ans_even += abs(sum - 1)
      sum = 1
  else:
    if sum >= 0:
      ans_even += abs(sum + 1)
      sum = -1

sum = 0
for index, i in enumerate(A):
  sum += i
  
  if index % 2 == 0:
    if sum >= 0:
      ans_odd += abs(sum + 1)
      sum = -1
  else:
    if sum <= 0:
      ans_odd += abs(sum - 1)
      sum = 1
      
print(min(ans_even, ans_odd))

Submission status: Accepted

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

x = 0
tmp = 0
for i in range(N):
    tmp += A[i]
    if i & 1 == 0:
        if tmp < 1:
            x += 1-tmp
            tmp = 1
    else:
        if tmp > -1:
            x += 1+tmp
            tmp = -1
y = 0
tmp = 0
for i in range(N):
    tmp += A[i]
    if i & 1:
        if tmp < 1:
            y += 1-tmp
            tmp = 1
    else:
        if tmp > -1:
            y += 1+tmp
            tmp = -1
print(min(x,y))

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int, input().split()))
c1, c2 = 0, 0
s = 0
for i in range(len(a)):
    S = s+a[i]
    if i%2 == 0:
        if S > 0:
            s = S
        else:
            c1 += abs(S)+1
            s = 1
    else:
        if S < 0:
            s = S
        else:
            c1 += abs(S)+1
            s = -1
s=0
for i in range(len(a)):
    S = s+a[i]
    if i%2 != 0:
        if S > 0:
            s = S
        else:
            c2 += abs(S)+1
            s = 1
    else:
        if S < 0:
            s = S
        else:
            c2 += abs(S)+1
            s = -1
print(min(c1, c2))

Submission status: Accepted

Solution code:
n = int(input())
list_a = [int(x) for x in input().split()]

sum = 0
ans_even = 0
ans_odd = 0

for i in range(n) :
    sum += list_a[i]
    if i%2 == 0 :
        if sum <= 0 :
            ans_even += abs(sum) + 1
            sum = 1
    else :
        if sum >= 0 :
            ans_even += abs(sum) + 1
            sum = -1

sum = 0

for i in range(n) :
    sum += list_a[i]
    if i%2 == 1 :
        if sum <= 0 :
            ans_odd += abs(sum) + 1
            sum = 1
    else :
        if sum >= 0 :
            ans_odd += abs(sum) + 1
            sum = -1

print(min(ans_even, ans_odd))

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
num = float('inf')
for k in [0, 1]:
    s = 0
    tmp = 0
    for i in range(N):
        s += a[i]
        if i % 2 == k:
            if s <= 0:
                tmp += abs(s) + 1
                s = 1
        else:
            if s >= 0:
                tmp += abs(s) + 1
                s = -1
    num = min(tmp, num)

print(num)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

def main():
    n = int(input())
    a_list = list(map(int, input().split()))
    a_sum = 0
    sign = "plus"
    ans1 = 0
    for i in range(0, n):
        if sign == "plus":
            sign = "minus"
            if a_sum + a_list[i] == 0:
                ans1 += 1
                a_sum = -1
            elif a_sum + a_list[i] > 0:
                ans1 += a_sum + 1 + a_list[i]
                a_sum = -1
            else:
                a_sum += a_list[i]
        elif sign == "minus":
            sign = "plus"
            if a_sum + a_list[i] == 0:
                ans1 += 1
                a_sum = 1
            elif a_sum + a_list[i] < 0:
                ans1 += -1 * a_sum + 1 + -1 * a_list[i]
                a_sum = 1
            else:
                a_sum += a_list[i]

    a_sum = 0
    sign = "minus"
    ans2 = 0
    for i in range(0, n):
        if sign == "plus":
            sign = "minus"
            if a_sum + a_list[i] == 0:
                ans2 += 1
                a_sum = -1
            elif a_sum + a_list[i] > 0:
                ans2 += a_sum + 1 + a_list[i]
                a_sum = -1
            else:
                a_sum += a_list[i]
        elif sign == "minus":
            sign = "plus"
            if a_sum + a_list[i] == 0:
                ans2 += 1
                a_sum = 1
            elif a_sum + a_list[i] < 0:
                ans2 += -1 * a_sum + 1 + -1 * a_list[i]
                a_sum = 1
            else:
                a_sum += a_list[i]

    print(min(ans1, ans2))

if __name__ == '__main__':
    main()

Submission status: Accepted

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


def check(a_list :list, flg :int) -> int:
    # flg:  1 初項(奇数の項)の和が、正の数になるように操作する
    # flg: -1 　　　　〃　　　〃　　　　　、負の数になるように操作する

    count=0
    sum=0
    for num in a_list:
        sum_next = sum + num
        
        sum_next_p_m_sign = (sum_next>0) - (sum_next<0)
        
        sign_chck = flg * sum_next_p_m_sign

        if sign_chck == -1:
            # flgと符号が一致しなかった場合、+1 or -1まで操作
            diff = (abs(sum_next) + 1) * (-1)*sum_next_p_m_sign
            sum += num + diff
            count += abs(diff)
        
        elif sign_chck == 0:
            # sum_next=0の時
            diff = (1)*flg
            sum += num + diff
            count += abs(diff)
        
        else:
            sum = sum + num
 
        flg *= (-1)
    return count
        
#----------
n = int(input().strip())
a_list = list(map(int, input().rstrip().split()))
#----------
print( min(check(a_list, 1), check(a_list, -1)) )

Submission status: Accepted

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

s = 0
ans = [0, 0]
for i in range(n):
    s += a[i]
    if (i+1)%2 == 0 and s >= 0:
        ans[0] += s+1
        s -= s+1
    elif (i+1)%2 == 1 and s <= 0:
        ans[0] += abs(s)+1
        s += abs(s)+1

s = 0
for i in range(n):
    s += a[i]
    if (i+1)%2 == 1 and s >= 0:
        ans[1] += s+1
        s -= s+1
    elif (i+1)%2 == 0 and s <= 0:
        ans[1] += abs(s)+1
        s += abs(s)+1

print(min(ans))

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
def test(L, sgn):
  count = 0
  s = 0
  for i in L:
    sgn *= -1
    s += i
    if s*sgn > 0:
      continue
    if sgn > 0:
      count += (1 - s)
      s = 1
    else:
      count += (s + 1)
      s = -1
  return count

ans = min(test(A, 1), test(A, -1))
print(ans)

Submission status: Accepted

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

cnt = 0
s = 0
for i in range(n):
    #偶数インデックスをプラスにする
    s+=a[i]
    if i%2 and s>=0:
        cnt+=s+1
        s = -1
    elif i%2==0 and s<=0:
        cnt+=1-s
        s = 1
ans = cnt
cnt = 0
s = 0
for i in range(n):
    #偶数インデックスをマイナスにする
    s+=a[i]
    if i%2==0 and s>=0:
        cnt+=s+1
        s = -1
    elif i%2 and s<=0:
        cnt+=1-s
        s = 1
ans = min(ans,cnt)
print(ans)