#include <bits/stdc++.h>
using namespace std;
unsigned sed = std::chrono::system_clock::now().time_since_epoch().count();
mt19937 rand_num(sed);
uniform_int_distribution<long long> dis(0, 1000000007);
template <typename T>
void read(T &x) {
  x = 0;
  char ch = getchar();
  long long fh = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-') fh = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  x *= fh;
}
template <typename T>
void write(T x) {
  if (x < 0) x = -x, putchar('-');
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
}
template <typename T>
void writeln(T x) {
  write(x);
  puts("");
}
long long n, m;
long long l1, l2, l3;
long long c1[100005], c2[100005], c3[100005];
long long sum1[100005], sum2[100005], sum3[100005];
signed main() {
  read(n);
  read(m);
  for (long long i = 1; i <= n; ++i) {
    long long w, c;
    read(w);
    read(c);
    if (w == 1) c1[++l1] = c;
    if (w == 2) c2[++l2] = c;
    if (w == 3) c3[++l3] = c;
  }
  sort(c1 + 1, c1 + l1 + 1);
  sort(c2 + 1, c2 + l2 + 1);
  sort(c3 + 1, c3 + l3 + 1);
  reverse(c1 + 1, c1 + l1 + 1);
  reverse(c2 + 1, c2 + l2 + 1);
  reverse(c3 + 1, c3 + l3 + 1);
  for (long long i = 1; i <= l1; ++i) sum1[i] = sum1[i - 1] + c1[i];
  for (long long i = 1; i <= l2; ++i) sum2[i] = sum2[i - 1] + c2[i];
  for (long long i = 1; i <= l3; ++i) sum3[i] = sum3[i - 1] + c3[i];
  long long ans = 0;
  for (long long i = 0; i <= min(m / 3, l3); ++i) {
    long long left = m - i * 3;
    long long l = 1, r = min(left / 2, l2);
    while (l < r) {
      long long mid = l + r + 1 >> 1;
      long long left1 = left - mid * 2;
      long long val =
          sum1[min(l1, left1)] + sum2[min(l2, mid)] + sum3[min(l3, i)];
      long long mid1 = mid - 1;
      long long left2 = left - mid1 * 2;
      long long val2 =
          sum1[min(l1, left2)] + sum2[min(l2, mid1)] + sum3[min(l3, i)];
      if (val >= val2)
        l = mid;
      else
        r = mid - 1;
    }
    for (long long j = max(0LL, l - 2); j <= min(l2, l + 2); ++j) {
      long long left1 = m - i * 3 - j * 2;
      if (left1 < 0) continue;
      ans =
          max(ans, sum1[min(l1, left1)] + sum2[min(l2, j)] + sum3[min(l3, i)]);
    }
  }
  writeln(ans);
  return 0;
}
