alexjercan/bugnet huggingface dataset:
Problem p03161 (Python) — buggy submission, verdict: Runtime Error

N, K = map(int, input().split())
H = list(map(int, input().split()))
dp = [1 << 62] * N
dp[0] = 0
for i in range(1, N):
    for j in range(i, min(i + K, N)):
        cost = abs(H[i - 1] - H[j])
        dp[j] = min(dp[j], dp[i - 1] + cost)
print(dp[-1])


Error: IndexError: list index out of range
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03161/Python/s787574815.py", line 7, in <module>
    cost = abs(H[i - 1] - H[i + j])
IndexError: list index out of range


Fixed submission:

N, K = map(int, input().split())
H = list(map(int, input().split()))
dp = [1 << 62] * N
dp[0] = 0
for i in range(1, N):
    for j in range(i, min(i + 1 + K, N)):
        cost = abs(H[i - 1] - H[i + j])
        dp[i + j] = min(dp[i + j], dp[i - 1] + cost)
print(dp[-1])
Problem p03161 (Python) — buggy submission, verdict: Runtime Error

N, K = map(int, input().split())
h = list(map(int, input().split()))
inf = 10**9
dp = [inf] * N
dp[0] = 0


def chmin(num: int, sbn: int):
    dp[num + sbn] = min((dp[num + sbn], dp[num] + abs(h[i + sbn] - h[num])))


for i in range(N):
    for sbn in range(K):
        try:
            chmin(i, sbn + 1)
        except IndexError:
            break
print(dp[-1])


Error: TypeError: chmin() takes 2 positional arguments but 3 were given
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03161/Python/s825976309.py", line 15, in <module>
    chmin(dp, i, sbn + 1)
TypeError: chmin() takes 2 positional arguments but 3 were given


Fixed submission:

N, K = map(int, input().split())
h = list(map(int, input().split()))
inf = 10**9
dp = [inf] * N
dp[0] = 0


def chmin(num: int, sbn: int):
    dp[num + sbn] = min((dp[num + sbn], dp[num] + abs(h[i + sbn] - h[num])))


for i in range(N):
    for sbn in range(K):
        try:
            chmin(dp, i, sbn + 1)
        except IndexError:
            break
print(dp[-1])
Problem p03161 (Python) — buggy submission, verdict: Time Limit Exceeded

#!/usr/bin/env python3
import numpy as np

N, K = list(map(int, input().split()))
h_list = list(map(int, input().split()))
h_list = np.array(h_list)
dp_list = np.zeros(N)
dp_list[0] = 0

for i in range(1, N):
    # j番目の足場からi番目の足場に飛ぶ

    min_j = max(i - K, 0)
    cost_list = dp_list[min_j:i] + np.abs(h_list[i] - h_list[min_j:i])
    # 最適コストで更新
    dp_list[i] = np.min(cost_list)

ans = int(dp_list[-1])
print(ans)


Error: TLE

Fixed submission:

#!/usr/bin/env python3

N, K = list(map(int, input().split()))
h_list = list(map(int, input().split()))
dp_list = [float("inf")] * N
dp_list[0] = 0

for i in range(1, N):
    # j番目の足場からi番目の足場に飛ぶ
    min_cost = float("inf")
    for j in range(max(i - K, 0), i):

        # j番目の足場までの最適コスト
        cum_cost = dp_list[j]

        # j番目の足場から飛んだ場合のコスト
        cost = dp_list[j] + abs(h_list[j] - h_list[i])
        if cost < min_cost:
            min_cost = cost

    # 最適コストで更新
    dp_list[i] = min_cost

ans = dp_list[-1]
print(ans)
Problem p03161 (Python) — buggy submission, verdict: Runtime Error

import numpy as np

#  入力
n, k = list(map(int, input().split()))
h = np.array(list(map(int, input().split())))

#  方針
#  i番目の足場にいるという状態に対して
#  その足場に最も少ないコストを記録する．

