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

input = sys.stdin.readline


def main():
    N, W = map(int, input().split())
    wv = [list(map(int, input().split())) for _ in range(N)]

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

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

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

            DP[i+1][sum_w] = max(DP[i][sum_w], DP[i+1][sum_w])

    print(DP[N][W])


if __name__ == '__main__':
    main()


Fixed submission:
import sys

def main():
    _input = sys.stdin.readline

    N, W = map(int, _input().split())
    wv = [list(map(int, _input().split())) for _ in range(N)]

    wv.sort()

    DP = [-1] * (W+1)
    DP[0] = 0

    sum_w = 0
    for w, v in wv:
        sum_w += w
        for j in range(min(W, sum_w), w-1, -1):
            if DP[j-w] == -1:
                continue
            if DP[j] < DP[j-w] + v:
                DP[j] = DP[j-w] + v
    print(max(DP))


if __name__ == '__main__':
    main()

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

input = sys.stdin.readline

def main():
    N, W = map(int, input().split())
    wv = [list(map(int, input().split())) for _ in range(N)]

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

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

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

            DP[i+1][sum_w] = max(DP[i][sum_w], DP[i+1][sum_w])

    print(DP[N][W])


if __name__ == '__main__':
    main()

Fixed submission:
import sys

def main():
    _input = sys.stdin.readline

    N, W = map(int, _input().split())
    wv = [list(map(int, _input().split())) for _ in range(N)]

    wv.sort()

    DP = [-1] * (W+1)
    DP[0] = 0

    sum_w = 0
    for w, v in wv:
        sum_w += w
        for j in range(min(W, sum_w), w-1, -1):
            if DP[j-w] == -1:
                continue
            if DP[j] < DP[j-w] + v:
                DP[j] = DP[j-w] + v
    print(max(DP))


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N, W = map(int, input().split())
    wv = [list(map(int, input().split())) for _ in range(N)]

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

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

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

            DP[i+1][sum_w] = max(DP[i][sum_w], DP[i+1][sum_w])

    print(DP[N][W])


if __name__ == '__main__':
    main()

Fixed submission:
import sys

def main():
    _input = sys.stdin.readline

    N, W = map(int, _input().split())
    wv = [list(map(int, _input().split())) for _ in range(N)]

    wv.sort()

    DP = [-1] * (W+1)
    DP[0] = 0

    sum_w = 0
    for w, v in wv:
        sum_w += w
        for j in range(min(W, sum_w), w-1, -1):
            if DP[j-w] == -1:
                continue
            if DP[j] < DP[j-w] + v:
                DP[j] = DP[j-w] + v
    print(max(DP))


if __name__ == '__main__':
    main()