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:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(300000)

def solve(n: int, a: "List[int]"):
    tmp = 0
    s = 0
    for i, v in enumerate(a):
        s += v
        if i % 2 == 0 and s <= 0:
            tmp += abs(s) + 1
            s = 1
        elif i % 2 != 0 and s >= 0:
            tmp += abs(s) + 1
            s = -1
    ret = tmp
    tmp = 0
    s = 0
    for i, v in enumerate(a):
        s += v
        if i % 2 == 0 and s >= 0:
            tmp += abs(s) + 1
            s = -1
        elif i % 2 != 0 and s <= 0:
            tmp += abs(s) + 1
            s = 1
    ret = min(ret, tmp)
    print(ret)


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    n = int(next(tokens))  # type: int
    a = [ int(next(tokens)) for _ in range(n) ]  # type: "List[int]"
    solve(n, a)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(300000)

def solve(n: int, a: "List[int]"):
    tmp = 0
    s = 0
    for i, v in enumerate(a):
        s += v
        if i % 2 == 0 and s <= 0:
            tmp += abs(s) + 1
            s = 1
        elif i % 2 != 0 and s >= 0:
            tmp += abs(s) + 1
            s = -1
    ret = tmp
    tmp = 0
    s = 0
    for i, v in enumerate(a):
        s += v
        if i % 2 == 0 and s >= 0:
            tmp += abs(s) + 1
            s = -1
        elif i % 2 != 0 and s <= 0:
            tmp += abs(s) + 1
            s = 1
    ret = min(ret, tmp)
    print(ret)

def _solve(n: int, a: "List[int]"):
    o, e = 0, 0
    tmp = 0
    for i, v in enumerate(a):
        if i % 2 == 0:
            e += v
            if e < 0:
                tmp += abs(e) + 1
                e = 1
            else:
                tmp += max(0, abs(o) - abs(e) + 1)
                e += tmp
        else:
            o += v
            if o > 0:
                tmp += abs(o) + 1
                o = -1
            else:
                tmp += max(0, abs(e) - abs(o) + 1)
                o -= tmp
    ret = tmp

    o, e = 0, 0
    tmp = 0
    for i, v in enumerate(a):
        if i % 2 == 0:
            e += v
            if e > 0:
                tmp += abs(e) + 1
                e = -1
            else:
                tmp += max(0, abs(o) - abs(e) + 1)
                e -= tmp
        else:
            o += v
            if o < 0:
                tmp += abs(o) + 1
                o = 1
            else:
                tmp += max(0, abs(e) - abs(o) + 1)
                o += tmp
    ret = min(ret, tmp)
    print(ret)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    n = int(next(tokens))  # type: int
    a = [ int(next(tokens)) for _ in range(n) ]  # type: "List[int]"
    solve(n, a)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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


print(min(ans_plus, ans_minus))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
cnt1 = 0
now1 = 0
cnt2 = 0
now2 = 0
#+-+-
for i in range(n):
    if i%2==0:
        now1 += a[i]
        if now1>0:
            pass
        else:
            cnt1 += 1-now1
            now1 =1
    else:
        now1 +=a[i]
        if now1<0:
            pass
        else:
            cnt1 += -(-1-now1)
            now1 = -1
#-+-+
#+-+-
for i in range(n):
    if i%2==1:
        now2 += a[i]
        if now2>0:
            pass
        else:
            cnt2 += 1-now2
            now2 =1
    else:
        now2 +=a[i]
        if now2<0:
            pass
        else:
            cnt2 += -(-1-now2)
            now2 = -1

print(min(cnt1,cnt2))

Submission status: Accepted

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

ans=10**20
for flg in (-1,1):
    c=0
    s=0
    for a in a_list:
        s+=a
        if s==0:
            c+=1
            s=flg
        if s//abs(s)!=flg:
            c+=abs(s)+1
            s=flg
        flg*=-1
    ans=min(ans,c)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int,input().split()))
b = [ a[i] for i in range(N)]

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

