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, math, fractions, itertools


def solve(n: int, a: "List[int]"):
    cum = 0
    mn = 10000000000
    counter1 = 0
    for i in range(n):
        if cum + a[i] == 0:
            counter1 += 1
            if i % 2 == 0:
                cum += a[i] + 1
            else:
                cum += a[i] - 1
        elif cum + a[i] > 0:
            if i % 2 == 0:
                cum += a[i]
            else:
                counter1 += abs(a[i] + abs(cum) + 1)
                cum -= abs(cum)+1
        else:
            if i % 2 == 0:
                counter1 += abs(a[i] - abs(cum) - 1)
                cum += abs(cum)+1
            else:
                cum += a[i]

    cum = 0
    counter2 = 0
    for i in range(n):
        if cum + a[i] == 0:
            counter2 += 1
            if i % 2 != 0:
                cum += a[i] + 1
            else:
                cum += a[i] - 1
        elif cum + a[i] > 0:
            if i % 2 != 0:
                cum += a[i]
            else:
                counter2 += abs(a[i] + abs(cum) + 1)
                cum -= abs(cum) + 1
        else:
            if i % 2 != 0:
                counter2 += abs(a[i] - abs(cum) - 1)
                cum += abs(cum) + 1
            else:
                cum += a[i]
    print(min(counter1, counter2))

    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
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 = [int(x) for x in input().split()]


def solve(A, N):
    S = [0]*N
    answer = 0
    total = 0
    for i in range(N):
        total += A[i]
        S[i] = total
        if total == 0:
            d = 1 if S[i-1] < 0 else -1
            A[i] += d
            answer += 1
            total += d
        else:
            # S[i-1] < 0 and S[i] > 0
            # S[i-1] > 0 and S[i] < 0
            # S[i-1] > 0 and S[i] > 0
            if S[i-1] < 0 and S[i] < 0:
                answer += -S[i]+1
                total += -S[i]+1
            elif S[i-1] > 0 and S[i] > 0:
                answer += S[i]+1
                total += -(S[i]+1)
        S[i] = total
        #print(i, answer, "S[i-1]=%d"%S[i-1], "S[i]=%d"%S[i], sep="\t")
    return answer

if A[0] == 0:
    A[0] = -1
    m1 = solve(A, N) + 1
    A[0] = +1
    m2 = solve(A, N) + 1
    print(min(m1, m2))
else:
    m1 = solve(A, N)
    t = abs(A[0])
    A[0] = 1 if A[0] < 0 else -1
    m2 = solve(A, N) + t+1
    print(min(m1, m2))

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
n=int(input())
a_list=list(map(int,input().split()))
a_c=[]
Sum=0
for i in a_list:
    a_c.append(Sum+i)
    Sum+=i
ans_p=0
count=0
for i in range(n):
    if i%2==0:
        if a_c[i]+count<=0:
            ans_p+=1-a_c[i]-count
            count=1-a_c[i]
    else:
        if a_c[i]+count>=0:
            ans_p+=1+a_c[i]+count
            count=-1-a_c[i]
ans_n=0
count=0
for i in range(n):
    if i%2==1:
        if a_c[i]+count<=0:
            ans_n+=1-a_c[i]-count
            count=1-a_c[i]
    else:
        if a_c[i]+count>=0:
            ans_n+=1+a_c[i]+count
            count=-1-a_c[i]
ans=min(ans_p,ans_n)
print(ans)

Submission status: Accepted

Solution code:

import itertools
from collections import Counter
from collections import defaultdict
import bisect
from heapq import heappush, heappop

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

    cumulative = 0
    count1 = 0
    for i in range(len(a)):
        cumulative += a[i]
        if i % 2 == 0: # positive
            if cumulative <= 0:
                count1 += abs(cumulative) + 1
                cumulative += (abs(cumulative) + 1)
        else:  # negative
            if cumulative >= 0:
                count1 += abs(cumulative) + 1
                cumulative -= (abs(cumulative) + 1)
        # print('count = {} c = {}'.format(count, cumulative))

    cumulative = 0
    count2 = 0
    for i in range(len(a)):
        cumulative += a[i]
        if i % 2 == 0: # negative
            if cumulative >= 0:
                count2 += abs(cumulative) + 1
                cumulative -= (abs(cumulative) + 1)
        else:  # positive
            if cumulative <= 0:
                count2 += abs(cumulative) + 1
                cumulative += (abs(cumulative) + 1)
        # print('count = {} c = {}'.format(count, cumulative))

    print(min(count1, count2))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

