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:
# -*- coding: utf-8 -*-

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

sum1 = 0
cnt1 = 0
for i in range(N):
    sum1 += aN[i]
    if i % 2 == 0:
        if sum1 >= 0:
            tmp = abs(sum1) +1
            cnt1 += tmp
            sum1 -= tmp
    else:
        if sum1 <= 0:
            tmp = abs(sum1) +1
            cnt1 += tmp
            sum1 += tmp

sum1 = 0
cnt2 = 0
for i in range(N):
    sum1 += aN[i]
    if i % 2 == 1:
        if sum1 >= 0:
            tmp = abs(sum1) +1
            cnt2 += tmp
            sum1 -= tmp
    else:
        if sum1 <= 0:
            tmp = abs(sum1) +1
            cnt2 += tmp
            sum1 += tmp

# 開始を偶奇それぞれでやって、良かった方を出力
print(min(cnt1, cnt2))

Submission status: Accepted

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

ans1=0
ans2=0

now=0

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

now=0

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

#print(ans1,ans2)
print(min([ans1,ans2]))

Submission status: Accepted

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

# 偶数番目が＋
A_even = A[:]
S_even = 0
ans_even = 0
for i in range(n):
    if (S_even + A_even[i]) * ((-1) ** (i - 1)) > 0:
        S_even += A_even[i]
        continue
    A_even[i] = (-1) ** (i - 1) - S_even
    ans_even += abs(A[i] - A_even[i])
    S_even = (-1) ** (i - 1)

# 奇数番目が＋
A_odd = A[:]
S_odd = 0
ans_odd = 0
for i in range(n):
    if (S_odd + A_odd[i]) * ((-1) ** i) > 0:
        S_odd += A_odd[i]
        continue
    A_odd[i] = (-1) ** i - S_odd
    ans_odd += abs(A[i] - A_odd[i])
    S_odd = (-1) ** i

ans = int(min(ans_even, ans_odd))

print(ans)

Submission status: Accepted

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

def calc(s,number,sign):
    if sign:
        if s+number>0:
            return (s+number,0,sign^1)
        else:
            return (1,1-s-number,sign^1)
    else:
        if s+number<0:
            return (s+number,0,sign^1)
        else:
            return (-1,s+number+1,sign^1)
signPlus=1
ansP = 0
s = 0
for number in a:
    s,d,signPlus=calc(s,number,signPlus)
    ansP+=d
signPlus=0
ansM = 0
s = 0
for number in a:
    s,d,signPlus=calc(s,number,signPlus)
    ansM+=d
print(min(ansP,ansM))

Submission status: Accepted

Solution code:
n = int(input())
A = map(int, input().split())
sum_b = 0
cnt_b = 0
sum_c = 0
cnt_c = 0
for i,a in enumerate(A):
  sum_b += a
  if not sum_b or ((sum_b > 0) != ((i % 2) > 0)):
    cnt_b += abs(sum_b) + 1
    sum_b = 1 if ((i % 2) > 0) else -1
  sum_c += a
  if not sum_c or ((sum_c > 0) == ((i % 2) > 0)):
    cnt_c += abs(sum_c) + 1
    sum_c = -1 if ((i % 2) > 0) else 1
print(min(cnt_b, cnt_c))

Submission status: Accepted

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

s1 = 0
SUM = 0
for i in range(len(a)):
    nxt = SUM + a[i]

    # plus
    if i % 2 == 0:
        if nxt > 0:
            SUM = nxt
            ops = 0
        else:
            SUM = 1
            ops = (1 - nxt)
    # minus
    else:
        if nxt < 0:
            SUM = nxt
            ops = 0
        else:
            SUM = -1
            ops = abs(-1 - nxt)

    s1 += ops

s2 = 0
SUM = 0
for i in range(len(a)):
    nxt = SUM + a[i]

    # plus
    if i % 2 == 1:
        if nxt > 0:
            SUM = nxt
            ops = 0
        else:
            SUM = 1
            ops = (1 - nxt)
    # minus
    else:
        if nxt < 0:
            SUM = nxt
            ops = 0
        else:
            SUM = -1
            ops = abs(-1 - nxt)

    s2 += ops

