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:
an = list(map(int, open(0).read().split()[1:]))


def f(flg):
    accum = 0
    ret = 0
    for ai in an:
        accum += ai
        if flg:
            flg = False
            if accum <= 0:
                ret += -accum + 1
                accum = 1
        else:
            flg = True
            if 0 <= accum:
                ret += accum + 1
                accum = -1
    return ret


def main():
    print(min(f(False), f(True)))
    return


main()

Submission status: Accepted

Solution code:
an = list(map(int, open(0).read().split()[1:]))


def f(flg):
    accum = 0
    ret = 0
    for i, ai in enumerate(an):
        accum += ai
        if flg:
            flg = False
            if accum <= 0:
                ret += -accum + 1
                accum = 1
        else:
            flg = True
            if 0 <= accum:
                ret += accum + 1
                accum = -1
    return ret


def main():
    print(min(f(False), f(True)))
    return


main()

Submission status: Accepted

Solution code:
def f(flg, an):
    accum = 0
    ret = 0
    for i, ai in enumerate(an):
        accum += ai
        if flg:
            flg = False
            if accum <= 0:
                ret += -accum + 1
                accum = 1
        else:
            flg = True
            if 0 <= accum:
                ret += accum + 1
                accum = -1
    return ret


def main():
    an = list(map(int, open(0).read().split()[1:]))
    print(min(f(False, an), f(True, an)))
    return


main()

Submission status: Accepted

Solution code:
def f(flg, an):
    accum = 0
    ret = 0
    for i, ai in enumerate(an):
        accum += ai
        if flg:
            flg = False
            if accum <= 0:
                ret += -accum + 1
                accum = 1
        else:
            flg = True
            if 0 <= accum:
                ret += accum + 1
                accum = -1
    return ret


def main():
    an = [int(ai) for ai in open(0).read().split()[1:]]
    print(min(f(False, an), f(True, an)))
    return


main()

Submission status: Accepted

Solution code:
def f(flg, an):
    accum = 0
    ret = 0
    for i, ai in enumerate(an):
        accum += ai
        if flg:
            flg = False
            if accum <= 0:
                ret += -accum + 1
                accum = 1
        else:
            flg = True
            if 0 <= accum:
                ret += accum + 1
                accum = -1
    return ret


def main():
    n, *an = map(int, open(0).read().split())
    print(min(f(False, an), f(True, an)))
    return


main()

Submission status: Accepted

Solution code:
def f(flg, an):
    accum = 0
    ret = 0
    for i, ai in enumerate(an):
        accum += ai
        if i % 2:
            if flg and 0 <= accum:
                ret += accum + 1
                accum = -1
            elif not flg and accum <= 0:
                ret += -accum + 1
                accum = 1
        else:
            if flg and accum <= 0:
                ret += -accum + 1
                accum = 1
            elif not flg and 0 <= accum:
                ret += accum + 1
                accum = -1

    return ret


def main():
    n, *an = map(int, open(0).read().split())
    print(min(f(0, an), f(1, an)))
    return


main()

Submission status: Accepted

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

sums = 0
ans = 0
# プラスがはじめ
for itr, val in enumerate(a):
  sums += val
  if itr % 2 == 0:
    if sums <= 0:
      ans += 1 - sums
      sums = 1
  else:
    if sums >= 0:
      ans += sums + 1
      sums = - 1
gans = ans

sums = 0
ans = 0
# マイナスがはじめ
for itr, val in enumerate(a):
  sums += val
  if itr % 2 != 0:
    if sums <= 0:
      ans += 1 - sums
      sums = 1
  else:
    if sums >= 0:
      ans += sums + 1
      sums = - 1
kans = ans
print(min(gans, kans))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans_even = 0
ans_odd = 0
sum = 0
# 偶数番目を正にする
for i in range(n):
    # 和を正にする
    if i % 2 == 1:
        sum += a[i]
        if sum > 0:
            pass
        else:
            ans_even += 1 - sum
            sum = 1
    # 和を負にする
    else:
        sum += a[i]
        if sum < 0:
            pass
        else:
            ans_even += abs(-1 - sum)
            sum = -1
