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 = [int(x) for x in input().split()]
sum_start_plus = 0
sum_start_minus = 0
ans_start_plus = 0
ans_start_minus = 0
flag_start_plus = True
flag_start_minus = True

# プラス始まりの場合を計算する
for a in A:
    sum_start_plus += a
    if flag_start_plus:
        flag_start_plus = False
        if sum_start_plus > 0:
            continue
        else:
            ans_start_plus += abs(1 - sum_start_plus)
            sum_start_plus = 1
    else:
        flag_start_plus = True
        if sum_start_plus < 0:
            continue
        else:
            ans_start_plus += abs(-1 - sum_start_plus)
            sum_start_plus = -1

# マイナス始まりの場合を計算する
for a in A:
    sum_start_minus += a
    if flag_start_minus:
        flag_start_minus = False
        if sum_start_minus < 0:
            continue
        else:
            ans_start_minus += abs(-1 - sum_start_minus)
            sum_start_minus = -1
    else:
        flag_start_minus = True
        if sum_start_minus > 0:
            continue
        else:
            ans_start_minus += abs(1 - sum_start_minus)
            sum_start_minus = 1

print(min(ans_start_plus, ans_start_minus))

Submission status: Accepted

Solution code:
import numpy as np

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

sum_start_plus = 0
sum_start_minus = 0
ans_start_plus = 0
ans_start_minus = 0
flag_start_plus = True
flag_start_minus = True

# プラス始まりの場合を計算する
for a in A:
    sum_start_plus += a
    if flag_start_plus:
        flag_start_plus = False
        if np.sign(sum_start_plus) == 1:
            continue
        else:
            ans_start_plus += abs(1 - sum_start_plus)
            sum_start_plus = 1
    else:
        flag_start_plus = True
        if np.sign(sum_start_plus) == -1:
            continue
        else:
            ans_start_plus += abs(-1 - sum_start_plus)
            sum_start_plus = -1

# マイナス始まりの場合を計算する
for a in A:
    sum_start_minus += a
    if flag_start_minus:
        flag_start_minus = False
        if np.sign(sum_start_minus) == -1:
            continue
        else:
            ans_start_minus += abs(-1 - sum_start_minus)
            sum_start_minus = -1
    else:
        flag_start_minus = True
        if np.sign(sum_start_minus) == 1:
            continue
        else:
            ans_start_minus += abs(1 - sum_start_minus)
            sum_start_minus = 1

print(min(ans_start_plus, ans_start_minus))

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
A = [int(x) for x in input().split()]
sum_start_plus = 0
sum_start_minus = 0
ans_start_plus = 0
ans_start_minus = 0

# プラス始まりの場合を計算する
for i in range(n):
    sum_start_plus += A[i]
    if i % 2 == 0:
        if np.sign(sum_start_plus) == 1:
            continue
        else:
            ans_start_plus += abs(1 - sum_start_plus)
            sum_start_plus = 1
    else:
        if np.sign(sum_start_plus) == -1:
            continue
        else:
            ans_start_plus += abs(-1 - sum_start_plus)
            sum_start_plus = -1

# マイナス始まりの場合を計算する
for i in range(n):
    sum_start_minus += A[i]
    if i % 2 == 0:
        if np.sign(sum_start_minus) == -1:
            continue
        else:
            ans_start_minus += abs(-1 - sum_start_minus)
            sum_start_minus = -1
    else:
        if np.sign(sum_start_minus) == 1:
            continue
        else:
            ans_start_minus += abs(1 - sum_start_minus)
            sum_start_minus = 1

print(min(ans_start_plus, ans_start_minus))

Submission status: Accepted

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

minA = 0
sumA = 0
# 奇数項を正としたときの操作回数minA 
for i in range(n):
  sumA += a[i]
  if i%2 == 0:        
    if sumA <= 0:
      minA += abs(sumA)+1
      sumA += abs(sumA)+1
  else:
    if sumA >= 0:
      minA += abs(sumA)+1
      sumA -= abs(sumA)+1
temp = 0
# 偶数項を正としたときの操作回数minB
minB = 0
sumB = 0
for i in range(n):
  sumB += a[i]
  if i%2 != 0:
    if sumB <= 0:
      minB += abs(sumB)+1
      sumB += abs(sumB)+1
  else:
    if sumB >= 0:
      minB += abs(sumB)+1
      sumB -= abs(sumB)+1
