Problem:


Score : 300 points

Problem Statement

You are given an integer sequence of length N. The i-th term in the sequence is a_i.
In one operation, you can select a term and either increment or decrement it by one.

At least how many operations are necessary to satisfy the following conditions?

For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.

For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.

Constraints

2 ≤ n ≤ 10^5

|a_i| ≤ 10^9

Each a_i is an integer.

Input

Input is given from Standard Input in the following format:

n
a_1 a_2 ... a_n

Output

Print the minimum necessary count of operations.

Sample Input 1

4
1 -3 1 0

Sample Output 1

4

For example, the given sequence can be transformed into 1, -2, 2, -2 by four operations. The sums of the first one, two, three and four terms are 1, -1, 1 and -1, respectively, which satisfy the conditions.

Sample Input 2

5
3 -6 4 -5 7

Sample Output 2

0

The given sequence already satisfies the conditions.

Sample Input 3

6
-1 4 3 2 -5 4

Sample Output 3

8



Sample input:
4
1 -3 1 0


Sample output:
4

Submission status: Accepted

Solution code:
from collections import defaultdict


def solve(pre_is_positive, a_list):
    total = 0
    ans = 0
    for a in a_list:
        total += a
        if pre_is_positive and total >= 0:
            ans += abs(total) + 1
            total = -1
        elif not pre_is_positive and total <= 0:
            ans += abs(total) + 1
            total = 1
        pre_is_positive = total > 0
    return ans


def main():
    n = int(input())
    a_list = list(map(int, input().split()))
    print(min(solve(True, a_list), solve(False, a_list)))


if __name__ == "__main__":
    main()

Submission status: Accepted

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

INF = float('inf')

ans = INF

for sign in (1, -1):
    res, total_a = 0, 0
    for a in A:
        total_a += a
        if total_a * sign <= 0:
            res += abs(total_a - sign)
            total_a = sign
        sign *= -1
    ans = min(ans, res)

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=[int(x) for x in input().split()]
def arrange(a0_is_plus):
    r=0 if a0_is_plus==True else 1
    sumNum=0
    ans=0
    for i,an in enumerate(a):
        sumNum+=an
        if i%2==r and sumNum<=0:
            ans+=1-sumNum
            sumNum=1
        elif i%2!=r and sumNum>=0:
            ans+=sumNum+1
            sumNum=-1
    return ans
print(min(arrange(True),arrange(False)))

Submission status: Accepted

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

sum1 = sum2 = src[0]
op1 = op2 = 0
if sum1 < 1:
    op1 += (1 - sum1)
    sum1 = 1
if sum2 > -1:
    op2 += (sum2 + 1)
    sum2 = -1

for i in range(1,N):
    if i%2 == 1:
        if sum1 + src[i] > -1:
            op1 += (sum1 + src[i] + 1)
            sum1 = -1
        else:
            sum1 += src[i]
        if sum2 + src[i] < 1:
            op2 += (1 - sum2 - src[i])
            sum2 = 1
        else:
            sum2 += src[i]
    else:
        if sum2 + src[i] > -1:
            op2 += (sum2 + src[i] + 1)
            sum2 = -1
        else:
            sum2 += src[i]
        if sum1 + src[i] < 1:
            op1 += (1 - sum1 - src[i])
            sum1 = 1
        else:
            sum1 += src[i]

print(min(op1,op2))

Submission status: Accepted

Solution code:
N = int(input())
arr = list(map(int,input().split()))
def S1(a,b):
    c,total,ans = 0,0,0
    for t in arr:
        c += 1
        if c % 2 == a:
            if total+t <= 0:
                ans += abs(total+t)+1
                total = 1
                continue
        elif c % 2 == b:
            if total+t >= 0:
                ans += abs(total+t)+1
                total = -1
                continue
        total += t
    return ans
print(min(S1(1,0),S1(0,1)))

Submission status: Accepted

Solution code:
from collections import defaultdict


def solve(pre_is_positive, a_list):
    total = 0
    ans = 0
    for a in a_list:
        total += a
        if pre_is_positive and total >= 0:
            ans += abs(total) + 1
            total = -1
        elif not pre_is_positive and total <= 0:
            ans += abs(total) + 1
            total = 1
        pre_is_positive = total > 0

    return ans


def main():
    n = int(input())
    a_list = list(map(int, input().split()))
    print(min(solve(True, a_list), solve(False, a_list)))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict


def solve(pre_is_positive, total, ans, a_list):
    for a in a_list:
        total += a
        if pre_is_positive:
            if total == 0:
                ans += 1
                total = -1
            elif total > 0:
                ans += abs(total) + 1
                total = -1
        elif not pre_is_positive:
            if total == 0:
                ans += 1
                total = 1
            elif total < 0:
                ans += abs(total) + 1
                total = 1

        pre_is_positive = total > 0

    return ans


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

    total = a_list[0]
    if total == 0:
        ans = solve(True, 1, 1, a_list[1:])
        ans = min(ans, solve(False, -1, 1, a_list[1:]))
    elif total > 0:
        ans = solve(True, total, 0, a_list[1:])
        ans = min(ans, solve(False, -1, abs(total) + 1, a_list[1:]))
    else:
        ans = solve(False, total, 0, a_list[1:])
        ans = min(ans, solve(True, 1, abs(total) + 1, a_list[1:]))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

total = 0
ans1 = 0
ans2 = 0

for i, j in enumerate(l):
    total += j
    if i % 2 == 1:
        if total <= 0:
            ans1 += 1 + abs(total)
            total = 1
    else:
        if total >= 0:
            ans1 += 1 + abs(total)
            total = -1

total = 0

for i, j in enumerate(l):
    total += j
    if i % 2 == 0:
        if total <= 0:
            ans2 += 1 + abs(total)
            total = 1
    else:
        if total >= 0:
            ans2 += 1 + abs(total)
            total = -1

print(min(ans1, ans2))

Submission status: Accepted

Solution code:
import sys

# sys.stdin = open('c1.in')


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


def read_str_list():
    return input().split()


def read_int():
    return int(input())


def read_str():
    return input()


n = read_int()
a0 = read_int_list()
res = 10 ** 20
for first in [-1, 1]:
    a = list(a0)
    sign = first
    r = 0
    s = 0
    for i in range(n):
        if (s + a[i]) * sign <= 0:
            if sign < 0:
                dif = s + a[i] + 1
                a[i] -= dif
                r += dif
            if sign > 0:
                dif = -(s + a[i]) + 1
                a[i] += dif
                r += dif
        sign *= -1
        s += a[i]
    if res > r:
        res = r
print(res)

Submission status: Accepted

Solution code:
n=int(input())
arr=list(map(int,input().split()))
totalodd=0
totaleven=0
ansodd=0
anseven=0
for i in range(len(arr)):
    if i%2==0:      # 
        if totalodd+arr[i]>0:
            totalodd+=arr[i]
        else:
            ansodd+=1-(totalodd+arr[i])
            totalodd=1
        if totaleven+arr[i]<0:
            totaleven+=arr[i]
        else:
            anseven+=(totaleven+arr[i])+1
            totaleven=-1
    else:
        if totalodd+arr[i]<0:
            totalodd+=arr[i]
        else:
            ansodd+=(totalodd+arr[i])+1
            totalodd=-1
        if totaleven+arr[i]>0:
            totaleven+=arr[i]
        else:
            anseven+=1-(totaleven+arr[i])
            totaleven=1

print(min(ansodd,anseven))

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split(' ')]
_a = [-1*i for i in a]

def func(l):
    _ans = 0
    product = 0
    for i in range(n):
        product += l[i]
        if i%2 == 0:
            if product<=0:
                _ans += abs(product) + 1
                product = 1
        elif i%2 == 1:
            if product>=0:
                _ans += abs(product) + 1
                product = -1
    return _ans

ans = min(func(a), func(_a))
print(ans)

Submission status: Accepted

Solution code:
n = int(raw_input())
data = map(int, raw_input().split())
result = []
for i in range(2):
    sum = 0
    count = 0
    for j in range(n):
        sum += data[j]
        if (i+j) % 2 == 0:
            if sum <= 0:
                count += abs(1-sum)
                sum = 1
        else:
            if sum >= 0:
                count += abs(-1-sum)
                sum = -1
    result.append(count)
print min(result)

Submission status: Accepted

Solution code:
n = input()
l = map(int, raw_input().split())

lastPos, lastNeg = l[0], l[0]
countPos, countNeg = 0, 0

if(lastPos<=0):
    countPos = 1 - lastPos
    lastPos = 1