ans2 = 0
s = 0
for i in range(N):
    s += b[i]
    if i % 2 == 0 and s >= 0:
        b[i] -= abs(-1 - s)
        ans2 += abs(-1-s)
        s = -1
    elif i % 2 == 1 and s <= 0:
        b[i] += abs(1 - s)
        ans2 += abs(1 - s)
        s = 1

print(min(ans,ans2))

Submission status: Accepted

Solution code:
n = int(input())
a = [int(_) for _ in input().split()]
even_p = 0
even_n = 0
odd_p = 0
odd_n = 0

cnt = 0
# a[0] positive ver.
if a[0] > 0:
    sum_i = a[0]
else:
    cnt += abs(a[0]-1)
    sum_i = 1

for i in range(1, n):
    if sum_i > 0:
        if sum_i + a[i] < 0:
            sum_i += a[i]
        else:
            cnt += abs(a[i]+sum_i+1)
            sum_i = -1
    elif sum_i < 0:
        if sum_i + a[i] > 0:
            sum_i += a[i]
        else:
            cnt += abs(a[i]+sum_i-1)
            sum_i = 1
ans = cnt

cnt = 0
# a[0] negative ver.
if a[0] < 0:
    sum_i = a[0]
else:
    cnt += abs(a[0]+1)
    sum_i = -1

for i in range(1, n):
    if sum_i > 0:
        if sum_i + a[i] < 0:
            sum_i += a[i]
        else:
            cnt += abs(a[i]+sum_i+1)
            sum_i = -1
    elif sum_i < 0:
        if sum_i + a[i] > 0:
            sum_i += a[i]
        else:
            cnt += abs(a[i]+sum_i-1)
            sum_i = 1

print(min(ans, cnt))

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
ans=10**30
for t in range(2):
    a=0
    s=0
    for i in range(n):
        a+=A[i]
        if t and a<=0:
            s+=-a+1
            a=1
            t=0
        elif not(t) and a>=0:
            s+=a+1
            a=-1
            t=1
        else:
            t=1-t
    ans=min(s,ans)
print(ans)

Submission status: Accepted

Solution code:
# ARC072C - Sequence (ABC059C)
def seq_modifier(lst: list, flg: bool) -> int:  # true: positive
    """
    ARC072C - Sequence
    https://atcoder.jp/contests/abc072/tasks/arc072_a
    """
    cur, cnt = 0, 0
    for i in lst:
        cur += i
        if flg and cur <= 0:
            cnt += abs(cur) + 1
            cur = 1
        elif not flg and cur >= 0:
            cnt += abs(cur) + 1
            cur = -1
        flg = not flg
    return cnt


def main():
    n = int(input())
    lst = list(map(int, input().rstrip().split()))
    print(min(seq_modifier(lst, True), seq_modifier(lst, False)))


if __name__ == "__main__":
    main()

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:
n = int(input())
a = list(map(int,input().split()))

ans = [0,0]
cum = [0,0]
sn = [1,-1]

for i, aa in enumerate(a):
    for j in range(2):
        if (-1)**i * sn[j] * aa > 0 and abs(aa) > abs(cum[j]):
            cum[j] += aa
            continue
        if cum[j]*aa < 0:
            ans[j] += abs(cum[j]) - abs(aa) + 1
        else:
            ans[j] += abs(cum[j]) + abs(aa) + 1
        cum[j] = (-1)**i * sn[j]
    
print(min(ans))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
d1 = [0] * (n + 1)
d2 = [0] * (n + 1)
ans1 = 0
ans2 = 0
n1 = 0
n2 = 0
# + - + - + - の順番
for i in range(n):
    n1 += a[i]
    if(i % 2 == 0 and n1 <= 0):
        ans1 += abs(n1) + 1
        n1 = 1
    if(i % 2 == 1 and n1 >= 0):
        ans1 += abs(n1) + 1
        n1 = -1

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

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

Submission status: Accepted

Solution code:
n = int(input())

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

ans = 0
cnt = 0

s = 1
for i in range(n):
    ans += li[i]
    if ans <= 0 and s == -1:
        cnt += abs(ans) + 1
        ans = 1
    if ans >= 0 and s == 1:
        cnt += abs(ans) + 1
        ans = -1
    s *= -1
    
res1 = cnt