def count(s):
    ans = 0
    for i, a in enumerate(A):
        if i == 0:
            continue
        if s < 0:
            if 1-s-a >= 0:
                ans += 1-s-a
                s = 1
            else:
                s += a
        elif s > 0:
            if s+a+1 >= 0:
                ans += s+a+1
                s = -1
            else:
                s += a
    return ans

b = A[0]
ans1 = count(b)
if b > 0:
    ans2 = count(-1)+b+1
elif b < 0:
    ans2 = count(1)+1-b
else:
    ans1 = count(1)+1
    ans2 = count(-1)+1
print(min(ans1, ans2))

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
tmp1 = 0
tmp2 = 0
S = [0]*n

if a[0] > 0:
  S[0] = a[0]
else:
  S[0] = 1
  tmp1 += abs(1-a[0])
  
for i in range(1,n):
  tmpS = a[i] + S[i-1]
  if tmpS*S[i-1] < 0:
    S[i] = tmpS
    continue
  else:
    if i%2 == 0:
      S[i] = 1
      tmp1 += abs(1 - tmpS)
    else:
      S[i] = -1
      tmp1 += abs(-1-tmpS)
if a[0] < 0:
  S[0] = a[0]
else:
  S[0] = -1
  tmp2 += abs(-1-a[0])
for i in range(1,n):
  tmpS = a[i] + S[i-1]
  if tmpS*S[i-1] < 0:
    S[i] = tmpS
    continue
  else:
    if i%2 == 0:
      S[i] = -1
      tmp2 += abs(-1 - tmpS)
    else:
      S[i] = 1
      tmp2 += abs(1-tmpS)
print(min(tmp1,tmp2))

Submission status: Accepted

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


def f(p):
    sa = 0
    ans = 0
    for ai in a:
        if p:
            sa += ai
            if sa >= 0:
                ans += sa + 1
                sa = -1
            p = False
        else:
            sa += ai
            if 0 >= sa:
                ans += 1 - sa
                sa = 1
            p = True
    return ans


def main():
    print(min(f(True), f(False)))


main()

Submission status: Accepted

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

s1=c1=s2=c2=0
for i in range(n):
    if i%2:
        c1+=max(0,a[i]+s1+1); s1+=min(-s1-1,a[i])
        c2+=max(0,-s2+1-a[i]); s2+=max(-s2+1,a[i])
    else:
        c1+=max(0,-s1+1-a[i]); s1+=max(-s1+1,a[i])
        c2+=max(0,a[i]+s2+1); s2+=min(-s2-1,a[i])
print(min(c1,c2))

Submission status: Accepted

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

if A[0] < 0:
	sumP = 1
	sumM = A[0]
	countP = -A[0] + 1
	countM = 0
elif A[0] == 0:
	sumP = 1
	sumM = -1
	countP = 1
	countM = 1
else:
	sumP = A[0]
	sumM = -1
	countP = 0
	countM = A[0] + 1

for i in range(1, N):
	if sumP > 0:
		if A[i] + sumP >= 0:
			countP += A[i] + sumP + 1
			sumP = -1
		else:
			sumP += A[i]
	else:
		if A[i] + sumP <= 0:
			countP += -(A[i] + sumP) + 1
			sumP = 1
		else:
			sumP += A[i]
	
	if sumM > 0:
		if A[i] + sumM >= 0:
			countM += A[i] + sumM + 1
			sumM = -1
		else:
			sumM += A[i]
	else:
		if A[i] + sumM <= 0:
			countM += -(A[i] + sumM) + 1
			sumM = 1
		else:
			sumM += A[i]

print(min(countP, countM))

Submission status: Accepted

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

def solve(n):
    ans = 0
    cusm = []
    if n == -1 and a[0] > 0:
        cusm.append(-1)
        ans += abs(-1 - a[0])
    elif n == -1 and a[0] < 0:
        cusm.append(1)
        ans += abs(1 - a[0])
    elif a[0] == 0:
        cusm.append(n)
        ans += abs(n - a[0])
    else:
        cusm.append(a[0])

    for i, ai in enumerate(a):
        if i == 0: continue
        if (cusm[-1] + ai) * cusm[-1] < 0: #符号が逆なら
            cusm.append(cusm[-1] + ai)
        elif cusm[-1] > 0: #操作の必要があって前が正なら
            ans += abs(-1 - (cusm[-1] + ai))
            cusm.append(-1)
        else:  #操作の必要があって前が負なら
            ans += abs(1 - (cusm[-1] + ai))
            cusm.append(1)
    return ans

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

