#include <bits/stdc++.h>
using namespace std;
const int MAXN = 300010;
int n, m;
vector<int> ar1, ar2, ar3;
long long sum1[MAXN], sum2[MAXN], sum3[MAXN];
long long ans = 0;
long long f(int x, int k) { return sum2[x] + sum1[k - 2 * x]; }
long long get(int k) {
  if (!k) return 0;
  int lo = 0, hi = k / 2;
  while (lo < hi) {
    int mid = lo + (hi - lo) / 2;
    long long a = f(mid, k);
    long long b = f(mid + 1, k);
    if (lo == hi - 1) return max(a, b);
    if (a == b) return a;
    if (a > b)
      hi = mid;
    else
      lo = mid;
  }
  return f(lo, k);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    int w, c;
    scanf("%d%d", &w, &c);
    (w == 1 ? ar1 : (w == 2 ? ar2 : ar3)).push_back(c);
  }
  sort(ar1.begin(), ar1.end(), greater<int>());
  sort(ar2.begin(), ar2.end(), greater<int>());
  sort(ar3.begin(), ar3.end(), greater<int>());
  for (int i = 0; i < ar1.size(); i++) {
    sum1[i + 1] = ar1[i];
    if (i) sum1[i + 1] += sum1[i];
  }
  for (int i = 0; i < ar2.size(); i++) {
    sum2[i + 1] = ar2[i];
    if (i) sum2[i + 1] += sum2[i];
  }
  for (int i = 0; i < ar3.size(); i++) {
    sum3[i + 1] = ar3[i];
    if (i) sum3[i + 1] += sum3[i];
  }
  for (int i = 1; i <= m; i++) {
    if (!sum1[i]) sum1[i] = sum1[i - 1];
    if (!sum2[i]) sum2[i] = sum2[i - 1];
    if (!sum3[i]) sum3[i] = sum3[i - 1];
  }
  for (int i = 0; 3 * i <= m && i <= ar3.size(); i++) {
    long long cur = (i > 0 ? sum3[i] : 0);
    cur += get(m - 3 * i);
    ans = max(ans, cur);
  }
  printf("%lld\n", ans);
}