print(min(s1, s2))

Submission status: Accepted

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


def calc(sign):
    r = 0
    b = [None] * n
    for i in range(n):
        if i % 2 == sign:
            if r + a[i] > 0:
                b[i] = a[i]
            else:
                b[i] = - r + 1
        else:
            if r + a[i] < 0:
                b[i] = a[i]
            else:
                b[i] = - r - 1
        r += b[i]
    return b


ans = float('inf')
for sign in [0, 1]:
    b = calc(sign)
    d = 0
    for i in range(n):
        d += abs(a[i] - b[i])
    ans = min(ans, d)
print(ans)

Submission status: Accepted

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

def find(a,flag):
	c = 0
	s = 0
	for i in range(n):
		if flag and s + a[i] > 0:
			flag = False
			s = s + a[i]
		elif flag:
			c = c + abs(s + a[i])+1
			flag = False
			s = 1
		elif not flag and s + a[i] < 0:
			flag = True
			s = s + a[i]
	#		print((s,f,c))
		else:
			c = c + abs(s+a[i])+1
			flag = True
			s = -1
	return c

cplus = find(a,True)
cmin = find(a,False)
print(min(cplus,cmin))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def solve():
    n = int(input())
    a = list(map(int,input().split()))
    sum1 = sum2 = cost1 = cost2 = 0
    for i in range(n):
        cur = a[i]
        sum1 += cur
        if (sum1<=0, sum1>=0)[i%2]:
            cost1 += 1+sum1*[-1,1][i%2]
            sum1 = [1,-1][i%2]
        sum2 += cur
        if (sum2>=0, sum2<=0)[i%2]:
            cost2 += 1+sum2*[1,-1][i%2]
            sum2 = [-1,1][i%2]
    print(min(cost1, cost2))
if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
sum1 = sum2 = cost1 = cost2 = 0
for i in range(n):
    cur = a[i]
    sum1 += cur
    if (sum1<=0, sum1>=0)[i%2]:
        cost1 += 1+sum1*[-1,1][i%2]
        sum1 = [1,-1][i%2]
    sum2 += cur
    if (sum2>=0, sum2<=0)[i%2]:
        cost2 += 1+sum2*[1,-1][i%2]
        sum2 = [-1,1][i%2]
print(min(cost1, cost2))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
num1, num2 = 0, 0
s = 0
for i in range(n):
    s += a[i]
    if i % 2 == 0 and s <= 0:
        num1 += 1 - s
        s = 1
    elif i % 2 == 1 and s >= 0:
        num1 += s + 1
        s = -1

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

print(min(num1, num2))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = [a_ for a_ in a]

ans = 10**14
for a0 in [-1,a[0],1]:
    if a0 == 0:
        continue
    ans_ = abs(a[0]-a0)
    b[0] = a0
    sign = b[0] > 0
    for i in range(1,n):
        b[i] = a[i]+b[i-1]
        if sign:
            if b[i] >= 0:
                ans_ += b[i]+1
                b[i] = -1
        else:
            if b[i] <= 0:
                ans_ += 1-b[i]
                b[i] = 1
        sign = not sign
    ans = min(ans, ans_)
print(ans)

Submission status: Accepted

Solution code:
def sign(number):
    if number < 0:
        return -1
    elif number > 0:
        return 1
    else:
        return 0


n = int(input())
a = list(map(int, input().split()))
c_sum = 0
cnt_lst = []

for i in range(1, -2, -2):
    code = i
    cnt, c_sum = 0, 0
    for j in range(n):
        if sign(c_sum+a[j]) == code:
            c_sum += a[j]

        else:
            cnt += abs(code-(c_sum+a[j]))
            c_sum = code

        code *= -1

    cnt_lst.append(cnt)

print(min(cnt_lst))

Submission status: Accepted

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

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