dp = np.zeros(n, dtype=np.int)
for i in range(1, n):
    st = max(0, i - k)
    dp[i] = np.min(dp[st:i] + np.abs(h[i] - h[st:i]))

print(dp[-1])


Error: TLE

Fixed submission:

import numpy as np

#  入力
n, k = list(map(int, input().split()))
h = list(map(int, input().split()))

#  方針
#  i番目の足場にいるという状態に対して
#  その足場に最も少ないコストを記録する．

dp = np.zeros(n)
for i in range(1, n):
    st = max(0, i - k)
    dp[i] = np.min(dp[st:i] + np.abs(h[i] - h[st:i]))

print(dp[n - 1])
Problem p03161 (Python) — buggy submission, verdict: Time Limit Exceeded

N, K = list(map(int, input().split()))
h_list = list(map(int, input().split()))
dp_list = [float("inf")] * N
dp_list[0] = 0

for i in range(1, N):
    # j番目の足場からi番目の足場に飛ぶ

    for j in range(max(i - K, 0), i + 1):

        # j番目の足場までの最適コスト
        cum_cost = dp_list[j]

        # j番目の足場から飛んだ場合のコスト
        cost = dp_list[j] + abs(h_list[j] - h_list[i])

        # 最適コストで更新
        dp_list[i] = min(dp_list[i], cost)

ans = dp_list[-1]
print(ans)


Error: TLE

Fixed submission:

#!/usr/bin/env python3

N, K = list(map(int, input().split()))
h_list = list(map(int, input().split()))
dp_list = [float("inf")] * N
dp_list[0] = 0

for i in range(1, N):
    # j番目の足場からi番目の足場に飛ぶ

    for j in range(max(i - K, 0), i + 1):

        # j番目の足場までの最適コスト
        cum_cost = dp_list[j]

        # j番目の足場から飛んだ場合のコスト
        cost = dp_list[j] + abs(h_list[j] - h_list[i])

        # 最適コストで更新
        dp_list[i] = min(dp_list[i], cost)

ans = dp_list[-1]
print(ans)
Problem p03161 (Python) — buggy submission, verdict: Time Limit Exceeded

n, k, *L = map(int, open(0).read().split())

# dp = [float("inf")] * n
dp = [0] * n

for i in range(1, n):
    j = max(0, i - k)
    e = L[i]
    dp[i] = min([DP + abs(e - COST) for DP, COST in zip(dp[j:i], L[j:i])])

print(dp[-1])


Error: TLE

Fixed submission:

n, k, *L = map(int, open(0).read().split())

# dp = [float("inf")] * n
dp = [0] * n

for i in range(1, n):
    j = max(0, i - k)
    dp[i] = min([DP + abs(L[i] - COST) for DP, COST in zip(dp[j:i], L[j:i])])

print(dp[-1])
Problem p03161 (Python) — buggy submission, verdict: Time Limit Exceeded

# DP(貰うDP)
def main():
    N, K = map(int, input().split())
    h = list(map(int, input().split()))

    dp = [0] * N
    for i in range(1, N):
        dp[i] = min(dp[j] + abs(h[i] - h[j]) for j in range(max(0, i - K), i))
    print(dp[N - 1])


main()


Error: TLE

Fixed submission:

# DP(貰うDP)
N, K = map(int, input().split())
h = list(map(int, input().split()))

dp = [0] * N
for i in range(1, N):
    dp[i] = min(dp[j] + abs(h[i] - h[j]) for j in range(max(0, i - K), i))
print(dp[N - 1])
Problem p03161 (Python) — buggy submission, verdict: Time Limit Exceeded

n, k = map(int, input().split())
h = tuple(map(int, input().split()))

INF = 10**10
dp = [INF] * n
dp[0] = 0
# dp[i] = (足場iにたどり着いた時点でのコストの最小値)

for i in range(1, n):
    dp[i] = min(dp[j] + abs(h[j] - h[i]) for j in range(max(0, i - k), i))

print(dp[n - 1])


Error: TLE

Fixed submission:

n, k = map(int, input().split())
h = tuple(map(int, input().split()))

