import sys

input = sys.stdin.readline



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

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



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



DP=[[0,0] for i in range(T+1)]



for ind,(x,y) in enumerate(D):

    for time in range(T,x-1,-1):

        if DP[time][0]<DP[time-x][0]+y:



            if DP[time-x][1]==ind:

                DP[time]=[DP[time-x][0]+y,ind+1]

            else:

                DP[time]=[DP[time-x][0]+y,DP[time-x][1]]



        elif DP[time][0]==DP[time-x][0]+y:



            if DP[time-x][1]==ind:

                DP[time][1]=min(ind+1,DP[time][1])

            else:

                DP[time][1]=min(DP[time-x][1],DP[time][1])



    #print(DP)



D.append((0,0))

                

ANS=DP[-1][0]



for x,y in DP[:-1]:

    #if x==0:

    #    continue

    ANS=max(ANS,x+D[y][1])



print(ANS)

    

import sys

input = sys.stdin.readline



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

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

D.sort()



DP=[0]*(T+1)

ANS=0



for i in range(N):

    a,b=D[i]



    for j in range(T,a-1,-1):

        DP[j]=max(DP[j-a]+b,DP[j])



    for j in range(i+1,N):

        ANS=max(ANS,DP[T-1]+D[j][1])



print(ANS)
import sys

input = sys.stdin.readline
N, T = list(map(int, input().split()))
D = [tuple(map(int, input().split())) for i in range(N)]
D.sort(key=lambda x: x[1], reverse=True)
DP = [[0, 0] for i in range(T + 1)]
for ind, (x, y) in enumerate(D):
    for time in range(T, x - 1, -1):
        if DP[time][0] < DP[time - x][0] + y:
            if DP[time - x][1] == ind:
                DP[time] = [DP[time - x][0] + y, ind + 1]
            else:
                DP[time] = [DP[time - x][0] + y, DP[time - x][1]]
        elif DP[time][0] == DP[time - x][0] + y:
            if DP[time - x][1] == ind:
                DP[time][1] = min(ind + 1, DP[time][1])
            else:
                DP[time][1] = min(DP[time - x][1], DP[time][1])
    # print(DP)
D.append((0, 0))
ANS = DP[-1][0]
for x, y in DP[:-1]:
    # if x==0:
    #    continue
    ANS = max(ANS, x + D[y][1])
print(ANS)

import sys

input = sys.stdin.readline
N, T = list(map(int, input().split()))
D = [tuple(map(int, input().split())) for i in range(N)]
D.sort()
DP = [0] * (T + 1)
ANS = 0
for i in range(N):
    a, b = D[i]
    for j in range(T, a - 1, -1):
        DP[j] = max(DP[j - a] + b, DP[j])
    for j in range(i + 1, N):
        ANS = max(ANS, DP[T - 1] + D[j][1])
print(ANS)

-D.sort(key=lambda x: x[1], reverse=True) -DP = [[0, 0] for i in range(T + 1)] -for ind, (x, y) in enumerate(D): -    for time in range(T, x - 1, -1): -        if DP[time][0] < DP[time - x][0] + y: -            if DP[time - x][1] == ind: -                DP[time] = [DP[time - x][0] + y, ind + 1] -            else: -                DP[time] = [DP[time - x][0] + y, DP[time - x][1]] -        elif DP[time][0] == DP[time - x][0] + y: -            if DP[time - x][1] == ind: -                DP[time][1] = min(ind + 1, DP[time][1]) -            else: -                DP[time][1] = min(DP[time - x][1], DP[time][1]) -    # print(DP) -D.append((0, 0)) -ANS = DP[-1][0] -for x, y in DP[:-1]: -    # if x==0: -    #    continue -    ANS = max(ANS, x + D[y][1]) +D.sort() +DP = [0] * (T + 1) +ANS = 0 +for i in range(N): +    a, b = D[i] +    for j in range(T, a - 1, -1): +        DP[j] = max(DP[j - a] + b, DP[j]) +    for j in range(i + 1, N): +        ANS = max(ANS, DP[T - 1] + D[j][1])