Submission status: Accepted

Solution code:
n = int(raw_input())
A = map(int, raw_input().split())
if A[0] > 0:
    psep = A[0]
    psop = -1
    opop = abs(-1 - A[0])
    opep = 0
elif A[0] < 0:
    psep = 1
    psop = A[0]
    opep = abs(1-A[0])
    opop = 0
else:
    psep = 1
    psop = -1
    opep = 1
    opop = 1
for i in range(1, n):
    if i % 2 == 0:
        if psep + A[i] <= 0:
            opep += abs(1 - (psep + A[i]))
            psep = 1
        else:
            psep = psep + A[i]
        if psop + A[i] >= 0:
            opop += abs(-1 - (psop + A[i]))
            psop = -1
        else:
            psop += A[i]
    else:
        if psep + A[i] >= 0:
            opep += abs(-1 - (psep+A[i]))
            psep = -1
        else:
            psep += A[i]
        if psop + A[i] <= 0:
            opop += abs(1-(psop+A[i]))
            psop = 1
        else:
            psop = psop + A[i]
print min(opop, opep)

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    a = input().split()
    a = list(map(lambda x: x.capitalize(), a))
    a,b,c = a
    print(a[0]+b[0]+c[0])
    return

#B
def B():
    a = II()
    b = II()
    if a > b:
        print("GREATER")
    if a < b:
        print("LESS")
    if a == b:
        print("EQUAL")
    return

#C
def C():
    II()
    a = LI()
    def f(suma, b):
        for i in a[1:]:
            if suma * (suma + i) < 0:
                suma += i
                continue
            b += (abs(suma + i) + 1)
            suma = (-1 * (suma > 0)) or 1
        return b

    if a[0] == 0:
        ans = min(f(1, 1), f(-1, 1))
    else:
        ans = min(f(a[0], 0), f(1 * (a[0] < 0) or - 1, abs(a[0]) + 1))
    print(ans)
    return

#D
def D():
    s = S()
    for i in range(len(s) - 1):
        if s[i] == s[i+1]:
            print(i + 1, i + 2)
            return
    for i in range(len(s) - 2):
        if s[i] == s[i + 2]:
            print(i + 1, i + 3)
            return
    print(-1, -1)
    return


#Solve
if __name__ == '__main__':
    C()

Submission status: Accepted

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

res = 0
former = 0
s = 0

#はじめが正のとき
for i in range(N):
    if i == 0:
        if A[0] <= 0:
            res += (-A[0]+1)
            s = 1
        else:
            s = A[0]
    else:
        former = s
        s += A[i]
        if former*s >= 0:
            if s == 0:
                if former > 0:
                    res += 1
                    s = -1
                else:
                    res += 1
                    s = 1
            elif s > 0:
                res += s+1
                s = -1
            elif s < 0:
                res += (-s+1)
                s = 1
        else:
            pass
    #print(i,res)

#はじめが負の数
t = 0
for i in range(N):
    if i == 0:
        if A[0] >= 0:
            t += (A[0]+1)
            s = -1
        else:
            s = A[0]
    else:
        former = s
        s += A[i]
        if former*s >= 0:
            if s == 0:
                if former > 0:
                    t += 1
                    s = -1
                else:
                    t += 1
                    s = 1
            elif s > 0:
                t += s+1
                s = -1
            elif s < 0:
                t += (-s+1)
                s = 1
        else:
            pass
    #print(i,t)
if res > t:
    res = t
print(res)

Submission status: Accepted

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

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

sum_a = 0

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

print(min(ans1,ans2))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
import itertools

INF = 10**9+7

n = int(input())
s = list(map(int,input().split()))
for i in range(1,n):
	s[i] = s[i] + s[i-1]

def solve(x):
	global s
	cost = 0
	debt = 0
	y = [1,-1]
	for i in range(len(s)):
		c = s[i]*x + debt
		if(y[i%2]!=sgn(c)):
			cost += abs(y[i%2]-c)
			debt += y[i%2]-c
	return cost

def sgn(n):
	if(n>0): return 1
	if(n<0): return -1
	return 0

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

Submission status: Accepted

