#include <bits/stdc++.h>
#define rep(i, N) for(int i=0;i<(N);i++)
#define PF(d,x) printf("%"#d"\n",x);
#define SCD(n) scanf("%d",&n)
#define PB push_back
#define MP make_pair
#define ALL(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
///////////////////////////////////////////////
/* (๑╹◡╹) */
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
template<class T>
inline bool chmin(T &a, T b) {
	if (a > b) {
		a = b;
		return true;
	}
	return false;
}
 /* (๑╹◡╹)(๑╹◡╹)(๑╹◡╹)*/
template<class T>
inline bool chmax(T &a, T b) {
	if (a < b) {
		a = b;
		return true;
	}
	return false;
}
/* (๑╹◡╹)(๑╹◡╹)(๑╹◡╹)(๑╹◡╹)(๑╹◡╹)(๑╹◡╹) */
///////////////////////////////////////////////
const int MAX = 5100000;
const int MOD = 1000000007;
long long fac[MAX], finv[MAX], inv[MAX];
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

// 二項係数計算
long long COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
/*

// modint (1000000007 で割ったあまりを扱う構造体)
template<int MOD> struct Fp {
    long long val;
    constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
        if (val < 0) v += MOD;
    }
    constexpr int getmod() { return MOD; }
    constexpr Fp operator - () const noexcept {
        return val ? MOD - val : 0;
    }
    constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; }
    constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; }
    constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; }
    constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; }
    constexpr Fp& operator += (const Fp& r) noexcept {
        val += r.val;
        if (val >= MOD) val -= MOD;
        return *this;
    }
    constexpr Fp& operator -= (const Fp& r) noexcept {
        val -= r.val;
        if (val < 0) val += MOD;
        return *this;
    }
    constexpr Fp& operator *= (const Fp& r) noexcept {
        val = val * r.val % MOD;
        return *this;
    }
    constexpr Fp& operator /= (const Fp& r) noexcept {
        long long a = r.val, b = MOD, u = 1, v = 0;
        while (b) {
            long long t = a / b;
            a -= t * b; swap(a, b);
            u -= t * v; swap(u, v);
        }
        val = val * u % MOD;
        if (val < 0) val += MOD;
        return *this;
    }
    constexpr bool operator == (const Fp& r) const noexcept {
        return this->val == r.val;
    }
    constexpr bool operator != (const Fp& r) const noexcept {
        return this->val != r.val;
    }
    friend constexpr ostream& operator << (ostream &os, const Fp<MOD>& x) noexcept {
        return os << x.val;
    }
    friend constexpr istream& operator >> (istream &is, Fp<MOD>& x) noexcept {
        return is >> x.val;
    }
    friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
        if (n == 0) return 1;
        auto t = modpow(a, n / 2);
        t = t * t;
        if (n & 1) t = t * a;
        return t;
    }
};

// 二項係数ライブラリ
template<class T> struct BiCoef {
    vector<T> fact_, inv_, finv_;
    constexpr BiCoef() {}
    constexpr BiCoef(int n) noexcept : fact_(n, 1), inv_(n, 1), finv_(n, 1) {
        init(n);
    }
    constexpr void init(int n) noexcept {
        fact_.assign(n, 1), inv_.assign(n, 1), finv_.assign(n, 1);
        int MOD = fact_[0].getmod();
        for(int i = 2; i < n; i++){
            fact_[i] = fact_[i-1] * i;
            inv_[i] = -inv_[MOD%i] * (MOD/i);
            finv_[i] = finv_[i-1] * inv_[i];
        }
    }
    constexpr T com(int n, int k) const noexcept {
        if (n < k || n < 0 || k < 0) return 0;
        return fact_[n] * finv_[k] * finv_[n-k];
    }
    constexpr T fact(int n) const noexcept {
        if (n < 0) return 0;
        return fact_[n];
    }
    constexpr T inv(int n) const noexcept {
        if (n < 0) return 0;
        return inv_[n];
    }
    constexpr T finv(int n) const noexcept {
        if (n < 0) return 0;
        return finv_[n];
    }
};

const int MOD = 1000000007;
using mint = Fp<MOD>; //intの代わりにmintって書くとMODで割ってくれる．


//二部探索上手にやるコード
    /*ll N,M; vector<ll> A;
    bool check(ll target)
    {
        ll m_remain = M;
        ll k_count = 0;
        rep(i,N){
            ll y = 1;
            while(A[i]/y > target){y *=2;}
            if(A[i]/y == target){k_count++;}
            m_remain -= y / 2;
        }
        if(m_remain > k_count){return true;}
        else return false;
    }

    ll binary_search(){
        ll left = -1;
        ll right = 1e12+3;
        while(right - left > 1)
        {
            ll mid = (left + right) / 2;
            if(check(mid)){right = mid;}
            else left = mid;
        }
        return right;
    }
*/
//ここからコード書くよ～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～～



int main() {
    ll ans1 = 0;
    ll N,T; cin >> N >> T;
    ll dp[N+1][T] = {};

    //ll A[N],B[N]; rep(i,N){cin >> A[i] >> B[i];}
    vector <pair<ll,ll>> I(N); rep(i,N){cin >> I[i].second >> I[i].first;}
    sort(ALL(I));
    //reverse(ALL(I));
    rep(i,N){
        rep(w,T){
            if(i==0){dp[i][w]=0;}
            if(w>=I[i].second){dp[i+1][w] = max(dp[i][w-I[i].second] + I[i].first,dp[i][w]);}
            else dp[i+1][w] = dp[i][w];
            //cout << "dp[" << i+1 << "][" << w << "] = " << dp[i+1][w] << endl;
        }
    
    }

    ll after = 0;
    for (ll i = N; i > 0; i--)
    {
        if(dp[i][T-1] == dp[i-1][T-1]){after = max(after, I[i-1].first);}
    }
    ans1 = dp[N][T-1] + after;


    reverse(ALL(I));
    rep(i,N){
        rep(w,T){
            if(i==0){dp[i][w]=0;}
            if(w>=I[i].second){dp[i+1][w] = max(dp[i][w-I[i].second] + I[i].first,dp[i][w]);}
            else dp[i+1][w] = dp[i][w];
            //cout << "dp[" << i+1 << "][" << w << "] = " << dp[i+1][w] << endl;
        }
    
    }

    after = 0;
    for (ll i = N; i > 0; i--)
    {
        if(dp[i][T-1] == dp[i-1][T-1]){after = max(after, I[i-1].first);}
    }
    ll ans2 = dp[N][T-1] + after;
    ll ans = max(ans1,ans2);
    cout << ans << endl;
    }


Measured memory_bytes: 70656