#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using VI = vector<ll>;
using VV = vector<VI>;
using VS = vector<string>;

// tourist set
template <typename A, typename B>
string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string& s) {
  return '"' + s + '"';
}

string to_string(const char* s) {
  return to_string((string) s);
}

string to_string(bool b) {
  return (b ? "true" : "false");
}

string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}

template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cerr << '\n'; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
// tourist set end

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define rep(i,b) FOR(i, 0, b)
#define ALL(v) (v).begin(), (v).end()
#define p(s) cout<<(s)<<'\n'
#define p2(s, t) cout << (s) << " " << (t) << '\n'
#define br() p("")
#define pn(s) cout << (#s) << " " << (s) << '\n'
#define p_yes() p("YES")
#define p_no() p("NO")
#define SZ(x) ((int)(x).size())
#define SORT(A) sort(ALL(A))
#define RSORT(A) sort(ALL(A), greater<ll>())

void no(){p_no(); exit(0);}
void yes(){p_yes(); exit(0);}

const ll mod = 1e9 + 7;
const ll inf = 1e18;
const double PI = acos(-1);

ll N, T;

ll dp[3010][3010];
ll prev_w[3010][3010];

void reset(){
  rep(i, 3010){
    rep(j, 3010){
      dp[i][j] = 0;
      prev_w[i][j] = 0;
    }
  }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    // input
    cin >> N >> T;

    VI A(N);
    VI B(N);
    rep(i, N){
      cin >> A[i] >> B[i];
    }

    ll n = N;
    ll W = T;

    auto weight = A;
    auto value = B;

    // DPループ
    for (int i = 0; i < n; ++i) {
        for (int w = 0; w <= W; ++w) {
            // i 番目の品物を選ぶ場合を考える
            if (w >= weight[i]) {
                if (dp[i+1][w] < dp[i][w-weight[i]] + value[i]) {
                    dp[i+1][w] = dp[i][w-weight[i]] + value[i];
                    prev_w[i+1][w] = w - weight[i];
                }
            }

            // i 番目の品物を選ばない場合を考える
            if (dp[i+1][w] < dp[i][w]) {
                dp[i+1][w] = dp[i][w];
                prev_w[i+1][w] = w;
            }
        }
    }

    // 最適値の出力
    // cout << dp[n][W] << endl;
    ll ans = dp[n][W];

    // キャパシティを１つ減らしてもう１回ナップサック
    reset();
    W--;
    // DPループ
    for (int i = 0; i < n; ++i) {
        for (int w = 0; w <= W; ++w) {
            // i 番目の品物を選ぶ場合を考える
            if (w >= weight[i]) {
                if (dp[i+1][w] < dp[i][w-weight[i]] + value[i]) {
                    dp[i+1][w] = dp[i][w-weight[i]] + value[i];
                    prev_w[i+1][w] = w - weight[i];
                }
            }

            // i 番目の品物を選ばない場合を考える
            if (dp[i+1][w] < dp[i][w]) {
                dp[i+1][w] = dp[i][w];
                prev_w[i+1][w] = w;
            }
        }
    }

    vector<bool> selected(N);

    // 復元
    // cout << "Selected items are..." << endl;
    int cur_w = W;
    for (int i = n-1; i >= 0; --i) {
        // 選んでいた場合
        if (prev_w[i+1][cur_w] == cur_w - weight[i]) {
          selected[i] = true;
            //cout << i << " th item (weight = " << weight[i] << ", value = " << value[i] << ")" << endl;
        }

        // 復元テーブルをたどる
        cur_w = prev_w[i+1][cur_w];
    }

    // 選ばれなかった奴ら
    ll max_taste = 0;
    rep(i, N){
      if(selected[i]) continue;
      chmax(max_taste, B[i]);
    }
    // debug(selected);

    chmax(ans, dp[n][W]+max_taste);
    p(ans);
    return 0;

    // pn(max_taste);

    VI V;
    VI X; // 重さ
    rep(i, N){
      if(!selected[i]) continue;
      // 選ばれた奴らだけ
      V.push_back(B[i]);
      X.push_back(A[i]);
    }
    V.push_back(max_taste);
    X.push_back(1);

    ll C = W;
    debug(C);
    rep(i, 3010){
      rep(j, 3010){
        dp[i][j] = 0;
      }
    }

    debug(N);
    N = V.size();
    // i番目以降を見て
    // jキャパシティに入る最大の価値
    for(int i=N-1; i>=0; i--){
        FOR(j, 0, C+1){
            if(X[i]>j){
                // 入らない
                dp[i][j] = dp[i+1][j];
            }
            else{
                ll v0 = dp[i+1][j];
                ll v1 = dp[i+1][j-X[i]] + V[i];
                dp[i][j] = max(v0, v1);
            }
        }
    }
    p(dp[0][C]);


    
    return 0;
}

Measured memory_bytes: 141952