print(min(minA, minB))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

cost1, cost2 = 0, 0
base1, base2 = 0, 0
for i in range(n):
    base1 += a[i]
    if i % 2 == 1:
        if base1 < 1:
            cost1 += (1 - base1)
            base1 = 1
    else:
        if base1 > -1:
            cost1 += (base1 + 1)
            base1 = -1

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

print(min(cost1, cost2))

Submission status: Accepted

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

# +-+-...
t1 = 0
s1 = 0
for (i, x) in enumerate(xs):
    s1 += x
    if i % 2 == 0:
        if s1 < 1:
            t1 += (1 - s1)
            s1 = 1
    else:
        if s1 > -1:
            t1 += (s1 + 1)
            s1 = -1

# -+-+...
t2 = 0
s2 = 0
for (i, x) in enumerate(xs):
    s2 += x
    if i % 2 == 0:
        if s2 > -1:
            t2 += (s2 + 1)
            s2 = -1
    else:
        if s2 < 1:
            t2 += (1 - s2)
            s2 = 1

print(min(t1, t2))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def sequence(lis, first_value):
    count = 0
    now_sum = first_value
    pre_sum = first_value
    for num in lis[1:]:
        now_sum += num
        if now_sum == 0 or pre_sum * now_sum > 0:
            count += abs(now_sum) + 1
            now_sum = -1 * pre_sum / abs(pre_sum)
        pre_sum = now_sum
    return int(count)

n = int(input())
a = [int(i) for i in input().split()]
if a[0] == 0:
    print(min(sequence(a, 1)+1, sequence(a, -1)+1))
else:
    print(min(sequence(a, a[0]), sequence(a, -1*a[0]/abs(a[0]))+abs(a[0])+1))

Submission status: Accepted

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

mi = 1e20
for sign in [-1, 1]:
    res, acc = 0, 0
    for a in A:
        acc += a
        if sign * acc <= 0:
            res += abs(acc - sign)
            acc = sign
        sign *= -1
    mi = min(mi, res)

print(mi)

Submission status: Accepted

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math

mod = 10**9+7

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

def solve(a):
    if a[0] > 0:
        status_pos = True
    elif a[0] < 0:
        status_pos = False

    tmp = 0
    ans = 0
    for i in range(n):
        tmp += a[i]
        if tmp == 0:
            ans += 1
            if status_pos:
                tmp = -1
            else:
                tmp = 1

        elif status_pos and tmp < 0:
            ans += 1+abs(tmp)
            tmp = 1
        elif status_pos == False and tmp > 0:
            ans += 1+abs(tmp)
            tmp = -1
        status_pos = not(status_pos)
    return ans

if a[0] == 0:
    a[0] = 1
    ans = 1
    ans += solve(a)

    a[0] = -1
    ans2 = 1
    ans2 += solve(a)
    ans = min(ans,ans2)
else:
    ans = solve(a)
    #値反転
    ans3 = abs(a[0])+1
    if a[0] > 0:
        a[0] = -1
    else:
        a[0] = 1
    ans3 += solve(a)
    ans = min(ans,ans3)

print(ans)

Submission status: Accepted

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

o = []

for b in [-1,1]:
    c = 0
    s = [0]*n
    for i in range(0,n):
        s[i] = a[i] + (i and s[i-1])
        if s[i] == 0 or s[i] < 0 < b or b < 0 < s[i]:
            c += abs(s[i] - b)
            s[i] = b
        b *= -1
    o.append(c)

print(min(o))

Submission status: Accepted

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

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

cost_list.append(cost)
sum = 0
cost = 0
for i in range(n):
    sum += a[i]
    if i % 2:
        if sum <= 0:
            cost += abs(sum) + 1
            sum += abs(sum) + 1
    else:
        if sum >= 0:
            cost += sum + 1
            sum -= sum + 1

cost_list.append(cost)

print(min(cost_list))

Submission status: Accepted

Solution code:
N = int(input())
*A, = map(int,input().split())
ans1 = 0
S = A[0]
if S <= 0:
    ans1 = abs(S)+1
    S = 1
for a in A[1:]:
    S1 = S+a
    if S1*S >= 0:
        ans1 += abs(S1)+1
        S1 = -S//abs(S)
    S = S1