# 奇数番目を正にする
sum = 0
for i in range(n):
    # 和を正にする
    if i % 2 == 0:
        sum += a[i]
        if sum > 0:
            pass
        else:
            ans_odd += 1 - sum
            sum = 1
    # 和を負にする
    else:
        sum += a[i]
        if sum < 0:
            pass
        else:
            ans_odd += abs(-1 - sum)
            sum = -1
print(min(ans_even, ans_odd))

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int,input().split()))
B = A[:]
for i in range(1,n):
    B[i] += B[i-1]
C = B[:]
cnt1 = 0
cnt2 = 0
if B[0]<=0:
    cnt1 += abs(1-B[0])
    cnt2 += 1-B[0]
    B[0] += cnt2
for i in range(1,n):
    if i%2==0:
        if B[i]+cnt2<=0:
            cnt1 += abs(1-B[i]-cnt2)
            cnt2 += 1-B[i]-cnt2
            B[i] += cnt2
    else:
        if B[i]+cnt2>=0:
            cnt1 += abs(-1-B[i]-cnt2)
            cnt2 += -1-B[i]-cnt2
            B[i] += cnt2
cnt3 = 0
cnt4 = 0
if C[0]>=0:
    cnt3 += abs(-1-C[0])
    cnt4 += -1-C[0]
    C[0] += cnt4
for i in range(1,n):
    if i%2==0:
        if C[i]+cnt4>=0:
            cnt3 += abs(-1-C[i]-cnt4)
            cnt4 += -1-C[i]-cnt4
            C[i] += cnt4
    else:
        if C[i]+cnt4<=0:
            cnt3 += abs(1-C[i]-cnt4)
            cnt4 += 1-C[i]-cnt4
            C[i] += cnt4
print(min(cnt1,cnt3))

Submission status: Accepted

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

def wh(cst,ttl,flg):
    for i in range(n):
        ttl += a[i]

        if ttl*flg < 0:
            flg *= -1
        else:
            if flg > 0:
                memo = abs(ttl)+1
                ttl -= memo
                cst += memo
            elif flg < 0:
                memo = abs(ttl)+1
                ttl += memo
                cst += memo
            flg *= -1

        
    return cst




ttl = 0
cst = 0



cst = wh(cst,ttl,1)
ttl = 0
cst2 = 0
cst2 = wh(cst2,ttl,-1)
   

print(min(cst,cst2))

Submission status: Accepted

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

count1 = 0
accum1 = 0
for i in range(n):
    if i % 2 == 0:
        if accum1 + A[i] <= 0:
            count1 += 1 - accum1 - A[i]
            accum1 = 1
        else:
            accum1 += A[i]
    else:
        if accum1 + A[i] >= 0:
            count1 += 1 + accum1 + A[i]
            accum1 = -1
        else:
            accum1 += A[i]
            
count2 = 0
accum2 = 0
for i in range(n):
    if i % 2 != 0:
        if accum2 + A[i] <= 0:
            count2 += 1 - accum2 - A[i]
            accum2 = 1
        else:
            accum2 += A[i]
    else:
        if accum2 + A[i] >= 0:
            count2 += 1 + accum2 + A[i]
            accum2 = -1
        else:
            accum2 += A[i]
            
print(min(count1,count2))

Submission status: Accepted

Solution code:
n=int(input())
l=[int(i) for i in input().split()]
sm=l[0]
req=0 
pr=1 
if sm>0:
    pass 
else:
    req+=-sm+1 
    sm=1 
for i in range(1,n):
    if pr==1:
        n1=sm+l[i]
        if n1<0:
            sm+=l[i]
            pass 
        else: 
            sm+=l[i]
            req+=sm+1 
            sm=-1 
    else: 
        n1=sm+l[i]
        if n1>0:
            sm+=l[i]
            pass 
        else: 
            sm+=l[i]
            req+=(-sm+1) 
            sm=1 
    pr=1-pr  
req1=0 
pr=0
sm=l[0]
if sm<0:
    pass 
else:
    req1+=sm+1 
    sm=-1 
for i in range(1,n):
    if pr==1:
        n1=sm+l[i]
        if n1<0:
            sm+=l[i]
            pass 
        else: 
            sm+=l[i]
            req1+=sm+1 
            sm=-1 
    else: 
        n1=sm+l[i]
        if n1>0:
            sm+=l[i]
            pass 
        else: 
            sm+=l[i]
            req1+=(-sm+1) 
            sm=1 
    pr=1-pr  