Solution code:
from subprocess import*
with open('Main.java','w')as f:f.write(r"""
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = Integer.parseInt(sc.next());
		int[] a = new int[n];
		for (int i = 0; i < n; i++)
			a[i] = Integer.parseInt(sc.next());
		int diff = 0;
		long c = 0;
		for (int i = 0; i < n; i++) {
			diff += a[i];
			if (i % 2 == 0) {
				if (diff <= 0) {
					c += -diff + 1;
					diff = 1;
				}
			}
			else {
				if (diff >= 0) {
					c += diff + 1;
					diff = -1;
				}
			}
		}
		long ans = c;
		c = 0;
		diff = 0;
		for (int i = 0; i < n; i++) {
			diff += a[i];
			if (i % 2 != 0) {
				if (diff <= 0) {
					c += -diff + 1;
					diff = 1;
				}
			}
			else {
				if (diff >= 0) {
					c += diff + 1;
					diff = -1;
				}
			}
		}
		ans = Math.min(ans, c);
		System.out.println(ans);
	}
}
""")
call(('javac','Main.java'))
call(('java','Main'))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#ABC59 C
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:
            continue
        else:
            ans1 += 1 - s1
            s1 = 1
    else:
        if s1 < 0:
            continue
        else:
            ans1 += s1 + 1
            s1 = -1
ans2 = 0
s2 = 0
for i in range(n):
    s2 += a[i]
    if i % 2 == 0:
        if s2 < 0:
            continue
        else:
            ans2 += s2 + 1
            s2 = -1
    else:
        if s2 > 0:
            continue
        else:
            ans2 += 1 - s2
            s2 = 1

print(min(ans1,ans2))

Submission status: Accepted

Solution code:

def main():
    num = int(input())
    data = list(map(int, input().split()))

    now_num = [0, 0]
    now_c = [0, 0]
    for i in range(num):
        now_num[0] += data[i]
        if i % 2 == 0:
            if now_num[0] <= 0:
                now_c[0] += 1 - now_num[0]
                now_num[0] = 1
        else:
            if now_num[0] >= 0:
                now_c[0] += now_num[0] + 1
                now_num[0] = -1

        now_num[1] += data[i]
        if i % 2 == 1:
            if now_num[1] <= 0:
                now_c[1] += 1 - now_num[1]
                now_num[1] = 1
        else:
            if now_num[1] >= 0:
                now_c[1] += now_num[1] + 1
                now_num[1] = -1

    print(min(now_c))






if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
cnt1,cnt2 = 0,0
S1,S2 = A[0],A[0]
#
if A[0]==0:
    cnt1,cnt2 = 1,1
    S1,S2 = 1,-1
elif A[0]>0:
    cnt2 += 1+A[0]
    S2 = -1
else:
    cnt1 += 1-A[0]
    S1 = 1
#
for i in range(1,N):
    K = S1+A[i]
    if S1>0 and K>=0:
        cnt1 += 1+K
        S1 = -1
    elif S1<0 and K<=0:
        cnt1 += 1-K
        S1 = 1
    else:
        S1 = K
#
for i in range(1,N):
    K = S2+A[i]
    if S2>0 and K>=0:
        cnt2 += 1+K
        S2 = -1
    elif S2<0 and K<=0:
        cnt2 += 1-K
        S2 = 1
    else:
        S2 = K
print(min(cnt1,cnt2))

Submission status: Accepted

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

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

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

print(min(pos, neg))

Submission status: Accepted

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

def func(x):
	global a

	s = 0
	k = 0

	for i in a:
		s += i
		if s*x <= 0:
			k += abs(x-s)
			s = x
		x *= -1

	return k


print(min(func(1), func(-1)))

Submission status: Accepted

Solution code:
import copy

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

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

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

print(min(ans_a,ans_b))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

import sys

sys.setrecursionlimit(10**7)


def read_h(typ=int):
    return list(map(typ, input().split()))


def read_v(n, m=1, typ=int):
    return [read_h() if m > 1 else typ(input()) for _ in range(n)]


def solve(arr, positive):
    num_op = 0
    cumul = 0
    #  print('current cumul:', cumul)

    for a in arr:
        cumul += a

        if positive and cumul >= 0:
            num_op += 1 + cumul
            cumul = -1
        elif not positive and cumul <= 0:
            num_op += 1 - cumul
            cumul = 1

        positive = not positive

    return num_op


def main():
    read_h()
    arr = read_h()

    op_plus = solve(arr, True)
    #  print('op plus:', op_plus)
    op_minus = solve(arr, False)
    #  print('op minus:', op_minus)

    print(min(op_plus, op_minus))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