INF = 10**10
dp = [INF] * n
dp[0] = 0
# dp[i] = (足場iにたどり着いた時点でのコストの最小値)

for i in range(n):
    for j in range(i + 1, min(i + k + 1, n)):
        if dp[j] > dp[i] + abs(h[j] - h[i]):
            dp[j] = dp[i] + abs(h[j] - h[i])

print(dp[n - 1])
Problem p03161 (Python) — buggy submission, verdict: Time Limit Exceeded

def main():
    N, K = map(int, input().split())
    h = [0] + list(map(int, input().split()))
    dp = [float("inf") for _ in range(N + 1)]

    dp[1] = 0
    for i in range(2, N + 1):
        dp[i] = min(dp[j] + abs(h[i] - h[j]) for j in range(max(0, i - K), i))

    print(dp[-1])


main()


Error: TLE

Fixed submission:

N, K = map(int, input().split())
h = [0] + list(map(int, input().split()))
dp = [float("inf") for _ in range(N + 1)]

dp[1] = 0
for i in range(2, N + 1):
    dp[i] = min(dp[j] + abs(h[i] - h[j]) for j in range(max(0, i - K), i))

print(dp[-1])
Problem p03161 (Python) — buggy submission, verdict: Runtime Error

N, K = map(int, input().split())
hlist = list(map(int, input().split())) + [0] * (K + 2)
inf = float("inf")
dp = [inf] * (N * K + 2)


def abs_sbn(a, b):
    return abs(hlist[b] - hlist[a])


dp[0] = 0
for i in range(N):
    for j in range(1, K + 1):
        dp[i + j] = min(dp[i + j], dp[i] + abs_sbn(i, i + j))

print(dp[N - 1])


Error: 0

Fixed submission:

N, K = map(int, input().split())
hlist = list(map(int, input().split())) + [0] * K
inf = float("inf")
dp = [inf] * (N * K)


def abs_sbn(a, b):
    return abs(hlist[b] - hlist[a])


dp[0] = 0
for i in range(N):
    for j in range(1, K + 1):
        dp[i + j] = min(dp[i + j], dp[i] + abs_sbn(i, i + j))

print(dp[N - 1])
Problem p03162 (Python) — buggy submission, verdict: Runtime Error

# dp[i日目][i-1日目にどれを選んだか] = i日目までの最大の幸福度
import sys

sys.setrecursionlimit(10**8)

n = int(input())
a, b, c = [0 for _ in range(n)], [0 for _ in range(n)], [0 for _ in range(n)]
for i in range(n):
    a[i], b[i], c[i] = map(int, input().split())

dp = [[-1 for _ in range(3)] for _ in range(n + 1)]
for i in range(3):
    dp[0][i] = 0


def rec(i, choice):
    if dp[i][choice] > 0:
        return dp[i][choice]
    if i == n:
        return 0
    ret = 0  # ループじゃなくてifでやってるのでそれぞれ確実に大きい値が入る（ので更新する必要がない）
    if choice == 0:
        ret = a[i] + max(rec(i + 1, 1), rec(i + 1, 2))
    elif choice == 1:
        ret = b[i] + max(rec(i + 1, 0), rec(i + 1, 2))
    elif choice == 2:
        ret = c[i] + max(rec(i + 1, 0), rec(i + 1, 1))
    dp[i][choice] = ret
    return ret


print(max(rec(0, 0), rec(0, 1), rec(0, 2)))


Error: 0

Fixed submission:

# dp[i日目][i-1日目にどれを選んだか] = i日目までの最大の幸福度
n = int(input())
a, b, c = [0 for _ in range(n)], [0 for _ in range(n)], [0 for _ in range(n)]
for i in range(n):
    a[i], b[i], c[i] = map(int, input().split())

dp = [[-1 for _ in range(3)] for _ in range(n + 1)]
for i in range(3):
    dp[0][i] = 0


