# Python3 TLE, PyPy3 AC



import sys



input = sys.stdin.readline



ri = lambda: int(eval(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(list(range(n))):

    a, b = ls[i]

    if val <= 0:

        break

    if val == dp[i][t0]:

        continue

    else:

        res.add(i)

        val -= b

        t0 -= a



m = 0

for i, (_, v) in enumerate(ls):

    if i not in res:

        m = max(m, v)

print((dp[n][t - 1] + m))
import sys



input = sys.stdin.readline



ri = lambda: int(eval(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(list(range(lim - a))):

        if j < t:

            values[j + a] = max(values[j + a], values[j] + b)

print((max(values)))
# Python3 TLE, PyPy3 AC
import sys

input = sys.stdin.readline
ri = lambda: int(eval(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(list(range(n))):
    a, b = ls[i]
    if val <= 0:
        break
    if val == dp[i][t0]:
        continue
    else:
        res.add(i)
        val -= b
        t0 -= a
m = 0
for i, (_, v) in enumerate(ls):
    if i not in res:
        m = max(m, v)
print((dp[n][t - 1] + m))

import sys

input = sys.stdin.readline
ri = lambda: int(eval(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(list(range(lim - a))):
        if j < t:
            values[j + a] = max(values[j + a], values[j] + b)
print((max(values)))

-# Python3 TLE, PyPy3 AC -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(list(range(n))): -    a, b = ls[i] -    if val <= 0: -        break -    if val == dp[i][t0]: -        continue -    else: -        res.add(i) -        val -= b -        t0 -= a -m = 0 -for i, (_, v) in enumerate(ls): -    if i not in res: -        m = max(m, v) -print((dp[n][t - 1] + m)) +ls.sort() +lim = t + 3001 +values = [0] * lim +for a, b in ls: +    for j in reversed(list(range(lim - a))): +        if j < t: +            values[j + a] = max(values[j + a], values[j] + b) +print((max(values)))