// 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 + 3001, 0));

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

    rep(i, N) {

        P p = items[i];

        rep(t, T + 3000) {

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

                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(i, T + 3000) {

        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.4293399228501296x):
#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>> dp1(N + 1, vector<ll>(T, 0));

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

    rep(i, N) {

        P p = items[i];

        rep(t, T) {

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

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

            }

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

        }

    }



    rrep(i, N - 1, 0) {

        P p = items[i];

        rep(t, T) {

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

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

            }

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

        }

    }

    ll ans = 0;

    rep(i, N) {

        P p = items[i];

        rep(j, T) {

            ans = max(ans, dp1[i][j] + dp2[i+1][T - 1 - j] + p.second);

        }

    }

    cout << ans << '\n';

}



int main() {

    cin.tie(nullptr);

    ios::sync_with_stdio(false);

    cout << fixed << setprecision(15);

    Main();

    return 0;

}