def rec(i, choice):
    if dp[i][choice] > 0:
        return dp[i][choice]
    if i == n:
        return 0
    ret = 0  # ループじゃなくてifでやってるのでそれぞれ確実に大きい値が入る（ので更新する必要がない）
    if choice == 0:
        ret = a[i] + max(rec(i + 1, 1), rec(i + 1, 2))
    elif choice == 1:
        ret = b[i] + max(rec(i + 1, 0), rec(i + 1, 2))
    elif choice == 2:
        ret = c[i] + max(rec(i + 1, 0), rec(i + 1, 1))
    dp[i][choice] = ret
    return ret


print(max(rec(0, 0), rec(0, 1), rec(0, 2)))
Problem p03163 (Python) — buggy submission, verdict: Time Limit Exceeded

import sys

import numba as nb
import numpy as np

input = sys.stdin.readline


@nb.njit("i8(i8, i8)")
def chmax(a, b):
    if a > b:
        return a
    else:
        return b


@nb.njit("i8(i8,i8,i8[:,:])")
def solve(N, W, wv):
    dp = [[0] * (W + 1) for _ in range(N + 1)]
    for i in range(1, N + 1):
        w, v = wv[i - 1]
        for j in range(1, W + 1):
            if j < w:
                dp[i][j] = dp[i - 1][j]
            else:
                dp[i][j] = chmax(dp[i - 1][j - w] + v, dp[i - 1][j])
    return dp[-1][-1]


def main():
    N, W = map(int, input().split())
    wv = np.zeros(shape=(N, 2), dtype=np.int64)
    for i in range(N):
        w, v = map(int, input().split())
        wv[i] = w, v

    ans = solve(N, W, wv)
    print(ans)


if __name__ == "__main__":
    main()


Error: TLE

Fixed submission:

import sys

input = sys.stdin.readline


def chmax(a, b):
    if a > b:
        return a
    else:
        return b


def main():
    N, W = map(int, input().split())
    wv = [None] * N
    for i in range(N):
        wv[i] = tuple(map(int, input().split()))

    dp = [[0] * (W + 1) for _ in range(N + 1)]
    for i, (w, v) in enumerate(wv, start=1):
        dp_i = dp[i]
        dp_im = dp[i - 1]
        for j in range(1, W + 1):
            if j < w:
                dp_i[j] = dp_im[j]
            else:
                dp_i[j] = chmax(dp_im[j - w] + v, dp_im[j])

    ans = dp[-1][-1]
    print(ans)


if __name__ == "__main__":
    main()
Problem p03163 (Python) — buggy submission, verdict: Runtime Error

N, W = map(int, input().split())
wv = []
for _ in range(N):
    wv.append(list(map(int, input().split())))
# print(wv)
dp = [[0] * 100100 for _ in range(110)]


for i in range(N):
    for sum_w in range(W + 1):

        if wv[i][0] <= sum_w:
            dp[i + 1][sum_w] = max(dp[i + 1][sum_w], dp[i][sum_w - wv[i][0]] + wv[i][1])

        dp[i + 1][sum_w] = max(dp[i + 1][sum_w], dp[i][sum_w])
print(dp[N][W])


Error: 1
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03163/Python/s607479372.py", line 6, in <module>
    dp = [[0] * 110 for _ in range(100100)]
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03163/Python/s607479372.py", line 6, in <listcomp>
    dp = [[0] * 110 for _ in range(100100)]
MemoryError


Fixed submission:

N, W = map(int, input().split())
wv = []
for _ in range(N):
    wv.append(list(map(int, input().split())))
# print(wv)
dp = [[0] * 110 for _ in range(100100)]


for i in range(N):
    for sum_w in range(W + 1):

        if wv[i][0] <= sum_w:
            dp[i + 1][sum_w] = max(dp[i + 1][sum_w], dp[i][sum_w - wv[i][0]] + wv[i][1])

        dp[i + 1][sum_w] = max(dp[i + 1][sum_w], dp[i][sum_w])
print(dp[N][W])
Problem p03163 (Python) — buggy submission, verdict: Time Limit Exceeded

import sys

input = sys.stdin.readline


