#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<long long> A, B, C;
vector<long long> b, c, bb, cc;
long long solve(int mm) {
  b.push_back(0);
  bb.resize(b.size());
  bb[0] = b[0];
  for (int i = 1; i < (int)b.size(); i++) bb[i] = b[i] + bb[i - 1];
  cc.resize(c.size());
  cc[0] = c[0];
  for (int i = 1; i < (int)c.size(); i++) cc[i] = c[i] + cc[i - 1];
  long long ans = 0;
  for (int i = 0; i * 3 <= mm && i < (int)c.size(); i++) {
    int r = mm - i * 3;
    r = min(r / 2, -1 + (int)b.size());
    ans = max(ans, cc[i] - c[i] + bb[r] - b[r]);
  }
  return ans;
}
int main() {
  scanf("%d", &n), scanf("%d", &m);
  for (int i = 0; i < (int)n; i++) {
    int w, c;
    scanf("%d", &w), scanf("%d", &c);
    if (w == 1) A.push_back(c);
    if (w == 2) B.push_back(c);
    if (w == 3) C.push_back(c);
  }
  sort(A.begin(), A.end(), greater<long long>());
  sort(B.begin(), B.end(), greater<long long>());
  sort(C.begin(), C.end(), greater<long long>());
  for (int i = 0; i + 1 < (int)A.size(); i += 2) {
    b.push_back(A[i] + A[i + 1]);
  }
  for (int i = 0; i < (int)B.size(); i++) b.push_back(B[i]);
  sort(b.begin(), b.end(), greater<long long>());
  for (int i = 0; i < (int)C.size(); i++) c.push_back(C[i]);
  c.push_back(0);
  long long ans = solve(m);
  if (A.size() > 0) {
    b.clear();
    for (int i = 1; i + 1 < (int)A.size(); i += 2) {
      b.push_back(A[i] + A[i + 1]);
    }
    for (int i = 0; i < (int)B.size(); i++) b.push_back(B[i]);
    sort(b.begin(), b.end(), greater<long long>());
    ans = max(ans, solve(m - 1) + A[0]);
  }
  printf("%lld\n", ans);
}
