Problem p02604 (python)

Fixed submission:
import numpy as np
from numba import njit, int64
import sys

read = sys.stdin.buffer.read


@njit((int64[:], ), cache=True)
def solve(inp):
    INF = 10 ** 18

    n = inp[0]
    x = inp[1::3]
    y = inp[2::3]
    p = inp[3::3]

    ver = np.empty((1 << n, n), np.int64)
    ver[0] = np.abs(x) * p

    for i in range(n):
        bt = 1 << i
        ver[bt:2 * bt] = np.minimum(ver[:bt], np.abs(x - x[i]) * p)

    hor = np.empty((1 << n, n), np.int64)
    hor[0] = np.abs(y) * p

    for i in range(n):
        bt = 1 << i
        hor[bt:2 * bt] = np.minimum(hor[:bt], np.abs(y - y[i]) * p)

    ans = [INF] * (n + 1)
    for i in range(pow(3, n)):
        vi = 0
        hi = 0
        cnt = 0
        pow2 = 1
        pow3 = i
        while pow3:
            pow3, r = divmod(pow3, 3)
            if r == 1:
                vi += pow2
                cnt += 1
            elif r == 2:
                hi += pow2
                cnt += 1

            pow2 *= 2

        score = np.minimum(ver[vi], hor[hi]).sum()
        ans[cnt] = min(ans[cnt], score)

    return ans


inp = np.array(read().split(), np.int64)
ans = solve(inp)
print(*ans, sep="\n")

Buggy submission (verdict: Runtime Error):
import numpy as np
from numba import njit, int64
import sys

read = sys.stdin.buffer.read


@njit((int64, int64[:], int64[:], ), cache=True)
def pre_calc(n, pos, coef):
    ret = np.empty((1 << n, n), np.int64)
    ret[0] = np.abs(pos) * coef

    for i in range(n):
        bt = 1 << i
        ret[bt:2 * bt] = np.minimum(ret[:bt], np.abs(pos - pos[i]) * coef)

    return ret


@njit((int64[:], ), cache=True)
def solve(inp):
    INF = 10 ** 18

    n = inp[0]
    x = inp[1::3]
    y = inp[2::3]
    p = inp[3::3]

    ver = pre_calc(n, x, p)
    hor = pre_calc(n, y, p)

    states = np.zeros((2, pow(3, n)), np.int16)
    cnts = np.zeros(pow(3, n), np.int8)
    for i in range(pow(3, n)):
        pw2 = 1
        now = i
        for j in range(n):
            r = now % 3
            if r != 0:
                states[r - 1, i] += pw2
                cnts[i] += 1
            pw2 *= 2
            now //= 3

    vals = np.minimum(ver[states[0]], hor[states[1]]).sum(axis=1)
    ans = [INF] * (n + 1)
    for ev, ec in zip(vals, cnts):
        ans[ec] = min(ans[ec], ev)

    return ans


inp = np.array(read().split(), np.int64)
ans = solve(inp)
print(*ans, sep="\n")

Buggy submission (verdict: Runtime Error):
import numpy as np
from numba import njit, int64
import sys

read = sys.stdin.buffer.read


@njit((int64, int64[:], int64[:], ), cache=True)
def pre_calc(n, pos, coef):
    ret = np.empty((1 << n, n), np.int64)
    ret[0] = np.abs(pos) * coef

    for i in range(n):
        bt = 1 << i
        ret[bt:2 * bt] = np.minimum(ret[:bt], np.abs(pos - pos[i]) * coef)

    return ret


@njit((int64[:], ), cache=True)
def solve(inp):
    INF = 10 ** 18

    n = inp[0]
    x = inp[1::3]
    y = inp[2::3]
    p = inp[3::3]

    ver = pre_calc(n, x, p)
    hor = pre_calc(n, y, p)

    states = np.zeros((2, pow(3, n)), np.int64)
    cnts = np.zeros(pow(3, n), np.int64)
    for i in range(pow(3, n)):
        pw2 = 1
        now = i
        for j in range(n):
            r = now % 3
            if r != 0:
                states[r - 1, i] += pw2
                cnts[i] += 1
            pw2 *= 2
            now //= 3

    vi, hi = states[0], states[1]
    vals = np.minimum(ver[vi], hor[hi]).sum(axis=1)
    ans = [INF] * (n + 1)
    for ev, ec in zip(vals, cnts):
        ans[ec] = min(ans[ec], ev)

    return ans


inp = np.array(read().split(), np.int64)
ans = solve(inp)
print(*ans, sep="\n")