def main():
    N, W = map(int, input().split())
    wv = [None] * N
    for i in range(N):
        wv[i] = tuple(map(int, input().split()))

    dp = [[0] * (W + 1) for _ in range(N + 1)]
    for i, (w, v) in enumerate(wv, start=1):
        dp_i = dp[i]
        dp_im = dp[i - 1]
        for j in range(1, W + 1):
            if j < w:
                dp_i[j] = dp_im[j]
            else:
                a = dp_im[j - w] + v
                b = dp_im[j]
                if a > b:
                    dp_i[j] = a
                else:
                    dp_i[j] = b

    ans = dp[-1][-1]
    print(ans)


if __name__ == "__main__":
    main()


Error: TLE

Fixed submission:

import sys

input = sys.stdin.readline


def main():
    N, W = map(int, input().split())
    wv = [None] * N
    for i in range(N):
        wv[i] = tuple(map(int, input().split()))

    dp = [[0] * (W + 1) for _ in range(N + 1)]
    for i, (w, v) in enumerate(wv, start=1):
        dp_i = dp[i]
        dp_im = dp[i - 1]
        for j in range(1, W + 1):
            if j < w:
                dp_i[j] = dp_im[j]
            elif dp_im[j - w] + v > dp_im[j]:
                dp_i[j] = dp_im[j - w] + v
            else:
                dp_i[j] = dp_im[j]

    ans = dp[-1][-1]
    print(ans)


if __name__ == "__main__":
    main()
Problem p03163 (Python) — buggy submission, verdict: Time Limit Exceeded

import numpy as np
from numba import njit, i8


@njit(i8(i8, i8, i8[:], i8[:]), cache=True)
def solve(N, W, weights, values):
    dp = np.full((N + 1, W + 1), 0, dtype=np.int64)
    dp[0][0] = 0
    for i in range(N):
        for j in range(W + 1):
            dp[i + 1][j] = max(dp[i + 1][j], dp[i][j])
            if j + weights[i] <= W:
                dp[i + 1][j + weights[i]] = max(
                    dp[i + 1][j + weights[i]], dp[i][j] + values[i]
                )
    return dp[-1].max()


N, W = map(int, input().split())
weights = np.zeros(N, dtype=np.int64)
values = np.zeros(N, dtype=np.int64)
for i in range(N):
    w, v = map(int, input().split())
    weights[i] = w
    values[i] = v
print(solve(N, W, weights, values))


Error: TLE

Fixed submission:

import numpy as np

N, W = map(int, input().split())
dp = np.full((N + 1, W + 1), -np.inf, dtype=np.int64)
dp[0][0] = 0
for i in range(N):
    w, v = map(int, input().split())
    for j in range(W + 1):
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j])
        if j + w <= W:
            dp[i + 1][j + w] = max(dp[i + 1][j + w], dp[i][j] + v)
print(max(dp[-1]))
Problem p03163 (Python) — buggy submission, verdict: Time Limit Exceeded

n, w = map(int, input().strip().split())
data = [list(map(int, input().strip().split())) for _ in range(n)]

# i番目に重さがj以下となるように選んだときの価値の総和の最大値
dp = [[0] * (w + 1) for _ in range(n)]

for i in range(0, n):
    for j in range(w + 1):
        # i 番目の品物の重さと価値
        wi, vi = data[i]
        if i == 0:
            take = vi if j - wi >= 0 else 0
            miss = 0
            dp[i][j] = max(take, miss)
        else:
            # i番目の品物を入れる場合
            take = dp[i - 1][j - wi] + vi if j - wi >= 0 else 0
            # 入れない場合
            miss = dp[i - 1][j]
            dp[i][j] = max(take, miss)

print(max(dp[n - 1]))


Error: TLE

Fixed submission:

import numpy as np

n, w = map(int, input().strip().split())
data = [list(map(int, input().strip().split())) for _ in range(n)]

# i番目に重さがj以下となるように選んだときの価値の総和の最大値
dp = np.zeros((n, w + 1), dtype=np.int64)

