// 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() {

    int N, T;

    cin >> N >> T;

    vector<P> items(N);

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

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

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

    rep(i, N) {

        P p = items[i];

        rep(t, T + 3001) {

            if (t - p.first >= 0 && T > t - p.first) {

                dp[i + 1][t] = max(dp[i + 1][t], dp[i][t - p.first] + p.second);

            }

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

        }

    }

    ll ans = 0;

    rep(t, T + 3004) {

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

    }

    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.4318351782823904x):
#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;

}