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

def sequence(plus_start):
    cost = 0
    cum = 0

    for i in range(N):
        tmp = cum + A[i]
        target = tmp
        if plus_start:
            if i % 2 == 0:
                if tmp <= 0:
                    target = 1
            else:
                if tmp >= 0:
                    target = -1
        else:
            if i % 2 == 0:
                if tmp >= 0:
                    target = -1
            else:
                if tmp <= 0:
                    target = 1
        diff = target - tmp
        cum += A[i] + diff
        cost += abs(diff)
    return cost

print(min(sequence(True), sequence(False)))

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# coding: utf-8

def ri():
    return int(input())

def rl():
    return list(input().split())

def rli():
    return list(map(int, input().split()))

def solve(la):
    ans = 0
    s = la[0]
    for i in range(1, len(la)):
        a = la[i]
        ns = s+a
        # print(i, s, a, ns)
        if s*ns < 0:
            s = ns
            continue
        ans += abs(ns)+1
        if ns < 0:
            ns += abs(ns)+1
        else:
            ns -= abs(ns)+1
        s = ns
    return ans

def main():
    n = ri()
    la = rli()
    first = la[0]

    ans1 = 0
    if first <= 0:
        ans1 += abs(first)+1
        la[0] = 1
    ans1 += solve(la)

    ans2 = 0
    la[0] = first
    if first >= 0:
        ans2 += abs(first)+1
        la[0] = -1
    ans2 += solve(la)
    print(min(ans1, ans2))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

j = 1
for i in range(n):
    if (aa + A[i]) * j <= 0:
        ct_1 += abs(aa + A[i]) + 1
        aa = j
    else:
        aa += A[i]
    j *= -1

aa = 0
j = -1
for i in range(n):
    if (aa + A[i]) * j <= 0:
        ct_2 += abs(aa + A[i]) + 1
        aa = j
    else:
        aa += A[i]
    j *= -1

print(min(ct_1, ct_2))

Submission status: Accepted

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

Submission status: Accepted

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

def calc(a, t):
    ans = 0
    p_flag = t < 0
    t += a
    if p_flag:
        if t < 0:
            ans = 1 - t
            t = 1
    else:
        if t >= 0:
            ans = 1 + t
            t = -1
    if t == 0:
        ans += 1
        if p_flag:
            t = 1
        else:
            t = -1

    return ans, t

ans_p = 0
t_p = 0
ans_m = 0
t_m = 0
if A[0] > 0:
    ans_p = 0
    t_p = A[0]
    ans_m = A[0]+1
    t_m = -1
elif A[0] < 0:
    ans_p = -A[0]+1
    t_p = 1
    ans_m = 0
    t_m = A[0]
else:
    ans_p = 1
    t_p = 1
    ans_m = 1
    t_m = -1

for i in range(1, N):
    a, t_p = calc(A[i], t_p)
    ans_p += a
    a, t_m = calc(A[i], t_m)
    ans_m += a

print(min(ans_p, ans_m))

Submission status: Accepted

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


ans = 0
ansans = 0
now = 0

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

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

print(min(ans, ansans))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
pm_sum = 0
pm_count = 0
mp_sum = 0
mp_count = 0
for i in range(n):
    pm_sum += a[i]
    mp_sum += a[i]
    if i % 2 == 0:
        pm_d = max(0, 1 - pm_sum)
        mp_d = max(0, mp_sum - (-1))
        pm_sum += pm_d
        mp_sum -= mp_d
    else:
        pm_d = max(0, pm_sum - (-1))
        mp_d = max(0, 1 - mp_sum)
        pm_sum -= pm_d
        mp_sum += mp_d
    pm_count += pm_d
    mp_count += mp_d
print(min(pm_count, mp_count))

Submission status: Accepted

