#include <bits/stdc++.h>
using namespace std;
int n, m;
struct Souvenir {
  int w, c;
  bool operator<(const Souvenir &ot) const {
    if (ot.w == w) return c > ot.c;
    return w < ot.w;
  }
} s[100002];
int cnt[3];
long long sum[3][100002];
long long ans;
long long cost(int i, int w) {
  return sum[1][i] + sum[0][min(cnt[0], w - 2 * i)];
}
int bs(int w) {
  int i = 0, p = 1 << 16;
  while (p) {
    if (i + p <= cnt[1] && ((i + p) * 2 <= w) &&
        (cost(i + p, w) >= cost(i + p - 1, w)))
      i += p;
    p >>= 1;
  }
  return i;
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    scanf("%d %d", &s[i].w, &s[i].c);
    ++cnt[--s[i].w];
  }
  sort(s + 1, s + n + 1);
  int j = 0, crt = 0;
  for (int i = 1; i <= n; ++i) {
    if (s[i].w != s[i - 1].w) j = 0;
    crt = s[i].w;
    ++j;
    sum[crt][j] = sum[crt][j - 1] + 1LL * s[i].c;
  }
  for (int i = 0; i <= cnt[2] && m >= 3 * i; ++i) {
    int nr2 = bs(m - 3 * i), nr1 = min(cnt[0], m - 2 * nr2 - 3 * i);
    ans = max(ans, sum[0][nr1] + sum[1][nr2] + sum[2][i]);
  }
  printf("%lld\n", ans);
  return 0;
}
