#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<pair<long long, long long>> p;
  for (int i = 0; i < n; ++i) {
    int w, c;
    cin >> w >> c;
    p.emplace_back(w, c);
  }
  sort(p.begin(), p.end(),
       [](const pair<long long, long long> &p1,
          const pair<long long, long long> &p2) {
         return p1.first * p2.second < p1.second * p2.first;
       });
  vector<long long> dp(m + 1, 0);
  int z = 0;
  for (int i = 0; i < p.size(); ++i) {
    int w = p[i].first;
    int c = p[i].second;
    z = min(z + w, m);
    int y = max(z - 3, w);
    for (int j = z; j >= y; --j) dp[j] = max(dp[j], dp[j - w] + c);
  }
  cout << *max_element(dp.begin(), dp.end()) << endl;
}