ans = 0
cnt = 0

    
s = -1
for i in range(n):
    ans += li[i]
    if ans <= 0 and s == -1:
        cnt += abs(ans) + 1
        ans = 1
    if ans >= 0 and s == 1:
        cnt += abs(ans) + 1
        ans = -1
    s *= -1
    
res2 = cnt

print(min(res1,res2))

Submission status: Accepted

Solution code:
N = int(input())
*A, = map(int, input().split())
ans1 = 0
sum1 = 0
for i, a in enumerate(A):
    sum1 += a
    if i % 2 == 0 and sum1 <= 0:
        ans1 += -sum1 + 1
        sum1 = 1
    elif i % 2 == 1 and sum1 >= 0:
        ans1 += sum1 + 1
        sum1 = -1
ans2 = 0
sum2 = 0
for i, a in enumerate(A):
    sum2 += a
    if i % 2 == 1 and sum2 <= 0:
        ans2 += -sum2 + 1
        sum2 = 1
    elif i % 2 == 0 and sum2 >= 0:
        ans2 += sum2 + 1
        sum2 = -1
print(min(ans1, ans2))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
ac = 0
ans1 = 0
for i in range(n):
    ac += a[i]
    if ac * (-1) ** i > 0:
        continue
    else:
        ans1 += abs(ac) + 1
        ac = (-1) ** i

ac = 0
ans2 = 0
for i in range(n):
    ac += a[i]
    if ac * (-1) ** i < 0:
        continue
    else:
        ans2 += abs(ac) + 1
        ac = (-1) ** (i + 1)

print(min(ans1, ans2))

Submission status: Accepted

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

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

answer = min(F(A,1), F(A,-1))
print(answer)

Submission status: Accepted

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

# + - + - ...
cost1, sum1 = 0, 0
for i, a in enumerate(A):
	sum1 += a
	#print(sum1, cost1)
	if not i % 2:
		if sum1 <= 0:
			cost1 += (1 - sum1)
			sum1 = 1
	else:
		if 0 <= sum1:
			cost1 += (1 + sum1)
			sum1 = -1
#print(sum1, cost1)

# - + - + ...
cost2, sum2 = 0, 0
for i, a in enumerate(A):
	sum2 += a
	if i % 2:
		if sum2 <= 0:
			cost2 += (1 - sum2)
			sum2 = 1
	else:
		if 0 <= sum2:
			cost2 += (1 + sum2)
			sum2 = -1

print(min(cost1, cost2))

Submission status: Accepted

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

def check(TF):
    wa = 0
    flag = TF
    count = 0
    for i in a:
        if flag: #正にしたい時  
            if wa + i> 0:
                wa += i
            else:
                count += abs(wa) + 1 - i
                wa = 1
            flag = False
        else: #負にしたい場合
            if wa + i < 0:
                wa += i
            else:
                count += abs(wa) + 1  + i
                wa = -1
            flag = True
    return count

# print (check(True))
# print (check(False))
            
print (min(check(True), check(False)))

Submission status: Accepted

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

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

    if a[0]>0:
        sm=-1
    else:
        sm=1
    ans[1]+=abs(a[0])+1
    for i in range(n-1):
        sm1=sm+a[i+1]
        if sm*sm1>=0:
            ans[1]+=abs(sm1)+1
            if sm>0:
                sm=-1
            else:
                sm=1
        else:
            sm=sm1
if a[0]==0:
    sm=1
    ans[0]+=1
    for i in range(n-1):
        sm1=sm+a[i+1]
        if sm*sm1>=0:
            ans[0]+=abs(sm1)+1
            if sm>0:
                sm=-1
            else:
                sm=1
        else:
            sm=sm1

    sm=-1
    ans[1]+=1
    for i in range(n-1):
        sm1=sm+a[i+1]
        if sm*sm1>=0:
            ans[1]+=abs(sm1)+1
            if sm>0:
                sm=-1
            else:
                sm=1
        else:
            sm=sm1
print(min(ans))

Submission status: Accepted

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

Submission status: Accepted

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

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

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

