from collections import Counter,defaultdict,deque

from heapq import heappop,heappush,heapify

import sys,bisect,math,itertools,fractions,pprint

sys.setrecursionlimit(10**8)

mod = 10**9+7

INF = float('inf')

def inp(): return int(sys.stdin.readline())

def inpl(): return list(map(int, sys.stdin.readline().split()))



n,t = inpl()

wv = [inpl() for _ in range(n)]

wv.sort(key = lambda x:x[1],reverse = True)

res = 0

tmp = min(n,5)

for i in range(tmp):

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

    for j in range(n):

        w,v = wv[j]

        for k in range(t):

            if k < w or i == j:

                dp[j+1][k] = dp[j][k]

            else:

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

    # print(dp[n][t-1])

    res = max(dp[n][t-1] + wv[i][1], res)

print(res)
from collections import Counter,defaultdict,deque

from heapq import heappop,heappush,heapify

import sys,bisect,math,itertools,fractions,pprint

sys.setrecursionlimit(10**8)

mod = 10**9+7

INF = float('inf')

def inp(): return int(sys.stdin.readline())

def inpl(): return list(map(int, sys.stdin.readline().split()))



n,t = inpl()

wv = [inpl() for _ in range(n)]

rwv = wv[::-1]

udp = [[0] * t for _ in range(n)]

ddp = [[0] * t for _ in range(n)]



for i in range(n-1):

    w,v = wv[i]

    for j in range(t):

        if j < w:

            udp[i+1][j] = udp[i][j]

        else:

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

res = udp[n-1][t-1] + wv[n-1][1]



for i in range(n-1):

    w,v = rwv[i]

    for j in range(t):

        if j < w:

            ddp[i+1][j] = ddp[i][j]

        else:

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

    res = max(res,ddp[n-1][t-1] + wv[0][1])

# print(res)

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

    u = i; d = n-i-1

    mx = 0

    for j in range(t):

        tmp = udp[u][j] + ddp[d][t-1-j]

        mx = max(mx, tmp)

    res = max(res, mx + wv[i][1])

print(res)
from collections import Counter, defaultdict, deque
from heapq import heappop, heappush, heapify
import sys, bisect, math, itertools, fractions, pprint

sys.setrecursionlimit(10**8)
mod = 10**9 + 7
INF = float("inf")


def inp():
    return int(sys.stdin.readline())


def inpl():
    return list(map(int, sys.stdin.readline().split()))


n, t = inpl()
wv = [inpl() for _ in range(n)]
wv.sort(key=lambda x: x[1], reverse=True)
res = 0
tmp = min(n, 5)
for i in range(tmp):
    dp = [[0] * (t + 5) for _ in range(n + 5)]
    for j in range(n):
        w, v = wv[j]
        for k in range(t):
            if k < w or i == j:
                dp[j + 1][k] = dp[j][k]
            else:
                dp[j + 1][k] = max(dp[j][k], dp[j][k - w] + v)
    # print(dp[n][t-1])
    res = max(dp[n][t - 1] + wv[i][1], res)
print(res)

from collections import Counter, defaultdict, deque
from heapq import heappop, heappush, heapify
import sys, bisect, math, itertools, fractions, pprint

sys.setrecursionlimit(10**8)
mod = 10**9 + 7
INF = float("inf")


def inp():
    return int(sys.stdin.readline())


def inpl():
    return list(map(int, sys.stdin.readline().split()))


n, t = inpl()
wv = [inpl() for _ in range(n)]
rwv = wv[::-1]
udp = [[0] * t for _ in range(n)]
ddp = [[0] * t for _ in range(n)]
for i in range(n - 1):
    w, v = wv[i]
    for j in range(t):
        if j < w:
            udp[i + 1][j] = udp[i][j]
        else:
            udp[i + 1][j] = max(udp[i][j], udp[i][j - w] + v)
res = udp[n - 1][t - 1] + wv[n - 1][1]
for i in range(n - 1):
    w, v = rwv[i]
    for j in range(t):
        if j < w:
            ddp[i + 1][j] = ddp[i][j]
        else:
            ddp[i + 1][j] = max(ddp[i][j], ddp[i][j - w] + v)
    res = max(res, ddp[n - 1][t - 1] + wv[0][1])
# print(res)
for i in range(1, n - 1):
    u = i
    d = n - i - 1
    mx = 0
    for j in range(t):
        tmp = udp[u][j] + ddp[d][t - 1 - j]
        mx = max(mx, tmp)
    res = max(res, mx + wv[i][1])
print(res)

-wv.sort(key=lambda x: x[1], reverse=True) -res = 0 -tmp = min(n, 5) -for i in range(tmp): -    dp = [[0] * (t + 5) for _ in range(n + 5)] -    for j in range(n): -        w, v = wv[j] -        for k in range(t): -            if k < w or i == j: -                dp[j + 1][k] = dp[j][k] -            else: -                dp[j + 1][k] = max(dp[j][k], dp[j][k - w] + v) -    # print(dp[n][t-1]) -    res = max(dp[n][t - 1] + wv[i][1], res) +rwv = wv[::-1] +udp = [[0] * t for _ in range(n)] +ddp = [[0] * t for _ in range(n)] +for i in range(n - 1): +    w, v = wv[i] +    for j in range(t): +        if j < w: +            udp[i + 1][j] = udp[i][j] +        else: +            udp[i + 1][j] = max(udp[i][j], udp[i][j - w] + v) +res = udp[n - 1][t - 1] + wv[n - 1][1] +for i in range(n - 1): +    w, v = rwv[i] +    for j in range(t): +        if j < w: +            ddp[i + 1][j] = ddp[i][j] +        else: +            ddp[i + 1][j] = max(ddp[i][j], ddp[i][j - w] + v) +    res = max(res, ddp[n - 1][t - 1] + wv[0][1]) +# print(res) +for i in range(1, n - 1): +    u = i +    d = n - i - 1 +    mx = 0 +    for j in range(t): +        tmp = udp[u][j] + ddp[d][t - 1 - j] +        mx = max(mx, tmp) +    res = max(res, mx + wv[i][1])