print(min(req,req1))

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
A2=[0]*N
for i in range(N):
    A2[i]=A2[i-1]+A[i]
r,f,s=0,1,0
for a in A2:
    if (a+s)*-f>=0:
        x=a+s-f
        s-=x
        r+=abs(x)
    f*=-1
r2,f,s=0,-1,0
for a in A2:
    if (a+s)*-f>=0:
        x=a+s-f
        s-=x
        r2+=abs(x)
    f*=-1
print(min(r,r2))

Submission status: Accepted

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

for i in [1,-1]:
    B = 0
    C = 0
    for j in a:
        B += j
        if B * i <= 0:
            C += abs(B - i)
            B = i
        i *= -1
    A = min(A,C)
print(A)

Submission status: Accepted

Solution code:
import copy

n = int(input())
A = list(map(int,input().split()))
sums = [0 for _ in range(n)]

sums[0] = A[0]

for i in range(1,n):
    sums[i] = A[i] + sums[i-1]
sums2 = copy.copy(sums)
tyousei = 0
ans1 = 0
for i in range(n):
    if i%2 ==0:
        if sums[i]+tyousei <=0:
            ans1 += abs(sums[i]+tyousei) +1
            tyousei += abs(sums[i]+tyousei) +1
    if i%2 ==1:
        if sums[i]+tyousei >=0:
            ans1 += abs(sums[i]+tyousei) +1
            tyousei -= sums[i]+tyousei +1

ans2 = 0
tyousei =0
for i in range(n):
    if i%2 ==1:
        if sums[i]+tyousei <=0:
            ans2 += abs(sums[i]+tyousei) +1
            tyousei += abs(sums[i]+tyousei) +1
    if i%2 ==0:
        if sums[i]+tyousei >=0:
            ans2 += sums[i]+tyousei +1
            tyousei -= sums[i]+tyousei +1

print(min(ans1,ans2))

Submission status: Accepted

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

ans1 = 0
s = 0
flg = 1
for ai in a:
    s += ai
    if s * flg <= 0:
        ans1 += abs(s) + 1
        s = flg
    flg *= -1

ans2 = 0
s = 0
flg = -1
for ai in a:
    s += ai
    if s * flg <= 0:
        ans2 += abs(s) + 1
        s = flg
    flg *= -1

print(min(ans1, ans2))

Submission status: Accepted

Solution code:
# C - Sequence

import numpy as np

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

def ope_sequence(a, plus_flg):
    a = a.copy()
    output = 0
    
    if plus_flg == 1: # 偶数項で累積和を正とする
        c = 0 # 累積和
        for idx in range(n):
            c += a[idx]
            if idx % 2 == 0 and c <= 0:
                a[idx] += 1-c # 累積和が+1となるように操作
                output += abs(1-c)
                c = 1
            elif idx % 2 == 1 and c >= 0:
                a[idx] += -1-c # 累積和が-1となるように操作
                output += abs(-1-c)
                c = -1
                
    else: # 偶数項で累積和を負とする
        c = 0
        for idx in range(n):
            c += a[idx]
            if idx % 2 == 0 and c >= 0:
                a[idx] += -1-c 
                output += abs(-1-c)
                c = -1
            elif idx % 2 == 1 and c <= 0:
                a[idx] += 1-c 
                output += abs(1-c)
                c = 1
    
    return output

print(min(ope_sequence(original_a,1), ope_sequence(original_a,-1)))

Submission status: Accepted

Solution code:
def re(k1,l,s):
    r=0
    for i in range(0,n):
        if s==1:
            s=0
            k1+=l[i]
            if k1>=0:
                r+=k1+1
                k1=-1
            continue
        if s==0:
            s=1
            k1+=l[i]
            if k1<=0:
                r+=abs(k1)+1
                k1=1
    return r

n=int(input())
l=list(map(int,input().split()))
ans=0

k1=0

ans=min(re(k1,l,1),re(k1,l,0))



print(ans)

Submission status: Accepted

Solution code:
inf = float('inf')

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

def accmulate(arr):
    N = len(arr)
    cs = [0]*(N+1)
    for i in range(N):
        cs[i+1] = cs[i] + arr[i]
    return cs

# [l,r)
def query(cs, l, r):
    return cs[r] - cs[l]

cs = accmulate(A)[1:]
ans = inf

