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

cnt_1 = 0
base = 0
#奇数番目が正
for i in range(n):
    if ((base + a[i] >= 0) and (i % 2 == 1)) or ((base + a[i] <= 0) and (i % 2 == 0)):
        cnt_1 += (1 + abs(base + a[i]))
        base += a[i] + (1 + abs(base + a[i])) * (-1) ** i #調整後のa[0]~a[i]の和
    else:
      base += a[i] #調整後のa[0]~a[i]の和


cnt_2 = 0
base = 0
for i in range(n):
    if ((base + a[i] <= 0) and (i % 2 == 1)) or ((base + a[i] >= 0) and (i % 2 == 0)):
        #奇数番目が負
        cnt_2 += (1 + abs(base + a[i]))
        base += a[i] + (1 + abs(base + a[i])) * (-1) ** (i + 1)  #調整後のa[0]~a[i]の和
    else:
      base += a[i] #調整後のa[0]~a[i]の和

print(min(cnt_1, cnt_2))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
s = [a[0]]
#和sのリストを作成
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:
N = int(input())
A = [int(x) for x in input().split()]

def f(sgn):
    cum = 0
    cnt_operation = 0
    for a in A:
        sgn *= -1
        cum += a
        if cum * sgn > 0:
            continue
        else:
            cnt_operation += abs(cum) + 1
            cum = sgn
    return cnt_operation
print(min(f(-1), f(1)))

Submission status: Accepted

Solution code:
import numpy as np
def main():
    n=int(input())
    a=list(map(int,input().split(' ')))
    sumcum_odd = 0
    sumcum_even = 0
    ans_odd = 0
    ans_even = 0
    for i in range(n):
        if i%2 ==0:#odd
            if sumcum_odd + a[i] >0:
                sumcum_odd += a[i]
            elif sumcum_odd <=0:
                ans_odd += 1-1*(sumcum_odd+a[i])
                sumcum_odd = 1
            if sumcum_even + a[i] <0:
                sumcum_even += a[i]
            elif sumcum_even >= 0:
                ans_even += sumcum_even + a[i] +1
                sumcum_even = -1
        else:
            if sumcum_odd + a[i] <0:
                sumcum_odd += a[i]
            elif sumcum_odd >=0:
                ans_odd += sumcum_odd + a[i] +1
                sumcum_odd = -1
            if sumcum_even + a[i] >0:
                sumcum_even += a[i]
            elif sumcum_even <= 0:
                ans_even += 1-1*(sumcum_even+a[i])
                sumcum_even = 1
    print(min(ans_odd,ans_even))



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
s = 0
ans1 = 0
for i in range(N):
    if i%2 == 0 and s+A[i] > 0:
        s += A[i]
    elif i%2 == 0 and s+A[i] <= 0:
        ans1 += 1-s-A[i]
        s = 1
    elif i%2 == 1 and s+A[i] >= 0:
        ans1 += s+A[i]+1
        s = -1
    else:
        s += A[i]
s = 0
ans2 = 0
for i in range(N):
    if i%2 == 1 and s+A[i] > 0:
        s += A[i]
    elif i%2 == 1 and s+A[i] <= 0:
        ans2 += 1-s-A[i]
        s = 1
    elif i%2 == 0 and s+A[i] >= 0:
        ans2 += s+A[i]+1
        s = -1
    else:
        s += A[i]
print(min(ans1,ans2))

Submission status: Accepted

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

#第一項を正とするか負とするかで2パターン
#正
count1 = 0
if A[0] <= 0:
  sum1 = 1
  count1 = 1 - A[0]
else:
  sum1 = A[0]

for i in range(1, N):
  if i % 2 == 0:#和が正になるとき
    if sum1 + A[i] <= 0:
      count1 += 1 - (sum1 + A[i]) 
      sum1 = 1
    else:
      sum1 += A[i]
  if i % 2 == 1:
    if sum1 + A[i] >= 0:
      count1 += 1 + (sum1 + A[i]) 
      sum1 = -1
    else:
      sum1 += A[i]
  #print(count1,sum1)
#負
count2 = 0
if A[0] >= 0:
  sum2 = -1
  count2 = 1 + A[0]
else:
  sum2 = A[0]

