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:
from itertools import accumulate
n, *A = map(int, open(0).read().split())
B = list(accumulate(A))
p0 = n0 = p1 = n1 = 0
for i, b in enumerate(B):
    if i % 2 == 0:
        if b+p0-n0 <= 0:
            p0 += abs(b+p0-n0)+1
        if b+p1-n1 >= 0:
            n1 += abs(b+p1-n1)+1
    else:
        if b+p0-n0 >= 0:
            n0 += abs(b+p0-n0)+1
        if b+p1-n1 <= 0:
            p1 += abs(b+p1-n1)+1
print(min(p0+n0, p1+n1))

Submission status: Accepted

Solution code:
import copy
n = int(input())
a = list(map(int,input().split()))
b = copy.copy(a)
ans_a = 0
ans_b = 0
sum_a = 0
sum_b = 0
for i in range(n):
    if i%2==0:
        if sum_a+a[i]>0:
            tmp = 0
        else:
            tmp = abs(sum_a+a[i])+1
            a[i] += tmp
            ans_a += tmp
    else:
        if sum_a+a[i]<0:
            tmp = 0
        else:
            tmp = abs(sum_a+a[i])+1
            a[i] -= tmp
            ans_a += tmp
    sum_a+=a[i]
    if i%2==1:
        if sum_b+b[i]>0:
            tmp = 0
        else:
            tmp = abs(sum_b+b[i])+1
            b[i] += tmp
            ans_b += tmp
    else:
        if sum_b+b[i]<0:
            tmp = 0
        else:
            tmp = abs(sum_b+b[i])+1
            b[i] -= tmp
            ans_b += tmp
    sum_b+=b[i]
#print(a)
#print(sum_a)
print(min(ans_a,ans_b))

Submission status: Accepted

Solution code:
from typing import List, Any


def read_int() -> int:
    return int(input().strip())


def read_ints() -> List[int]:
    return list(map(int, input().strip().split(' ')))


def solve() -> Any:
    N = read_int()
    A = read_ints()
    min_moves = 10**18
    for positive in [False, True]:
        prefix = 0
        base = 0
        moves = 0
        for a in A:
            prefix += a
            if positive:
                if prefix+base <= 0:
                    moves += 1-(prefix+base)
                    base = 1-prefix
            else:
                if prefix+base >= 0:
                    moves += prefix+base+1
                    base = -1-prefix
            positive = not positive
        min_moves = min(min_moves, moves)
    return min_moves



if __name__ == '__main__':
    print(solve())

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import math
from math import gcd,pi,sqrt
INF = float("inf")
MOD = 10**9 + 7

import sys
sys.setrecursionlimit(10**6)
import itertools
import bisect
from collections import Counter,deque
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]


def main():
    N = i_input()
    A = i_list()

    def solve(f):
        acc = 0
        cnt = 0
        for a in A:
            acc += a
            if f and acc <= 0:
                 cnt += 1 - acc
                 acc = 1
            elif not f and acc >= 0:
                cnt += acc + 1
                acc = -1
            f = not f
        return cnt

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






if __name__=="__main__":
    main()

Submission status: Accepted

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

# pattern1
goukei = 0
ans1 = 0
for i in range(n):
    goukei += A[i]
    if i % 2:
        if goukei >= 0:
            ans1 += goukei + 1
            goukei = -1
    else:
        if goukei <= 0:
            ans1 += 1 - goukei
            goukei = 1
# pattern2
goukei = 0
ans2 = 0
for i in range(n):
    goukei += A[i]
    if i % 2:
        if goukei <= 0:
            ans2 += 1 - goukei
            goukei = 1
    else:
        if goukei >= 0:
            ans2 += goukei + 1
            goukei = -1
print(min(ans1, ans2))

Submission status: Accepted

Solution code:
def f(c, cost):
    for i in a[1:]:
        if c > 0:
            if c + i >= 0:
                cost += c + i + 1
                c = -1
            else:
                c += i
        else:
            if c + i <= 0:
                cost += 1 - c - i
                c = 1
            else:
                c += i
    return c, cost


n = int(input())
a = list(map(int, input().split()))
cost = (a[0] == 0)
c = a[0] + (a[0] == 0)
c, cost = f(c, cost)
tmp = cost
cost = 0
if a[0] >= 0:
    cost = a[0] + 1
    c = -1