res = 0
tmp = 0
for i in range(len(cs)):
    if i % 2 == 0:
        # 負にする
        d = -1 - (cs[i] + tmp)
        d = min(d, 0)
        res += -d
        tmp += d
    else:
        # 正にする
        d = 1 - (cs[i] + tmp)
        d = max(d, 0)
        res += d
        tmp += d

ans = min(ans, res)

res = 0
tmp = 0
for i in range(len(cs)):
    if i % 2 == 1:
        # 負にする
        d = -1 - (cs[i] + tmp)
        d = min(d, 0)
        res += -d
        tmp += d
    else:
        # 正にする
        d = 1 - (cs[i] + tmp)
        d = max(d, 0)
        res += d
        tmp += d

ans = min(ans, res)
print(ans)

Submission status: Accepted

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

check=a[0]
ans=0
if check<=0:
    ans=abs(check)+1
    check=1

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

            
check=a[0]
ans2=0
if check>=0:
    ans2=abs(check)+1
    check=-1

for i in range(1,n):
    check2=check+a[i]
    if i%2!=0:
        if check2<=0:
            ans2+=(abs(check2)+1)
            check=1
        else:
            check=check2
            
    else:
        if check2>=0:
            ans2+=(abs(check2)+1)
            check=-1
        else:
            check=check2
print(min(ans,ans2))

Submission status: Accepted

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

def calc(flag_sign):
  temp_flag = flag_sign
  sum_a = 0
  count = 0
  for one_a in list_a:
    if temp_flag:
      if sum_a + one_a > 0:
        sum_a += one_a
      else:
        count += abs(sum_a) - one_a + 1
        sum_a = 1
      temp_flag = False
    else:
      if sum_a + one_a < 0:
        sum_a += one_a
      else:
        count += abs(sum_a) + one_a + 1
        sum_a = -1
      temp_flag = True
  return count
    
print(min(calc(True), calc(False)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int, input().split()))
ans_l = []
for j in range(2):
    last_sum = l[0]
    ans = 0
    if j == 0:
        if last_sum <= 0:
            ans += abs(1 - last_sum)
            last_sum += 1 - last_sum
    else:
        if last_sum >= 0:
            ans += abs(-1 - last_sum)
            last_sum += -1 - last_sum
    for i in range(n - 1):
        # print(last_sum)
        if last_sum > 0:
            if last_sum + l[i + 1] < 0:
                last_sum += l[i + 1]
            else:
                a = -1 - last_sum - l[i + 1]
                ans += abs(a)
                last_sum += a + l[i + 1]
        else:
            if last_sum + l[i + 1] > 0:
                last_sum += l[i + 1]
            else:
                a = 1 - last_sum - l[i + 1]
                ans += abs(a)
                last_sum += a + l[i + 1]
    ans_l.append(ans)
print(min(ans_l))

Submission status: Accepted

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

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

Submission status: Accepted

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


def check(op):  # 最初の数の正負を引数とする
    total = 0
    ans = 0
    for i in range(0, n):
        if (total+a[i]) > 0 and op == "+":
            ans += abs(-1 - (total + a[i]))
            total = -1
            op = "-"
        elif (total+a[i]) < 0 and op == "-":
            ans += abs(1 - (total + a[i]))
            total = 1
            op = "+"
        elif total+a[i] == 0:
            ans += 1
            if op == "+":
                total = -1
                op = "-"
            else:
                total = 1
                op = "+"
        else:
            total += a[i]
            if op == "+":
                op = "-"
            else:
                op = "+"
    return ans


print(min(check("+"), check("-")))

Submission status: Accepted

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


def f(x):
    if x == 0:
        return 0
    else:
        return x // abs(x)


a1 = [0] * n

if a[0] != 0:
    a1[0] = a[0]
    ans = 0
else:
    a1[0] = 1
    ans = 1

b = a1[0]

for i in range(1, n):
    if b * (b + a[i]) >= 0:
        if a[i] != 0:
            a1[i] = -f(a1[i - 1]) - b
        else:
            if b > 0:
                a1[i] = -1
            else:
                a1[i] = 0
        if b + a1[i] == 0:
            a1[i] += f(a1[i])
        ans += abs(a1[i] - a[i])
    else:
        a1[i] = a[i]

    b += a1[i]

a2 = [0] * n

if a[0] != 0:
    a2[0] = -f(a[0])
    ans1 = abs(a2[0] - a[0])
