Problem p03739 (python)

Fixed submission:
n = int(input())
a_list = list(map(int, input().split(' ')))

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

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

Buggy submission (verdict: Runtime Error):
import numpy as np

n = int(input())
a_array = np.array([int(elem) for elem in input().split(' ')])


def calculate_cumulative_sum(a_array, n):
    cumul_sum_array = np.zeros(n, dtype=np.int32)
    cumul_sum_array[0] = a_array[0]
    for i in range(1, n):
        cumul_sum_array[i] = cumul_sum_array[i - 1] + a_array[i]
    return cumul_sum_array


def check_1(cumul_sum_array):
    result = (cumul_sum_array != 0).all()
    return result


def check_2(cumul_sum_array):
    odd = np.sign(cumul_sum[::2])
    even = np.sign(cumul_sum[1::2])
    odd_sign = np.unique(odd)
    even_sign = np.unique(even)
    return (len(odd_sign) == 1) * (len(even_sign) == 1) * (odd_sign[0] != even_sign[0])


cumul_sum = calculate_cumulative_sum(a_array, n)
count = {'plus': 0, 'minus': 0}

def calc_num_manipulation(cumul_sum, start, n):
    cumul_sum = cumul_sum.copy()
    count = 0
    if check_1(cumul_sum) and check_2(cumul_sum):
        return count
    
    for i in range(n):
        if i == 0:
            diff = cumul_sum[i] - start
            count += abs(diff)
            cumul_sum[i:] -= diff
        else:
            if cumul_sum[i - 1] < 0 and cumul_sum[i] <= 0:
                diff = cumul_sum[i] - 1
                count += abs(diff)
                cumul_sum[i:] -= diff
            elif cumul_sum[i - 1] > 0 and cumul_sum[i] >= 0:
                diff = cumul_sum[i] - (-1)
                count += abs(diff)
                cumul_sum[i:] -= diff
            else:
                continue
    return count

count['plus'] = calc_num_manipulation(cumul_sum, 1, n)
count['minus'] = calc_num_manipulation(cumul_sum, -1, n)
print(min(count.values()))

Buggy submission (verdict: Runtime Error):
import numpy as np

n = int(input())
a_array = np.array([int(elem) for elem in input().split(' ')])


def calculate_cumulative_sum(a_array, n):
    cumul_sum_array = np.zeros(n, dtype=np.int32)
    cumul_sum_array[0] = a_array[0]
    for i in range(1, n):
        cumul_sum_array[i] = cumul_sum_array[i - 1] + a_array[i]
    return cumul_sum_array


def check_1(cumul_sum_array):
    result = (cumul_sum_array != 0).all()
    return result


def check_2(cumul_sum_array):
    odd = np.sign(cumul_sum[::2])
    even = np.sign(cumul_sum[1::2])
    odd_sign = np.unique(odd)
    even_sign = np.unique(even)
    return (len(odd_sign) == 1) * (len(even_sign) == 1) * (odd_sign[0] != even_sign[0])


cumul_sum = calculate_cumulative_sum(a_array, n)
count = {'plus': 0, 'minus': 0}

def calc_num_manipulation(cumul_sum, start, n):
    cumul_sum = cumul_sum.copy()
    count = 0
    for i in range(n):
        if check_1(cumul_sum) and check_2(cumul_sum):
            break
        else:
            if i == 0:
                diff = cumul_sum[i] - start
                count += abs(diff)
                cumul_sum[i:] -= diff
            else:
                if cumul_sum[i - 1] < 0 and cumul_sum[i] <= 0:
                    diff = cumul_sum[i] - 1
                    count += abs(diff)
                    cumul_sum[i:] -= diff
                elif cumul_sum[i - 1] > 0 and cumul_sum[i] >= 0:
                    diff = cumul_sum[i] - (-1)
                    count += abs(diff)
                    cumul_sum[i:] -= diff
                else:
                    continue
    return count

count['plus'] = calc_num_manipulation(cumul_sum, 1, n)
count['minus'] = calc_num_manipulation(cumul_sum, -1, n)
print(min(count.values()))