Solution code:
# ARC072C - Sequence (ABC059C)
def main():
    n = int(input())
    lst = list(map(int, input().rstrip().split()))
    cnt = [0] * 2
    
    # start with positive number
    cur = 0
    for i, j in enumerate(lst):
        cur += j
        if i % 2 == 0 and cur <= 0:
            cnt[0] += abs(cur) + 1
            cur = 1
        elif i % 2 == 1 and cur >= 0:
            cnt[0] += abs(cur) + 1
            cur = -1

    # start with negative number
    cur = 0
    for i, j in enumerate(lst):
        cur += j
        if i % 2 == 0 and cur >= 0:
            cnt[1] += abs(cur) + 1
            cur = -1
        elif i % 2 == 1 and cur <= 0:
            cnt[1] += abs(cur) + 1
            cur = 1

    print(min(cnt))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ARC072C - Sequence (ABC059C)
def main():
    n = int(input())
    lst = list(map(int, input().rstrip().split()))
    # start with positive number
    cur, cnt_1 = 0, 0
    for i, j in enumerate(lst):
        new = cur + j
        if i % 2 == 0 and new <= 0:
            cnt_1 += abs(new) + 1
            cur = 1
        elif i % 2 == 1 and new >= 0:
            cnt_1 += abs(new) + 1
            cur = -1
        else:
            cur = new

    # start with negative number
    cur, cnt_2 = 0, 0
    for i, j in enumerate(lst):
        new = cur + j
        if i % 2 == 0 and new >= 0:
            cnt_2 += abs(new) + 1
            cur = -1
        elif i % 2 == 1 and new <= 0:
            cnt_2 += abs(new) + 1
            cur = 1
        else:
            cur = new
    print(min(cnt_1, cnt_2))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ARC072C - Sequence (ABC059C)
import sys
input = sys.stdin.readline

def main():
    n = int(input())
    lst = tuple(map(int, input().split()))
    
    # start with positive number
    cur, cnt_1 = 0, 0
    for i, j in enumerate(lst):
        new = cur + j
        if i % 2 == 0 and new <= 0:
            cnt_1 += abs(new) + 1
            cur = 1
        elif i % 2 == 1 and new >= 0:
            cnt_1 += abs(new) + 1
            cur = -1
        else:
            cur += j

    # start with negative number
    cur, cnt_2 = 0, 0
    for i, j in enumerate(lst):
        new = cur + j
        if i % 2 == 0 and new >= 0:
            cnt_2 += abs(new) + 1
            cur = -1
        elif i % 2 == 1 and new <= 0:
            cnt_2 += abs(new) + 1
            cur = 1
        else:
            cur += j
    
    print(min(cnt_1, cnt_2))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ARC072C - Sequence (ABC059C)
def main():
    n = int(input())
    lst = list(map(int, input().rstrip().split()))
    
    # start with positive number
    cur, cnt_1 = 0, 0
    for i, j in enumerate(lst):
        new = cur + j
        if i % 2 == 0 and new <= 0:
            cnt_1 += abs(new) + 1
            cur = 1
        elif i % 2 == 1 and new >= 0:
            cnt_1 += abs(new) + 1
            cur = -1
        else:
            cur += j

    # start with negative number
    cur, cnt_2 = 0, 0
    for i, j in enumerate(lst):
        new = cur + j
        if i % 2 == 0 and new >= 0:
            cnt_2 += abs(new) + 1
            cur = -1
        elif i % 2 == 1 and new <= 0:
            cnt_2 += abs(new) + 1
            cur = 1
        else:
            cur += j
    
    print(min(cnt_1, cnt_2))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
a = [int(_) for _ in input().split()]
ans1 = 0
ans2 = 0
tmp1 = 0
tmp2 = 0

flag = False
for i in a:
    tmp1 += i
    if flag and tmp1 <= 0:
        ans1 += 1-tmp1
        tmp1 = 1
    elif not flag and tmp1 >= 0:
        ans1 += tmp1+1
        tmp1 = -1
    
    if flag:
        flag = False
    else:
        flag = True

flag = True
for i in a:
    tmp2 += i
    if flag and tmp2 <= 0:
        ans2 += 1-tmp2
        tmp2 = 1
    elif not flag and tmp2 >= 0:
        ans2 += tmp2+1
        tmp2 = -1
    
    if flag:
        flag = False
    else:
        flag = True