for i in range(1, N):
  if i % 2 == 0:#和が負になるとき
    if sum2 + A[i] >= 0:
      count2 += 1 + (sum2 + A[i]) 
      sum2 = -1
    else:
      sum2 += A[i]
  if i % 2 == 1:
    if sum2 + A[i] <= 0:
      count2 += 1 - (sum2 + A[i]) 
      sum2 = 1
    else:
      sum2 += A[i]

print(min(count1, count2))
#print(count1, count2)

Submission status: Accepted

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

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

    # 偶数番目を正にする
    cost1 = 0
    sum_ = 0
    for i in range(n):
        sum_ += a[i]
        if i&1:#奇数番目 負
            if sum_ >= 0:
                tmp = -1 - sum_
                cost1 += abs(tmp)
                sum_ = -1
        else:# 偶数番目 正
            if sum_ <= 0:
                tmp = 1 - sum_
                cost1 += abs(tmp)
                sum_ = 1
    # 奇数番目を正にする
    cost2 = 0
    sum_ = 0
    for i in range(n):
        sum_ += a[i]
        if not i&1:#偶数番目 負
            if sum_ >= 0:
                tmp = -1 - sum_
                cost2 += abs(tmp)
                sum_ = -1
        else:# 奇数番目 正
            if sum_ <= 0:
                tmp = 1 - sum_
                cost2 += abs(tmp)
                sum_ = 1
    print(min(cost1, cost2))

if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
# sys.stdin.readline
import sys

input = sys.stdin.readline


class AtCoder:
    def main(self):
        n = int(input())
        a = list(map(int, input().split()))

        even_plus_ans = 0
        before = a[0]
        if a[0] <= 0:
            even_plus_ans -= a[0] - 1
            before = 1

        for i in range(1, n):
            if i % 2 == 0:
                v = a[i] + before
                if v > 0:
                    before = v
                elif v <= 0:
                    before = 1
                    even_plus_ans -= v - 1
            else:
                v = a[i] + before
                if v < 0:
                    before = v
                elif v >= 0:
                    before = -1
                    even_plus_ans += v + 1

        odd_plus_ans = 0
        before = a[0]
        if a[0] >= 0:
            odd_plus_ans += a[0] + 1
            before = -1

        for i in range(1, n):
            if i % 2 != 0:
                v = a[i] + before
                if v > 0:
                    before = v
                elif v <= 0:
                    before = 1
                    odd_plus_ans -= v - 1
            else:
                v = a[i] + before
                if v < 0:
                    before = v
                elif v >= 0:
                    before = -1
                    odd_plus_ans += v + 1

        print(min(even_plus_ans, odd_plus_ans))


# Run main
if __name__ == '__main__':
    AtCoder().main()

Submission status: Accepted

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

def calc(A):
  ans = 0
  s = A[0]
  if s > 0:
    flag = 1
  elif s < 0:
    flag = -1
  for i in range(1,N):
    s += A[i]
    if flag == 1 and s >= 0:
      ans += s + 1
      s = -1
    elif flag == -1 and s <= 0:
      ans += 1 - s
      s = 1
    flag *= -1
  return ans

if A[0] > 0:
  print(min(calc(A),calc([-1]+A[1:])+A[0]+1))
elif A[0] < 0:
  print(min(calc(A),calc([1]+A[1:])-A[0]+1))
else:
  print(min(calc([1]+A[1:]),calc([-1]+A[1:]))+1)

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

#正から始まるver
cnt_p = 0
app_p = 0

for i in range(N):
	if i == 0:
		if S[i] <= 0:
			cnt_p += 1 - S[i]
			app_p += 1 - S[i]
		continue

	if i % 2 == 1: #負でないとだめ
		if S[i]+app_p >= 0:
			cnt_p += (S[i]+app_p)  - (-1)
			app_p += -1 - (S[i]+app_p)
	elif i % 2 == 0: #正でないとだめ
		if S[i] + app_p <= 0:
			cnt_p += 1 - (S[i]+app_p)
			app_p += 1 - (S[i]+app_p)

#負から始まるver
cnt_n = 0
app_n = 0

for i in range(N):
	if i == 0:
		if S[i] >= 0:
			cnt_n += S[i] - (-1)
			app_n += -1 - S[i]
		continue

	if i % 2 == 1: #正でないとだめ
		if S[i]+app_n <= 0:
			cnt_n += 1 - (S[i]+app_n)
			app_n += 1 - (S[i]+app_n)
	elif i % 2 == 0: #負でないとだめ
		if S[i] + app_n >= 0:
			cnt_n += (S[i]+app_n) - (-1)
			app_n += -1 - (S[i]+app_n)