else:
    cost = -a[0] + 1
    c = 1
c, cost = f(c, cost)
print(int(min(cost, tmp)))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
def calc(flag):
    s = res = 0
    for i in range(N):
        flag = flag * -1
        s += a[i]
        if flag == 1:
            if s <= 0:
                res += abs(s) + 1
                s = 1
        elif flag == -1:
            if s >= 0:
                res += abs(s) + 1
                s = -1
    return res

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
from itertools import accumulate
from copy import copy
A=list(accumulate(a))
'''
if a[0]<0:
    temp=0
    count=0
    for i in range(1,n):
        if i%2==1:
'''
def seq(xc:list):
    x=copy(xc)
    n=len(x)
    temp1=0
    count1=0
    for i in range(n):
        x[i]+=temp1
        if i%2==1:
            if x[i]<=0:
                count1+=(1-x[i])
                temp1+=(1-x[i])
        else:
            if x[i]>=0:
                count1+=(x[i]+1)
                temp1-=(x[i]+1)
    temp2=0
    count2=0
    for j in range(n):
        xc[j]+=temp2
        if j%2==1:
            if xc[j]>=0:
                count2+=(xc[j]+1)
                temp2-=(xc[j]+1)
        else:
            if xc[j]<=0:
                count2+=(1-xc[j])
                temp2+=(1-xc[j])
    
    return min(count1,count2)
    

print(seq(A))

Submission status: Accepted

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

def Cost(a, Sum):
    if Sum > 0:
        if Sum + a < 0:
            return 0
        else:
            return abs(Sum + a) + 1
    if Sum < 0:
        if Sum + a > 0:
            return 0
        else:
            return abs(Sum + a) + 1

Sum = lambda a, preSum:preSum + a if Cost(a, preSum) == 0 else (-1)**int(preSum > 0)

pCost, pSum = (lambda:(0, A[0]) if A[0] > 0 else (abs(A[0])+1, 1))()
nCost, nSum = (lambda:(0, A[0]) if A[0] < 0 else (abs(A[0])+1,-1))()
for a in A[1:]:
    pCost += Cost(a, pSum)
    pSum = Sum(a, pSum)
    nCost += Cost(a, nSum)
    nSum = Sum(a, nSum)
print(min(pCost, nCost))

Submission status: Accepted

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

def culc(pattern):
    sum=0
    cost=0
    for i in range(n):
        sum+=num[i]
        if i%2==pattern:
            if sum>=0:
                cost+=sum+1
                sum=-1
        else:
            if sum<=0:
                cost+=-sum+1
                sum=1
        #print("sum is ", sum, "cost is ", cost)
    #print("")
    return cost


print(min(culc(0), culc(1)))

Submission status: Accepted

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

count_minus=0
sum_minus=0
for i in range(n):
    if i%2 != 0:
        if a[i]+sum_minus > 0:
            sum_minus += a[i]
        else:
            count_minus += 1-a[i]-sum_minus
            sum_minus = 1
    else:
        if a[i]+sum_minus < 0:
            sum_minus += a[i]
        else:
            count_minus += 1+a[i]+sum_minus
            sum_minus=-1
            
print(min(count_plus,count_minus))

Submission status: Accepted

Solution code:
# vim: fileencoding=utf-8


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

    evenAns, oddAns = 0, 0
    evenTotal, oddTotal = 0, 0

    # 奇数を負に
    for i in range(n):
        oddTotal += a[i]
        if i % 2 == 0:
            if oddTotal <= 0:
                oddAns += abs(oddTotal) + 1
                oddTotal += abs(oddTotal) + 1
        else:
            if oddTotal >= 0:
                oddAns += abs(oddTotal + 1)
                oddTotal -= abs(oddTotal) + 1

    # 偶数を負に
    for i in range(n):
        evenTotal += a[i]
        if i % 2 == 1:
            if evenTotal <= 0:
                evenAns += abs(evenTotal) + 1
                evenTotal += abs(evenTotal) + 1
        else:
            if evenTotal >= 0:
                evenAns += abs(evenTotal) + 1
                evenTotal -= abs(evenTotal) + 1
    print(min(evenAns, oddAns))


if __name__ == "__main__":
    main()

Submission status: Accepted

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

