#include <bits/stdc++.h>
using namespace std;
#define ll int64_t
#include <cstdlib> 
#include <math.h>
#include<cstdio>
#include<cstring>
#define dbg(x) cout << #x << " = " << x << endl
#define dbg2(x,y) cout << #x << " = " << x << ", " << #y << " = " << y << endl
#define dbg3(x,y,z) cout << #x << " = " << x << ", " << #y << " = " << y << ", " << #z << " = " << z << endl
#define dbg4(x,y,z,q) cout << #x << " = " << x << ", " << #y << " = " << y << ", " << #z << " = " << z << ", " << #q << " = " << q << endl
 
#define FOR(I, A, B) for (int I = (A); I <= (B); I++)
typedef vector< int > vi;
typedef vector< vi > vvi;
typedef pair< int,int > ii;
#define gap cout<<endl;
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define fo(i,n) for(int i=0;i<n;i++)
#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(typeof((c)).begin() i = (c).begin(); i != (c).end(); i++)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())
 
 
int main(){
    int n,t;
    cin>>n>>t;
    t--;
    vector<pair<int,int>> pairs;
    int a,b;
    fo(i,n){
         cin>>a>>b;
         pairs.pb({a,b});
    }
    //dbg(sz(pairs));
    //sort(all(pairs));
    int dp[n][t+1];
    int index=0;
    fo(i,n){
        fo(j,t+1){
            dp[i][j]=0;
        }
    }
    sort(all(pairs));
    int time,weight;
    time=pairs[0].first;
    weight=pairs[0].second;
    int tmp2=t;
        //dbg(o);
    while(tmp2>=0){

           if(tmp2+time<=t) dp[0][tmp2+time]=weight;
           tmp2--;
    
     }
    
    std::vector<int> visited(n,0);
    index=1;
    while(index<=n){
        if(index==n) break;

        time=pairs[index].first;
        if(time>t) break;
        weight=pairs[index].second;
        tmp2=t;
        //dbg(o);
        while(tmp2>=0){

           if(tmp2+time<=t) dp[index][tmp2+time]=max(dp[index-1][tmp2+time],dp[index-1][tmp2]+weight);
           else{
            dp[index][tmp2]=dp[index-1][tmp2];
           }    
           tmp2--;
        }
        index++;
 
 
    }
    //dbg(visited[1]);
    index--;
    int last=index;
    int temp=t;
    while(index>=0){
        if(temp==0) break;
        if(index==0){
            if(temp>0) visited[0]=1;
            break;
        }
        else if(dp[index][temp]==dp[index-1][temp]){
            index-=1;
        }
        else{
            visited[index]=1;
            index--;
            temp=temp-pairs[index].first;
        }
    }
    int maax=0;
    FOR(i,0,n){
        if(visited[i]) continue;
        else maax=max(maax,pairs[i].second);

    }
    
    int ans=dp[last][t]+maax;
    
    auto compare=[](ii a, ii b) {
        return a.second < b.second;};
    sort(all(pairs),compare);
    maax=pairs[sz(pairs)-1].second;
    pairs.pop_back();
     index=0;
    fo(i,n){
        fo(j,t+1){
            dp[i][j]=0;
        }
    }
    
    
    time=pairs[0].first;
    weight=pairs[0].second;
    tmp2=t;
        //dbg(o);
    while(tmp2>=0){

           if(tmp2+time<=t) dp[0][tmp2+time]=weight;
           tmp2--;
    
     }
    
   visited.resize(n,0);
    index=1;
    while(index<=n-1){
        if(index==n-1) break;

        time=pairs[index].first;
        if(time>t) break;
        weight=pairs[index].second;
        tmp2=t;
        //dbg(o);
        while(tmp2>=0){

           if(tmp2+time<=t) dp[index][tmp2+time]=max(dp[index-1][tmp2+time],dp[index-1][tmp2]+weight);
           else{
            dp[index][tmp2]=dp[index-1][tmp2];
           }    
           tmp2--;
        }
        index++;
 
 
    }
    //dbg(visited[1]);
    index--;
     last=index;
    temp=t;
    while(index>=0){
        if(temp==0) break;
        if(index==0){
            if(temp>0) visited[0]=1;
            break;
        }
        else if(dp[index][temp]==dp[index-1][temp]){
            index-=1;
        }
        else{
            visited[index]=1;
            index--;
            temp=temp-pairs[index].first;
        }
    }
    
    
    ans=max(ans,dp[last][t]+maax);
    cout<<ans<<endl;

    //dbg(visited[2]);
     //dbg(dp[2][6]);
}
   

Measured memory_bytes: 35456