ans2=0
s2=0
for i in range(N):
  s2+=A[i]
  if i % 2 ==0:
    if s2>=0:
      ans2+=(abs(s2)+1)
      s2=-1
  else:
    if s2<=0:
      ans2+=(abs(s2)+1)
      s2=1
print(min(ans,ans2))

Submission status: Accepted

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

s = 0
ans1 = 0
for k in range(N):
    s += A[k]
    if k%2==0:
        if s <= 0:
            ans1 += -s+1
            s = 1
    else:
        if s >= 0:
            ans1 += s+1
            s = -1

s = 0
ans2 = 0
for k in range(N):
    s += A[k]
    if k%2==0:
        if s >= 0:
            ans2 += s+1
            s = -1
    else:
        if s <= 0:
            ans2 += -s+1
            s = 1

print(min(ans1,ans2))

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int, input().split()))
even = 0
e_sum = 0
odd = 0
o_sum = 0
for i in range(n):
  e_sum += l[i]
  if i % 2 == 0 and e_sum <= 0:
    even += abs(e_sum)+1
    e_sum = 1
  elif not i % 2 == 0 and e_sum >= 0:
    even += abs(e_sum)+1
    e_sum = -1

for i in range(n):
  o_sum += l[i]
  if i % 2 == 0 and o_sum >= 0:
    odd += abs(o_sum)+1
    o_sum = -1
  elif not i % 2 == 0 and o_sum <= 0:
    odd += abs(o_sum)+1
    o_sum = 1

print(min(even, odd))

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
ten=0
ans=0
s=[0]*n
for i in range(n):
    s[i]=a[i]+ten
    if i%2==0:
        if s[i]<=0:
            ans+=abs(1-s[i])
            ten=1
        else:
            ten=s[i]
    elif i%2==1:
        if 0<=s[i]:
            ans+=abs(-1-s[i])
            ten=-1
        else:
            ten=s[i]
ten_2=0
ans_2=0
s_2=[0]*n
for i in range(n):
    s_2[i]=a[i]+ten_2
    if i%2==0:
        if 0<=s_2[i]:
            ans_2+=abs(-1-s_2[i])
            ten_2=-1
        else:
            ten_2=s_2[i]
    elif i%2==1:
        if s_2[i]<=0:
            ans_2+=abs(1-s_2[i])
            ten_2=1
        else:
            ten_2=s_2[i]
print(min(ans,ans_2))

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
b = a[::]
ans1 = ans2 = 0
sign = 1
total = 0
for i in range(n):
    total += a[i]
    if total * sign <= 0:
        k = abs(total - sign)
        ans1 += k
        total = sign
    sign *= -1

sign = -1
total = 0
for i in range(n):
    total += b[i]
    if total * sign <= 0:
        k = abs(total - sign)
        ans2 += k
        total = sign
    sign *= -1
print(min(ans1, ans2))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
ansa,ansb,t=0,0,0
for i in range(n):#偶数正
    t+=a[i]
    if i%2==1:
        if t>=0:
            ansa+=abs(t+1)
            t=-1
    else:
        if t<=0:
            ansa+=abs(t-1)
            t=1
t=0
for i in range(n):#奇数正
    t+=a[i]
    if i%2==1:
        if t<=0:
            ansb+=abs(t-1)
            t=1
    else:
        if t>=0:
            ansb+=abs(t+1)
            t=-1
print(min(ansa, ansb))

Submission status: Accepted

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

def count_manipulation(a_list, start):
    assert start in (0, 1)
    sum = 0
    count = 0
    for i, a in enumerate(a_list):
        sum += a
        if i % 2 == start:
            if sum > 0:
                continue
            else:
                diff = 1 - sum
                count += diff
                sum += diff
        else:
            if sum < 0:
                continue
            else:
                diff = sum - (-1)
                count += diff
                sum -= diff
    return count

count_plus = count_manipulation(a_list, 0)
count_minus = count_manipulation(a_list, 1)
print(min(count_plus, count_minus))

Submission status: Accepted

