Buggy python submission (verdict: Runtime Error):
import sys
from operator import itemgetter


def main():
    input = sys.stdin.readline
    N, T = map(int, input().strip().split())
    foods = sorted([(map(int, input().strip().split())) for _ in range(N)], key=itemgetter(0))

    ans = 0
    dp = [[0] * (T+1) for _ in range(N+1)]
    for j, food in enumerate(foods):
        n_a, n_b = food
        for k in range(T+1):
            if k == T:
                dp[j+1][k] = max(dp[j][k], dp[j][k-1]+n_b)
            elif k < n_a:
                dp[j+1][k] = dp[j][k]
            else:
                dp[j+1][k] = max(dp[j][k], dp[j][k-n_a]+n_b)
        #print(dp[j+1])
    ans = dp[-1][-1]
    print(ans)

if __name__ == "__main__":
    main()


Fixed submission:
import sys
from operator import itemgetter


def main():
    input = sys.stdin.readline
    N, T = map(int, input().strip().split())
    foods = sorted([list(map(int, input().strip().split())) for _ in range(N)], key=itemgetter(0))

    ans = 0
    dp = [[0] * (T+1) for _ in range(N+1)]
    for j, food in enumerate(foods):
        n_a, n_b = food
        for k in range(T+1):
            if k == T:
                dp[j+1][k] = max(dp[j][k], dp[j][k-1]+n_b)
            elif k < n_a:
                dp[j+1][k] = dp[j][k]
            else:
                dp[j+1][k] = max(dp[j][k], dp[j][k-n_a]+n_b)
        #print(dp[j+1])
    ans = dp[-1][-1]
    print(ans)

if __name__ == "__main__":
    main()

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


def main():
    input = sys.stdin.readline
    N, T = map(int, input().strip().split())
    foods = [list(map(int, input().strip().split())) for _ in range(N)]

    ans = 0
    before_dp = [[0] * T for _ in range(N)]
    for i, food in enumerate(foods):
        now_foods = foods[:i] + foods[i+1:]
        dp = before_dp[:i] + [[0] * T for _ in range(N-i)]
        for j, n_food in enumerate(now_foods):
            if j < i-1:
                continue
            n_a, n_b = n_food
            for k in range(T):
                if k < n_a:
                    dp[j+1][k] = dp[j][k]
                else:
                    dp[j+1][k] = max(dp[j][k], dp[j][k-n_a]+n_b)
        #print(dp[j+1])
        ans = max(ans, dp[-1][-1]+food[1])
        before_dp = dp
    print(ans)

if __name__ == "__main__":
    main()


Fixed submission:
import sys
from operator import itemgetter


def main():
    input = sys.stdin.readline
    N, T = map(int, input().strip().split())
    foods = sorted([list(map(int, input().strip().split())) for _ in range(N)], key=itemgetter(0))

    ans = 0
    dp = [[0] * (T+1) for _ in range(N+1)]
    for j, food in enumerate(foods):
        n_a, n_b = food
        for k in range(T+1):
            if k == T:
                dp[j+1][k] = max(dp[j][k], dp[j][k-1]+n_b)
            elif k < n_a:
                dp[j+1][k] = dp[j][k]
            else:
                dp[j+1][k] = max(dp[j][k], dp[j][k-n_a]+n_b)
        #print(dp[j+1])
    ans = dp[-1][-1]
    print(ans)

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N, T = map(int, input().split())
    foods = [list(map(int, input().split())) for _ in range(N)]
    ans = 0
    for i, food in enumerate(foods):
        now_foods = foods[:i] + foods[i+1:]
        dp = [[0] * T for _ in range(N)]
        for j, n_food in enumerate(now_foods):
            n_a, n_b = n_food
            for k in range(T):
                if k < n_a:
                    dp[j+1][k] = dp[j][k]
                else:
                    dp[j+1][k] = max(dp[j][k], dp[j][k-n_a]+n_b)
        #print(dp[j+1])
        ans = max(ans, dp[-1][-1]+food[1])
    print(ans)

if __name__ == "__main__":
    main()


Fixed submission:
import sys
from operator import itemgetter


def main():
    input = sys.stdin.readline
    N, T = map(int, input().strip().split())
    foods = sorted([list(map(int, input().strip().split())) for _ in range(N)], key=itemgetter(0))

    ans = 0
    dp = [[0] * (T+1) for _ in range(N+1)]
    for j, food in enumerate(foods):
        n_a, n_b = food
        for k in range(T+1):
            if k == T:
                dp[j+1][k] = max(dp[j][k], dp[j][k-1]+n_b)
            elif k < n_a:
                dp[j+1][k] = dp[j][k]
            else:
                dp[j+1][k] = max(dp[j][k], dp[j][k-n_a]+n_b)
        #print(dp[j+1])
    ans = dp[-1][-1]
    print(ans)

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N, T = map(int, input().split())
    foods = [list(map(int, input().split())) for _ in range(N)]
    ans = 0
    for i, food in enumerate(foods):
        now_foods = foods[:i] + foods[i+1:]
        dp = [[0] * T for _ in range(N)]
        for j, n_food in enumerate(now_foods):
            n_a, n_b = n_food
            for k in range(T):
                if k < n_a:
                    dp[j+1][k] = dp[j][k]
                else:
                    dp[j+1][k] = max(dp[j][k], dp[j][k-n_a]+n_b)
        #print(dp[j+1])
        ans = max(ans, dp[-1][-1]+food[1])
    print(ans)

if __name__ == "__main__":
    main()


Fixed submission:
import sys
from operator import itemgetter


def main():
    input = sys.stdin.readline
    N, T = map(int, input().strip().split())
    foods = sorted([list(map(int, input().strip().split())) for _ in range(N)], key=itemgetter(0))

    ans = 0
    dp = [[0] * (T+1) for _ in range(N+1)]
    for j, food in enumerate(foods):
        n_a, n_b = food
        for k in range(T+1):
            if k == T:
                dp[j+1][k] = max(dp[j][k], dp[j][k-1]+n_b)
            elif k < n_a:
                dp[j+1][k] = dp[j][k]
            else:
                dp[j+1][k] = max(dp[j][k], dp[j][k-n_a]+n_b)
        #print(dp[j+1])
    ans = dp[-1][-1]
    print(ans)

if __name__ == "__main__":
    main()