#include <bits/stdc++.h>
using namespace std;
const int Maxn = 300003;
struct Node {
  int p1, p2;
  long long val;
} d[Maxn];
int n, m;
vector<int> save_val[5];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    int w, v;
    scanf("%d%d", &w, &v);
    save_val[w].push_back(v);
  }
  for (int i = 1; i <= 3; ++i)
    sort(save_val[i].begin(), save_val[i].end(), greater<int>());
  for (int i = 1; i <= m; ++i) {
    d[i] = d[i - 1];
    if (d[i - 1].p1 + 1 <= save_val[1].size() &&
        d[i - 1].val + save_val[1][d[i - 1].p1] > d[i].val) {
      d[i].val = d[i - 1].val + save_val[1][d[i - 1].p1];
      d[i].p1 = d[i - 1].p1 + 1;
      d[i].p2 = d[i - 1].p2;
    }
    if (i > 1 && d[i - 2].p2 + 1 <= save_val[2].size() &&
        d[i - 2].val + save_val[2][d[i - 2].p2] > d[i].val) {
      d[i].val = d[i - 2].val + save_val[2][d[i - 2].p2];
      d[i].p1 = d[i - 2].p1;
      d[i].p2 = d[i - 2].p2 + 1;
    }
  }
  long long sum = 0, ans = 0;
  for (int i = 0; i <= save_val[3].size() && i * 3 <= m; ++i) {
    ans = max(ans, sum + d[m - i * 3].val);
    if (i < save_val[3].size()) sum += save_val[3][i];
  }
  printf("%lld\n", ans);
  return 0;
}