Solution code:
def smaller(x, y):
	if x < y: return x
	else: return y

def judge(a, posneg):
	cnt = 0
	cur = posneg
	sum = 0
	for i in range(0, len(a)):
		sum = sum + a[i]
		sumt = sum * cur
		if sumt < 1:
			cnt = cnt + abs(cur - sum)
			sum = cur
		cur = -1 * cur
	return cnt

n = int(raw_input())
a = map(int, raw_input().split())
print smaller(judge(a, 1), judge(a, -1))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
sums = 0
count = 0
for i in range(n):
    sums = sums + a[i]
    if i % 2 ==0 :
        if sums > 0:
            None
        else:
            count = count + 1 -sums
            sums = 1
    else:
        if sums < 0:
            None
        else:
            count = count + 1 + sums
            sums = -1
            
count2 = 0
sums2 = 0
for i in range(n):
    sums2 = sums2 + a[i]
    if i % 2 ==1 :
        if sums2 > 0:
            None
        else:
            count2 = count2 + 1 -sums2
            sums2 = 1
    else:
        if sums2 < 0:
            None
        else:
            count2 = count2 + 1 + sums2
            sums2 = -1
            
print(min(count,count2))

Submission status: Accepted

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

total = 0
flag = -1
minus = 0
for tmp in range(len(array)):
    total += array[tmp]
    if total * flag <= 0:
        minus += abs(total*flag)+1
        total = flag
    flag *= -1

total = 0
flag = 1
plus = 0
for tmp in range(len(array)):
    total += array[tmp]
    if total * flag <= 0:
        plus += abs(total*flag)+1
        total = flag
    flag *= -1

print(min(minus, plus))

Submission status: Accepted

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

cum1 = cum2 = 0
ans1 = ans2 = 0
for i,a in enumerate(src):
    cum1 += a
    cum2 += a
    if i%2:
        if cum1 >= 0:
            ans1 += cum1+1
            cum1 = -1
        if cum2 <= 0:
            ans2 += 1-cum2
            cum2 = 1
    else:
        if cum1 <= 0:
            ans1 += 1-cum1
            cum1 = 1
        if cum2 >= 0:
            ans2 += cum2+1
            cum2 = -1

print(min(ans1,ans2))

Submission status: Accepted

Solution code:
# coding: utf-8
# hello worldと表示する
N=int(input())
s=list(map(int,input().split()))
def f(N,s,t):
    ss=0
    w=0
    for i in range(N):
        ss+=s[i]
        if t==1:
            if ss<=0:
                w+=1-ss
                ss=1
            t=-1    
        elif t==-1:
            if ss>=0:
                w+=1+ss
                ss=-1
            t=1
    return w
t=1
a=f(N,s,t)
t=-1
b=f(N,s,t)
print(min(a,b))

Submission status: Accepted

Solution code:
import itertools

def main():
    N = int(input())
    A = tuple(map(int, input().split()))

    a = list(itertools.accumulate(A))
    func1 = (plus, minus)
    func2 = (minus, plus)

    cnt1, cnt2 = 0, 0
    offset1, offset2 = 0, 0
    for i, v in enumerate(a):
        ret = func1[i%2](v + offset1)
        offset1 += ret
        cnt1 += abs(ret)
        ret = func2[i%2](v + offset2)
        offset2 += ret
        cnt2 += abs(ret)
    print(min(cnt1, cnt2))

def plus(n):
    if n <= 0:
        x = 1 - n
        return x
    else:
        return 0

def minus(n):
    if n >= 0:
        x = -1 - n
        return x
    else:
        return 0

main()

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
cnt=cut=total=0
for i in range(n):
    total+=a[i]
    if i%2==0:
        if total<=0:
            cnt+=abs(1-total)
            total=1
    else:
        if 0<=total:
            cnt+=abs(-1-total)
            total=-1
total=0
for i in range(n):
    total+=a[i]
    if i%2==0:
        if 0<=total:
            cut+=abs(-1-total)
            total=-1
    else:
        if total<=0:
            cut+=abs(1-total)
            total=1