plus_ans = 0
minus_ans = 0
sum = 0

for i in range(len(A)):
    sum += A[i]
    if i % 2 == 0:
        if sum <= 0:
            plus_ans += abs(sum) + 1
            sum = 1
    else:
        if sum >= 0:
            plus_ans += abs(sum) + 1
            sum = -1

sum = 0
for i in range(len(A)):
    sum += A[i]
    if i % 2 == 0:
        if sum >= 0:
            minus_ans += abs(sum) + 1
            sum = -1
    else:
        if sum <= 0:
            minus_ans += abs(sum) + 1
            sum = 1

print(min(plus_ans, minus_ans))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = [A[0]]
for i in range(1,N):
  A[i] += A[i-1]

th = min(0, A[0]-1)
ans_pos = abs(th) #正スタート
for i in range(1,N):
  if i % 2 == 1: #負にしたい
    ans_pos += abs(th - max(th, A[i]+1))
    th = max(th, A[i]+1)
  else: #正にしたい
    ans_pos += abs(th - min(th, A[i]-1))
    th = min(th, A[i]-1)
  #print(ans_pos, th, i)

th = max(0, A[0]+1)
ans_neg = abs(th) #負スタート
for i in range(1,N):
  if i % 2 == 1: #正にしたい
    ans_neg += abs(th - min(th, A[i]-1))
    th = min(th, A[i]-1)
  else: #負にしたい
    ans_neg += abs(th - max(th, A[i]+1))
    th = max(th, A[i]+1)
  #print(ans_neg, th, i)

print(min(ans_pos, ans_neg))

Submission status: Accepted

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


def is_diff_sign(s, t):
    return (s > 0 > t) or (s < 0 < t)


def count(_list, first):
    cnt = abs(first - _list[0])
    prev_sum = first
    prev_positive = first > 0
    for a in _list[1:]:
        _sum = prev_sum + a
        if is_diff_sign(prev_sum, _sum):
            prev_sum = _sum
            prev_positive = prev_sum > 0
            continue
        if prev_positive:
            prev_sum = -1
        else:
            prev_sum = 1
        prev_positive = not prev_positive
        cnt += abs(_sum) + 1
    return cnt


ans = 0
if al[0] == 0:
    ans = min(count(al, 1), count(al, -1))