if(lastNeg>=0):
    countNeg = lastNeg+1
    lastNeg = -1

for i in xrange(1,n):
    if(lastPos>0):
        #now it has to be negative
        if(lastPos+l[i] >= 0):
            diff = lastPos+l[i] + 1
            countPos+=diff
            lastPos = -1
        else:
            lastPos = lastPos + l[i]
    else:
        #now it has to be positive
        if(lastPos+l[i] <= 0):
            diff = 1 - (lastPos+l[i])
            countPos+=diff
            lastPos = 1
        else:
            lastPos = lastPos+l[i]
    
    if(lastNeg<0):
        #now it has to be positive
        if(lastNeg+l[i] <= 0):
            diff = 1 - (lastNeg+l[i])
            countNeg+= diff
            lastNeg = 1
        else:
            lastNeg = lastNeg + l[i]
    else:
        #now it has to be negative
        if(lastNeg+l[i] >= 0):
            diff = lastNeg+l[i]+1
            countNeg+= diff
            lastNeg = -1
        else:
            lastNeg = lastNeg + l[i]

print min(countPos, countNeg)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
_a = list(map(lambda x: x * (-1), a))


def func(l: list):
    _ans = 0
    product = 0
    for i in range(n):
        product += l[i]

        if i % 2 == 0:
            if product <= 0:
                _ans += abs(product) + 1
                product = 1

        elif i % 2 == 1:
            if product >= 0:
                _ans += abs(product) + 1
                product = -1
    return _ans

ans = min(func(a), func(_a))
print(ans)

Submission status: Accepted

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


def calcSteps(sign):
    res = 0
    s = 0
    for i in range(n):
        if (s+a[i])*sign < 0:
            s += a[i]
        else:
            step = -s -sign -a[i]
            res +=  abs(step)
            s = -sign
        sign *= -1
    return res



print(min(calcSteps(-1),calcSteps(1)))

Submission status: Accepted

Solution code:
n = int(raw_input())
a = map(int, raw_input().split())
ans = []
for k in range(2):
    s, cur = 0, 0
    for j in range(n):
        s += a[j]
        if (k+j) % 2 == 0:
            if s <= 0:
                cur += abs(1 - s)
                s = 1
        else:
            if s >= 0:
                cur += abs(-1 - s)
                s = -1
    ans.append(cur)