ans2 = 0
S = A[0]
if S >= 0:
    ans2 = abs(S)+1
    S = -1
for a in A[1:]:
    S1 = S+a
    if S1*S >= 0:
        ans2 += abs(S1)+1
        S1 = -S//abs(S)
    S = S1
print(min(ans1,ans2))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
A = list(map(int, input().split()))
ans = 0
sm = 0
for i in range(n):
    if i % 2 == 0:
        if sm + A[i] > 0:
            sm += A[i]
        elif sm + A[i] <= 0:
            num = -(sm + A[i]) + 1
            sm += A[i] + num
            ans += num
    else:
        if sm + A[i] < 0:
            sm += A[i]
        elif sm + A[i] >= 0:
            num = -(sm + A[i]) - 1
            sm += A[i] + num
            ans += -num
ans1 = ans
ans, sm = 0, 0
for i in range(n):
    if i % 2 != 0:
        if sm + A[i] > 0:
            sm += A[i]
        elif sm + A[i] <= 0:
            num = -(sm + A[i]) + 1
            sm += A[i] + num
            ans += num
    else:
        if sm + A[i] < 0:
            sm += A[i]
        elif sm + A[i] >= 0:
            num = -(sm + A[i]) - 1
            sm += A[i] + num
            ans += -num
print(min(ans, ans1))

Submission status: Accepted

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

ans = 0
sm = 0
for i in range(n):
    if i % 2 == 0:
        if sm + A[i] > 0:
            sm += A[i]
        elif sm + A[i] <= 0:
            num = -(sm + A[i]) + 1
            sm += A[i] + num
            ans += num
    else:
        if sm + A[i] < 0:
            sm += A[i]
        elif sm + A[i] >= 0:
            num = -(sm + A[i]) - 1
            sm += A[i] + num
            ans += -num
ans1 = ans
ans, sm = 0, 0
for i in range(n):
    if i % 2 != 0:
        if sm + A[i] > 0:
            sm += A[i]
        elif sm + A[i] <= 0:
            num = -(sm + A[i]) + 1
            sm += A[i] + num
            ans += num
    else:
        if sm + A[i] < 0:
            sm += A[i]
        elif sm + A[i] >= 0:
            num = -(sm + A[i]) - 1
            sm += A[i] + num
            ans += -num
print(min(ans, ans1))

Submission status: Accepted

Solution code:
n = int(input())
lstA = list(map(int, input().split()))
cntCase1, cntCase2 = 0, 0


# 偶数番目を正、奇数番目を負とする場合
accum = 0
for i in range(n):
    ai = lstA[i]
    tmpSum = accum
    tmpSum += ai
    if i % 2 == 0:
        if tmpSum <= 0:
            cntCase1 += abs(tmpSum) + 1
            accum = 1
        else:
            accum = tmpSum
    else:
        if tmpSum >= 0:
            cntCase1 += abs(tmpSum) + 1
            accum = -1
        else:
            accum = tmpSum


# 偶数番目を負、奇数番目を正とする場合
accum = 0
for i in range(n):
    ai = lstA[i]
    tmpSum = accum
    tmpSum += ai
    if i % 2 == 0:
        if tmpSum >= 0:
            cntCase2 += abs(tmpSum) + 1
            accum = -1
        else:
            accum = tmpSum
    else:
        if tmpSum <= 0:
            cntCase2 += abs(tmpSum) + 1
            accum = 1
        else:
            accum = tmpSum


ans = min(cntCase1, cntCase2)
print(ans)

Submission status: Accepted

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

ans = 0
s = 0
ans2 = 0
s2 = 0
plus = True
for i in a:
    s += i
    s2 += i
    if plus:
        if s <= 0:
            ans += 1 - s
            s = 1
        if s2 >= 0:
            ans2 += s2 + 1
            s2 = -1
    else:
        if s >= 0:
            ans += s + 1
            s = -1
        if s2 <= 0:
            ans2 += 1 - s2
            s2 = 1

    plus = not plus

print(min(ans, ans2))

Submission status: Accepted

Solution code:
from collections import Counter
from functools import reduce
# import statistics
import bisect
import copy
import fractions
import math
import pprint
# for debug
pp = pprint.PrettyPrinter(width = 150)
import random
import sys
import time
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def MI(): return map(int, sys.stdin.readline().split())
def II(): return int(sys.stdin.readline())
def IS(): return input()
def C(x): return Counter(x)
def GCD_LIST(numbers):
    return reduce(fractions.gcd, numbers)