print(min(cnt_n, cnt_p))

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict
import sys,heapq,bisect,math,itertools,string,queue,fractions
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))
def inpl_str(): return list(sys.stdin.readline().split())
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)

n = inp()
a = inpl()
acc = list(itertools.accumulate(a))
#+-+-+-
cnt = 0
tmp = 0
for i in range(n):
    if i%2==0:
        tt = acc[i] + tmp
        if tt <= 0:
            cnt += 1 - tt
            tmp += 1 - tt
    else:
        tt = acc[i] + tmp
        if tt >= 0:
            cnt += tt + 1
            tmp -= tt + 1
ans = cnt
#-+-+-+
cnt = 0
tmp = 0
for i in range(n):
    if i%2:
        tt = acc[i] + tmp
        if tt <= 0:
            cnt += 1 - tt
            tmp += 1 - tt
    else:
        tt = acc[i] + tmp
        if tt >= 0:
            cnt += tt + 1
            tmp -= tt + 1
    # print(i,cnt,tmp,tt)
print(min(ans,cnt))

Submission status: Accepted

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

odd_ans = 0
odd_sum = 0

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

even_ans = 0
even_sum = 0

for i in range(n):
    even_sum += A[i]
    
    if i % 2 == 0:
        if even_sum <= 0:
            even_ans += 1 - even_sum
            even_sum = 1
    else:
        if even_sum >= 0:
            even_ans += even_sum + 1
            even_sum = -1
                        
print(min(odd_ans, even_ans))

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    n = int(input())
    a = list(map(int, input().split()))
    
    total = 0
    ans_a0_plus = 0
    for i in range(n):
        if i % 2 == 0:#plus
            if total + a[i] > 0:
                total = total + a[i]
            else:
                ans_a0_plus += 1 - (total + a[i])
                total = 1
        else:#minus
            if total + a[i] < 0:
                total = total + a[i]
            else:
                ans_a0_plus += 1 + (total + a[i])
                total = -1
    
    total = 0
    ans_a0_minus = 0
    for i in range(n):
        if i % 2 == 0:#minus
            if total + a[i] < 0:
                total = total + a[i]
            else:
                ans_a0_minus += 1 + (total + a[i])
                total = -1
        else:#plus
            if total + a[i] > 0:
                total = total + a[i]
            else:
                ans_a0_minus += 1 - (total + a[i])
                total = 1
    
    print(min(ans_a0_plus, ans_a0_minus))

Submission status: Accepted

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

for f in [0, 1]:
  sumA = 0
  for i, a in enumerate(A):
    sumA += a
    if i % 2 == f:
      if sumA < 1:
        d = 1 - sumA
        c[f] += d
        sumA += d
    else:
      if sumA > -1:
        d = 1 + sumA
        c[f] += d
        sumA -= d
      
print(min(c))

Submission status: Accepted

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

ans = 0

oans, eans = 0, 0
oacum, eacum = 0, 0
acum = 0

for i in range(0, N):
    oacum += A[i]
    eacum += A[i]

    if i & 1:
        # should be neg
        if oacum >= 0:
            oans += abs(oacum) + 1
            oacum = -1
        

        # should be pos
        if eacum <= 0:
            eans += abs(eacum) + 1
            eacum = 1

    else:
        # should be pos
        if oacum <= 0:
            oans += abs(oacum) + 1
            oacum = 1
        

        # should be neg
        if eacum >= 0:
            eans += abs(eacum) + 1
            eacum = -1

print(min(oans, eans))

Submission status: Accepted

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

ans = 0

"""
acum = A[0]
for i in range(1, N):
    if (acum >= 0 and acum + A[i] >= 0) or (acum < 0 and acum + A[i] < 0) or (acum + A[i] == 0):
        # same signs
        ans += abs(acum + A[i]) + 1
        acum = 1 if acum < 0 else -1

    else:
        # different signs
        acum += A[i]
"""
oans, eans = 0, 0
oacum, eacum = 0, 0
acum = 0

