import sys

input=sys.stdin.readline

import copy

def knapsack(N,W,weight,value):

  dp=[[999999999999 for i in range(W+1)] for j in range(N+1)]

  dp2=[[0 for i in range(W+1)] for j in range(N+1)]

  for i in range(W+1): dp[0][i]=0

 

  for i in range(N):

    for w in range(W+1):

      if weight[i]<=w:

        if dp[i][w-weight[i]]+value[i]>dp[i][w]:

          dp[i+1][w]=dp[i][w-weight[i]]+value[i]

          dp2[i+1][w]=w-weight[i]

        else:

          dp[i+1][w]=dp[i][w]

          dp2[i+1][w]=w

        

      else:

        dp[i+1][w]=dp[i][w]

        dp2[i+1][w]=w

  

  l=[]

  x=W

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

    if dp2[i+1][x]==x-weight[i]:

      l.append(i)

    x=dp2[i+1][x]

  return dp[N][W],l

 

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

l=[list(map(int,input().split())) for i in range(n)]

a=[];b=[]

for i in range(n):

  a.append(l[i][0])

  b.append(l[i][1])

z=knapsack(n,t-1,a,b)

L=[0]*n

for i in range(len(z[1])):

  L[z[1][i]]=1

M=[]

for i in range(n):

  if L[i]==0:

    M.append(l[i][1])

if len(M)==0:

  print((z[0]))

else:

  print((z[0]+max(M)))
import sys

input=sys.stdin.readline

def knapsack(N,W,weight,value):

  dp=[[999999999999 for i in range(W+1)] for j in range(N+1)]

  for i in range(W+1):

    dp[0][i]=0

 

  for i in range(N):

    for w in range(W+1):

      if weight[i]<=w:

        if dp[i][w-weight[i]]+value[i]>dp[i][w]:

          dp[i+1][w]=dp[i][w-weight[i]]+value[i]

        else:

          dp[i+1][w]=dp[i][w]

      else:

        dp[i+1][w]=dp[i][w]

  return dp

 

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

l=[list(map(int,input().split())) for i in range(n)]

l.sort()

a=[];b=[]

for i in range(n):

  a.append(l[i][0])

  b.append(l[i][1])

dp=knapsack(n,t-1,a,b)

ans=[]

for i in range(n):

  if i!=n:

    ans.append(dp[i][t-1]+b[i])

  else:

    ans.append(dp[n][t])

print((max(ans)))
import sys

input = sys.stdin.readline
import copy


def knapsack(N, W, weight, value):
    dp = [[999999999999 for i in range(W + 1)] for j in range(N + 1)]
    dp2 = [[0 for i in range(W + 1)] for j in range(N + 1)]
    for i in range(W + 1):
        dp[0][i] = 0
    for i in range(N):
        for w in range(W + 1):
            if weight[i] <= w:
                if dp[i][w - weight[i]] + value[i] > dp[i][w]:
                    dp[i + 1][w] = dp[i][w - weight[i]] + value[i]
                    dp2[i + 1][w] = w - weight[i]
                else:
                    dp[i + 1][w] = dp[i][w]
                    dp2[i + 1][w] = w
            else:
                dp[i + 1][w] = dp[i][w]
                dp2[i + 1][w] = w
    l = []
    x = W
    for i in range(n - 1, -1, -1):
        if dp2[i + 1][x] == x - weight[i]:
            l.append(i)
        x = dp2[i + 1][x]
    return dp[N][W], l


n, t = list(map(int, input().split()))
l = [list(map(int, input().split())) for i in range(n)]
a = []
b = []
for i in range(n):
    a.append(l[i][0])
    b.append(l[i][1])
z = knapsack(n, t - 1, a, b)
L = [0] * n
for i in range(len(z[1])):
    L[z[1][i]] = 1
M = []
for i in range(n):
    if L[i] == 0:
        M.append(l[i][1])
if len(M) == 0:
    print((z[0]))
else:
    print((z[0] + max(M)))

import sys

input = sys.stdin.readline


def knapsack(N, W, weight, value):
    dp = [[999999999999 for i in range(W + 1)] for j in range(N + 1)]
    for i in range(W + 1):
        dp[0][i] = 0
    for i in range(N):
        for w in range(W + 1):
            if weight[i] <= w:
                if dp[i][w - weight[i]] + value[i] > dp[i][w]:
                    dp[i + 1][w] = dp[i][w - weight[i]] + value[i]
                else:
                    dp[i + 1][w] = dp[i][w]
            else:
                dp[i + 1][w] = dp[i][w]
    return dp


n, t = list(map(int, input().split()))
l = [list(map(int, input().split())) for i in range(n)]
l.sort()
a = []
b = []
for i in range(n):
    a.append(l[i][0])
    b.append(l[i][1])
dp = knapsack(n, t - 1, a, b)
ans = []
for i in range(n):
    if i != n:
        ans.append(dp[i][t - 1] + b[i])
    else:
        ans.append(dp[n][t])
print((max(ans)))

-import copy -    dp2 = [[0 for i in range(W + 1)] for j in range(N + 1)] -                    dp2[i + 1][w] = w - weight[i] -                    dp2[i + 1][w] = w -                dp2[i + 1][w] = w -    l = [] -    x = W -    for i in range(n - 1, -1, -1): -        if dp2[i + 1][x] == x - weight[i]: -            l.append(i) -        x = dp2[i + 1][x] -    return dp[N][W], l +    return dp +l.sort() -z = knapsack(n, t - 1, a, b) -L = [0] * n -for i in range(len(z[1])): -    L[z[1][i]] = 1 -M = [] +dp = knapsack(n, t - 1, a, b) +ans = [] -    if L[i] == 0: -        M.append(l[i][1]) -if len(M) == 0: -    print((z[0])) -else: -    print((z[0] + max(M))) +    if i != n: +        ans.append(dp[i][t - 1] + b[i]) +    else: +        ans.append(dp[n][t]) +print((max(ans)))