def LCM_LIST(numbers):
    return reduce(LCM, numbers)
def LCM(m, n):
    return (m * n // fractions.gcd(m, n))
def unite(x, y):
    # それぞれのノードの根を求める
    x = root(x)
    y = root(y)
    if x == y:
        return
    # node[x]の根をyに変更する
    node[x] = y
def same(x,y): return bool(root(x) == root(y))
def root(x):
    if node[x] == x:  # xが根の場合
        return x
    else:
        node[x] = root(node[x])  # 経路圧縮
        return node[x]
def dfs(v):
    # ノードに訪れた
    visited[v] = True
    for v2 in range(n):
        if graph[v][v2] == False:  # そもそもvとv2が辺でつながっていない
            continue
        if visited[v2] == True:  # もう見た
            continue
        dfs(v2)


n, a = II(), LI()
ruisekiwa = [0] * (n)
# 累積和の配列の最初の要素は、元のリストの最初の要素にする
ruisekiwa[0] = a[0]
for i in range(1, n):
    ruisekiwa[i] = ruisekiwa[i-1] + a[i]

ans1 = 0
ans2 = 0
diff = 0

# 偶数番目を正にする
for i in range(n):
    # 偶数番目なのに累積和が負
    # ruisekiwa[0] == -1 ならば1にするために2を加算する
    if i % 2 == 0 and ruisekiwa[i] + diff <= 0:
        ans1 += abs(1 - (ruisekiwa[i] + diff))
        diff += (1 - (ruisekiwa[i] + diff))
        # print(ans1, diff)
    # 奇数番目なのに累積和が正
    elif i % 2 == 1 and ruisekiwa[i] + diff >= 0:
        ans1 += abs(ruisekiwa[i] + diff - (-1))
        diff += ((-1) - (ruisekiwa[i] + diff))
        # print(ans1, diff)
        
diff = 0
# 偶数番目を負にする
for i in range(n):
    # 偶数番目なのに累積和が正
    if i % 2 == 0 and ruisekiwa[i] + diff >= 0:
        # -1にするためにどれだけ減算するか
        ans2 += abs(ruisekiwa[i] + diff - (-1))
        diff += ((-1) - (ruisekiwa[i] + diff))
        # print(ans2, diff)
    # 奇数番目なのに累積和が負
    elif i % 2 == 1 and ruisekiwa[i] + diff <= 0:
        ans2 += abs(1 - (ruisekiwa[i] + diff))
        diff += (1 - (ruisekiwa[i] + diff))
        # print(ans2, diff)
        
print(min(ans1, ans2))

Submission status: Accepted

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

def calc(sgn):
    ans = 0
    sum = 0
    for ai in a:
        sum += ai
        sgn *= -1
        if sum == 0:
            ans += 1
            sum = sgn
        elif sum * sgn < 0:
            ans += abs(sum) + 1
            sum = sgn
    return ans

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

Submission status: Accepted

Solution code:
input()
*A,=map(int,input().split())
f=True
s1=0;r1=0
s2=0;r2=0
for a in A:
  s1+=a;s2+=a
  if f and s1<=0:r1+=1-s1;s1=1
  elif not(f) and s1>=0:r1+=s1+1;s1=-1
  if not(f) and s2<=0:r2+=1-s2;s2=1
  elif f and s2>=0:r2+=s2+1;s2=-1
  f=not(f)
print(min(r1,r2))

Submission status: Accepted

Solution code:
n = int(input())

a = list(map(int, input().split()))
s = [a[0]]

for i in range(1, n):
    s.append(s[-1]+a[i])


cnt_a = 0
base = 0
for i in range(n):
    if ((base + s[i] >= 0) and (i % 2 == 1)) or ((base + s[i] <= 0) and (i % 2 == 0)):
        cnt_a += (1 + abs(base + s[i]))
        base += (1 + abs(base + s[i])) * (-1) ** i


cnt_b = 0
base = 0
for i in range(n):
    if ((base + s[i] <= 0) and (i % 2 == 1)) or ((base + s[i] >= 0) and (i % 2 == 0)):
        cnt_b += (1 + abs(base + s[i]))
        base -= (1 + abs(base + s[i])) * (-1) ** i

print(min(cnt_a, cnt_b))

Submission status: Accepted

Solution code:

def read_input():
    n = int(input())
    alist = list(map(int, input().split()))
    return n, alist

def get_sign(x):
    if x > 0:
        return 1
    elif x < 0:
        return -1
    return 0

def submit():
    n, alist = read_input()

    # pattern 1
    s = 0
    sign = 1
    edit = 0

    for a in alist:
        temp = s + a
        temp_sign = get_sign(temp)

        if sign == temp_sign:
            edit += temp_sign * temp
            temp -= temp

        if temp == 0:
            edit += 1
            temp -= sign

        s = temp
        sign = get_sign(s)

    edit1 = edit

    # pattern 2
    s = 0
    sign = -1
    edit = 0

    for a in alist:
        temp = s + a
        temp_sign = get_sign(temp)

        if sign == temp_sign:
            edit += temp_sign * temp
            temp -= temp

        if temp == 0:
            edit += 1
            temp -= sign

        s = temp
        sign = get_sign(s)

    edit2 = edit

    print(min(edit1, edit2))


if __name__ == '__main__':
    submit()

Submission status: Accepted

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

import numpy as np 
#first_plus
count = 0
summention =0
plus_minus = 1

for i in range (0,n):
    summention_temp = a[i] + summention
    if np.sign(summention_temp)!=plus_minus:
        
        summention = plus_minus
        count += abs(summention-summention_temp)
    else:
        summention = summention_temp
    plus_minus = plus_minus*(-1)

count_plus = count

#first_minus
count = 0
summention =0
plus_minus = -1

for i in range (0,n):
    summention_temp = a[i] + summention
    if np.sign(summention_temp)!=plus_minus:
        
        summention = plus_minus
        count += abs(summention-summention_temp)
    else:
        summention = summention_temp
    plus_minus = plus_minus*(-1)

count_minus = count
print(min(count_plus,count_minus))

Submission status: Accepted

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

y = [0, 0]
for k in range(2):
    s = 0
    x = (-1) ** k
    for ai in a:
        si = s + ai
        if si * x > 0:
            s = si
            x *= -1
        else:
            y[k] += abs(si) + 1
            s = x
            x *= -1
print(min(y))

Submission status: Accepted

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

SUM=A[0]
ANS=0
if SUM<=0:
    ANS=1-SUM
    SUM=1

for a in A[1:]:
    if SUM>0:
        y=SUM+a
        if y<0:
            SUM=y
        else:
            ANS+=y+1
            SUM=-1

    else:

        y=SUM+a
        if y>0:
            SUM=y
        else:
            ANS+=1-y
            SUM=1


SUM=A[0]
ANS2=0
if SUM>=0:
    ANS2+=SUM+1
    SUM=-1

for a in A[1:]:
    if SUM>0:
        y=SUM+a
        if y<0:
            SUM=y
        else:
            ANS2+=y+1
            SUM=-1

    else:

        y=SUM+a
        if y>0:
            SUM=y
        else:
            ANS2+=1-y
            SUM=1

print(min(ANS,ANS2))

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
 
def adj(n, a ,flag):
    na = 0
    cnt = 0
    for i in range(n):
        na += a[i]
        if flag == 1:
            if na <= 0:
                cnt += 1 - na
                na = 1
            flag = -1
        
        elif flag == -1:
            if na >= 0:
                cnt += na + 1
                na = -1
            flag = 1
    return cnt            

print(min(adj(n, A, 1),adj(n, A, -1)))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = [a_ for a_ in a]
 
ans = 10**14
for a0 in [-1,a[0],1]:
    if a0 == 0:
        continue
    ans_ = abs(a[0]-a0)
    b[0] = a0
    sign = b[0] > 0
    for i in range(1,n):
        b[i] = a[i]+b[i-1]
        if sign:
            if b[i] >= 0:
                ans_ += b[i]+1
                b[i] = -1
        else:
            if b[i] <= 0:
                ans_ += 1-b[i]
                b[i] = 1
        sign = not sign
    ans = min(ans, ans_)
print(ans)

Submission status: Accepted

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

if A[0] != 0:
    ans1 = 0
    sum = A[0]
else:
    ans1 = 1
    sum = 1
# A[0]=0 のときは1にする。下で-1にするパターンも試している。なお0は不可。

for a in A[1:]:
    if (sum + a) * sum < 0:
        sum += a
    else:
        nextsum = 1 if sum < 0 else -1
        # fugo = sum // abs(sum) 割り算のときは0に気をつけろ！
        a_should_be = nextsum - sum
        dif = abs(a_should_be - a)
        sum = nextsum
        ans1 += dif

#A[0]を反転したほうがいいパターンの処理
ans2 = abs(A[0]) + 1
sum = 1 if A[0] < 0 else -1
#ある意味、ここでA[0]=0のときに-1にすることもやっている

for a in A[1:]:
    if (sum + a) * sum < 0:
        sum += a
    else:
        nextsum = 1 if sum < 0 else -1
        a_should_be = nextsum - sum
        dif = abs(a_should_be - a)
        sum = nextsum
        ans2 += dif
        
print(min(ans1, ans2))

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
tmp_a = a.copy()
Sum = [a[0]]
if a[0] > 0:
    flg = 1
    ans = 0
elif a[0] < 0:
    flg = 0
    ans = 0
elif a[0] == 0:
    Sum[0] = 1
    flg = 1
    ans = 1
idx = 0
for i in range(n-1):
    Sum.append(Sum[idx] + a[i+1])
    if flg  == 1:
        flg = 0
        if Sum[i+1] >= 0:
            tmp = Sum[i+1]
            Sum[i+1] = -1
            ans += tmp + 1
    else:
        flg = 1
        if Sum[i+1] <= 0:
            tmp = Sum[i+1]
            Sum[i+1] = 1
            ans += 1 - tmp
    idx += 1

tmp_ans = ans

a = tmp_a
Sum = [a[0]]
if a[0] > 0:
    Sum[0] = -1
    flg = 0
    ans = a[0] + 1
elif a[0] < 0:
    Sum[0] = 1
    flg = 1
    ans = 1 - a[0]
elif a[0] == 0:
    Sum[0] = -1
    flg = 0
    ans = 1
idx = 0
for i in range(n-1):
    Sum.append(Sum[idx] + a[i+1])
    if flg  == 1:
        flg = 0
        if Sum[i+1] >= 0:
            tmp = Sum[i+1]
            Sum[i+1] = -1
            ans += tmp + 1
    else:
        flg = 1
        if Sum[i+1] <= 0:
            tmp = Sum[i+1]
            Sum[i+1] = 1
            ans += 1 - tmp
    idx += 1

print(min(tmp_ans, ans))

Submission status: Accepted

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

s = 0
num1 = 0
for i, x in enumerate(a):
    if i == 0:
        if x > 0:
            s = x
        else:
            s = 1
            num1 += abs(1-x)
    else:
        if s > 0:
            if s + x < 0:
                s += x
            else:
                num1 += abs(-s-x-1)
                s = -1
        else:
            if s + x > 0:
                s += x
            else:
                num1 += abs(-s-x+1)
                s = 1
s = 0
num2 = 0
for i, x in enumerate(a):
    if i == 0:
        if x < 0:
            s = x
        else:
            s = -1
            num2 += abs(-1-x)
    else:
        if s > 0:
            if s + x < 0:
                s += x
            else:
                num2 += abs(-s-x-1)
                s = -1
        else:
            if s + x > 0:
                s += x
            else:
                num2 += abs(-s-x+1)
                s = 1
print(min([num1, num2]))

Submission status: Accepted

Solution code:
n=int(input())
a=map(int,input().split())
ans1,ans2=0,0
m1,m2=0,0
k=1
for i in a:
  m1+=i
  m2+=i
  if k:
    k=0
    if m1>=0:
      ans1+=m1+1
      m1=-1
    if m2<=0:
      ans2+=1-m2
      m2=1
  else:
    k=1
    if m1<=0:
      ans1+=1-m1
      m1=1
    if m2>=0:
      ans2+=m2+1
      m2=-1
print(min(ans1,ans2))

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int,input().split()))
sum1,sum2 = 0,0
ans1,ans2 = 0,0 #ans1:正負正負...,ans2:負正負正...

