import sys

input = sys.stdin.readline

read = sys.stdin.read



n, t = list(map(int, input().split()))

m = list(map(int, read().split()))

AB = sorted(zip(m, m))

A, B = list(zip(*AB))

dp = [[0]*t for _ in range(n+1)]

for i, a in enumerate(A[:-1]):

  for j in range(t):

    if j < a:

      dp[i+1][j] = dp[i][j]

    else:

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

ans = 0

maxB = [B[-1]]*n

for i in range(n-2, 0, -1):

  maxB[i] = max(B[i], maxB[i+1])

for i in range(n-1):

  ans = max(ans, dp[i+1][-1] + maxB[i+1])

print(ans)
import sys

input = sys.stdin.readline

read = sys.stdin.read

import numpy as np



n, t = list(map(int, input().split()))

m = list(map(int, read().split()))

AB = sorted(zip(m, m))

A, B = list(zip(*AB))

dp = np.zeros((n+1, t), dtype=np.int64)

for i, (a, b) in enumerate(AB):

  np.maximum(dp[i, a:], dp[i, :-a]+b, out=dp[i+1, a:])

maxB = np.maximum.accumulate(B[::-1])[::-1]

ans = (dp[1:-1, -1]+maxB[1:]).max()

print(ans)
import sys

input = sys.stdin.readline
read = sys.stdin.read
n, t = list(map(int, input().split()))
m = list(map(int, read().split()))
AB = sorted(zip(m, m))
A, B = list(zip(*AB))
dp = [[0] * t for _ in range(n + 1)]
for i, a in enumerate(A[:-1]):
    for j in range(t):
        if j < a:
            dp[i + 1][j] = dp[i][j]
        else:
            dp[i + 1][j] = max(dp[i][j - a] + B[i], dp[i][j])
ans = 0
maxB = [B[-1]] * n
for i in range(n - 2, 0, -1):
    maxB[i] = max(B[i], maxB[i + 1])
for i in range(n - 1):
    ans = max(ans, dp[i + 1][-1] + maxB[i + 1])
print(ans)

import sys

input = sys.stdin.readline
read = sys.stdin.read
import numpy as np

n, t = list(map(int, input().split()))
m = list(map(int, read().split()))
AB = sorted(zip(m, m))
A, B = list(zip(*AB))
dp = np.zeros((n + 1, t), dtype=np.int64)
for i, (a, b) in enumerate(AB):
    np.maximum(dp[i, a:], dp[i, :-a] + b, out=dp[i + 1, a:])
maxB = np.maximum.accumulate(B[::-1])[::-1]
ans = (dp[1:-1, -1] + maxB[1:]).max()
print(ans)

+import numpy as np + -dp = [[0] * t for _ in range(n + 1)] -for i, a in enumerate(A[:-1]): -    for j in range(t): -        if j < a: -            dp[i + 1][j] = dp[i][j] -        else: -            dp[i + 1][j] = max(dp[i][j - a] + B[i], dp[i][j]) -ans = 0 -maxB = [B[-1]] * n -for i in range(n - 2, 0, -1): -    maxB[i] = max(B[i], maxB[i + 1]) -for i in range(n - 1): -    ans = max(ans, dp[i + 1][-1] + maxB[i + 1]) +dp = np.zeros((n + 1, t), dtype=np.int64) +for i, (a, b) in enumerate(AB): +    np.maximum(dp[i, a:], dp[i, :-a] + b, out=dp[i + 1, a:]) +maxB = np.maximum.accumulate(B[::-1])[::-1] +ans = (dp[1:-1, -1] + maxB[1:]).max()