print(min(ans1, ans2))

Submission status: Accepted

Solution code:
n = int(input())
*A, = map(int, input().split())
even = 0; odd = 0;
S = 0
for i, a in enumerate(A):
    S += a
    if i % 2 == 0 and S <= 0:
        even += -S + 1
        S = 1
    elif i % 2 == 1 and S >= 0:
        even += S + 1
        S = -1
S = 0
for i, a in enumerate(A):
    S += a
    if i % 2 == 0 and S >= 0:
        odd += S + 1
        S = -1
    elif i % 2 == 1 and S <= 0:
        odd += -S + 1
        S = 1
print(min(even, odd))

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
x,y,z=0,0,0
for i in range(n):
    x+=a[i]
    if i%2==0:
        if x>0:pass
        else:
            y+=1-x
            x=1
    else:
        if x<0:pass
        else:
            y+=x+1
            x=-1
x=0
for i in range(n):
    x+=a[i]
    if i%2==1:
        if x>0:pass
        else:
            z+=1-x
            x=1
    else:
        if x<0:pass
        else:
            z+=x+1
            x=-1
print(min(y,z))

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
num = float('inf')
for k in [0, 1]:
    s = 0
    tmp = 0
    for i in range(N):
        s += a[i]
        if i % 2 == k:
            if s <= 0:
                tmp += abs(s) + 1
                s = 1
        else:
            if s >= 0:
                tmp += abs(s) + 1
                s = -1
    num = min(tmp, num)

print(num)

Submission status: Accepted

Solution code:
from itertools import accumulate

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

a = list(accumulate(a))

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

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

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

Submission status: Accepted

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

ans1 = 0
ans2 = 0

sum1 = 0
sum2 = 0

for i in range(n):
    sum1 += a[i]
    sum2 += a[i]
    if sum1*(-1)**i>=0:
        ans1 += abs(sum1)+1
        sum1 = -(-1)**i
    if sum2*(-1)**i<=0:
        ans2 += abs(sum2)+1
        sum2 = (-1)**i

print(min(ans1,ans2))

Submission status: Accepted

Solution code:
n,*a=map(int,open(0).read().split())
pre1=0
pre2=0
ans1=0
ans2=0
for i in range(n):
    x=(-1)**(i%2)
    y=-x
    pre1+=a[i]
    pre2+=a[i]
    if x*pre1<=0:
        ans1+=abs(pre1-x)
        pre1=x
    if y*pre2<=0:
        ans2+=abs(pre2-y)
        pre2=y
print(min(ans1,ans2))

Submission status: Accepted

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

ms, ps = 0, 0
mc, pc = 0, 0
if alist[0] < 0:
  ms = alist[0]
  ps = 1
  pc = abs(ms) + 1
elif alist[0] > 0:
  ps = alist[0]
  ms = -1
  mc = ps + 1
else:
  ps = 1
  pc = 1
  ms = -1
  mc = 1

for a in alist[1:]:
  if a < 0:
    if ms < 0:
      mc += abs(a + ms) + 1
      ms = 1
    else:
      if a + ms < 0:
        ms += a
      else:
        mc += abs(a + ms) + 1
        ms = -1
    if ps < 0:
      pc += abs(a + ps) + 1
      ps = 1
    else:
      if a + ps < 0:
        ps += a
      else:
        pc += abs(a + ps) + 1
        ps = -1
  elif a > 0:
    if ms > 0:
      mc += abs(a + ms) + 1
      ms = -1
    else:
      if a + ms > 0:
        ms += a
      else:
        mc += abs(a + ms) + 1
        ms = 1
    if ps > 0:
      pc += abs(a + ps) + 1
      ps = -1
    else:
      if a + ps > 0:
        ps += a
      else:
        pc += abs(a + ps) + 1
        ps = 1
  else:
    if ms > 0:
      mc += abs(ms) + 1
      ms = -1
    else:
      mc += abs(ms) + 1
      ms = 1
    if ps > 0:
      pc += abs(ps) + 1
      ps = -1
    else:
      pc += abs(ps) + 1
      ps = 1