if(n == 1):
  if(A[0] != 0): ans = 0
  else: ans = 1
    
else:
 for i in range(n):
    sum1 += A[i]
    if(i%2 == 0):
     if(sum1 <= 0):
        ans1 += abs(1-sum1)
        sum1 = 1
    else:
     if(sum1 >= 0):
        ans1 += abs(sum1-(-1))
        sum1 = -1

    sum2 += A[i]    
    if(i%2 == 1):
     if(sum2 <= 0):
        ans2 += abs(1-sum2)
        sum2 = 1
    else:  
     if(sum2 >= 0):
        ans2 += abs(sum2-(-1))
        sum2 = -1   
 ans = min(ans1,ans2)

print(ans)

Submission status: Accepted

Solution code:
import sys, os

f = lambda:list(map(int,input().split()))
if 'local' in os.environ :
    sys.stdin = open('./input.txt', 'r')

def solve():
    n = f()[0]
    a = f()
    
    suma = [0] * n
    minop = 1e15

    for greater in [True, False]:
        oper = 0
        greater0 = greater
        for i in range(n):
            if i == 0:
                suma[i] = a[i]
            else:
                suma[i] = a[i] + suma[i-1]
            
            greater0 = not greater0
            
            if greater0 and suma[i]<=0:
                oper +=  1 - suma[i]
                suma[i] = 1
                continue
            
            if (not greater0) and suma[i]>=0:
                oper += 1 + suma[i]
                suma[i] = -1
                continue

        minop = min(minop, oper)
    
    print(minop)
            