print(min(cut,cnt))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = [int(x) for x in input().split()]
cnt1 = 0
cnt2 = 0
total = 0
for i in range(len(a)):
    total += a[i]
    if i % 2 == 0:
        if total >= 0:
            cnt1 += 1 + total
            total -= 1 + total
    else:
        if total <= 0:
            cnt1 += 1 - total
            total += 1 - total
total = 0
for i in range(len(a)):
    total += a[i]
    if i % 2 != 0:
        if total >= 0:
            cnt2 += 1 + total
            total -= 1 + total
    else:
        if total <= 0:
            cnt2 += 1 - total
            total += 1 - total
print(min(cnt1, cnt2))

Submission status: Accepted

Solution code:
def sign(p):
    if p<0:
        return -1
    elif p>0:
        return 1
    else:
        return 0
N=int(input())
a=[int(i) for i in input().split()]
m=[0,0]
for sgn in [-1,1]:
    x=0
    S=0
    for i in range(N):
        S+=a[i]
        if sign(S)==sgn*(-1)**(i%2) or sign(S)==0:
            x+=abs(S)+1
            S=sgn*(-1)**((i+1)%2)
    m[(sgn+1)//2]=x
print(min(m))

Submission status: Accepted

Solution code:
def ops_needed(x, target):
    if abs(target - x) < abs(x):
        return 0
    else:
        return target - x

def count_ops(a, first_target):
    total_ops = 0
    accum = 0
    for i, ai in enumerate(a):
        new_accum = accum + a[i]
        if i % 2 == 1:
            ops = ops_needed(new_accum, first_target)
        else:
            ops = ops_needed(new_accum, -1 * first_target)
        total_ops += abs(ops)
        accum = new_accum + ops
    return total_ops
        

if __name__ == '__main__':
    _ = int(input())
    a = list(map(int, input().split()))
    print(min(count_ops(a, 1), count_ops(a, -1)))

Submission status: Accepted

Solution code:
def k(n, a, p):
    x = 0
    c = 0
    for i in range(n):
        s = x + a[i]
        if p and s > 0 or not p and s < 0: x = s
        else:
            c += abs(s) + 1
            if p: x = 1
            else: x = -1
        p = not p
    return c

n = int(input())
a = [int(x) for x in input().split()]
print(min(k(n, a, True), k(n, a, False)))

Submission status: Accepted

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

Submission status: Accepted

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

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

print(min(ans))

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
num,answer1= a[0],0
if a[0]==0:
	num,answer1 = 1,1
for i in range(1,n):
	if num*(num+a[i]) >= 0:
		x = (-num)//abs(num)
		answer1 += abs((x-num)-a[i])
		num = x
	else:
		num += a[i]
if a[0]==0: num,answer2 = -1,1
else: num,answer2 = -a[0]//abs(a[0]),abs(a[0]-(-a[0]//abs(a[0])))
for i in range(1,n):
	if num*(num+a[i]) >= 0:
		x = (-num)//abs(num)
		answer2 += abs((x-num)-a[i])
		num = x
	else:
		num += a[i]
print(min(answer1,answer2))

Submission status: Accepted

Solution code:
#abc059c

n=int(raw_input())
a=map(int,raw_input().split())
def f(a,s):
 ac=0
 cnt=0
 for i in xrange(n):
  ac+=a[i]
  if i%2:
   if s*ac>=0:
    cnt+=1+s*ac
    ac=-s
  else:
   if s*ac<=0:
    cnt+=1-s*ac
    ac=s
 return cnt
print min(f(a,1),f(a,-1))

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
sum=a[0]
if(sum==0):
  opp=1
  sum=1
  for i in a[1:]:
    if(sum*(sum+i)>=0):
      opp+=abs(sum+i)+1
      if(sum<0):sum=1
      else:sum=-1
    else:sum+=i
  opm=1
  sum=-1
  for i in a[1:]:
    if(sum*(sum+i)>=0):
      opm+=abs(sum+i)+1
      if(sum<0):sum=1
      else:sum=-1
    else:sum+=i
  op=min(opm,opp)
else:
  opp=0
  for i in a[1:]:
    if(sum*(sum+i)>=0):
      opp+=abs(sum+i)+1
      if(sum<0):sum=1
      else:sum=-1
    else:sum+=i
  sum=a[0]
  opm=abs(sum)+1
  if(sum>0):sum=-1
  else:sum=1
  for i in a[1:]:
    if(sum*(sum+i)>=0):
      opm+=abs(sum+i)+1
      if(sum<0):sum=1
      else:sum=-1
    else:sum+=i
  op=min(opp,opm)
    
print(op)

Submission status: Accepted

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


def judge(A,t):
    a_sum = 0
    cnt = 0
    for i in range(len(A)):
        a_sum += A[i]
        if i % 2 == 0:
            if t*a_sum  <= 0:
                cnt += 1-t*a_sum
                a_sum = t
        
        else:
            if t*a_sum >= 0:
                cnt += 1 + t*a_sum
                a_sum = -t
    
    return cnt
    
print(min(judge(A,1),judge(A,-1)))

Submission status: Accepted

Solution code:
def c_Sequence(N, A):
    total = 0  # 変化量の総量
    tmp1 = 0  # 条件を満たすために今見ている項をどれだけ変化させるか
    for i in range(N):
        total += A[i]
        # 奇数番目を正に、偶数番目を負にする(0-indexedで)
        if i % 2 == 0:
            if total >= 0:
                tmp1 += abs(-1 - total)
                total -= abs(-1 - total)
        else:
            if total <= 0:
                tmp1 += 1 - total
                total += 1 - total

    total = 0
    tmp2 = 0
    for i in range(N):
        total += A[i]
        # 奇数番目を負に、偶数番目を正にする
        if i % 2 == 0:
            if total <= 0:
                tmp2 += 1 - total
                total += 1 - total
        else:
            if total >= 0:
                tmp2 += abs(-1 - total)
                total -= abs(-1 - total)
    ans = min(tmp1, tmp2)
    return ans
N = int(input())
A = [int(i) for i in input().split()]
print(c_Sequence(N, A))

Submission status: Accepted

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


def is_positive(sumval,modify):
	if sumval <= 0:
		modify += abs(sumval - 1)
		sumval = 1

	return sumval,modify

def is_negative(sumval,modify):
	if sumval >= 0:
		modify += abs(sumval + 1)
		sumval = (-1)


	return sumval,modify

pos_sum,pos_modi = is_negative(a[0],0)
neg_sum,neg_modi = is_positive(a[0],0)

for i in range(1,N):
	tmp_sum,tmp_modi = is_positive(pos_sum + a[i],pos_modi)
	pos_sum,pos_modi = is_negative(neg_sum + a[i],neg_modi)
	neg_sum = tmp_sum
	neg_modi = tmp_modi

print(min(pos_modi,neg_modi))

Submission status: Accepted

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

gokei = 0
ans1 = 0

for i in range(N):
    gokei += A[i]
    # 偶数番目を負に
    if i % 2 == 0:
        if gokei >= 0:
            ans1 += abs(-1 - gokei)
            gokei -= abs(-1 - gokei)
    if i % 2 == 1:
        if gokei <= 0:
            ans1 += 1 - gokei
            gokei += (1 - gokei)


gokei = 0
ans2 = 0
for i in range(N):
    gokei += A[i]
    # 偶数番目を正に
    if i % 2 == 0:
        if gokei <= 0:
            ans2 += 1 - gokei
            gokei += (1 - gokei)
    else:
        if gokei >= 0:
            ans2 += abs(-1 - gokei)
            gokei -= abs(-1 - gokei)

print(min(ans1, ans2))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
https://abc059.contest.atcoder.jp/tasks/arc072_a
WA
"""
import sys
from sys import stdin
input = stdin.readline


def check_sign(n):
    if n > 0:
        return 1
    elif n < 0:
        return -1
    else:
        return 0


def solve(A):
    p_ans = 0
    n_ans = 0
    i_ans = 0

    # 最初をプラス側に振った場合の解
    total = A[0]
    prev_sign = check_sign(total)
    if prev_sign == 0:
        p_ans += 1
        total += 1
        prev_sign = 1

    for a in A[1:]:
        total += a
        sign = check_sign(total)
        if sign == 0:
            total -= prev_sign
            p_ans += 1
        elif prev_sign != sign:
            prev_sign = sign
        else:
            p_ans += (abs(total) + 1)
            if prev_sign < 0:
                total = 1
                prev_sign = 1
            else:
                total = -1
                prev_sign = -1

    # 最初をマイナス側に振った場合の解
    total = A[0]
    prev_sign = check_sign(total)
    if prev_sign == 0:
        n_ans += 1
        total -= 1
        prev_sign = -1

    for a in A[1:]:
        total += a
        sign = check_sign(total)
        if sign == 0:
            total -= prev_sign
            n_ans += 1
        elif prev_sign != sign:
            prev_sign = sign
        else:
            n_ans += (abs(total) + 1)
            if prev_sign < 0:
                total = 1
                prev_sign = 1
            else:
                total = -1
                prev_sign = -1

    # 反転させてからスタートする
    total = A[0]
    prev_sign = check_sign(total)
    if prev_sign == 0:
        i_ans += 1
        total += 1
        prev_sign = 1
    else:
        i_ans += (abs(total) + 1)
        if prev_sign > 0:
            prev_sign = -1
            total = -1
        else:
            prev_sign = 1
            total = 1

    for a in A[1:]:
        total += a
        sign = check_sign(total)
        if sign == 0:
            total -= prev_sign
            i_ans += 1
        elif prev_sign != sign:
            prev_sign = sign
        else:
            i_ans += (abs(total) + 1)
            if prev_sign < 0:
                total = 1
                prev_sign = 1
            else:
                total = -1
                prev_sign = -1

    return min(p_ans, n_ans, i_ans)


def main(args):
    n = int(input())
    A = [int(x) for x in input().split()]
    ans = solve(A)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
import math


"""
e.g. N=3 a=[-1 1 1]
 -1 2  1 :1
 -1 2 -2 :4

e.g. N=3 a=[0 0 -1]
 1  0 -1 :1
 1 -2 -1 :3
 1 -2  2 :6

 -1 0 -1 :1
 -1 2 -1 :3
 -1 2 -2 :4

e.g. N=3 a=[0 0 1]
 1  0 1 :1
 1 -2 1 :3
 1 -2 2 :4

 -1 0  1 :1
 -1 2  1 :3
 -1 2 -2 :6

e.g. N=4 a=[0 0 0 -1]
 1  0 0 -1 :1
 1 -2 0 -1 :3
 1 -2 2 -1 :5
 1 -2 2 -2 :6

 -1 0  0 -1 :1
 -1 2  0 -1 :3
 -1 2 -2 -1 :5
 -1 2 -2  2 :8

e.g. N=4 a=[0 0 0 1]
 1  0 0  1 :1
 1 -2 0  1 :3
 1 -2 2  1 :5
 1 -2 2 -2 :8

 -1 0  0 1 :1
 -1 2  0 1 :3
 -1 2 -2 1 :5
 -1 2 -2 2 :6
"""


def main2():
    """
    累積和は、偶奇,奇偶のどちらかの数列になるので２種類試す
    """
    n = int(input())
    A = list(map(int, input().split()))

    ans = float("inf")
    for sign in (1, -1):
        s, n_op = 0, 0
        for a in A:
            s += a
            # if sign == 1 and s <= 0:
            # elif sign == -1 and s >= 0:
            if s * sign <= 0:
                # 調整
                n_op += abs(s) + 1
                s = sign

            # 偶奇の入れ替え
            sign *= -1

        ans = min(ans, n_op)

    print(ans)


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

    ans = 0

    # 最初から0続きのとき、操作が少なくなるように同じ符号にする
    def sign(x):
        if x == 0:
            return 0
        return int(math.copysign(1, x))

    if a[0] == 0:
        for i in range(1, n):
            if a[i] != 0:
                sign_a = sign(a[i])
                if i % 2 == 0:
                    # odd: 1-indexed
                    a[0] = sign_a
                else:
                    a[0] = -sign_a

                ans += 1
                break
    s = a[0]

    for i in range(1, n):
        s1 = s + a[i]
        sign_s = sign(s)
        sign_s1 = sign(s1)

        if s1 == 0 or sign_s == sign_s1:
            # 逆方向に変更
            # new_s1 = -sign_s = s+a[i] + diff
            diff = -(sign_s + s + a[i])
            a[i] += diff
            ans += abs(diff)

        s += a[i]

        #print(i, a, sep="\t")
    print(ans)

if __name__ == '__main__':
    main2()

Submission status: Accepted

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

# 偶数番目を正とする
a_even = a[:]
a_even_sum = 0
curr = 0
for i in range(0,n):
	if i % 2 == 0:
		if a_even_sum + a_even[i] < 0:
			curr += abs(1 - (a_even_sum + a_even[i]))
			a_even[i] = abs(1 - a_even_sum)
		elif a_even_sum + a_even[i] == 0:
			curr += 1
			a_even[i] = 1
	else: # if i % 2 != 0
		if a_even_sum + a_even[i] > 0:
			curr += abs(-1 - (a_even_sum + a_even[i]))
			a_even[i] = -abs(-1 - a_even_sum)
		elif a_even_sum + a_even[i] == 0:
			curr += 1
			a_even[i] = -1
	a_even_sum += a_even[i]

#print(a_even,"curr:", curr, file=sys.stderr)

res = curr
# 奇数番目を正とする
a_odd = a[:]
a_odd_sum = 0
curr = 0
for i in range(0,n):
	if i % 2 == 0:
		if a_odd_sum + a_odd[i] > 0:
			curr += abs(-1 - (a_odd_sum + a_odd[i]))
			a_odd[i] = -abs(-1 - a_odd_sum)
		elif a_odd_sum + a_odd[i] == 0:
			curr += 1
			a_odd[i] = -1
	else: # if i % 2 != 0
		if a_odd_sum + a_odd[i] < 0:
			curr += abs(1 - (a_odd_sum + a_odd[i]))
			a_odd[i] = abs(1 - a_odd_sum)
		elif a_odd_sum + a_odd[i] == 0:
			curr += 1
			a_odd[i] = 1
	a_odd_sum += a_odd[i]

#print(a_odd, "curr:", curr, file=sys.stderr)

res = min(res, curr)
print(res)

Submission status: Accepted

Solution code:
n=input()
a=map(int,raw_input().split())
#print n
#print a

a1_sum=0
a1=[]
for i in a:
	a1_sum+=i
	a1.append(a1_sum)

#print a1


#+,-
b=[]
chn_val=0
for i,val in enumerate(a1):
	val+=chn_val
	if i%2==0:
		if val>0:
			pass
		else:
			t_val=1-val
			chn_val+=t_val
			b.append(t_val)
	else:
		if val<0:
			pass
		else:
			t_val=-1-val
			chn_val+=t_val
			b.append(t_val)
ans1=0
if len(b)==0:
	ans1=0
else:
	for i in b:
		ans1+=abs(i)

#-,+
b=[]
chn_val=0
for i,val in enumerate(a1):
	val+=chn_val
	if i%2==0:
		if val<0:
			pass
		else:
			t_val=-1-val
			chn_val+=t_val
			b.append(t_val)
	else:
		if val>0:
			pass
		else:
			t_val=1-val
			chn_val+=t_val
			b.append(t_val)

ans2=0
if len(b)==0:
	ans2=0
else:
	for i in b:
		ans2+=abs(i)


print min(ans1,ans2)