print(min(pc, mc))

Submission status: Accepted

Solution code:
n = int(input())

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


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

P = A[:]
pcount = 0
if P[0] <= 0:
    pcount += abs(P[0]) + 1
    P[0] = 1
for i in range(1, n):
    P[i] +=  P[i-1]
    if i % 2 == 0:
        if P[i] <= 0:
            pcount += abs(P[i]) + 1
            P[i] = 1
    else:
        if P[i] >= 0:
            pcount += abs(P[i]) + 1
            P[i] = -1



print(min(ncount, pcount))

Submission status: Accepted

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

if A[0]>0:
    s = A[0]
    ans1 = 0
else:
    s = 1
    ans1 = -A[0]+1

for i in range(1,n):
    if s*(s+A[i])>=0:
        ans1 += abs(s+A[i])+1
        if s>0:     
            s=-1
        else:
            s=1
    else:   
        s += A[i]
                  
if A[0]<0:        
    s = A[0]
    ans2 = 0
else:
    s = -1
    ans2 = A[0]+1
                    
for i in range(1,n):
    if s*(s+A[i])>=0:
        ans2 += abs(s+A[i])+1
        if s>0:
            s=-1
        else:
            s=1
    else:   
        s += A[i]
print(min(ans1,ans2))

Submission status: Accepted

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

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

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

print(min(ans1, ans2))

Submission status: Accepted

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

ans1, ans2 = 0, 0
sum_v = 0

# 最初が負の時
for i in range(len(a)):
    sum_v += a[i]
    # sumv := minus
    if i % 2 == 0:
        if sum_v >= 0:
            ans1 += abs(sum_v)+1
            sum_v = -1
    # sumv := plus
    else:
        if sum_v <= 0:
            ans1 += abs(sum_v)+1
            sum_v = 1

sum_v = 0

# 最初がplus
for i in range(len(a)):
    sum_v += a[i]
    # sumv := minus
    if i % 2 == 0:
        if sum_v <= 0:
            ans2 += abs(sum_v)+1
            sum_v = 1
    # sumv := plus
    else:
        if sum_v >= 0:
            ans2 += abs(sum_v)+1
            sum_v = -1

print (min(ans1, ans2))

Submission status: Accepted

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

# even is +
now = 0
even_count = 0
for i, a in enumerate(A):
    now += a
    if i % 2 == 0 and now <= 0:
        even_count += 1 - now
        now = 1
    elif i % 2 == 1 and now >= 0:
        even_count += abs(-1 - now)
        now = -1

# odd is +
now = 0
odd_count = 0
for i, a in enumerate(A):
    now += a
    if i % 2 == 0 and now >= 0:
        odd_count += abs(-1 - now)
        now = -1
    elif i % 2 == 1 and now <= 0:
        odd_count += 1 - now
        now = 1
print(min(even_count, odd_count))

Submission status: Accepted

Solution code:
n = input()
import numpy
a = map(int, raw_input().split())
cumsum_a = numpy.cumsum(a)
cumsum_v = 0
res1 = 0
for i, v in enumerate(cumsum_a):
  # -|0 to +
  if i % 2 == 0:
    if v + cumsum_v <= 0:
      res1 += -(v + cumsum_v) + 1
      cumsum_v += -(v + cumsum_v) + 1
  # + to -
  else:
    if v + cumsum_v >= 0:
      res1 += (v + cumsum_v) + 1
      cumsum_v += -(v + cumsum_v) - 1

cumsum_v = 0
res2 = 0
for i, v in enumerate(cumsum_a):
  # -|0 to +
  if i % 2 != 0:
    if v + cumsum_v <= 0:
      res2 += -(v + cumsum_v) + 1
      cumsum_v += -(v + cumsum_v) + 1
  # + to -
  else:
    if v + cumsum_v >= 0:
      res2 += (v + cumsum_v) + 1
      cumsum_v += -(v + cumsum_v) - 1
