// Slower C++ solution:
#include <bits/stdc++.h>



using namespace std;



using ll = long long;

using P = pair<ll, ll>;

using Graph = vector<vector<ll>>;

#define rep(i, n) for(ll i=0;i<(ll)(n);i++)

#define rep2(i, m, n) for(ll i=m;i<(ll)(n);i++)

#define rrep(i, n, m) for(ll i=n;i>=(ll)(m);i--)

const int dx[4] = {1, 0, -1, 0};

const int dy[4] = {0, 1, 0, -1};

const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};

const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};

const ll MOD = 1000000007;

const ll INF = 1000000000000000000L;

#ifdef __DEBUG



/**

 * For DEBUG

 * https://github.com/ta7uw/cpp-pyprint

 */

#include "cpp-pyprint/pyprint.h"



#endif



void Main() {

    ll N, T;

    cin >> N >> T;

    vector<P> items(N);

    rep(i, N) cin >> items[i].first >> items[i].second;

    sort(items.begin(), items.end());

    vector<vector<ll>> dp(N + 1, vector<ll>(T + 3005, 0));

    rep(i, N) {

        P p = items[i];

        ll a = p.first;

        ll b = p.second;

        rep(j, T + 3005) {

            dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);

            if (j < T) {

                dp[i + 1][j + a] = max(dp[i + 1][j + a], dp[i][j] + b);

            }

        }

    }

    ll ans = 0;

    rep(i, T + 3005) {

        ans = max(ans, dp[N][i]);

    }

    cout << ans << '\n';

}



int main() {

    cin.tie(nullptr);

    ios::sync_with_stdio(false);

    cout << fixed << setprecision(15);

    Main();

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.4283930720378268x):
#include <bits/stdc++.h>



using namespace std;



using ll = long long;

using P = pair<ll, ll>;

using Graph = vector<vector<ll>>;

#define rep(i, n) for(ll i=0;i<(ll)(n);i++)

#define rep2(i, m, n) for(ll i=m;i<(ll)(n);i++)

#define rrep(i, n, m) for(ll i=n;i>=(ll)(m);i--)

const int dx[4] = {1, 0, -1, 0};

const int dy[4] = {0, 1, 0, -1};

const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};

const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};

const ll MOD = 1000000007;

const ll INF = 1000000000000000000L;

#ifdef __DEBUG



/**

 * For DEBUG

 * https://github.com/ta7uw/cpp-pyprint

 */

#include "cpp-pyprint/pyprint.h"



#endif



void Main() {

    ll N, T;

    cin >> N >> T;

    vector<P> items(N);

    rep(i, N) cin >> items[i].first >> items[i].second;

    vector<vector<ll>> dp1(N + 1, vector<ll>(T, 0));

    vector<vector<ll>> dp2(N + 1, vector<ll>(T, 0));



    rep(i, N) {

        P p = items[i];

        ll a = p.first;

        ll b = p.second;

        rep(j, T) {

            if (j + a < T) {

                dp1[i + 1][j + a] = max(dp1[i + 1][j + a], dp1[i][j] + b);

            }

            dp1[i + 1][j] = max(dp1[i + 1][j], dp1[i][j]);

        }

    }

    rep(i, N) {

        P p = items[N - 1 - i];

        ll a = p.first;

        ll b = p.second;

        rep(j, T) {

            if (j + a < T) {

                dp2[i + 1][j + a] = max(dp2[i + 1][j + a], dp2[i][j] + b);

            }

            dp2[i + 1][j] = max(dp2[i + 1][j], dp2[i][j]);

        }

    }

    ll ans = 0;

    rep(i, N) {

        P p = items[i];

        ll x = p.second;

        ll y = 0;

        rep(t, T) {

            ll d1 = dp1[i][t];

            ll d2 = dp2[N - 1 - i][T - 1 - t];

            y = max(y, d1 + d2);

        }

        ans = max(ans, x + y);

    }

    cout << ans << '\n';

}



int main() {

    cin.tie(nullptr);

    ios::sync_with_stdio(false);

    cout << fixed << setprecision(15);

    Main();

    return 0;

}