#include <bits/stdc++.h>
using namespace std;
long n, m;
vector<long long> w[3];
long long f(long mid, long tot) {
  if (2 * mid > tot) return -(1 >> 30);
  if (mid < w[1].size() && (tot - 2 * mid) < w[0].size())
    return w[1][mid] + w[0][tot - 2 * mid];
  else if (mid < w[1].size())
    return w[1][mid] + w[0][w[0].size() - 1];
  else if ((tot - 2 * mid) < w[0].size())
    return w[1][w[1].size() - 1] + w[0][tot - 2 * mid];
  else
    return w[1][w[1].size() - 1] + w[0][w[0].size() - 1];
}
long long ternary(long y) {
  long l = 0, r = y / 2;
  while (r - l >= 3) {
    long m1 = l + (r - l) / 3;
    long m2 = r - (r - l) / 3;
    long long f1 = f(m1, y);
    long long f2 = f(m2, y);
    if (f1 < f2)
      l = m1;
    else
      r = m2;
  }
  long long ans = 0;
  for (long i = l; i <= r; i++) {
    ans = max(ans, f(i, y));
  }
  return ans;
}
int main() {
  cin >> n >> m;
  long i, j, we, c;
  for (i = 0; i < n; i++) {
    cin >> we >> c;
    w[we - 1].push_back(c);
  }
  for (i = 0; i < 3; i++) {
    sort(w[i].begin(), w[i].end(), greater<long>());
  }
  for (i = 0; i < 3; i++) w[i].insert(w[i].begin(), 0);
  for (i = 0; i < 3; i++) {
    for (j = 1; j < w[i].size(); j++) {
      w[i][j] += w[i][j - 1];
    }
  }
  long long ans = 0;
  for (i = 0; i < w[2].size() && i * 3 <= m; i++) {
    ans = max(ans, w[2][i] + ternary(m - 3 * i));
  }
  cout << ans << endl;
  return 0;
}