print(min(ans, c))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from itertools import accumulate
n = int(input())
a = list(map(int,input().split()))
aa = list(accumulate(a))
cnt1 = 0
cnt2 = 0
#+はじまり
modify_sum1 = 0
start1 = 1
for ai in aa:
    cur_val1 = (ai + modify_sum1)*start1
    if cur_val1 <= 0:
        cnt1+=1 - cur_val1
        modify_sum1+=(1 - cur_val1)*start1
    start1 *= (-1)
#-はじまり
modify_sum2 = 0
start2 = -1
for ai in aa:
    cur_val2 = (ai + modify_sum2)*start2
    if cur_val2 <= 0:
        cnt2+=1 - cur_val2
        modify_sum2+=(1 - cur_val2)*start2
    start2 *= (-1)
print(min([cnt1,cnt2]))

Submission status: Accepted

Solution code:
from sys import stdin
N = int(stdin.readline().rstrip())
A = [int(_) for _ in stdin.readline().rstrip().split()]

def solve(s):
  ans, tmp = 0, 0
  for i in range(N):
    tmp += A[i]
    if tmp * s <= 0:
      ans += abs(tmp - s)
      tmp = s
    s *= -1
  return ans
print(min(solve(1), solve(-1)))

Submission status: Accepted

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

res = []
for start in [1, -1]:

    ans = 0
    _a = []
    prev_sign = start
    for i in range(0, N):
        c = a[i]
        if c >= 0 and prev_sign > 0:
            ans += abs(c - (-1))
            c = -1
        elif c <= 0 and prev_sign < 0:
            ans += abs(c - 1)
            c = 1
        if c > 0:
            prev_sign = 1
        else:
            prev_sign = -1
        _a.append(c)

    __a = [_a[0]]
    acm_sum = _a[0]
    for i in range(1, N):
        c = _a[i]
        if abs(acm_sum) >= abs(c):
            if c < 0:
                c = -1*(abs(acm_sum)+1)
            else:
                c = abs(acm_sum)+1
        acm_sum += c
        ans += abs(c - _a[i])
        __a.append(c)
    res.append(ans)

print(min(res))

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
#pattern1
temp = a[0]
ans1 = 0
if temp <= 0:
    ans1 += abs(temp) + 1
    temp = 1
for i in range(1, n):
    if temp*(temp + a[i]) < 0:
        temp += a[i]
    else:
        if temp > 0:
            ans1 += (temp + a[i] + 1)
            temp = -1
        elif temp < 0:
            ans1 += abs(temp + a[i]) + 1
            temp = 1
#pattern2
temp = a[0]
ans2 = 0
if temp >= 0:
    ans2 += abs(temp) + 1
    temp = -1
for i in range(1, n):
    if temp*(temp + a[i]) < 0:
        temp += a[i]
    else:
        if temp > 0:
            ans2 += (temp + a[i] + 1)
            temp = -1
        elif temp < 0:
            ans2 += abs(temp + a[i]) + 1
            temp = 1
ans = min(ans1, ans2)
print(ans)

Submission status: Accepted

Solution code:
N = input()
a = list(map(int, input().split()))
 
def f(x):
  cnt = 0
  cur = 0
  for ai in a:
    cur += ai
    cur *= x
    if cur <= 0:
      cnt += 1 - cur
      cur = 1
    cur *= x
    x *= -1
  return cnt
 
print(min(f(1), f(-1)))

Submission status: Accepted

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

N = int(input())
A = list(map(int, input().split()))
cnt1 = 0
sum1 = 0
for i in range(0, N):
	sum1 += A[i]
	if i%2 == 0:
		if sum1 < 1:
			d = 1 - sum1
			cnt1 += d
			sum1 += d
	else:
		if sum1 > -1:
			d = sum1 + 1
			cnt1 += d
			sum1 -= d
cnt2 = 0
sum2 = 0
for i in range(0, N):
	sum2 += A[i]
	if i%2 == 0:
		if sum2 > -1:
			d = sum2 + 1
			cnt2 += d
			sum2 -= d
	else:
		if sum2 < 1:
			d = 1 - sum2
			cnt2 += d
			sum2 += d
print(smaller(cnt1, cnt2))

Submission status: Accepted

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