cnt=0
wa=0
condition='plus'
for i in range(n):
  wa+=a[i]
  if condition == 'plus':
    condition='minus'
    if wa<=0:
      cnt+=abs(wa)+1
      a[i]+=abs(wa)+1
      wa+=abs(wa)+1
  elif condition == 'minus':
    condition='plus'
    if wa>=0:
      cnt+=abs(wa)+1
      a[i]-=abs(wa)+1
      wa-=abs(wa)+1

cnt1=cnt
a=b[:]
cnt=0
wa=0
condition='minus'
for i in range(n):
  wa+=a[i]
  if condition == 'plus':
    condition='minus'
    if wa<=0:
      cnt+=abs(wa)+1
      a[i]+=abs(wa)+1
      wa+=abs(wa)+1
  elif condition == 'minus':
    condition='plus'
    if wa>=0:
      cnt+=abs(wa)+1
      a[i]-=abs(wa)+1
      wa-=abs(wa)+1

cnt2=cnt
print(min(cnt1,cnt2))

Submission status: Accepted

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

def main(N, A):
    ans = float("INF")
    for i in range(2):
        sm = 0
        cnt = 0
        for j in range(N):
            sm += A[j]
            #smを正にしたい
            if(j%2==i and sm<=0):
                cnt += 1 - sm
                sm = 1
            #smを負にしたい
            elif(j%2==1-i and sm>=0):
                cnt += 1 + sm
                sm = -1
        ans = min(ans, cnt)
    print(ans)
    
if __name__ == '__main__':
    N = int(input())
    A = [int(i) for i in input().split()]
    
    main(N, A)

Submission status: Accepted

Solution code:
import numpy as np
import copy
n = int(input())
a = list(map(int, input().split()))
a_copy = copy.copy(a)
count = np.zeros(2)


i = 0
sum_before = 0
sum_after = 0    
    
if a[0] <= 0:
    count[0] += abs(a[0]) + 1
    a[0] = 1
while i < n:
    sum_after = sum_before + a[i]
    if sum_after * sum_before > 0 or sum_after == 0:
        if sum_after < 0:
            a[i] = a[i] - sum_after + 1
        elif sum_after > 0:
            a[i] = a[i] - sum_after - 1
        elif sum_before < 0:
            a[i] += 1
        else:
            a[i] -= 1
        count[0] += abs(sum_after) + 1
        sum_after = sum_before + a[i]
    i += 1
    sum_before = sum_after

i = 0
sum_before = 0
sum_after = 0    

if a_copy[0] >= 0:
    count[1] += abs(a_copy[0]) + 1
    a_copy[0] = -1

while i < n:
    sum_after = sum_before + a_copy[i]
    if sum_after * sum_before > 0 or sum_after == 0:
        if sum_after < 0:
            a_copy[i] = a_copy[i] - sum_after + 1
        elif sum_after > 0:
            a_copy[i] = a_copy[i] - sum_after - 1
        elif sum_before < 0:
            a_copy[i] += 1
        else:
            a_copy[i] -= 1
        count[1] += abs(sum_after) + 1
        sum_after = sum_before + a_copy[i]
    i += 1
    sum_before = sum_after

print(int(min(count[0], count[1])))

Submission status: Accepted

Solution code:

n = int(input())



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

ans = float("inf")

now = 0
summ = 0
for i in range(n):

    summ += a[i]

    if i % 2 == 0 and summ <= 0:
        now += 1 - summ
        summ = 1

    elif i % 2 == 1 and summ >= 0:
        now += summ + 1
        summ = -1

ans = min(ans,now)

now = 0
summ = 0

for i in range(n):

    summ += a[i]

    if i % 2 == 1 and summ <= 0:
        now += 1 - summ
        summ = 1

    elif i % 2 == 0 and summ >= 0:
        now += summ + 1
        summ = -1

ans = min(ans,now)

print (ans)

Submission status: Accepted

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

x = 0
tmp = 0
for i in range(N):
    tmp += A[i]
    if i & 1 == 0:
        if tmp < 1:
            x += 1-tmp
            tmp = 1
    else:
        if tmp > -1:
            x += 1+tmp
            tmp = -1
y = 0
tmp = 0
for i in range(N):
    tmp += A[i]
    if i & 1:
        if tmp < 1:
            y += 1-tmp
            tmp = 1
    else:
        if tmp > -1:
            y += 1+tmp
            tmp = -1