# 奇数番目が正
for i in range(0, N):
    oacum += A[i]
    eacum += A[i]

    if i & 1:
        # should be neg
        if oacum >= 0:
            oans += abs(oacum) + 1
            oacum = -1
        
        # should be pos
        if eacum <= 0:
            eans += abs(eacum) + 1
            eacum = 1

    else:
        # should be pos
        if oacum <= 0:
            oans += abs(oacum) + 1
            oacum = 1
        
        # should be neg
        if eacum >= 0:
            eans += abs(eacum) + 1
            eacum = -1

# print(oans, eans)
print(min(oans, eans))

Submission status: Accepted

Solution code:
n=int(input())
a= (list(map(int,input().split())))
sm1=sm2=0
cnt1=cnt2=0
for i ,num in enumerate(a):
	sm1+=num
	if sm1<=0 and i%2==0:
		cnt1+=1-sm1
		sm1=1
	elif sm1>=0 and i%2!=0:
		cnt1+=1+sm1
		sm1=-1
		
	sm2+=num
	if sm2<=0 and i%2!=0:
		cnt2+=1-sm2
		sm2=1
	elif sm2>=0 and i%2==0:
		cnt2+=1+sm2
		sm2=-1
print(min(cnt1,cnt2))

Submission status: Accepted

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

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

			
even_ans = 0
even_sum = 0

for i in range(n):
	even_sum += a[i]
	if i%2 == 0:
		if even_sum <= 0:
			even_ans += 1-even_sum
			even_sum = 1
	else:
		if even_sum >= 0:
			even_ans += even_sum+1
			even_sum = -1
print(min(odd_ans, even_ans))

Submission status: Accepted

Solution code:
def resolve():
    n=int(input())
    A=[int(i) for i in input().split()]
    def check(A,flag): # flag=Trueなら上から、Falseなら下から
        cumsum=0
        ans=0
        for a in A:
            cumsum+=a
            if cumsum<=0 and flag:
                ans+=1-cumsum
                cumsum=1
            elif cumsum>=0 and (not flag):
                ans+=1+cumsum
                cumsum=-1
            flag = (not flag)
        return ans
    print(min(check(A,True),check(A,False)))
resolve()

Submission status: Accepted

Solution code:
import sys
import copy
import  math
from _bisect import *
from collections import *
from operator import itemgetter
from math import factorial
"""
from fractions import gcd
def lcm(x, y):
    return (x * y) // gcd(x, y)
"""
stdin = sys.stdin
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()

n = ni()
li = na()
ans = [0, 0]
for j in range(2):
    tmp = j
    s = 0
    for i in range(n):
        tmp = 1 - tmp
        s += li[i]
        if tmp:
            if s <= 0:
                d = abs(s - 1)
                ans[j] += d
                s = 1
        else:
            if s >= 0:
                d = abs(s - (-1))
                ans[j] += d
                s = -1
print(min(ans))

Submission status: Accepted

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

def solve(a, t):
  ans = 0
  x = 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())

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

ans = 0

tmp = a[0]

if a[0] == 0:
    tmp = 1
    ans += 1
    
for i in range(1,n):
    #print(tmp,ans)
    if tmp > 0:
        if tmp + a[i] >= 0:
            ans += tmp + a[i] + 1
            tmp = -1
        else:
            tmp += a[i]
    else:
        if tmp + a[i] <= 0:
            ans += abs(tmp + a[i]) + 1
            tmp = 1
        else:
            tmp += a[i]

#print(ans)


ans2 = 0

if a[0] > 0:
    ans2 += a[0]+1
    tmp = -1
elif a[0] < 0:
    ans2 += -a[0]+1
    tmp = 1
else:
    tmp = -1
    ans2 += 1

    
for i in range(1,n):
    #print(tmp,ans)
    if tmp > 0:
        if tmp + a[i] >= 0:
            ans2 += tmp + a[i] + 1
            tmp = -1
        else:
            tmp += a[i]
    else:
        if tmp + a[i] <= 0:
            ans2 += abs(tmp + a[i]) + 1
            tmp = 1
        else:
            tmp += a[i]

print(min(ans,ans2))
exit()

ans2 = 0

tmp = -a[0]

ans2 += abs(a[0])*2
    
for i in range(1,n):
    #print(tmp,ans)
    if tmp > 0:
        if tmp + a[i] >= 0:
            ans2 += tmp + a[i] + 1
            tmp = -1
        else:
            tmp += a[i]
    else:
        if tmp + a[i] <= 0:
            ans2 += abs(tmp + a[i]) + 1
            tmp = 1
        else:
            tmp += a[i]