solve()

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
cntA=cntB=sigA=sigB=0
for i in range(n):
  if i%2==0:
    if sigA+A[i]>=0:
      cntA+=abs(sigA+A[i])+1
      sigA=-1
    else:
      sigA+=A[i]
    if sigB+A[i]<=0:
      cntB+=abs(sigB+A[i])+1
      sigB=1
    else:
      sigB+=A[i]
  else:
    if sigA+A[i]<=0:
      cntA+=abs(sigA+A[i])+1
      sigA=1
    else:
      sigA+=A[i]
    if sigB+A[i]>=0:
      cntB+=abs(sigB+A[i])+1
      sigB=-1
    else:
      sigB+=A[i]
print(min(cntA,cntB))

Submission status: Accepted

Solution code:
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def _I(): return int(sys.stdin.readline())
def _F(): return float(sys.stdin.readline())
def pf(s): return print(s, flush=True)

N=_I()
A=LI()
#最初マイナスの場合とプラスの場合二回計算する
ans1=0
current = 0
is_positive=True
for a in A:
	current += a
	if is_positive:
		if current <= 0:
			ans1+=1-current
			current = 1
	else:
		if current >= 0:
			ans1+=1+current
			current=-1
	is_positive= not is_positive

ans2=0
current = 0
is_positive=False
for a in A:
	current += a
	if is_positive:
		if current <= 0:
			ans2 += 1-current
			current=1
	else:
		if current >= 0:
			ans2+=1+current
			current = -1
	is_positive= not is_positive
