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

input = sys.stdin.readline

ri = lambda: int(input())
rs = lambda: input().rstrip()
ril = lambda: list(map(int, input().split()))
rsl = lambda: input().rstrip().split()
ris = lambda n: [ri() for _ in range(n)]
rss = lambda n: [rs() for _ in range(n)]
rils = lambda n: [ril() for _ in range(n)]
rsls = lambda n: [rsl() for _ in range(n)]

n, t = ril()
ls = rils(n)
ls.sort()

lim = t + 3001
values = [0] * lim
for a, b in ls:
    for j in reversed(range(lim - a)):
        if j < t:
            values[j + a] = max(values[j + a], values[j] + b)
print(max(values))


Fixed submission:
import sys

input = sys.stdin.readline

ri = lambda: int(input())
rs = lambda: input().rstrip()
ril = lambda: list(map(int, input().split()))
rsl = lambda: input().rstrip().split()
ris = lambda n: [ri() for _ in range(n)]
rss = lambda n: [rs() for _ in range(n)]
rils = lambda n: [ril() for _ in range(n)]
rsls = lambda n: [rsl() for _ in range(n)]


def knapsack_01(items, weight_limit):
    values = [0] * (weight_limit + 1)
    for w, v in items:
        for j in reversed(range(w, weight_limit + 1)):
            values[j] = max(values[j], values[j - w] + v)
    return max(values)


n, t = ril()
ls = rils(n)

dp = [[0] * t for _ in range(n + 1)]
for i in range(n + 1):
    for tt in range(t):
        if i == 0 or tt == 0:
            continue
        elif ls[i - 1][0] <= tt:
            dp[i][tt] = max(dp[i - 1][tt],
                            dp[i - 1][tt - ls[i - 1][0]] + ls[i - 1][1])
        else:
            dp[i][tt] = dp[i - 1][tt]

val = dp[n][t - 1]

res = set()
tt = t - 1
for i in range(n, 0, -1):
    if val <= 0:
        break
    if val == dp[i - 1][tt]:
        continue
    else:
        res.add(i - 1)
        val = val - ls[i - 1][1]
        tt = tt - ls[i - 1][0]

remaining = [v for i, (_, v) in enumerate(ls) if i not in res]
print(dp[n][t - 1] + max(remaining) if len(remaining) else dp[n][t - 1])

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

input = sys.stdin.readline

ri = lambda: int(input())
rs = lambda: input().rstrip()
ril = lambda: list(map(int, input().split()))
rsl = lambda: input().rstrip().split()
ris = lambda n: [ri() for _ in range(n)]
rss = lambda n: [rs() for _ in range(n)]
rils = lambda n: [ril() for _ in range(n)]
rsls = lambda n: [rsl() for _ in range(n)]

n, t = ril()
ls = rils(n)

dp = [[0] * t for _ in range(n + 1)]
for i in range(n + 1):
    for t0 in range(t):
        a, b = ls[i - 1]
        if i == 0 or t0 == 0:
            continue
        elif a <= t0:
            dp[i][t0] = max(dp[i - 1][t0], dp[i - 1][t0 - a] + b)
        else:
            dp[i][t0] = dp[i - 1][t0]

res = set()
val = dp[n][t - 1]
t0 = t - 1
for i in reversed(range(n)):
    a, b = ls[i]
    if val <= 0:
        break
    if val == dp[i][t0]:
        continue
    else:
        res.add(i)
        val -= b
        t0 -= a

remaining = [v for i, (_, v) in enumerate(ls) if i not in res]
if len(remaining) > 0:
    print(dp[n][t - 1] + max(remaining))
else:
    print(dp[n][t - 1])

Fixed submission:
import sys

input = sys.stdin.readline

ri = lambda: int(input())
rs = lambda: input().rstrip()
ril = lambda: list(map(int, input().split()))
rsl = lambda: input().rstrip().split()
ris = lambda n: [ri() for _ in range(n)]
rss = lambda n: [rs() for _ in range(n)]
rils = lambda n: [ril() for _ in range(n)]
rsls = lambda n: [rsl() for _ in range(n)]


def knapsack_01(items, weight_limit):
    values = [0] * (weight_limit + 1)
    for w, v in items:
        for j in reversed(range(w, weight_limit + 1)):
            values[j] = max(values[j], values[j - w] + v)
    return max(values)


n, t = ril()
ls = rils(n)

dp = [[0] * t for _ in range(n + 1)]
for i in range(n + 1):
    for tt in range(t):
        if i == 0 or tt == 0:
            continue
        elif ls[i - 1][0] <= tt:
            dp[i][tt] = max(dp[i - 1][tt],
                            dp[i - 1][tt - ls[i - 1][0]] + ls[i - 1][1])
        else:
            dp[i][tt] = dp[i - 1][tt]

val = dp[n][t - 1]

res = set()
tt = t - 1
for i in range(n, 0, -1):
    if val <= 0:
        break
    if val == dp[i - 1][tt]:
        continue
    else:
        res.add(i - 1)
        val = val - ls[i - 1][1]
        tt = tt - ls[i - 1][0]

remaining = [v for i, (_, v) in enumerate(ls) if i not in res]
print(dp[n][t - 1] + max(remaining) if len(remaining) else dp[n][t - 1])