#include <bits/stdc++.h>
template <typename T1, typename T2>
inline void chmin(T1 &x, T2 b) {
  if (b < x) x = b;
}
template <typename T1, typename T2>
inline void chmax(T1 &x, T2 b) {
  if (b > x) x = b;
}
const int inf = 1039074182;
using namespace std;
int n, m;
int w[100005], c[100005];
vector<int> v1;
vector<int> v2;
vector<int> v3;
long long best[300005];
int rk1[100005];
int rk2[100005];
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> w[i] >> c[i];
    if (w[i] == 1) v1.push_back(c[i]);
    if (w[i] == 2) v2.push_back(c[i]);
    if (w[i] == 3) v3.push_back(c[i]);
  }
  sort(v1.begin(), v1.end());
  reverse(v1.begin(), v1.end());
  sort(v2.begin(), v2.end());
  reverse(v2.begin(), v2.end());
  sort(v3.begin(), v3.end());
  reverse(v3.begin(), v3.end());
  vector<pair<int, pair<int, int> > > vsort;
  for (int i = 0; i < v1.size(); i++) {
    vsort.push_back(make_pair(v1[i] * 2, make_pair(1, i)));
  }
  for (int i = 0; i < v2.size(); i++) {
    vsort.push_back(make_pair(v2[i], make_pair(2, i)));
  }
  sort(vsort.begin(), vsort.end());
  reverse(vsort.begin(), vsort.end());
  for (int i = 0; i < vsort.size(); i++) {
    if (vsort[i].second.first == 1)
      rk1[vsort[i].second.second] = i;
    else
      rk2[vsort[i].second.second] = i;
  }
  long long v12 = 0;
  int in12 = 0, cost12 = 0;
  int last1 = 0, last2 = 0;
  for (int i = 0; i <= m; i++) {
    while (in12 < vsort.size() && cost12 + vsort[in12].second.first <= i) {
      cost12 += vsort[in12].second.first;
      v12 += vsort[in12].first / (3 - vsort[in12].second.first);
      if (vsort[in12].second.first == 1)
        last1++;
      else
        last2++;
      in12++;
    }
    best[i] = v12;
    if (cost12 + 1 == i) {
      if (last1 < v1.size()) best[i] = max(best[i], v12 + v1[last1]);
      if (last2 < v2.size() && last1)
        best[i] = max(best[i], v12 - v1[last1 - 1] + v2[last2]);
    }
  }
  long long res = best[m];
  long long sum = 0;
  for (int i = 0; i < v3.size(); i++) {
    if (m - 3 * (i + 1) < 0) break;
    sum += v3[i];
    chmax(res, best[m - 3 * (i + 1)] + sum);
  }
  cout << res << endl;
  return 0;
}
