Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10000)

from functools import lru_cache

def resolve():
    N, W = map(int, input().split())    
    w_v = [list(map(int, input().split())) for _ in range(N)]  # [N][0:weight, 1:value]

    dp = [[-1] * (W+1) for _ in range(N+1)] 

    #print(dp)
 
    @lru_cache(maxsize=None)
    def dfs(w, i):
        if dp[i][w] >= 0:
            return dp[i][w]
        if i >= N:
            res = 0
        elif w < w_v[i][0]:
            res = dfs(w, i+1)
        else:
            res = max(dfs(w - w_v[i][0], i+1) + w_v[i][1], dfs(w, i+1))

        dp[i][w] = res
        return res

    res = dfs(W, 0)
    print(res)
    
    
if __name__ == "__main__":
    resolve()

Fixed submission:
def resolve():
    N, W = map(int, input().split())    
    w_v = [list(map(int, input().split())) for _ in range(N)]  # [N][0:weight, 1:value]

    dp = [[0] * (W+1) for _ in range(N+1)] 

    for i in range(N-1,-1,-1):
        for j in range(W+1):
            w = w_v[i][0]
            v = w_v[i][1]
            if j < w_v[i][0]:
                dp[i][j] = dp[i+1][j]
            else:
                dp[i][j] = max(dp[i+1][j], dp[i+1][j - w] + v)
 
    print(dp[0][W])
 
if __name__ == "__main__":
    resolve()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10000)

from functools import lru_cache

def resolve_by_dfs():
    N, W = map(int, input().split())    
    w_v = [list(map(int, input().split())) for _ in range(N)]  # [N][0:weight, 1:value]
 
    @lru_cache(maxsize=None)
    def dfs(w, i):
        if i >= N:
            res = 0
        elif w < w_v[i][0]:
            res = dfs(w, i+1)
        else:
            res = max(dfs(w - w_v[i][0], i+1) + w_v[i][1], dfs(w, i+1))
        return res

    res = dfs(W, 0)
    print(res)
    
if __name__ == "__main__":
    resolve_by_dfs()

Fixed submission:
def resolve():
    N, W = map(int, input().split())    
    w_v = [list(map(int, input().split())) for _ in range(N)]  # [N][0:weight, 1:value]

    dp = [[0] * (W+1) for _ in range(N+1)] 

    for i in range(N-1,-1,-1):
        for j in range(W+1):
            w = w_v[i][0]
            v = w_v[i][1]
            if j < w_v[i][0]:
                dp[i][j] = dp[i+1][j]
            else:
                dp[i][j] = max(dp[i+1][j], dp[i+1][j - w] + v)
 
    print(dp[0][W])
 
if __name__ == "__main__":
    resolve()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10000)

from functools import lru_cache

def resolve():
    N, W = map(int, input().split())    
    w_v = [list(map(int, input().split())) for _ in range(N)]  # [N][0:weight, 1:value]
    
    @lru_cache(maxsize=None)
    def dfs(w, v, i):
        if w > W:
            return 0
        if i >= N:
            return v
        return max(dfs(w + w_v[i][0], v + w_v[i][1], i+1), dfs(w, v, i+1))

    res = dfs(0, 0, 0)
    print(res)


if __name__ == "__main__":
    resolve()

Fixed submission:
def resolve():
    N, W = map(int, input().split())    
    w_v = [list(map(int, input().split())) for _ in range(N)]  # [N][0:weight, 1:value]

    dp = [[0] * (W+1) for _ in range(N+1)] 

    for i in range(N-1,-1,-1):
        for j in range(W+1):
            w = w_v[i][0]
            v = w_v[i][1]
            if j < w_v[i][0]:
                dp[i][j] = dp[i+1][j]
            else:
                dp[i][j] = max(dp[i+1][j], dp[i+1][j - w] + v)
 
    print(dp[0][W])
 
if __name__ == "__main__":
    resolve()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10000)

from functools import lru_cache

def resolve():
    N, W = map(int, input().split())    
    w_v = [list(map(int, input().split())) for _ in range(N)]  # [N][0:weight, 1:value]
 
    dp = [0] * W
    memo = [0] * N 
    
    @lru_cache(maxsize=None)
    def dfs(w, v, i):
        if w > W:
            return 0
        if i >= N:
            return v
        return max(dfs(w + w_v[i][0], v + w_v[i][1], i+1), dfs(w, v, i+1))

    res = dfs(0, 0, 0)
    print(res)


if __name__ == "__main__":
    resolve()

Fixed submission:
def resolve():
    N, W = map(int, input().split())    
    w_v = [list(map(int, input().split())) for _ in range(N)]  # [N][0:weight, 1:value]

    dp = [[0] * (W+1) for _ in range(N+1)] 

    for i in range(N-1,-1,-1):
        for j in range(W+1):
            w = w_v[i][0]
            v = w_v[i][1]
            if j < w_v[i][0]:
                dp[i][j] = dp[i+1][j]
            else:
                dp[i][j] = max(dp[i+1][j], dp[i+1][j - w] + v)
 
    print(dp[0][W])
 
if __name__ == "__main__":
    resolve()