else:
    ans = min(count(al, al[0]), count(al, -al[0] // abs(al[0])))
print(ans)

Submission status: Accepted

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

pa=0
s=A[0]
if A[0]<=0:
    pa=1-A[0]
    s=1

for i in range(1,n):
    if i%2==1:
        if s+A[i]>=0:
            pa+=s+A[i]-(-1)
            s=-1
        else:
            s+=A[i]
    else:
        if s+A[i]<=0:
            pa+=1-(s+A[i])
            s=1
        else:
            s+=A[i]
ma=0
s=A[0]
if A[0]>=0:
    ma=A[0]-(-1)
    s=-1

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

Submission status: Accepted

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

ans = float('inf')
for _ in range(2):
    tmp = 0
    if _:
        if a[0] > 0:
            x = a[0]
        else:
            x = 1
            tmp = 1 - a[0]
    else:
        if a[0] < 0:
            x = a[0]
        else:
            x = -1
            tmp = 1 + a[0]
    for i in range(1, n):
        y = x + a[i]
        if x*y >= 0:
            if x > 0:
                tmp += y+1
                y -= y+1
            elif x < 0:
                tmp -= y-1
                y -= y-1
        x = y
    ans = min(ans, tmp)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
acc = [0]*n
acc[0] = a[0]
def solve():
  for i in range(1,n):
    acc[i] = a[i]+acc[i-1]
  oddans = 0
  odddiff = 0
  #simulation in case of odd is positive
  for i in range(n):
    if i%2 == 0 and (acc[i]+odddiff)<=0:
      oddans += abs(acc[i]+odddiff)+1
      odddiff += (abs(acc[i]+odddiff))+1
    elif i%2 != 0 and (acc[i]+odddiff)>=0:
      oddans += acc[i]+odddiff+1
      odddiff -= acc[i]+odddiff+1
  #neg
  evenans = 0
  evendiff = 0
  for i in range(n):
    if i%2 == 0 and (acc[i]+evendiff)>=0:
      evenans += (acc[i]+evendiff+1) 
      evendiff -= (acc[i]+evendiff+1)
    elif i%2 != 0 and (acc[i]+evendiff)<=0:
      evenans += (abs(acc[i]+evendiff)+1)
      evendiff += (abs(acc[i]+evendiff)+1)
  return min(evenans,oddans)
  
print(solve())

Submission status: Accepted

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

A_csum = [0] * n
Sum = 0
for i in range(n):
    Sum += A[i]
    A_csum[i] = Sum

pat1 = 0
next_sign = 1
now = 0
for i in range(n):
    if (A_csum[i]+now) * next_sign <= 0:
        diff = next_sign-(A_csum[i]+now)
        pat1 += abs(diff)
        now += diff
    else:
        pass
    next_sign *= -1
    
pat2 = 0
next_sign = -1
now = 0
for i in range(n):
    if (A_csum[i]+now) * next_sign <= 0:
        diff = next_sign-(A_csum[i]+now)
        pat2 += abs(diff)
        now += diff
    else:
        pass
    next_sign *= -1

print(min(pat1,pat2))

Submission status: Accepted

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


def count(ls, front):
    # replace ls[0] by front
    res = abs(front - ls[0])
    sm = front
    for i in range(1, n):
        temp = ls[i]
        if sm > 0 and ls[i] > -sm-1:
            res += abs(-sm-1 - ls[i])
            temp = -sm-1
        elif sm < 0 and ls[i] < -sm+1:
            res += abs(-sm+1 - ls[i])
            temp = -sm+1
        sm += temp
    return res


ans = 0
if l[0] == 0:
    ans = min(count(l, 1), count(l, -1))
else:
    ans = min(count(l, l[0]), count(l, -l[0] // abs(l[0])))
print(ans)

Submission status: Accepted

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


N = I()
A = LI()

# A1を正に
a1 = 0
s1 = 0
for i in range(N):
    s1 += A[i]
    if i % 2 == 0:
        if s1 <= 0:
            a1 += 1-s1
            s1 = 1
    else:
        if s1 >= 0:
            a1 += 1+s1
            s1 = -1

# A1を負に
a2 = 0
s2 = 0
for i in range(N):
    s2 += A[i]
    if i % 2 == 1:
        if s2 <= 0:
            a2 += 1-s2
            s2 = 1
    else:
        if s2 >= 0:
            a2 += 1+s2
            s2 = -1

print(min(a1,a2))

Submission status: Accepted

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate, product # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
import math
import bisect
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入g
#
# インデックス系
# int min_y = max(0, i - 2), max_y = min(h - 1, i + 2);
# int min_x = max(0, j - 2), max_x = min(w - 1, j + 2);
#
#
import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
#mod = 998244353
from sys import stdin
readline = stdin.readline
def readInts():
  return list(map(int,readline().split()))
def readTuples():
    return tuple(map(int,readline().split()))
def I():
  return int(readline())
n = I()
A = readInts()
cumsum = list(accumulate(A))
cnt = 0

# -
plus = None
ans = float('inf')
for k in range(2):
    if k%2:
        plus = True
    else:
        plus = False
    #print(plus,'-')
    one = cumsum.copy()
    #print(one)
    cnt = 0
    change = 0
    for i in range(n):
        one[i] += change
        if plus:
            if one[i] > 0:
                pass
            else:
                cnt += abs(1-one[i])
                change += abs(1-one[i])
            plus = not plus
        else:
            if one[i] < 0:
                pass
            else:
                cnt += abs(one[i]+1)
                change -= abs(one[i]+1)
                #print(change)
            plus = not plus
    ans = min(ans, cnt)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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


def solve(parity):
    wa = 0
    ans = 0
    for i in range(n):
        wa += a[i]
        if i % 2 == parity:
            if wa >= 0:
                ans += wa + 1
                wa -= wa + 1
        else:
            if wa <= 0:
                ans += -wa + 1
                wa += -wa + 1
    return ans


print(min(solve(0), solve(1)))

Submission status: Accepted

Solution code:
from itertools import accumulate


def solve(condition):
    sub, add = 0, 0
    for i in range(N):
        cur = acc[i] + add - sub
        if i % 2 == condition:
            if cur > 0:
                continue
            add += 1 - cur
        else:
            if cur < 0:
                continue
            sub += cur + 1
    return sub + add


N, *A = map(int, open(0).read().split())
acc = list(accumulate(A))
ans = min(solve(0), solve(1))
print(ans)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  #空白あり
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  #空白なし
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())  #空白あり
def LS2(): return list(sys.stdin.readline().rstrip())  #空白なし


N = I()
A = LI()

a1 = 0
s1 = 0
for i in range(N):
    s1 += A[i]
    if i % 2 == 0:
        if s1 <= 0:
            a1 += 1-s1
            s1 = 1
    else:
        if s1 >= 0:
            a1 += 1+s1
            s1 = -1
a2 = 0
s2 = 0
for i in range(N):
    s2 += A[i]
    if i % 2 == 1:
        if s2 <= 0:
            a2 += 1-s2
            s2 = 1
    else:
        if s2 >= 0:
            a2 += 1+s2
            s2 = -1

print(min(a1,a2))

Submission status: Accepted

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


# inputna
n = int(input())
a = list(map(int, input().split()))
ans = float("inf")
def sub(c):
    val = 0
    s = 0
    for num in a:
        if c*(s+num)<=0:
            val += abs(s+num-c)
            s = c
        else:
            s += num
        c *= -1
    return val
ans = min(sub(1), sub(-1))
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
dp=[0]*n
dp[0]=a[0]
for i in range(1,n):
  dp[i]=dp[i-1]+a[i]
ans1,ans2,cnt1,cnt2,flag=0,0,0,0,True
for i in range(n):
  if flag==True:
    if dp[i]+cnt1<=0:
      ans1+=abs(1-(dp[i]+cnt1))
      cnt1+=1-(dp[i]+cnt1)
    flag=False
  else:
    if dp[i]+cnt1>=0:
      ans1+=abs(-1-(dp[i]+cnt1))
      cnt1+=-1-(dp[i]+cnt1)
    flag=True

flag=False
for i in range(n):
  if flag==True:
    if dp[i]+cnt2<=0:
      ans2+=abs(1-(dp[i]+cnt2))
      cnt2+=1-(dp[i]+cnt2)
    flag=False
  else:
    if dp[i]+cnt2>=0:
      ans2+=abs(-1-(dp[i]+cnt2))
      cnt2+=-1-(dp[i]+cnt2)
    flag=True
print(min(ans1,ans2))

Submission status: Accepted

Solution code:
n=input()
a=list(map(int,input().split()))
ans=10**18
for s in -1,1:
  c=0
  if a[0]*s<0:c=abs(s-a[0])
  elif a[0]*s>0:s=a[0]
  else:c=1
  for i in a[1:]:
    if s<0:
      b=-s+1
      c+=max(0,b-i)
      s+=max(i,b)
    else:
      b=-s-1
      c+=max(0,i-b)
      s+=min(i,b)
  ans=min(ans,c)
print(ans)

Submission status: Accepted

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

# evenがプラスの時
sum_even = 0
ans_even = 0

for i in range(N):
  sum_even += A[i]
  if i % 2 == 0:
    if sum_even <= 0:
      ans_even += abs(sum_even) + 1
      sum_even += abs(sum_even) + 1
  else:
    if sum_even >= 0:
      ans_even += abs(sum_even) + 1
      sum_even -= abs(sum_even) + 1

# oddがプラスの時
sum_odd = 0
ans_odd = 0
for i in range(N):
  sum_odd += A[i]
  if i % 2 == 1:
    if sum_odd <= 0:
      ans_odd += abs(sum_odd) + 1
      sum_odd += abs(sum_odd) + 1
  else:
    if sum_odd >= 0:
      ans_odd += abs(sum_odd) + 1
      sum_odd -= abs(sum_odd) + 1

answer = min(ans_even, ans_odd)
print(str(answer))

Submission status: Accepted

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
#import bisect
import numpy as np
#from copy import deepcopy
#from collections import deque
#from decimal import Decimal
#from numba import jit

INF = 1 << 50
EPS = 1e-8


def run():
    n, *A = map(int, read().split())
    v = 0
    acum = []
    for a in A:
        v += a
        acum.append(v)

    # greedy
    ans = INF
    for V in (-1, 1):
        cums = 0
        count = 0
        for a in acum:
            #print(a, '---------')
            V *= -1
            if (a + cums) * V > 0:
                continue
            else:
                update = abs(a + cums) + 1
                cums += (update) * V
                count += update
                #print(V, cums, count)
        ans = min(ans, count)
    print(ans)



if __name__ == "__main__":
    run()

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = [a[0]]
for i in range(n - 1):
    b.append(b[-1] + a[i + 1])
f = [True, False]
ans = float("inf")
for i in f:
    flag = i
    count = 0
    num = 0
    for j in range(n):
        if flag == True:
            if b[j] + num <= 0:
                count += 1 - b[j] - num
                num += 1 - b[j] - num
            flag = False
        else:
            if b[j] + num >= 0:
                count += b[j] + num + 1
                num -= b[j] + num + 1
            flag = True
    ans = min(ans, count)
print(ans)

Submission status: Accepted

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

#偶数番目を正にする場合

s = 0
t0 = 0

if a[0] > 0:
    s = a[0]
else:
    s = 1
    t0 = abs(a[0]) + 1

for i in range(1, n):
    if (s + a[i]) * (-1) ** i <= 0:
        t0 += abs(s + a[i]) + 1
        s = (-1) ** i
    else:
        s += a[i]

#偶数番目を負にする場合

s = 0
t1 = 0

if a[0] < 0:
    s = a[0]
else:
    s = -1
    t1 = abs(a[0]) + 1

for i in range(1, n):
    if (s + a[i]) * (-1) ** (i + 1) <= 0:
        t1 += abs(s + a[i]) + 1
        s = (-1) ** (i + 1)
    else:
        s += a[i]

ans = min(t0, t1)

print(ans)

Submission status: Accepted

Solution code:
def main():
    _ = int(input())
    a = [int(an) for an in input().split()]
    target = {1, -1}
    results = set()
    for sign in target:
        total = 0
        ans = 0
        for an in a:
            total += an
            if sign != ((total > 0) - (total < 0)):
                ans -= (total - sign) * sign
                total = sign
            sign *= -1
        results.add(ans)

    print(min(results))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# def sign(x):
#     return (x > 0) - (x < 0)


def main():
    _ = int(input())
    a = [int(an) for an in input().split()]
    target = {1, -1}
    results = set()
    for sign in target:
        total = 0
        ans = 0
        for an in a:
            total += an
            if sign != ((total > 0) - (total < 0)):
                ans -= (total - sign) * sign
                total = sign
            sign *= -1
        results.add(ans)

    print(min(results))


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

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

print(min([ans1, ans]))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans1, ans2 = 0, 0
cnt1, cnt2 = 0, 0
cur = 1
for i in a:
    cnt1 += i
    if cnt1 * cur <= 0:
        ans1 += abs(cnt1 - cur)
        cnt1 = cur
    cur *= -1
cur = -1
for i in a:
    cnt2 += i
    if cnt2 * cur <= 0:
        ans2 += abs(cnt2 - cur)
        cnt2 = cur
    cur *= -1
print(min(ans1, ans2))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n=int(input())
a=list(map(int,input().split()))
count1=0
sum1=a[0]
if a[0]==0:
    count1=1
    sum1=1
for ii in range(1,n):
    S=sum1
    sum1+=a[ii]
    if sum1*S>=0:
        if S>0:
            count1+=sum1+1
            sum1=-1
        elif S<0:
            count1+=1-sum1
            sum1=1

if a[0]>=0:
    count2=a[0]+1
    sum2=-1
else:
    count2=1-a[0]
    sum2=1
for ii in range(1,n):
    S=sum2
    sum2+=a[ii]
    if sum2*S>=0:
        if S>0:
            count2+=sum2+1
            sum2=-1
        elif S<0:
            count2+=1-sum2
            sum2=1

print(min(count1,count2))

Submission status: Accepted

Solution code:
import sys
from collections import deque
import bisect
import copy
import heapq
import itertools
import math
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
mod = 10 ** 9 + 7

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]


def main():
    N = int(input())
    A = read_list()
    res = 10 ** 20
    
    for i in range(2):
        tmp = 0
        t = 0
        s = (-1) ** i
        for a in A:
            t += a
            if  t * s >= 0:
                tmp += abs(t) + 1
                t = -s
            s = -s
        res = min(res, tmp)
    print(res)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

print(ans)