even_cnt = 0
odd_cnt = 0
even_opr = 0
odd_opr = 0
for i in a:
    stim.append(stim[-1]+i)
stim = stim[1:]
for ind,x in enumerate(stim):
    if(ind%2==0):
        if(x+odd_opr>0):
            odd_cnt += abs(-1-(x+odd_opr))
            odd_opr += -1-(x+odd_opr)
        elif(x+odd_opr==0):
            odd_opr -= 1
            odd_cnt += 1
        if(x+even_opr<0):
            even_cnt += abs(1-(x+even_opr))
            even_opr += 1-(x+even_opr)
        elif(x+even_opr==0):
            even_opr += 1
            even_cnt += 1
    else:
        if(x+odd_opr<0):
            odd_cnt += abs(1-(x+odd_opr))
            odd_opr += 1-(x+odd_opr)
        elif(x+odd_opr==0):
            odd_opr += 1
            odd_cnt += 1
        if(x+even_opr>0):
            even_cnt += abs(-1-(x+even_opr))
            even_opr += -1-(x+even_opr)
        elif(x+even_opr==0):
            even_opr -= 1
            even_cnt += 1
print(min(even_cnt,odd_cnt))

Submission status: Accepted

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


N = int(input())
An = list(map(int, input().split()))
ans = [0,0]

asum = 0
for i in range(N):
  asum += An[i]
  if asum*(-1)**i < 1:
    ans[0] += 1 - asum*(-1)**i
    asum = (-1)**i

asum = 0
for i in range(N):
  asum += An[i]
  if asum*(-1)**(i+1) < 1:
    ans[1] += 1 - asum*(-1)**(i+1)
    asum = (-1)**(i+1)

print(min(ans))

Submission status: Accepted

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

def dfs(sign):
    cnt=0
    tmp=0
    for a in A:
        tmp+=a
        if tmp*sign>=0:
            cnt+=abs(tmp)+1
            tmp=-sign
        sign*=-1
    return cnt

print(min(dfs(1),dfs(-1)))

# copy

Submission status: Accepted

Solution code:
def inpl(): return list(map(int, input().split()))
N = int(input())
A = inpl()
# +からのstart
S = [0]*N
tmp = max(0, 1 - A[0])
S[0] = max(1, A[0])
for i in range(1, N):
    if i%2:
        x = 1 + S[i-1] + A[i]
        if x > 0:
            tmp += x
            S[i] = -1
        else:
            S[i] = S[i-1] + A[i]
    else:
        x = 1 - S[i-1] - A[i]
        if x > 0:
            tmp += x
            S[i] = 1
        else:
            S[i] = S[i-1] + A[i]
ans = tmp*1

# -スタート
S = [0]*N
tmp = max(0, 1 + A[0])
S[0] = min(-1, A[0])
for i in range(1, N):
    if i%2 == 0:
        x = 1 + S[i-1] + A[i]
        if x > 0:
            tmp += x
            S[i] = -1
        else:
            S[i] = S[i-1] + A[i]
    else:
        x = 1 - S[i-1] - A[i]
        if x > 0:
            tmp += x
            S[i] = 1
        else:
            S[i] = S[i-1] + A[i]
ans = min(tmp, ans)
print(ans)

Submission status: Accepted

Solution code:
# 最初はa[0]がプラス、マイナスで場合分けしていたが、それだと最小にならない可能性があるため
# 偶数番目の和をプラス、奇数番目の和をプラスにする場合をやって最小になるほうを結果として出す

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

cnt = [0,0]

for i in [0,1]:
    tmp_sum = 0
    for index, value in enumerate(a):
        tmp_sum += value
        if (i + index) % 2 == 0:
            if tmp_sum <= 0:
                cnt[i] += abs(tmp_sum) + 1
                tmp_sum += abs(tmp_sum) + 1
        else:
            if tmp_sum >= 0:
                cnt[i] += tmp_sum + 1
                tmp_sum -= tmp_sum + 1