for i in range(0, n):
    for j in range(w + 1):
        # i 番目の品物の重さと価値
        wi, vi = data[i]
        if i == 0:
            take = vi if j - wi >= 0 else 0
            miss = 0
            dp[i][j] = max(take, miss)
        else:
            # i番目の品物を入れる場合
            take = dp[i - 1][j - wi] + vi if j - wi >= 0 else 0
            # 入れない場合
            miss = dp[i - 1][j]
            dp[i][j] = max(take, miss)

print(np.max(dp[n - 1]))
Problem p03163 (Python) — buggy submission, verdict: Time Limit Exceeded

n, max_weight = map(int, input().split())
WV = [list(map(int, input().split())) for _ in range(n)]
W, V = zip(*WV)
dp = list([0] * (max_weight + 1) for _ in range(n + 1))
for i in range(n):
    for w in range(1, max_weight + 1):
        if w >= W[i]:
            dp[i + 1][w] = max(dp[i][w], dp[i][w - W[i]] + V[i])
        else:
            dp[i + 1][w] = dp[i][w]

print(dp[n][max_weight])


Error: TLE

Fixed submission:

import numpy as np

n, max_weight = map(int, input().split())
WV = np.array([input().split() for _ in range(n)], np.int64)

W, V = zip(*WV)
dp = np.zeros((n + 1, max_weight + 1), np.int64)

# O(NW)
for i in range(n):
    for w in range(1, max_weight + 1):
        if w >= W[i]:
            dp[i + 1, w] = max(dp[i, w], dp[i, w - W[i]] + V[i])
        else:
            dp[i + 1, w] = dp[i, w]

print(dp[n, max_weight])
Problem p03163 (Python) — buggy submission, verdict: Time Limit Exceeded

def knapsack1(weight_value, w):
    cur = [0] * (w + 1)
    nxt = [0] * (w + 1)

    for weight, value in weight_value:
        for i in range(1, weight):
            nxt[i] = cur[i]
        for i in range(weight, w + 1):
            nxt[i] = max(cur[i], cur[i - weight] + value)
        cur, nxt = nxt, cur
    return cur[w]


def main():
    n, w = [int(x) for x in input().split()]
    weight_value = [None] * n
    for i in range(n):
        weight_value[i] = [int(x) for x in input().split()]
    return knapsack1(weight_value, w)


print(main())


Error: TLE

Fixed submission:

def knapsack1(weight_value, w):
    cur = [0] * (w + 1)
    nxt = cur.copy()

    for weight, value in weight_value:
        for i in range(1, weight):
            nxt[i] = cur[i]
        for i in range(weight, w + 1):
            nxt[i] = max(cur[i], cur[i - weight] + value)
        cur, nxt = nxt, cur
    return cur[w]


def main():
    n, w = [int(x) for x in input().split()]
    weight_value = [None] * n
    for i in range(n):
        weight_value[i] = [int(x) for x in input().split()]
    return knapsack1(weight_value, w)


print(main())
Problem p03163 (Python) — buggy submission, verdict: Time Limit Exceeded

"""
AtCoder Educational DP Contest
https://atcoder.jp/contests/dp/tasks/dp_d

D - Knapsack 1
"""


def solve(wv_pairs):
    dp = [0] * (W + 1)
    for i in range(N):
        w_i, v_i = wv_pairs[i]
        for ww in range(W, w_i - 1, -1):
            dp[ww] = max(dp[ww], dp[ww - w_i] + v_i)
    return max(dp)


N, W = map(int, input().split())
pairs = [[int(s) for s in input().split()] for _ in range(N)]
res = solve(pairs)
print(res)


Error: TLE

Fixed submission:

"""
AtCoder Educational DP Contest
https://atcoder.jp/contests/dp/tasks/dp_d

D - Knapsack 1
"""
INF = 100000007


def solve(wv_pairs):
    dp = [0] * (W + 1)
    for i in range(N):
        w_i, v_i = wv_pairs[i]
        for ww in range(W, w_i - 1, -1):
            dp[ww] = max(dp[ww], dp[ww - w_i] + v_i)
    return max(dp[ww] for ww in range(W + 1))