print min(res1, res2)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
p=[1,-1]
j=[-1,1]
c=0
l=0
d=0
e=0
for i in range(n):
    s=i%2
    c+=a[i]
    l+=a[i]
    if p[s]*c<=0:
        d+=abs(c-p[s])
        c=p[s]
    if j[s]*l<=0:
        e+=abs(l-j[s])
        l=j[s] 
print(min(e,d))
#####################################################

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
count1=a[0]
count2=a[0]
answer1=0
answer2=0

if a[0]<=0:
  answer1+=(1-a[0])
  count1=1
if a[0]>=0:
  answer2+=(1+a[0])
  count2=-1
    
for i in a[1:]:
  if (count1>0 and count1+i<0) or (count1<0 and count1+i>0):
    count1+=i
    continue
  else:
    answer1+=abs(count1+i)+1
    if count1>0:
      count1=-1
    else:
      count1=1
for i in a[1:]:
  if (count2>0 and count2+i<0) or (count2<0 and count2+i>0):
    count2+=i
    continue
  else:
    answer2+=abs(count2+i)+1
    if count2>0:
      count2=-1
    else:
      count2=1  
print(min(answer1,answer2))

Submission status: Accepted

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

calc1 = 0
calc2 = 0

L = A

if L[0] > 0 :
    su = L[0]
else :
    su = 1
    calc1 += 1 + abs(L[0])

for i in range(1,N) :
    su += L[i]
    if i % 2 :
        if su >= 0 :
            calc1 += 1 + abs(su)
            su = -1
    else :
        if su <= 0 :
            calc1 += 1 + abs(su)
            su = 1

L = A

if L[0] < 0 :
    su = L[0]
else :
    su = -1
    calc2 += 1 + abs(L[0])

for i in range(1,N) :
    su += L[i]
    if not i % 2 :
        if su >= 0 :
            calc2 += 1 + abs(su)
            su = -1
    else :
        if su <= 0 :
            calc2 += 1 + abs(su)
            su = 1
print(min(calc1,calc2))

Submission status: Accepted

Solution code:
N=int(input())

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

Pnum=0
Psum=0
if L[0]>=1:
    Pnum=L[0]
else:
    Psum=1-L[0]
    Pnum=1
for i in range(1,N):
    if i%2==1:
        if Pnum+L[i]<=-1:
            Pnum=Pnum+L[i]
        else:
            Psum+=(Pnum+L[i]+1)
            Pnum=-1
    else:
        if Pnum+L[i]>=1:
            Pnum=Pnum+L[i]
        else:
            Psum+=(1-Pnum-L[i])
            Pnum=1
            
Mnum=0
Msum=0
if L[0]<=-1:
    Mnum=L[0]
else:
    Msum=L[0]+1
    Mnum=-1
for i in range(1,N):
    if i%2==0:
        if Mnum+L[i]<=-1:
            Mnum=Mnum+L[i]
        else:
            Msum+=(Mnum+L[i]+1)
            Mnum=-1
    else:
        if Mnum+L[i]>=1:
            Mnum=Mnum+L[i]
        else:
            Msum+=(1-Mnum-L[i])
            Mnum=1

print(min(Psum,Msum))

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
p=[1,-1]
j=[-1,1]
c=0
l=0
d=0
e=0
for i in range(n):
    s=i%2
    c+=a[i]
    l+=a[i]
    if p[s]*c<=0:
        d+=abs(c-p[s])
        c=p[s]
    if j[s]*l<=0:
        e+=abs(l-j[s])
        l=j[s] 
print(min(e,d))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Feb  9 14:32:30 2019