print(min(cnt))

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  n=I()
  l=LI()

  sm=0
  add1=0
  f=1
  for i in range(n):
    sm+=l[i]
    if f==1:
      if sm<=0:
        add1+=abs(sm)+1
        sm=1
      f=-1
    else:
      if sm>=0:
        add1+=abs(sm)+1
        sm=-1
      f=1

  sm=0
  add2=0
  f=-1
  for i in range(n):
    sm+=l[i]
    if f==1:
      if sm<=0:
        add2+=abs(sm)+1
        sm=1
      f=-1
    else:
      if sm>=0:
        add2+=abs(sm)+1
        sm=-1
      f=1

  return min(add1,add2)

print(main())

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = [0, 0]
for i in [0, 1]:
  total = 0
  for idx, val in enumerate(a):
    total += val
    if (i + idx) % 2 == 0:
      if total >= 0:
        ans[i] += total + 1
        total = -1
    else:
      if total <= 0:
        ans[i] += 1-total
        total = 1
print(min(ans))

Submission status: Accepted

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

ans = 10 ** 20 + 1
for j in range(2):
    cnt= 0
    ruiseki = [0] * N
    ruiseki[0] = A[0]
    if j == 0:
        if A[0] <= 0:
            ruiseki[0] = 1
            cnt += 1 - A[0]
        #print(j,ruiseki[0],cnt)
    else:
        if A[0] >= 0:
            ruiseki[0] = -1
            cnt += 1 + A[0]
        #print(j,ruiseki[0],cnt)
    for i in range(1,N):
        ruiseki[i] = ruiseki[i-1]+A[i]
        totalp = 0
        if ruiseki[i] * ruiseki[i-1] >0:
            totalp= (int(ruiseki[i])>0)*(-1)*(1 + ruiseki[i]) +  (int(ruiseki[i])<0) * (1 - ruiseki[i])

        elif ruiseki[i] == 0:
            if ruiseki[i-1] > 0:
                totalp = -1
            else:
                totalp = 1
        ruiseki[i] += totalp 
        cnt += abs(totalp)
        #print(ruiseki,totalp,cnt)
    ans = min(ans,cnt)
        
print(ans)

Submission status: Accepted

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

tmp_sum = 0
diffs_by_plus = 0
for i in range(n):
    min_val = (-1) ** i  # start by plus
    tmp_sum += As[i]
    if i % 2 == 0 and tmp_sum <= 0:
        diffs_by_plus += abs(min_val - tmp_sum)
        tmp_sum = min_val
    elif i % 2 != 0 and tmp_sum >= 0:
        diffs_by_plus += abs(min_val - tmp_sum)
        tmp_sum = min_val


tmp_sum = 0
diffs_by_minus = 0
for i in range(n):
    min_val = (-1) ** (i + 1)  # start by plus
    tmp_sum += As[i]
    if i % 2 != 0 and tmp_sum <= 0:
        diffs_by_minus += abs(min_val - tmp_sum)
        tmp_sum = min_val
    elif i % 2 == 0 and tmp_sum >= 0:
        diffs_by_minus += abs(min_val - tmp_sum)
        tmp_sum = min_val


print(min(diffs_by_minus,diffs_by_plus))

Submission status: Accepted

Solution code:
n, *a = map(int, open(0).read().split())
results = []
for sign in [1, -1]:
    ans = 0
    acc = 0
    for v in a:
        acc += v
        if acc * sign <= 0:
            ans += abs(acc - sign)
            acc = sign
        sign *= -1
    results.append(ans)