else:
    a2[0] = -1
    ans1 = 1

b1 = a2[0]

for i in range(1, n):
    if b1 * (b1 + a[i]) > 0:
        if a[i] != 0:
            a2[i] = -f(a2[i - 1]) - b1
        else:
            if b1 > 0:
                a2[i] = -1
            else:
                a2[i] = 0
        if b1 + a2[i] == 0:
            a2[i] += f(a2[i])
        ans1 += abs(a2[i] - a[i])
    else:
        a2[i] = a[i]

    b1 += a2[i]

print(min(ans1, ans))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
cs = [A[0]] + [0] * (N - 1)
a = 0
b = 0
for i in range(N - 1):
  cs[i + 1] = cs[i] + A[i + 1]
temp = 0
for i in range(N):
  if i % 2:
    if cs[i] + temp >= 0:
      a += abs(cs[i] + temp) + 1
      temp -= abs(cs[i] + temp) + 1
  else:
    if cs[i] + temp <= 0:
      a += abs(cs[i] + temp) + 1
      temp += abs(cs[i] + temp) + 1
temp = 0
for i in range(N):
  if i % 2:
    if cs[i] + temp <= 0:
      b += abs(cs[i] + temp) + 1
      temp += abs(cs[i] + temp) + 1
  else:
    if cs[i] + temp >= 0:
      b += abs(cs[i] + temp) + 1
      temp -= abs(cs[i] + temp) + 1
print(min(a, b))

Submission status: Accepted

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

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


def solve(bl):
    if bl:
        tmp = -1
    else:
        tmp = 0
    sm = 0
    cnt = 0
    for i, ele in enumerate(a):
        sm += ele
        if i % 2 + tmp:
            if sm < 1:
                cnt += 1 - sm
                sm = 1
        else:
            if sm > -1:
                cnt += sm + 1
                sm = -1

    return cnt


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

Submission status: Accepted

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

#正負正負正…と展開する場合
S=0
counta=0
for i in range(n):
  if i%2 == 0:
    if S+a[i] < 1:
      counta += (1-S-a[i])
      S = 1
    else:
      S+= a[i]
  else:
    if S+a[i] > -1:
      counta += (S+a[i]+1)
      S = -1
    else:
      S+=a[i]

#負正負正負…と展開する場合
T=0
countb=0
for i in range(n):
  if i%2 == 1:
    if T+a[i] < 1:
      countb += (1-T-a[i])
      T = 1
    else:
      T+= a[i]
  else:
    if T+a[i] > -1:
      countb += (T+a[i]+1)
      T = -1
    else:
      T+=a[i]
      
print(min(counta,countb))

Submission status: Accepted

Solution code:
from sys import stdin
n = int(stdin.readline().rstrip())
a = [int(x) for x in stdin.readline().rstrip().split()]
ans1 = 0
sum_sec = 0
for ind,i in enumerate(a):
    sum_sec += i
    if ind % 2 == 0:
        if sum_sec <= 0:
            ans1 += (-sum_sec+1)
            sum_sec = 1
    else:
        if sum_sec >= 0:
            ans1 += (sum_sec+1)
            sum_sec = -1

ans2 = 0
sum_sec = 0
for ind,i in enumerate(a):
    sum_sec += i
    
    if ind % 2 == 0:
        if sum_sec >= 0:
            ans2 += (sum_sec+1)
            sum_sec = -1
    else:
        if sum_sec <= 0:
            ans2 += (-sum_sec+1)
            sum_sec = 1
    #print(sum_sec)
print(min(ans1,ans2))

Submission status: Accepted

Solution code:
#https://atcoder.jp/contests/abc059/tasks/arc072_a

n = int(input())
l = list(map(int, input().split()))
cas1 = [0, 0]
cas2 = [0, 0]
def plus(index, cas):
    cost = cas[0]
    sum = cas[1]
    if (sum + l[index]) <= 0:
        return [cost + abs(l[index]+sum-1), 1]
    else:
        return [cost, sum + l[index]]
def minus(index, cas):
    cost = cas[0]
    sum = cas[1]
    if (sum + l[index]) >= 0:
        return [cost + abs(l[index]+sum+1), -1]
    else:
        return [cost, sum + l[index]]

