import sys

input = sys.stdin.readline

N, T = list(map(int, input().split()))

a = []

for _ in range(N):

  w, v = list(map(int, input().split()))

  a.append((w, v))

a.sort()

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

for i in range(N):

  w, v = a[i]

  for j in range(T):

    if j + w >= T:

      dp[i + 1][T] = max(dp[i + 1][T], dp[i][j] + v)

    else:

      dp[i + 1][j + w] = max(dp[i + 1][j + w], dp[i][j] + v)

    dp[i + 1][j] = max(dp[i + 1][j], dp[i][j])

  dp[i + 1][T] = max(dp[i + 1][T], dp[i][T])

print((max(dp[-1])))
import sys

input = sys.stdin.readline

N, T = list(map(int, input().split()))

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

t.sort()

dp = [0] * (T + 1)

for a, b in t:

  for i in range(T - 1, -1, -1):

    if i + a < T: dp[i + a] = max(dp[i + a], dp[i] + b)

    else: dp[-1] = max(dp[-1], dp[i] + b) 

print((max(dp)))
import sys

input = sys.stdin.readline
N, T = list(map(int, input().split()))
a = []
for _ in range(N):
    w, v = list(map(int, input().split()))
    a.append((w, v))
a.sort()
dp = [[0] * (T + 1) for _ in range(N + 1)]
for i in range(N):
    w, v = a[i]
    for j in range(T):
        if j + w >= T:
            dp[i + 1][T] = max(dp[i + 1][T], dp[i][j] + v)
        else:
            dp[i + 1][j + w] = max(dp[i + 1][j + w], dp[i][j] + v)
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j])
    dp[i + 1][T] = max(dp[i + 1][T], dp[i][T])
print((max(dp[-1])))

import sys

input = sys.stdin.readline
N, T = list(map(int, input().split()))
t = [tuple(map(int, input().split())) for _ in range(N)]
t.sort()
dp = [0] * (T + 1)
for a, b in t:
    for i in range(T - 1, -1, -1):
        if i + a < T:
            dp[i + a] = max(dp[i + a], dp[i] + b)
        else:
            dp[-1] = max(dp[-1], dp[i] + b)
print((max(dp)))

-a = [] -for _ in range(N): -    w, v = list(map(int, input().split())) -    a.append((w, v)) -a.sort() -dp = [[0] * (T + 1) for _ in range(N + 1)] -for i in range(N): -    w, v = a[i] -    for j in range(T): -        if j + w >= T: -            dp[i + 1][T] = max(dp[i + 1][T], dp[i][j] + v) +t = [tuple(map(int, input().split())) for _ in range(N)] +t.sort() +dp = [0] * (T + 1) +for a, b in t: +    for i in range(T - 1, -1, -1): +        if i + a < T: +            dp[i + a] = max(dp[i + a], dp[i] + b) -            dp[i + 1][j + w] = max(dp[i + 1][j + w], dp[i][j] + v) -        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]) -    dp[i + 1][T] = max(dp[i + 1][T], dp[i][T]) -print((max(dp[-1]))) +            dp[-1] = max(dp[-1], dp[i] + b) +print((max(dp)))