#include <bits/stdc++.h>
using namespace std;
typedef struct {
  long long int cost, ones, twos;
} s;
int main() {
  long long int n, m;
  cin >> n >> m;
  vector<long long int> ones;
  vector<long long int> twos;
  vector<long long int> threes;
  for (long long int i = 0; i < n; i++) {
    long long int a, b;
    cin >> a >> b;
    if (a == 1) ones.push_back(b);
    if (a == 2) twos.push_back(b);
    if (a == 3) threes.push_back(b);
  }
  sort(ones.begin(), ones.end());
  sort(twos.begin(), twos.end());
  sort(threes.begin(), threes.end());
  vector<s> ss(m + 1);
  ss[0] = {0, 0, 0};
  if (ones.size() > 0) {
    ss[1] = {ones.back(), 1, 0};
  } else {
    ss[1] = {0, 0, 0};
  }
  for (long long int i = 2; i <= m; i++) {
    s a = {-1, 0, 0}, b = {-1, 0, 0}, c = ss[i - 1];
    if (ss[i - 1].ones < ones.size()) {
      a = {ss[i - 1].cost + ones[ones.size() - 1 - ss[i - 1].ones],
           ss[i - 1].ones + 1, ss[i - 1].twos};
    }
    if (ss[i - 2].twos < twos.size()) {
      b = {ss[i - 2].cost + twos[twos.size() - 1 - ss[i - 2].twos],
           ss[i - 2].ones, ss[i - 2].twos + 1};
    }
    s d;
    if (a.cost > b.cost)
      d = a;
    else
      d = b;
    if (d.cost > c.cost)
      ss[i] = d;
    else
      ss[i] = c;
  }
  vector<long long int> part_threes(threes.size() + 1, 0);
  for (long long int i = 1; i <= threes.size(); i++) {
    part_threes[i] = part_threes[i - 1] + threes[threes.size() - i];
  }
  long long int best = 0;
  for (long long int i = 0; i <= threes.size(); i++) {
    long long int a;
    if (3 * i > m)
      a = -1;
    else
      a = part_threes[i] + ss[m - 3 * i].cost;
    best = max(best, a);
  }
  cout << best << endl;
  return 0;
}