print(min(x,y))

Submission status: Accepted

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

x = 0
tmp = 0
for i in range(N):
    tmp += A[i]
    if i % 2 == 0:
        if tmp < 1:
            x += 1-tmp
            tmp = 1
    else:
        if tmp > -1:
            x += 1+tmp
            tmp = -1
y = 0
tmp = 0
for i in range(N):
    tmp += A[i]
    if i % 2 == 1:
        if tmp < 1:
            y += 1-tmp
            tmp = 1
    else:
        if tmp > -1:
            y += 1+tmp
            tmp = -1
print(min(x,y))

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
total_n=0
cnt_n=0
total_p=0
cnt_p=0

mark=-1
for i in range(n):
    total_n+=a[i]
    if mark<0:
        if total_n>=0:
            cnt_n+=abs(-1-total_n) 
            total_n=-1
    else:
        if total_n<=0:
            cnt_n+=abs(1-total_n)
            total_n=1
    mark*=-1

mark=1
for i in range(n):
    total_p+=a[i]
    if mark<0:
        if total_p>=0:
            cnt_p+=abs(-1-total_p)
            total_p=-1
    else:
        if total_p<=0:
            cnt_p+=abs(1-total_p)
            total_p=1
    mark*=-1

print(min(cnt_p,cnt_n))

Submission status: Accepted

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

cnt = [0,0]

for i in [0,1]:
    tmp_sum = 0
    for j, a in enumerate(A):
        tmp_sum += a
        if (i + j) % 2 == 0 and tmp_sum <= 0:
            cnt[i] += abs(tmp_sum) + 1
            tmp_sum += abs(tmp_sum) + 1
        elif (i + j) % 2 == 1 and tmp_sum >= 0:
            cnt[i] += tmp_sum + 1
            tmp_sum -= tmp_sum + 1

print(min(cnt))

Submission status: Accepted

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

Submission status: Accepted

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

cost = sub = 0
if b[0] > 0:pass
elif b[0] == 0:cost,sub = 1,1
else:cost,sub = 1 - b[0], 1-b[0]

for i in range(1,N):
    if i % 2 == 0:
        if b[i] + sub <= 0:
            newsub = -1 * (b[i] + sub) + 1
            cost += abs(newsub)
            sub += newsub
    else:
        if b[i] + sub >= 0:
            newsub = -1 * (b[i] + sub + 1)
            cost += abs(newsub)
            sub += newsub

cost2 = sub2 = 0
if b[0] < 0:pass
elif b[0] == 0:cost2,sub2 = 1,-1
else:cost2,sub2 = 1 + b[0], -1 *(1+b[0])

for i in range(1,N):
    if i % 2 == 1:
        if b[i] + sub2 <= 0:
            newsub2 = -1 * (b[i] + sub2) + 1
            cost2 += abs(newsub2)
            sub2 += newsub2
    else:
        if b[i] + sub2 >= 0:
            newsub2 = -1 * (b[i] + sub2 + 1)
            cost2 += abs(newsub2)
            sub2 += newsub2

print(min(cost,cost2))

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
cur = 0
ans1, ans2 = 0,0
for i, x in enumerate(a):
    cur += x
    if i%2 == 0:
        if cur >= 1:
            continue
        else:
            ans1 += abs(1-cur)
            cur = 1
    else:
        if cur <= -1:
            continue
        else:
            ans1 += abs(-1-cur)
            cur = -1
cur = 0
for i, x in enumerate(a):
    cur += x
    if i%2 != 0:
        if cur >= 1:
            continue
        else:
            ans2 += abs(1-cur)
            cur = 1
    else:
        if cur <= -1:
            continue
        else:
            ans2 += abs(-1-cur)
            cur = -1
print(min(ans1, ans2))

Submission status: Accepted

Solution code:
# ABC059C - Sequence (ARC072C)
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()))

wa=a[0]
ans1,ans2=0,0
if a[0]==0:
    ans1+=1
    wa=-1

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

if a[0]>0:
    ans2+=a[0]+1
    wa=-1
else:
    ans2+=-a[0]+1
    wa=1

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

Submission status: Accepted

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

s = [0]*(n+1)
for i in range(len(a)):
    s[i+1]=s[i]+a[i]

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

    else:
        if now > 0:
            continue
        else:
            case1 += abs(now)+1
            now = 1

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

    else:
        if now > 0:
            continue
        else:
            case2 += abs(now) + 1
            now = 1

print(min(case1,case2))