#print(ans1,ans2)
print(min(ans1,ans2))

Submission status: Accepted

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


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

ansPos = 0
change = 0
# a[0] を正にする場合
for i in range(n):
    if i%2 == 0 and a[i]+change <= 0:
        ansPos += -a[i]-change+1
        change += -a[i]-change+1
    if i%2 == 1 and a[i]+change >= 0: 
        ansPos +=  a[i]+change+1
        change -=  a[i]+change+1

ansMin = 0
change = 0
# a[0] を負にする場合
for i in range(n):
    if i%2 == 0 and a[i]+change >= 0:
        ansMin +=  a[i]+change+1
        change -=  a[i]+change+1
    if i%2 == 1 and a[i]+change <= 0:
        ansMin += -a[i]-change+1
        change += -a[i]-change+1

print( min(ansPos, ansMin) )

Submission status: Accepted

Solution code:
from itertools import accumulate
n = int(input())
a = [int(item) for item in input().split()]

ans = []
asum = list(accumulate(a))
for key in range(2):
    count = 0
    cum_diff = 0
    for i, item in enumerate(asum):
        if i%2 == key:
            if item + cum_diff <= 0:
                diff = 1 - item - cum_diff
                count += abs(diff)
                cum_diff += diff
        else:
            if item + cum_diff >= 0:
                diff = -1 - item - cum_diff
                count += abs(diff)
                cum_diff += diff
    ans.append(count)

print(min(ans))