for i in range(n):
    if i%2 == 0:
        cas1 = plus(i, cas1)
        cas2 = minus(i, cas2)
    else:
        cas1 = minus(i, cas1)
        cas2 = plus(i, cas2)

print(min(cas1[0], cas2[0]))

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
u=0
s=1
x=0
y=0
for i in a:
    u+=i
    if s*u<=0:
      x+=1-s*u
      u=s
    s=s*(-1)
s=-1
u=0
for i in a:
    u+=i
    if s*u<=0:
      y+=1-s*u
      u=s
    s=-1*s
print(min(x,y))

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007



flag = 1
n = I()
A = LI()
ret = 0
cumsum = 0
for i in range(n):
    cumsum += A[i]
    if flag and cumsum <= 0:
        ret += 1 - cumsum
        cumsum = 1
    elif not flag and cumsum >= 0:
        ret += cumsum + 1
        cumsum = -1
    flag ^= 1


ans = ret
ret = 0
flag = 0
cumsum = 0
for i in range(n):
    cumsum += A[i]
    if flag and cumsum <= 0:
        ret += 1 - cumsum
        cumsum = 1
    elif not flag and cumsum >= 0:
        ret += cumsum + 1
        cumsum = -1
    flag ^= 1


print(min(ans, ret))

Submission status: Accepted

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

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

count = 0
ans2 = 0
for i in range(N):
  if i%2==0:
    if count+a[i]<=0:
      ans2 += -a[i]+abs(count)+1
      count = 1
    else:
      count += a[i]
  else:
    if count+a[i]>=0:
      ans2 += count+1+a[i]
      count = -1
    else:
      count += a[i]
  

print(min(ans1, ans2))

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
p=0
ans_1=ans_2=0
for i in range(n):
	p+=a[i]
	if p>=0 and i%2==0:
		ans_1+=p+1
		p=-1
	elif p<=0 and i%2!=0:
		ans_1+=-p+1
		p=1
p=0
for i in range(n):
	p+=a[i]
	if p>=0 and i%2!=0:
		ans_2+=p+1
		p=-1
	elif p<=0 and i%2==0:
		ans_2+=-p+1
		p=1
print(min(ans_1,ans_2))

Submission status: Accepted

Solution code:

import sys
input = sys.stdin.readline
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
# A = [ int(input()) for _ in range(N) ]
##############################

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

def get_count(summary):
    count = 0
    for i in range(1, N):
    #    print(summary)

        # 次はマイナス
        if summary > 0:
            # 条件を満たしてる？
            if (summary + A[i]) < 0:
                summary += A[i]
            else:
                # プラスになっちゃってるので修正
                summary += A[i]
                count += (summary+1)
                summary = -1

        # 次はプラス
        else:
            if (summary + A[i]) > 0:
                summary += A[i]
            else:
                # マイナスになっちゃってるので修正
                summary += A[i]
                count += abs(summary)+1
                summary = 1

    return count

if A[0] > 0:
    plus = get_count(A[0])
    minus = get_count(-1)
    minus += (A[0]+1)
elif A[0] == 0:
    plus = get_count(1)+1
    minus = get_count(-1)+1
else:
    plus = get_count(1)
    plus += (abs(A[0])+1)
    minus = get_count(A[0])

print(min(plus, minus))

Submission status: Accepted

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


def calc(ls, sign):
    count = 0
    acc = 0
    for x in ls:
        y = acc + x
        if y == 0 or y // abs(y) != sign:
            z = sign - y
            count += abs(z)
            acc = sign
        else:
            acc = y
        sign *= -1
    return count


count = min(calc(ls, 1), calc(ls, -1))
print(count)

Submission status: Accepted

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

SUM = 0
ans = 0
flag = 0 #0+,1-
for i in range(N):
    if i == 0:
        SUM = A[i]
        if SUM <= 0:
            ans+=1-SUM
            SUM = 1
    else:
        SUM += A[i]
        if flag == 0:
            if SUM >= 0:
                ans += SUM+1
                SUM = -1
            flag = 1
        else:
            if SUM <= 0:
                ans += 1-SUM
                SUM = 1
            flag = 0
    #print(ans,SUM)