print min(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力
n=int(input())
a=list(map(int, input().split()))
b=a[:]
c=a[:]
# a[0]の符号をそのままにした場合の計算
counter_1=0
S=int(a[0])
for i in range(1,n):
  if S<0 and S+a[i]<=0:
    counter_1+=-S-a[i]+1
    a[i]=-S+1
  elif S>0 and S+a[i]>=0:
    counter_1+=S+a[i]+1
    a[i]=-S-1
  S+=a[i]
# a[0]を1に変えた場合の計算
counter_2=abs(b[0]-1)
b[0]=1
S=b[0]
for i in range(1,n):
  if S<0 and S+b[i]<=0:
    counter_2+=-S-b[i]+1
    b[i]=-S+1
  elif S>0 and S+b[i]>=0:
    counter_2+=S+b[i]+1
    b[i]=-S-1
  S+=b[i]
# a[0]を-1に変えた場合の計算
counter_3=abs(c[0]+1)
c[0]=-1
S=c[0]
for i in range(1,n):
  if S<0 and S+c[i]<=0:
    counter_3+=-S-c[i]+1
    c[i]=-S+1
  elif S>0 and S+c[i]>=0:
    counter_3+=S+c[i]+1
    c[i]=-S-1
  S+=c[i]
print(min(counter_1,counter_2,counter_3))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力
n=int(input())
a=list(map(int, input().split()))
b=a[:]
c=a[:]
# a[0]の符号をそのままにした場合の計算
counter_1=0
S=int(a[0])
for i in range(1,n):
  if S<0 and S+int(a[i])<=0:
    counter_1+=-S-int(a[i])+1
    a[i]=-S+1
  elif S>0 and S+int(a[i])>=0:
    counter_1+=S+int(a[i])+1
    a[i]=-S-1
  S+=int(a[i])
# a[0]を1に変えた場合の計算
counter_2=abs(int(b[0])-1)
b[0]=1
S=b[0]
for i in range(1,n):
  if S<0 and S+int(b[i])<=0:
    counter_2+=-S-int(b[i])+1
    b[i]=-S+1
  elif S>0 and S+int(b[i])>=0:
    counter_2+=S+int(b[i])+1
    b[i]=-S-1
  S+=int(b[i])
# a[0]を-1に変えた場合の計算
counter_3=abs(int(c[0])+1)
c[0]=-1
S=c[0]
for i in range(1,n):
  if S<0 and S+int(c[i])<=0:
    counter_3+=-S-int(c[i])+1
    c[i]=-S+1
  elif S>0 and S+int(c[i])>=0:
    counter_3+=S+int(c[i])+1
    c[i]=-S-1
  S+=int(c[i])
print(min(counter_1,counter_2,counter_3))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力
n=int(input())
a=input().split()
b=a[:]
c=a[:]
# a[0]の符号をそのままにした場合の計算
counter_1=0
S=int(a[0])
for i in range(1,n):
  if S<0 and S+int(a[i])<=0:
    counter_1+=-S-int(a[i])+1
    a[i]=-S+1
  elif S>0 and S+int(a[i])>=0:
    counter_1+=S+int(a[i])+1
    a[i]=-S-1
  S+=int(a[i])
# a[0]を1に変えた場合の計算
counter_2=abs(int(b[0])-1)
b[0]=1
S=b[0]
for i in range(1,n):
  if S<0 and S+int(b[i])<=0:
    counter_2+=-S-int(b[i])+1
    b[i]=-S+1
  elif S>0 and S+int(b[i])>=0:
    counter_2+=S+int(b[i])+1
    b[i]=-S-1
  S+=int(b[i])
# a[0]を-1に変えた場合の計算
counter_3=abs(int(c[0])+1)
c[0]=-1
S=c[0]
for i in range(1,n):
  if S<0 and S+int(c[i])<=0:
    counter_3+=-S-int(c[i])+1
    c[i]=-S+1
  elif S>0 and S+int(c[i])>=0:
    counter_3+=S+int(c[i])+1
    c[i]=-S-1
  S+=int(c[i])
print(min(counter_1,counter_2,counter_3))

Submission status: Accepted

Solution code:

def check1(a):
  sum = 0
  ans = 0
  for i in range(len(a)):
    if(i % 2 == 0):
      sum += a[i]
      if(sum >= 0):
        a[i] -= abs(sum)+1
        ans += abs(sum)+1
        sum -= abs(sum)+1
    else:
      sum += a[i]
      if(sum <= 0):
        a[i] += abs(sum)+1
        ans += abs(sum)+1
        sum += abs(sum)+1
  return ans

def check2(a):
  sum = 0
  ans = 0
  for i in range(len(a)):
    if(i % 2 == 0):
      sum += a[i]
      if(sum <= 0):
        a[i] += abs(sum)+1
        ans += abs(sum)+1
        sum += abs(sum)+1
    else:
      sum += a[i]
      if(sum >= 0):
        a[i] -= abs(sum)+1
        ans += abs(sum)+1
        sum -= abs(sum)+1
  return ans



n = input()
b = input().split()
a = [int(b[i]) for i in range(len(b))]
a2 = list(a)

ans1 = check1(a)
ans2 = check2(a2)


print(ans1) if(ans1<ans2) else print(ans2)

Submission status: Accepted

Solution code:
n = int(raw_input())
l = map(int, raw_input().split())
sp = 0
p = 0
sm = 0
m = 0
pflg = True
for i in l:
    sp += i
    if pflg and sp <= 0:
        p += abs(sp) + 1
        sp = 1
    elif not pflg and sp >= 0:
        p += abs(sp) + 1
        sp = -1
    sm += i
    if pflg and sm >= 0:
        m += abs(sm) + 1
        sm = -1
    elif not pflg and sm <= 0:
        m += abs(sm) + 1
        sm = 1
    pflg = not pflg
print min(p,m)

Submission status: Accepted

Solution code:
def count(seq, is_positive):
    operation = 0
    sum = 0
    for i in range(n):
        sum += seq[i]
        if sum == 0:
            operation += 1
            if is_positive:
                sum -= 1
            else:
                sum += 1
        elif sum > 0 and is_positive:
            operation += abs(sum) + 1
            sum = -1
        elif sum < 0 and not is_positive:
            operation += abs(sum) + 1
            sum = 1

        if sum > 0:
            is_positive = True
        else:
            is_positive = False

    return operation

if __name__ == "__main__":
    n = input()
    seq = map(int, raw_input().split(" "))

    print min(count(seq, False), count(seq, True))

Submission status: Accepted

Solution code:
import sys

def solve():
    n = int(input())
    a = [int(i) for i in input().split()]

    ans1 = scan(a, -1)
    ans2 = scan(a, 1)

    ans = min(ans1, ans2)

    print(ans)

def scan(a, sgn):
    if a[0] * sgn > 0:
        res = 0
        v = a[0]
    else:
        res = abs(a[0] - sgn)
        v = sgn

    for ai in a[1:]:
        if v * (v + ai) < 0:
            v += ai
        else:
            if v > 0:
                res += abs(v + ai + 1)
                v = -1
            else:
                res += abs(v + ai - 1)
                v = 1

    return res

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
import sys

def solve():
    n = int(input())
    a = [int(i) for i in input().split()]

    ans1 = 0 if a[0] > 0 else abs(a[0] - 1)
    v = a[0] if a[0] > 0 else 1

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

    ans2 = 0 if a[0] < 0 else abs(a[0] + 1)
    v = a[0] if a[0] < 0 else -1

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

    ans = min(ans1, ans2)

    print(ans)

if __name__ == '__main__':
    solve()

Submission status: Accepted

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

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

ans = 1e16
for s in (1, -1):
    res, cumsum = 0, 0
    for j in range(n):
        cumsum += a[j]
        if cumsum * s <= 0:
            res += abs(cumsum-s)
            cumsum = s
        s *= -1
    ans = min(ans, res)

print(ans)

Submission status: Accepted

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

ans = 1e16
for s in (1, -1):
    res, acc = 0, 0
    for a in A:
        acc += a
        if acc * s <= 0:
            res += abs(acc-s)
            acc = s
        s *= -1
    ans = min(ans, res)

print(ans)

Submission status: Accepted

Solution code:
#coding:utf-8

def count(seq, is_positive):
    operation = 0
    sum = 0
    for i in range(n):
        sum += seq[i]
        if sum == 0:
            operation += 1
            if is_positive:
                sum -= 1
            else:
                sum += 1
        elif sum > 0 and is_positive:
            operation += abs(sum) + 1
            sum = -1
        elif sum < 0 and not is_positive:
            operation += abs(sum) + 1
            sum = 1

        if sum > 0:
            is_positive = True
        else:
            is_positive = False

    return operation


if __name__ == "__main__":
    n = int(raw_input())
    seq = map(int, raw_input().split(" "))

    print min(count(seq, False), count(seq, True))

Submission status: Accepted

Solution code:
def read(): return list(map(int, input().split()))


n = int(input())
a = read()

ans = 1145141919810893
for j in range(2):
    sum, cur = 0, 0
    for i, v in enumerate(a):
        plus = (i % 2 == j % 2)
        sum += v
        if plus and sum > 0: continue
        if not plus and sum < 0: continue

        if plus:
            cur += 1 - sum
            sum = 1
        else:
            cur += sum + 1
            sum = -1

    ans = min(ans, cur)

print(ans)

Submission status: Accepted

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

m_count = 0
p_count = 0

s = [0]*n
s[0] = a[0]

# // - start
if a[0] >= 0:
    tmp = -a[0] -1
    s[0] += tmp
    m_count += abs(tmp)

for i in range(1,n):
    s[i] = s[i-1] + a[i]

    if s[i] == 0:
        tmp = -s[i-1]//abs(s[i-1])
        s[i] += tmp
        m_count += abs(tmp)
    elif s[i] * s[i-1] > 0:
        tmp = (-s[i]) + (-s[i]//abs(s[i]))
        s[i] += tmp
        m_count += abs(tmp)

# // + start
s[0] = a[0]

if a[0] <= 0:
    tmp = -a[0] +1
    s[0] += tmp
    p_count += abs(tmp)

for i in range(1,n):
    s[i] = s[i-1] + a[i]

    if s[i] == 0:
        tmp = -s[i-1]//abs(s[i-1])
        s[i] += tmp
        p_count += abs(tmp)
    elif s[i] * s[i-1] > 0:
        tmp = (-s[i]) + (-s[i]//abs(s[i]))
        s[i] += tmp
        p_count += abs(tmp)

print(min(p_count,m_count))

Submission status: Accepted

Solution code:
n = int(input())
b = [int(x) for x in input().split()]
a = list()
temp = 0
count1 = 0
count2 = 0

a = b.copy()

if a[0] == 0:
	a[0] = 1
	count1 = 1

sum = a[0]
for i in range(1, n):
	if abs(a[i]) <= abs(sum) or a[i] * sum >= 0:
		if sum > 0:
			temp = -1 * abs(sum) - 1
		else:
			temp = abs(sum) + 1
		count1 += abs(temp - a[i])
		a[i] = temp
	sum += a[i]

a = b.copy()

count2 = abs(a[0]) + 1
if a[0] >= 0:
	a[0] = -1
else:
	a[0] = 1
	
sum = a[0]
for i in range(1, n):
	if abs(a[i]) <= abs(sum) or a[i] * sum >= 0:
		if sum > 0:
			temp = -1 * abs(sum) - 1
		else:
			temp = abs(sum) + 1
		count2 += abs(temp - a[i])
		a[i] = temp
	sum += a[i]

print(min(count1, count2))

Submission status: Accepted

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

n = input()
a = map(int, raw_input().split())
b = a
asum = [0]*len(a)
bsum = [0]*len(a)

asum[0] = a[0]
bsum[0] = b[0]
cnt_a = 0
cnt_b = 0
if(a[0]==0):
    asum[0] = 1
    cnt_a = 1
    bsum[0] = -1
    cnt_b = 1

if(a[0]<0):
    cnt_a += (-1)*a[0]+1
    asum[0] = 1
for i in range(len(a)-1):
    asum[i+1] = a[i+1] + asum[i]
    if(i%2==0 and asum[i+1]>=0):
        cnt_a += asum[i+1]+1
        asum[i+1] = -1
    elif(i%2==1 and asum[i+1]<=0):
        cnt_a += (-1)*asum[i+1]+1
        asum[i+1] = 1
if(b[0]>0):
    cnt_b += b[0]+1
    bsum[0] = -1
for i in range(len(b)-1):
    bsum[i+1] = b[i+1] + bsum[i]
    if(i%2==0 and bsum[i+1]<=0):
        cnt_b += (-1)*bsum[i+1]+1
        bsum[i+1] = 1
    elif(i%2==1 and bsum[i+1]>=0):
        cnt_b += bsum[i+1]+1
        bsum[i+1] = -1
print(min(cnt_a,cnt_b))

Submission status: Accepted

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

def judge_pm(a,b):
    if a*b<0:
        return True
    else:
        return False

a1 = copy.deepcopy(a)
operate_num1 = 0
if a1[0]==0:
    a1[0] = 1
    operate_num1 += 1
tmp_sum = a1[0]
for i in range(1, n):
    if judge_pm(tmp_sum, tmp_sum+a1[i]):
        pass 
    elif tmp_sum<0:
        tmp_operate_num =  - tmp_sum + 1 - a1[i]
        operate_num1 += tmp_operate_num
        a1[i] += tmp_operate_num
    else:
        tmp_operate_num = tmp_sum + 1 + a1[i]
        operate_num1 += tmp_operate_num
        a1[i] -= tmp_operate_num
    tmp_sum += a1[i]
        
a2 = copy.deepcopy(a)
operate_num2 = 0
if a2[0]<0:
    operate_num2 += 1 - a2[0]
    a2[0] += operate_num2
else:
    operate_num2 += a2[0] + 1
    a2[0] -= operate_num2

tmp_sum = a2[0]

for i in range(1, n):
    if judge_pm(tmp_sum, tmp_sum+a2[i]):
        pass 
    elif tmp_sum<0:
        tmp_operate_num =  - tmp_sum + 1 - a2[i]
        operate_num2 += tmp_operate_num
        a2[i] += tmp_operate_num
    else:
        tmp_operate_num = tmp_sum + 1 + a2[i]
        operate_num2 += tmp_operate_num
        a2[i] -= tmp_operate_num
    tmp_sum += a2[i]

print(min(operate_num1, operate_num2))

Submission status: Accepted

Solution code:
n = int(input())
i = input()

i = i.split()
for item in range(len(i)):
    i[item] = int(i[item])

totn = 0
totp = 0

countp = 0
countn = 0

for x in range(len(i)):
    totp += i[x]
    totn += i[x]

    if x %2 == 1:
        if totn == 0:
            countn += 1
            totn = 1
        elif totn < 0:
            countn += abs(totn) + 1
            totn = 1

        if totp == 0:
            countp += 1
            totp = -1
        elif totp > 0:
            countp += abs(totp) + 1
            totp = -1

    if x %2 == 0:
        if totn == 0:
            countn += 1
            totn = -1
        elif totn > 0:
            countn += abs(totn) + 1
            totn = -1

        if totp == 0:
            countp += 1
            totp = 1
        elif totp < 0:
            countp += abs(totp) + 1
            totp = 1

count = min(countn, countp)
print(count)

Submission status: Accepted

Solution code:
n = int(input())
i = input()

i = i.split()
for item in range(len(i)):
    i[item] = int(i[item])

totn = 0
totp = 0

countp = 0
countn = 0

for x in range(len(i)):
    totp += i[x]
    totn += i[x]

    if x %2 == 1:
        if totn == 0:
            countn += 1
            totn = 1
        elif totn < 0:
            countn += abs(totn) + 1
            totn = 1

        if totp == 0:
            countp += 1
            totp = -1
        elif totp > 0:
            countp += abs(totp) + 1
            totp = -1

    if x %2 == 0:
        if totn == 0:
            countn += 1
            totn = -1
        elif totn > 0:
            countn += abs(totn) + 1
            totn = -1

        if totp == 0:
            countp += 1
            totp = 1
        elif totp < 0:
            countp += abs(totp) + 1
            totp = 1

count = min(countn, countp)
print(count)

Submission status: Accepted

Solution code:
n = int(input())
i = input()

i = i.split()
for item in range(len(i)):
    i[item] = int(i[item])

totn = 0
totp = 0

countp = 0
countn = 0

for x in range(len(i)):
    totp += i[x]
    totn += i[x]
    
    if x == 0:
        if totp == 0:   
            totn = -1
            countn = 1

            totp = 1
            countp = 1
            
        elif totp < 0:
            countp = abs(totp) + 1
            totp = 1

        elif totp > 0:
            countn = abs(totn) + 1
            totn = -1

    elif x %2 == 1:
        if totn == 0:
            countn += 1
            totn = 1
        elif totn < 0:
            countn += abs(totn) + 1
            totn = 1

        if totp == 0:
            countp += 1
            totp = -1
        elif totp > 0:
            countp += abs(totp) + 1
            totp = -1

    elif x %2 == 0:
        if totn == 0:
            countn += 1
            totn = -1
        elif totn > 0:
            countn += abs(totn) + 1
            totn = -1

        if totp == 0:
            countp += 1
            totp = 1
        elif totp < 0:
            countp += abs(totp) + 1
            totp = 1
    '''print('totn', totn)
    print('countn', countn)
    print('totp', totp)
    print('countp', countp) '''   
count = min(countn, countp)
print(count)

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-
n = int(input())
a = list(map(int, input().split()))

#nega
total = 0
sign = -1
nega_cost = 0
for tmp in range(len(a)):
    total += a[tmp]
    if total * sign <= 0:
        nega_cost += abs(total*sign)+1
        total = sign
    sign *= -1

#posi
total = 0
sign = 1
posi_cost = 0
for tmp in range(len(a)):
    total += a[tmp]
    if total * sign <= 0:
        posi_cost += abs(total*sign)+1
        total = sign
    sign *= -1

print(min(nega_cost, posi_cost))

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    n = int(input())
    a0 = input().split()
    a = [int(i) for i in a0]
    s1 = 0
    s2 = 0
    m1 = 0
    m2 = 0
    o = 1
    e = -1
    for i in a:
        s1 += i
        if s1 == 0:
            m1 += 1
            s1 = o
        elif o * s1 < 0:
            m1 += abs(s1) + 1
            s1 = o
        o *= -1

        s2 += i
        if s2 == 0:
            m2 += 1
            s2 = e
        elif e * s2 < 0:
            m2 += abs(s2) + 1
            s2 = e
        e *= -1
    print(min(m1, m2))

Submission status: Accepted

Solution code:
n = int(input())
sa = input().split()

a = [0 for i in range(n)]
sumeven = 0
sumodd = 0
for i,sai in enumerate(sa):
    a[i] = int(sai)

    if i % 2 == 0:
        sumeven += a[i]
    else:
        sumodd += a[i]

ret = 0
sm = 0
for i,ai in enumerate(a):
    if i % 2 == 1:
        ret += max(-sm+1-ai,0)
        sm += max(ai, -sm+1)
    else:
        ret += max(sm+1 + ai, 0)
        sm += min(ai, -(sm+1))
ret2 = 0
sm = 0
for i,ai in enumerate(a):
    if i % 2 == 0:
        ret2 += max(-sm+1-ai,0)
        sm += max(ai, -sm+1)
    else:
        ret2 += max(sm+1 + ai, 0)
        sm += min(ai, -(sm+1))
        
print(min(ret,ret2))

Submission status: Accepted

Solution code:
import itertools

def sign(num):
    if num < 0:
        return -1
    elif num > 0:
        return 1
    else:
        return 0

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

signs = [-1,1]
a_sum = 0
changes_1 = 0
for i, a in enumerate(a_i):
    a_sum += a
    if sign(a_sum) != signs[i%2]:
        changes_1 += abs(a_sum) + 1
        a_sum = signs[i%2]

signs = [1,-1]
a_sum = 0
changes_2 = 0
for i, a in enumerate(a_i):
    a_sum += a
    if sign(a_sum) != signs[i%2]:
        changes_2 += abs(a_sum) + 1
        a_sum = signs[i%2]

print(min(changes_1,changes_2))

Submission status: Accepted

Solution code:
def c(ints):
        
    sig = -1
    sig_ = +1
    total = 0
    total_ = 0
    mov = 0
    mov_ = 0
    
    for i_ in ints:
        tmp = total + i_
        tmp_ = total_ + i_
        if tmp == 0:
            mov +=1
            tmp = -sig
        elif sig * tmp > 0:
            mov += abs(tmp) + 1
            tmp = -sig
            
        if tmp_ == 0:
            mov_ +=1
            tmp_ = -sig_
        elif sig_ * tmp_ > 0:
            mov_ += abs(tmp_) + 1
            tmp_ = -sig_
            
        sig *= -1
        total = tmp
        sig_ *= -1
        total_ = tmp_

    return min(mov, mov_)

_ = input()
inp = input()
inp = inp.split(' ')
inp = [int(i_) for i_ in inp]
print(c(inp))

Submission status: Accepted

Solution code:
import sys
import math

INF = 10**9+7

def k(i):
    if(i == 1):
        return 1
    else:
        return(i * k(i-1))

def comb(n, r):
    if(n == r or r == 1):
        return 1
    else:
        return k(n) / (k(n-r) * k(r))

stdin = sys.stdin
def na(): return map(int, stdin.readline().split())
def ns(): return stdin.readline().strip()
def nsl(): return list(stdin.readline().strip())
def ni(): return int(stdin.readline())
def nil(): return list(map(int, stdin.readline().split()))

n = ni()
a = nil()

b = []

for i in range(n):
    b.append(a[i])


sum = 0

c1 = 0
c2 = 0

if a[0] == 0:
    a[0] = 1
    c1 += 1;

for i in range(0, n-1):
    sum += a[i]

    sum2 = sum + a[i+1]

    if sum2 == 0:
        if sum >0:
            a[i+1] -= 1
            sum2 -= 1
            c1 +=1

    if(sum * sum2 >= 0):
        k = abs(sum2) + 1

        h = k - (abs(sum) - 1)
        l = k - h

        if sum > 0 :
            a[i] -= l
            sum -= l
            a[i + 1] -= h
        else:
            a[i] += l
            sum += l
            a[i + 1] += h

        c1 += h+l


sum = 0

a = b

if a[0] == 0:
    a[0] = -1
    c2 += 1;
else:
    c2 = abs(a[0]) + 1
    if a[0] > 0:
        a[0] = -1
    else:
        a[0] = 1

for i in range(0, n-1):
    sum += a[i]

    sum2 = sum + a[i+1]

    if sum2 == 0:
        if sum >0:
            a[i+1] -= 1
            sum2 -= 1
            c2 +=1

    if(sum * sum2 >= 0):
        k = abs(sum2) + 1

        h = k - (abs(sum) - 1)
        l = k - h

        if sum > 0 :
            a[i] -= l
            sum -= l
            a[i + 1] -= h
        else:
            a[i] += l
            sum += l
            a[i + 1] += h

        c2 += k

print(min(c1, c2))