Problem p02648 (python)

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

input = sys.stdin.buffer.readline

N = int(input())
vws = [tuple(map(int, input().split())) for _ in range(N)]

n = min(2**11, N)
vws_arr = np.array(vws, np.int32)

dp = np.zeros((n, 10**5+1), np.int32)
for i in range(n):
    v, w = vws[i]
    dp[i] = dp[(i-1)//2]
    dp[i, w:] = np.maximum(dp[(i-1)//2, :-w]+v, dp[i, w:])


@njit("i4(i4[::1], i4[::1], i4, i4[::1])", cache = True)
def func(vs, ws, L, dp_):
    values = np.zeros(1 << len(vs), np.int32)
    weights = np.zeros(1 << len(vs), np.int32)
    for i in range(len(vs)):
        for bits in range(1 << len(vs)):
            if bits & (1 << i):
                values[bits] += vs[i]
                weights[bits] += ws[i]
    ret = 0
    for v, w in zip(values, weights):
        if w <= L:
            ret = max(dp_[L - w] + v, ret)
    return ret

Q = int(input())
for _ in range(Q):
    V, L = map(int, input().split())
    V -= 1
    if V < n:
        print(dp[V, L])
    else:
        Vs = []
        while V >= n:
            Vs.append(V)
            V = (V-1) // 2
        vs = vws_arr[Vs, 0]
        ws = vws_arr[Vs, 1]
        print(func(vs, ws, L, dp[V]))

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

input = sys.stdin.buffer.readline

#'''
N = int(input())
vws = [tuple(map(int, input().split())) for _ in range(N)]
'''
N = 2 ** 18
vws = [(1, i%10 + 1) for i in range(N)]
#'''
half = 10

n = min(2**half, N)
vws_arr = np.array(vws)

dp = np.zeros((n, 10**5+1), np.int32)
dp[0, vws[0][1]:] = vws[0][0]
for i in range(1, n):
    v, w = vws[i]
    dp[i] = dp[(i-1)//2]
    dp[i, w:] = np.maximum(dp[(i-1)//2, :-w]+v, dp[i, w:])


@njit("i4(i4[:], i4[:], i4, i4[:])", cache = True)
def func(vs, ws, L, dp_):
    values = np.zeros(len(vs), np.int32)
    weights = np.zeros(len(vs), np.int32)
    for bits in range(1 << len(vs)):
        for i in range(len(vs)):
            if bits & (1 << i):
                values[bits] += vs[i]
                weights[bits] += ws[i]
    mask = (weights <= L)     
    return np.amax(dp_[L - weights[mask]] + values[mask])

Q = int(input())
for _ in range(Q):
    V, L = map(int, input().split())
    V -= 1
    if V < n:
        print(dp[V, L])
    else:
        Vs = []
        while V >= n:
            Vs.append(V)
            V = (V-1) // 2
        vs = vws_arr[Vs, 0]
        ws = vws_arr[Vs, 1]
        print(func(np.array(vs, np.int32), np.array(ws, np.int32), L, dp[V]))

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

input = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(input())
vws = [tuple(map(int, input().split())) for _ in range(N)]
vs, ws = map(lambda ls: np.array(ls, np.int32), zip(*vws))
Q = int(input())
VLs = np.array(list(map(lambda s: tuple(map(int, s.split())), readlines())), np.int32)

n = min(2**10, N)

@njit("i4[:, :](i4, i4[:], i4[:])", cache = True)
def precompute(n, vs, ws):
    dp = np.zeros((n, 10**5+1), np.int32)
    dp[0, ws[0]:] = vs[0]
    for i in range(1, n):
        v = vs[i]
        w = ws[i]
        dp[i] = dp[(i-1)//2]
        dp[i, w:] = np.maximum(dp[(i-1)//2, :-w]+v, dp[i, w:])
    return dp

@njit("i4(i4[:], i4[:], i4, i4[:])", cache = True)
def func(vs_part, ws_part, L, dp_):
    bits = np.arange(1 << len(vs_part))
    values = np.zeros_like(bits, np.int32)
    weights = np.zeros_like(bits, np.int32)
    for i in range(len(vs_part)):
        indices = np.nonzero(np.bitwise_and(bits, 1<<i))
        values[indices] += vs_part[i]
        weights[indices] += ws_part[i]

    mask = (weights <= L)     
    return np.amax(dp_[L - weights[mask]] + values[mask])

#@njit("(i4, i4[:], i4[:], i4[:, :])", cache = True)
def main(n, vs, ws, VLs):
    dp = precompute(n, vs, ws)

    for V, L in VLs:
        V -= 1
        if V < n:
            print(dp[V, L])
        else:
            Vs = []
            while V >= n:
                Vs.append(V)
                V = (V-1) // 2
            Vs_arr = np.array(Vs)
            vs_part = vs[Vs_arr]
            ws_part = ws[Vs_arr]
            ans = func(vs_part, ws_part, L, dp[V])
            print(ans)

main(N, vs, ws, VLs)

Buggy submission (verdict: Runtime Error):
import sys
import numpy as np
from numba import njit
input = sys.stdin.buffer.readline

N = int(input())
vws = [tuple(map(int, input().split())) for _ in range(N)]

n = min(2**10, N)

dp = np.zeros((n, 10**5+1), np.int32)
dp[0, vws[0][1]:] = vws[0][0]
for i in range(1, n):
    v, w = vws[i]
    dp[i] = dp[(i-1)//2]
    dp[i, w:] = np.maximum(dp[(i-1)//2, :-w]+v, dp[i, w:])

@njit(cache = True)
def func():
    bits = np.arange(1 << len(vws_choose))
    values = np.zeros_like(bits, np.int32)
    weights = np.zeros_like(bits, np.int32)
    for i in range(len(vws_choose)):
        indices = np.nonzero(np.bitwise_and(bits, 1<<i))
        values[indices] += vws_choose[i][0]
        weights[indices] += vws_choose[i][1]

    mask = (weights <= L)     
    print(np.amax(dp[V][L - weights[mask]] + values[mask]))


Q = int(input())
for _ in range(Q):
    V, L = map(int, input().split())
    V -= 1
    if V < n:
        print(dp[V][L])
    else:
        vws_choose = []
        while V >= n:
            vws_choose.append(vws[V])
            V = (V-1) // 2
        func()

Buggy submission (verdict: Runtime Error):
import sys
import numpy as np
input = sys.stdin.buffer.readline

N = int(input())
vws = [tuple(map(int, input().split())) for _ in range(N)]

n = min(2**10, N)

dp = np.zeros((n, 10**5+1), np.int64)
dp[0, vws[0][1]:] = vws[0][0]
for i in range(1, n):
    v, w = vws[i]
    dp[i] = dp[(i-1)//2]
    dp[i, w:] = np.maximum(dp[(i-1)//2, :-w]+v, dp[i, w:])

Q = int(input())
for _ in range(Q):
    V, L = map(int, input().split())
    V -= 1
    if V < n:
        print(dp[V][L])
    else:
        vws_choose = []
        while V >= n:
            vws_choose.append(vws[V])
            V = (V-1) // 2

        bits = np.arange(1 << len(vws_choose))
        values = np.zeros_like(bits, np.int64)
        weights = np.zeros_like(bits, np.int64)
        for i in range(len(vws_choose)):
            indices = np.nonzero(np.bitwise_and(bits, 1<<i))
            values[indices] += vws_choose[i][0]
            weights[indices] += vws_choose[i][1]

        mask = (weights <= L)     
        print(np.amax(dp[V][L - weights[mask]] + values))

Buggy submission (verdict: Runtime Error):
import numpy as np
import sys
input = sys.stdin.buffer.readline

N = int(input())
vws = [tuple(map(int, input().split())) for _ in range(N)]

dp = np.zeros((N, 10**5+1), np.int64)
dp[0, vws[0][1]:] = vws[0][0]
for i in range(1, N):
    v, w = vws[i]
    dp[i] = dp[(i-1)//2]
    dp[i, w:] = np.maximum(dp[(i-1)//2, :-w]+v, dp[i, w:])

Q = int(input())
for _ in range(Q):
    V, L = map(int, input().split())
    print(dp[V-1][L])