MIN = ans
ans = 0
flag = 1 #0+,1-
for i in range(N):
    if i == 0:
        SUM = A[i]
        if SUM >= 0:
            ans+=1+SUM
            SUM = -1
    else:
        SUM += A[i]
        if flag == 0:
            if SUM >= 0:
                ans += SUM+1
                SUM = -1
            flag = 1
        else:
            if SUM <= 0:
                ans += 1-SUM
                SUM = 1
            flag = 0
    #print(ans,SUM)
                
ans = min(MIN,ans)
print(ans)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
a_s = input().split()
for i in range(N):
    a_s[i] = int(a_s[i])
a_s = np.array(a_s)
    
#sum = 1,-1,1, ...
ans1 = 0
sum0 = 0
for i ,a in enumerate(a_s):
    sum1 = sum0 + a
    if i==0:
        if sum1>0:
            pass
        else:
            ans1 += abs(1 - sum1)
            sum1 = 1
            
    elif i%2==0:
        if sum1*sum0<0:
            pass
        else:
            ans1 += abs(1 - sum1)
            sum1 = 1
            
    else:
        if sum1*sum0<0:
            pass
        else:
            ans1 += abs(-1 - sum1)
            sum1 = -1
    sum0 = sum1
    
#sum = -1,1,-1, ...
ans2 = 0
sum0 = 0
for i ,a in enumerate(a_s):
    sum2 = sum0 + a
    if i==0:
        if sum2<0:
            pass
        else:
            ans2 += abs(-1 - sum2)
            sum2 = -1
            
    elif i%2==0:
        if sum2*sum0<0:
            pass
        else:
            ans2 += abs(-1 - sum2)
            sum2 = -1
            
    else:
        if sum2*sum0<0:
            pass
        else:
            ans2 += abs(1 - sum2)
            sum2 = 1
    sum0 = sum2
    
ans = min(ans1,ans2)
print(ans)

Submission status: Accepted

Solution code:
from collections import deque

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

def calc(a, positive):
    curr = 0
    ans = 0
    while a:
        curr += a.popleft()
        if (positive and curr <= 0) or\
           ((not positive) and curr >= 0):
            ans += abs(curr) + 1
            curr = 1 if positive else -1
        positive = not positive
    return ans

def main():
    ans = min(calc(deque(A), True), calc(deque(A), False))
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
nums = list(map(int, input().split()))
ans = 10**14
for start in [-1, 1]:
  before = start
  cnt = 0
  sum_n = 0
  for num in nums:
    sum_n += num
    if before*sum_n >= 0:
      if before < 0:
        cnt += abs(1-sum_n)
        sum_n = 1
      else:
        cnt += abs(-1-sum_n)
        sum_n = -1
    before = sum_n
  ans = min(ans, cnt)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
sum = 0
A = [int(num) for num in input().split()]

counter_plus = 0
is_plus = True
sum = 0
for i in range(n):
  sum += A[i]
  if is_plus == True:
    if sum < 1:
      div = 1 - sum
      sum += div
      counter_plus += div
  else:
    if sum > -1:
      div = -1 -sum
      sum += div
      counter_plus -= div
  is_plus = not(is_plus)

counter_minus = 0
is_plus = False
sum = 0
for i in range(n):
  sum += A[i]
  if is_plus == True:
    if sum < 1:
      div = 1 - sum
      sum += div
      counter_minus += div
  else:
    if sum > -1:
      div = -1 -sum
      sum += div
      counter_minus -= div
  is_plus = not(is_plus)  
print(min(counter_plus, counter_minus))

Submission status: Accepted

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

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

Submission status: Accepted

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

ans = []
for first in (_arr[0], -1 * _arr[1] + 1, -1 * _arr[1] - 1, 1, -1):
    arr = _arr[:]
    c = 0
    prev = 0
    for i in range(n):
        t = prev + arr[i]
        if i == 0:
            c += abs(arr[i] - first)
            arr[i] = first
        elif prev > 0 and t >= 0:
            diff = t + 1
            c += diff
            arr[i] -= diff
        elif prev < 0 and t <= 0:
            diff = -1 * t + 1
            c += diff
            arr[i] += diff
        prev += arr[i]
    ans.append(c)
print(min(ans))

Submission status: Accepted

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

ans = float("inf")

for ii in [1, -1]:
    i = ii
    s = 0
    ans0 = 0
    for j in range(n):
        s += A[j]
        if s * i <= 0:
            ans0 += abs(s - i)
            s = i
        i = -i

    ans = min(ans, ans0)

print(ans)