@author: shinjisu
"""


# arc 072 c sequence
def getInt(): return int(input())


def getIntList(): return [int(x) for x in input().split()]


def db(x):
    global debug
    if debug:
        print(x)


def count(N, A, initSum):
    # global N, A
    sm = initSum
    cnt = abs(sm-A[0])
    db(cnt)
    for i in range(N-1):
        nextSum = sm + A[i+1]
        db((sm, nextSum))
        if sm > 0 and nextSum >= 0:
            cnt += abs(nextSum)+1
            sm = -1
        elif sm < 0 and nextSum <= 0:
            cnt += abs(nextSum)+1
            sm = 1
        else:
            sm = nextSum
        db(cnt)
    return cnt


def probC():
    N = getInt()
    A = getIntList()
    db((N, A))
    if A[0] != 0:
        initSum = A[0]
    else:
        initSum = 1
    cnt1 = count(N, A, initSum)
    db(('a[0] or +1: ', cnt1))
    if initSum > 0:
        cnt2 = count(N, A, -1)
    else:
        cnt2 = count(N, A, 1)
    db(('反対符号 or -1: ', cnt2))
    return min(cnt1, cnt2)


debug = False
print(probC())

Submission status: Accepted

Solution code:
n=input()
a=[int(i) for i in input().split()]
def chk(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(chk(a,True),chk(a,False)))

Submission status: Accepted

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

ans = 0


def func(A, flag):
    ans = 0
    acc = 0
    for i in range(0, N):
        acc = acc+A[i]
        if i % 2 == flag:
            if acc >= 0:
                ans += 1+acc
                acc = -1
        else:
            if acc <= 0:
                ans += 1-acc
                acc = 1
    return ans


print(min(func(A, 0), func(A, 1)))

Submission status: Accepted

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

ans = 0


def func(A, flag):
    l = [0]*N
    ans = 0
    acc = 0
    for i in range(N):
        l[i] = A[i]
    for i in range(0, N):
        acc = acc+l[i]
        if i % 2 == flag:
            if acc >= 0:
                l[i] = l[i] - (1 + acc)
                ans += 1+acc
                acc = -1
        else:
            if acc <= 0:
                l[i] = l[i]+(1-acc)
                ans += 1-acc
                acc = 1
    return ans


print(min(func(A, 0), func(A, 1)))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
ans1 = abs(a[0]-1)
ans2 = abs(a[0]+1)
ans3 = 0
def f(x,y):
  for i in range(1,n):
    if y > 0:
      y += a[i]
      if y < 0:
        continue
      else:
        x += abs(y+1)
        y = -1
    else:
      y += a[i]
      if y > 0:
        continue
      else:
        x += abs(y-1)
        y = 1
  return x
if a[0] == 0:
  ans3 = float("inf")
else:
  ans3 = f(ans3,a[0])
print(min(ans3,f(ans1,1),f(ans2,-1)))

Submission status: Accepted

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


def f(isPlus):
    sum = 0
    ret = 0
    for a in A:
        sum += a
        if isPlus:
            if sum <= 0:
                ret += abs(sum) + 1
                sum = 1
        else:
            if sum >= 0:
                ret += abs(sum) + 1
                sum = -1

        isPlus = not isPlus
    return ret


a1 = f(True)
a2 = f(False)
print(min(a1, a2))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split(' ')))
ans1 = 0
ans2 = 0
sum1 = 0
sum2 = 0
flag = True
for i in range(len(A)):
    sum1 = sum1 + A[i]
    sum2 = sum2 + A[i]
    flag = not(flag)
    if flag:
        if sum1 > 0:
            pass
        else:
            ans1 += abs(1-sum1)
            sum1 = 1
        if sum2 < 0:
            pass
        else:
            ans2 += abs(-1-sum2)
            sum2 = -1
    else:
        if sum2 > 0:
            pass
        else:
            ans2 += abs(1-sum2)
            sum2 = 1
        if sum1 < 0:
            pass
        else:
            ans1 += abs(-1-sum1)
            sum1 = -1
print(min(ans1, ans2))

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
s=0
t=0
a=0
b=0
for i in range(N):
    s+=A[i]
    if i%2==0:
        if s>=0:
            a+=s+1
            s=-1
    if i%2!=0:
        if s<=0:
            a+=-s+1
            s=1
for j in range(N):
    t+=A[j]
    if j%2!=0:
        if t>=0:
            b+=t+1
            t=-1
    if j%2==0:
        if t<=0:
            b+=-t+1
            t=1
print(min(a,b))

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
_ = int(input())
A = list(map(int, input().split()))


def solve(i, s, k, sign):
    if i >= len(A):
        return k

    if sign * (s + A[i]) > 0:
        return solve(i + 1, s + A[i], k, sign * -1)
    else:
        return solve(i + 1, sign, k + abs(s + A[i]) + 1, sign * -1)


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

Submission status: Accepted

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

ret = float('inf')

sign = 1
s = 0
k = 0
for a in A:
    if sign * (s + a) > 0:
        s += a
    else:
        k += abs(s + a) + 1
        s = sign
    sign *= -1
ret = min(ret, k)

sign = -1
s = 0
k = 0
for a in A:
    if sign * (s + a) > 0:
        s += a
    else:
        k += abs(s + a) + 1
        s = sign
    sign *= -1

ret = min(ret, k)


print(ret)

Submission status: Accepted

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

#positive
pos_count = 0
sign = 1
cumsum = 0
for a in A:
    cumsum += a
    if cumsum * sign <= 0:
        pos_count += abs(sign - cumsum)
        cumsum = sign
    sign *= -1

#negative
neg_count = 0
sign = -1
cumsum = 0
for a in A:
    cumsum += a
    if cumsum * sign <= 0:
        neg_count += abs(sign - cumsum)
        cumsum = sign
    sign *= -1

print(min(pos_count, neg_count))

Submission status: Accepted

Solution code:
def inpl(): return [int(i) for i in input().split()]
def minich(A, plus):
    ans = 0
    back = 0
    for i in A:
        if plus:
            if back + i < 0:
                back = back + i
            else:
                ans += back + i + 1
                back = -1
        else:
            if back + i > 0:
                back = back + i
            else:
                ans += 1 - back - i
                back = 1
        plus = not plus
    return ans
N = int(input())
A = inpl()
print(min(minich(A, True), minich(A, False)))

Submission status: Accepted

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

def calc(tmp, cnt=0):
    for i in a[1:]:
        if tmp < 0:
            tmp = tmp + i
            if tmp <= 0:
                cnt += -tmp + 1
                tmp = 1
        elif tmp > 0:
            tmp = tmp + i
            if tmp >= 0:
                cnt += tmp + 1
                tmp = -1
    return cnt

if a[0]:
    hugou = 1 if a[0] > 0 else -1
    print(min(calc(a[0]), calc(-hugou, abs(a[0]) + 1)))
else:
    print(min(calc(1, 1), calc(-1, 1)))

Submission status: Accepted

Solution code:
n=int(input().strip())
a=list(map(int, input().split()))
su=0
ans1=0
ans2=0
for i in range(n):
    if i%2==0:
        su+=a[i]
        if su<=0:
            ans1+=abs(su-1)
            su=1
    if i%2==1:
        su+=a[i]
        if su>=0:
            ans1+=abs(su+1)
            su=-1
su=0
for i in range(n):
    if i%2==0:
        su+=a[i]
        if su>=0:
            ans2+=abs(su+1)
            su=-1
    if i%2==1:
        su+=a[i]
        if su<=0:
            ans2+=abs(su-1)
            su=1

print(min(ans1,ans2))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
r = [int(i) for i in input().split()]
for i in range(1,n):
    r[i] += r[i-1]

a = r.copy()
ans_a = 0; change_v = 0
for i in range(n):
    a[i] += change_v
    if i%2 == 1:
        ans_a += max(0,1-a[i])
        change_v += max(0,1-a[i])
    else:
        ans_a += max(0,a[i]+1)
        change_v -= max(0,a[i]+1)

a = r.copy()
ans_b = 0; change_v = 0
for i in range(n):
    a[i] += change_v
    if i%2 == 0:
        ans_b += max(0,1-a[i])
        change_v += max(0,1-a[i])
    else:
        ans_b += max(0,a[i]+1)
        change_v -= max(0,a[i]+1)

print(min(ans_a,ans_b))