print(min(ans,ans2))

Submission status: Accepted

Solution code:
N = int(input())

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

# plus -> minus -> plus ...
p = 0
now = 0
for i in range(N):
    if  i % 2 == 0:
        if now + a[i] >= 1:
            now += a[i]
        else:
            p += 1 - (now + a[i])
            now = 1
    else:
        if now + a[i] <= -1:
            now += a[i]
        else:
            p += 1 + (now + a[i])
            now = -1

# minus -> plus -> minus ...
m = 0
now = 0
for i in range(N):
    if  i % 2 == 0:
        if now + a[i] <= -1:
            now += a[i]
        else:
            m += 1 + (now + a[i])
            now = -1
    else:
        if now + a[i] >= 1:
            now += a[i]
        else:
            m += 1 - (now + a[i])
            now = 1

print(min(p,m))

Submission status: Accepted

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

ans0 = ans1 = 0
cs = 0
for i, ai in enumerate(a):
    cs += ai

    if i % 2 == 0:
        if cs < 1:
            ans0 += 1 - cs
            cs = 1
    else:
        if cs > -1:
            ans0 += cs + 1
            cs = -1

cs = 0
for i, ai in enumerate(a):
    cs += ai

    if i % 2 == 0:
        if cs > -1:
            ans1 += cs + 1
            cs = -1
    else:
        if cs < 1:
            ans1 += 1 - cs
            cs = 1

ans = min(ans0, ans1)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
s1 = 0
s2 = 0
ans1 = 0
ans2 = 0

for i in range(n):
    s1 += a[i]
    s2 += a[i]
    if i%2 == 0 and s1 >= 0:
        ans1 += s1
        ans1 += 1
        s1 = -1
    elif i%2 != 0 and s1 <= 0:
        ans1 -= s1
        ans1 += 1
        s1 = 1
    if i%2 == 0 and s2 <= 0:
        ans2 -= s2
        ans2 += 1
        s2 = 1
    elif i%2 != 0 and s2 >= 0:
        ans2 += s2
        ans2 += 1
        s2 = -1

print(min(ans1, ans2))

Submission status: Accepted

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

def chk(A, first):
  sum = 0
  count = 0
  t = first
  for a in A:
    sum += a
    if t == True and sum < 1:
      count += 1-sum
      sum = 1
    elif t == False and sum > -1:
      count += sum + 1
      sum = -1
    t = not t
  return count
print(min(chk(A, True), chk(A, False)))

Submission status: Accepted

Solution code:
import numpy as np
import copy

n = int(input())

a = list(map(int,input().split()))
b=copy.deepcopy(a)

cntm=0
cntp=0
sum_a=0
sum_b=0

if a[0]<=0:
    cntp+=-a[0]+1
    a[0]=1
if b[0]>=0:
    cntm+=b[0]+1
    b[0]=-1
for i in range(n-1):
    sum_a += a[i]
    sum_b += b[i]
    if abs(sum_a) >= abs(a[i+1]) or sum_a*a[i+1]>=0:
            cntp += abs(-sum_a-np.sign(sum_a) -a[i+1])
            a[i+1]=-sum_a-np.sign(sum_a)
    if abs(sum_b) >= abs(b[i+1]) or sum_b*b[i+1]>=0:
            cntm += abs(-sum_b-np.sign(sum_b) -b[i+1])
            b[i+1]=-sum_b-np.sign(sum_b)
print(min(cntm, cntp))

Submission status: Accepted

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

# def exec_cnt_and_s(s,i,one_is_pos):
#     if 

def count_operation(one_is_pos):
    cnt = 0
    s = 0
    for i, a in enumerate(a_list):
        s += a
        if one_is_pos:
            if i % 2 == 0:
                if s <= 0:
                    cnt += -s + 1
                    s = 1
            else:
                if s >= 0:
                    cnt += s + 1
                    s = -1
        else:
            if i % 2 == 1:
                if s <= 0:
                    cnt += -s + 1
                    s = 1
            else:
                if s >= 0:
                    cnt += s + 1
                    s = -1
    return cnt