print(min(results))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def II(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  n=II()
  l=LI()

  ad1=0
  f=True
  sm=0
  for x in l:
    if f:
      sm+=x
      if sm<=0:
        ad1+=abs(sm)+1
        sm=1
      f=False
    else:
      sm+=x
      if sm>=0:
        ad1+=abs(sm)+1
        sm=-1
      f=True

  ad2=0
  f=False
  sm=0
  for x in l:
    if not f:
      sm+=x
      if sm>=0:
        ad2+=abs(sm)+1
        sm=-1
      f=True
    else:
      sm+=x
      if sm<=0:
        ad2+=abs(sm)+1
        sm=1
      f=False

  return min(ad1,ad2)

print(main())

Submission status: Accepted

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

A = a[0]
asum = [A]
if A == 0:
    ansp,ansm,tempp,tempm = 1,1,1,-1
elif A < 0:
    ansp,ansm,tempp,tempm = -A + 1, 0, -A + 1, 0
else:
    ansp,ansm,tempp,tempm = 0, A + 1, 0, -A - 1

for i in range(1,N):
    A = asum[i-1] + a[i]
    asum += [A]

    A1 = A + tempp
    if A1 == 0:
        ansp += 1
        tempp += ((i%2)*2-1)
    elif i%2==0 and A1 < 0:
        ansp += -A1 + 1
        tempp += -A1 + 1
    elif i%2!=0 and A1 > 0:
        ansp += A1 + 1
        tempp += -A1 - 1

    A2 = A + tempm
    if A2 == 0:
        ansm += 1
        tempm += -((i%2)*2-1)
    elif i%2!=0 and A2 < 0:
        ansm += -A2 + 1
        tempm += -A2 + 1
    elif i%2==0 and A2 > 0:
        ansm += A2 + 1
        tempm += -A2 - 1

print(min(ansp,ansm))

Submission status: Accepted

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

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

    """考え方
    ruiseki[i] := i番目までの累積和

    ・ruiseki[0]を+にするパターン
    ・ruiseki[0]を-にするパターン

    の2通りを求め、小さい方を答えにする
    """

    # ruiseki[0]を+にするパターン
    ans1 = 0
    ruiseki = [0] * N
    ruiseki[0] = A[0]
    if ruiseki[0] <= 0:
        diff = abs(ruiseki[0] - 1)
        ans1 += diff
        ruiseki[0] = 1

    for i in range(1, N):
        ruiseki[i] = ruiseki[i-1] + A[i]

        if i%2 != 0:
            # iが奇数
            if ruiseki[i] >= 0:
                diff = abs(ruiseki[i] - (-1))
                ans1 += diff
                ruiseki[i] = -1
        else:
            # iが偶数
            if ruiseki[i] <= 0:
                diff = abs(ruiseki[i] - 1)
                ans1 += diff
                ruiseki[i] = 1
    
    # ruiseki[0]を-にするパターン
    ans2 = 0
    ruiseki = [0] * N
    ruiseki[0] = A[0]
    if ruiseki[0] >= 0:
        diff = abs(ruiseki[0] - (-1))
        ans2 += diff
        ruiseki[0] = -1

    for i in range(1, N):
        ruiseki[i] = ruiseki[i-1] + A[i]

        if i%2 != 0:
            # iが奇数
            if ruiseki[i] <= 0:
                diff = abs(ruiseki[i] - 1)
                ans2 += diff
                ruiseki[i] = 1
        else:
            # iが偶数
            if ruiseki[i] >= 0:
                diff = abs(ruiseki[i] - (-1))
                ans2 += diff
                ruiseki[i] = -1
    
    print(min(ans1, ans2))


if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
import numpy as np
n=int(input())
a=list(map(int,input().split()))
r=[0]
for i in range(n):
    r.append(r[i]+a[i])
r.pop(0)
q=[r[i] for i in range(n)]

pm=[1-2*(i%2) for i in range(n)]
mp=[1-2*((i+1)%2) for i in range(n)]

sum1,sum2=0,0
sousa1,sousa2=0,0

for i in range(n):
    if np.sign(r[i]+sousa1) != pm[i]:
        sum1+=abs(pm[i]-r[i]-sousa1)
        sousa1=pm[i]-r[i]
        
for i in range(n):
    if np.sign(q[i]+sousa2) != mp[i]:
        sum2+=abs(mp[i]-q[i]-sousa2)
        sousa2=mp[i]-q[i]

print(min(sum1,sum2))

Submission status: Accepted

Solution code:
import itertools
N = input()
l = list(map(int,input().split(" ")))
ruiseki = 0
ans = [0,0]
for pattern in [0,1]: #奇偶による+-を制御
    ruiseki = 0
    for i,num in enumerate(l):
        ruiseki += num
        if (i+pattern) %2 == 0:
            if ruiseki >= 0:
                ans[pattern] += ruiseki + 1
                ruiseki = -1
        else:
            if ruiseki <= 0:
                ans[pattern] +=1-ruiseki
                ruiseki = 1
print(min(ans))