N, W = map(int, input().split())
pairs = [[int(s) for s in input().split()] for _ in range(N)]
res = solve(pairs)
print(res)
Problem p03163 (Python) — buggy submission, verdict: Time Limit Exceeded

import numpy as np

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

table = np.zeros((N + 1, W + 1), dtype=np.int64)
for n in range(N):
    tmp = np.zeros(W + 1, dtype=np.int64)
    tmp[goods[n][0] :] = table[n][: W + 1 - goods[n][0]] + goods[n][1]
    table[n + 1] = np.maximum(table[n], tmp)
print(table[N][W])


Error: TLE

Fixed submission:

N, W = map(int, input().split())
goods = []
for _ in range(N):
    goods.append(tuple(map(int, input().split())))

table = [[0] * (W + 1) for _ in range(N + 1)]
for n in range(N):
    for w in range(W + 1):
        addn = table[n][w - goods[n][0]] + goods[n][1] if w - goods[n][0] >= 0 else 0
        table[n + 1][w] = max(table[n][w], addn)
print(table[N][W])
Problem p03163 (Python) — buggy submission, verdict: Time Limit Exceeded

import numpy as np


def main():
    N, W = list(map(int, input().split(" ")))
    items = np.array([list(map(int, input().split(" "))) for _ in range(N)])
    dp = np.zeros((N, W + 1))
    dp[0, items[0, 0] :] = items[0, 1]
    for n in range(1, N):
        weight, value = items[n, 0], items[n, 1]
        dp[n, :] = dp[n - 1, :]
        dp[n, weight:] = np.fmax(dp[n, weight:], value + dp[n - 1, :(-weight)])
    print(int(dp[N - 1, W]))


if __name__ == "__main__":
    main()


Error: TLE

Fixed submission:

import numpy as np

#  入力
n, w = map(int, input().split())
wvList = []
wv = np.array([list(map(int, input().split())) for i in range(n)])

#  考えるDPテーブルは
#  重さの総和と使う荷物に関するもの
#  行が重さの上限
#  列が使える荷物の数に相当
dp = np.zeros((w + 1, n + 1), dtype=np.int)
for i in range(1, n + 1):
    #  更新の発想(wに意味はない)
    #  使える荷物が増えたが，利用はしない
    #  dp[w, i] = dp[w, i - 1]
    #  荷物を新しく許可された荷物を使う
    #  dp[w, i] = dp[w - wNew, i-1] + newValue
    #  このうちの価値の大きい方を使う
    dp[:, i] = dp[:, i - 1]
    wNew, vNew = wv[i - 1, 0], wv[i - 1, 1]
    dp[wNew:, :] = np.fmax(dp[wNew:, :], vNew + dp[:-wNew, :])

print(dp[w, n])
Problem p03163 (Python) — buggy submission, verdict: Time Limit Exceeded

def main():
    N, W = map(int, input().split())
    WV = []
    for _ in range(N):
        w, v = map(int, input().split())
        WV.append((w, v))
    dp = [[0] * (W + 1) for _ in range(N + 1)]
    for i in range(N):
        for j in range(W + 1):
            if j >= WV[i][0]:
                dp[i + 1][j] = max(dp[i][j], dp[i][j - WV[i][0]] + WV[i][1])
            else:
                dp[i + 1][j] = dp[i][j]
    print(dp[-1][-1])


main()


Error: TLE

Fixed submission:

def main():
    N, W = map(int, input().split())
    wl = []
    vl = []
    for _ in range(N):
        w, v = map(int, input().split())
        wl.append(w)
        vl.append(v)
    dp = [[0] * (W + 1) for _ in range(N + 1)]
    for i in range(N):
        for j in range(W + 1):
            if wl[i] > j:
                dp[i + 1][j] = dp[i][j]
            else:
                dp[i + 1][j] = max(dp[i][j], dp[i][j - wl[i]] + vl[i])
    print(dp[N][W])


main()