print(min(count_operation(True),count_operation(False)))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
def tat(t,a,n):
  ans = 0
  ssu = 0
  for x in a:
    ssu += x
    if ssu*t <= 0:
      ans += abs(ssu-t)
      ssu = t
    t *= -1
  return ans
print(min(tat(1,a,n),tat(-1,a,n)))

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc059/tasks/arc072_a
# C - Sequence
import copy
N = int(input().split()[0])
a_list = list(map(int, input().split()))

w_list_1 = copy.copy(a_list)
w_list_2 = copy.copy(a_list)
c_1, c_2 = 0, 0
s_1, s_2 = 0, 0
c_list = []

for i in range(N):  # 10 ** 5
    # 偶数項を正にする場合
    before = w_list_1[i]
    s_1 += before

    if i % 2 == 0 and s_1 < 0:
        w_list_1[i] += abs(s_1)
        c_1 += abs(s_1)
    elif i % 2 != 0 and s_1 >= 0:
        w_list_1[i] -= abs(s_1+1)
        c_1 += abs(s_1+1)

    s_1 = s_1 - before + w_list_1[i]

    if s_1 == 0:
        a = w_list_1[i]
        w_list_1[i] = a + 1 if a >= 0 else a - 1
        s_1 = 1 if a >= 0 else -1
        c_1 += 1

    # 偶数項を負にする場合
    before = w_list_2[i]
    s_2 += before

    if i % 2 == 1 and s_2 < 0:
        w_list_2[i] += abs(s_2)
        c_2 += abs(s_2)
    elif i % 2 != 1 and s_2 >= 0:
        w_list_2[i] -= abs(s_2+1)
        c_2 += abs(s_2+1)

    s_2 = s_2 - before + w_list_2[i]

    if s_2 == 0:
        a = w_list_2[i]
        w_list_2[i] = a + 1 if a >= 0 else a - 1
        s_2 = 1 if a >= 0 else -1
        c_2 += 1

ans = min([c_1, c_2])
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split(' ')))
result = 0
counter = 0
if a[0] == 0:
    counter += 1
    result += 1
else:
    counter += a[0]

for i in range(1, n):
    if counter < 0:
        counter += a[i]
        if counter == 0:
            counter += 1
            result +=1
        elif counter > 0:
            continue
        else:
            result += 1-counter
            counter = 1
    else:
        counter += a[i]
        if counter == 0:
            counter -= 1
            result += 1
        elif counter < 0:
            continue
        else:
            result += counter+1
            counter = -1
out = []
out.append(result)
result = 0
counter = 0
if a[0] == 0:
    counter -= 1
    result +=1
elif a[0]< 0:
    counter += 1
    result += 1-a[0]
else:
    counter -= 1
    result += 1+a[0]

for i in range(1, n):
    if counter < 0:
        counter += a[i]
        if counter == 0:
            counter += 1
            result += 1
        elif counter > 0:
            continue
        else:
            result += 1-counter
            counter = 1
    else:
        counter += a[i]
        if counter == 0:
            counter -= 1
            result +=1
        elif counter < 0:
            continue
        else:
            result += counter+1
            counter = -1
out.append(result)
print(min(out))

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import time
ts=time.time()
from heapq import heappush, heappop
from collections import deque,defaultdict,Counter
from itertools import permutations,groupby
import sys
import bisect
import string
import random
alp=string.ascii_lowercase
sys.setrecursionlimit(10**6)
def SI():
    return input().split()
def MI():
    return map(int,input().split())
def I():
    return int(input())
def LI():
    return [int(i) for i in input().split()]

n=I()
a=LI()
def solve(a,x):
    if a[0]*x>0:
        p=a[0]
        m=0
    else:
        p=x
        m=abs(a[0]-x)
    for i in range(1,n):
        t=-1 if p>0 else 1
        if (a[i]+p)*t>0:
            p+=a[i]
        else:
            m+=abs(a[i]+p-t)
            p+=a[i]+t*abs(a[i]+p-t)
    return m

print(min(solve(a,1),solve(a,-1)))

Submission status: Accepted

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

sa = [0, 0]
count = [0, 0]

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

    sa[1] += a[i]
    if sa[1] <= 0 and i%2 == 1:
        count[1] += 1 - sa[1]
        sa[1] = 1
    elif sa[1] >= 0 and i%2 == 0:
        count[1] += 1 + sa[1]
        sa[1] = -1

print(min(count))

Submission status: Accepted

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

sa = 0
count = [0, 0]

for i in range(n):
    now = a[i]
    sa += now
    if sa <= 0 and i%2 == 0:
        count[0] += 1 - sa
        sa = 1
    elif sa >= 0 and i%2 == 1:
        count[0] += 1 + sa
        sa = -1

sa = 0

for i in range(n):
    now = a[i]
    sa += now
    if sa <= 0 and i%2 == 1:
        count[1] += 1 - sa
        sa = 1
    elif sa >= 0 and i%2 == 0:
        count[1] += 1 + sa
        sa = -1

print(min(count))

Submission status: Accepted

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


def check(a, t):
    ans = 0
    x = 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(check(a, True), check(a, False)))

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
#input = open(sys.argv[1], "r").readline

def main():
    N = int(input())
    A = list(map(int, input().split()))
    tmp = A.copy()
    mi = -1
    for j in range(2):
        A = tmp.copy()
        s = 0
        n = 0
        if j == 0: # 偶数番目までの和が正 ⇒ A[0] を負にする
            if A[0] >= 0:
                n += abs(A[0] +1)
                A[0] = -1
        else:   # 奇数番目までの和が正 ⇒ A[0] を正にする
            if A[0] <= 0:
                n += abs(A[0] -1)
                A[0] = 1
        s = A[0]
#        print(A)
        for i in range(1,N):
            if s * (s+A[i]) >= 0:
                if s < 0:
                    n += abs(-s+1 -A[i])
                    A[i] = -s+1
                else:
                    n += abs(-s-1 -A[i])
                    A[i] = -s-1
            s += A[i]
        if mi == -1:
            mi = n
        else:
            mi = min(mi, n)
    print(mi)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#import pysnooper
#import os,re,sys,operator,math,heapq,string
from collections import Counter,deque
#from operator import itemgetter
#from itertools import accumulate,combinations,groupby,combinations_with_replacement
from sys import stdin,setrecursionlimit
#from copy import deepcopy

setrecursionlimit(10**6)
input=stdin.readline
n=int(input().rstrip())
a=[int(i) for i in input().split()]

def rep(l,b):
    cnt,s=0,0
    for i in l:
        s+=i
        if b and s<1:
           # print("True",s,i)
            cnt+=1-s
            s=1
        elif not b and s>-1:
            #print("False",s,i)
            cnt+=s+1
            s=-1
        #print(cnt,s)
        b=not b
    return cnt

print(min(rep(a,1),rep(a,0)))

Submission status: Accepted

Solution code:
N = int(input())
A = [int(s) for s in input().split()]
ans1 = 0
s = 0
for i, a in enumerate(A):
    s += a
    if i % 2 is 0:
        d = max(0, 1 - s)
        s += d
        ans1 += d
    else:
        d = max(0, 1 + s)
        s -= d
        ans1 += d
ans2 = 0
s = 0
for i, a in enumerate(A):
    s += a
    if i % 2 is 1:
        d = max(0, 1 - s)
        s += d
        ans2 += d
    else:
        d = max(0, 1 + s)
        s -= d
        ans2 += d
print(min(ans1, ans2))

Submission status: Accepted

Solution code:
import numpy as np
import bisect
from operator import itemgetter
import math
import functools
import itertools
import numpy as np
import sys
MAX_INT = int(10e10)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N = I()
a = IL()

l = []
tmp = 0
for i in range(N):
  tmp += a[i]
  l.append(tmp)

ans1 = 0
cnt = 0
tmp = 0
for i in range(N):
  if i%2 == 0:
    if l[i]+tmp > 0:
      continue
    else:
      cnt = -(l[i]+tmp) +1
      tmp += cnt
      ans1 += abs(cnt)
  else:
    if l[i]+tmp < 0:
      continue
    else:
      cnt = -(l[i]+tmp) -1
      tmp += cnt
      ans1 += abs(cnt)

ans2 = 0
cnt = 0
tmp = 0
for i in range(N):
  if i%2 == 0:
    if l[i]+tmp < 0:
      continue
    else:
      cnt = -(l[i]+tmp) -1
      tmp += cnt
      ans2 += abs(cnt)
  else:
    if l[i]+tmp > 0:
      continue
    else:
      cnt = -(l[i]+tmp) +1
      tmp += cnt
      ans2 += abs(cnt)


print(min(ans1,ans2))