#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5;
vector<int> g[4];
int n, m;
long long ans = 0;
long long sum[4][maxn + 50];
bool cmp(const int &a, const int &b) { return a > b; }
long long check(int x, int m) {
  long long s = sum[2][x];
  m = m - 2 * x;
  if (m > g[1].size())
    return s + sum[1][g[1].size()];
  else
    return s + sum[1][m];
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    int x, y;
    scanf("%d%d", &x, &y);
    g[x].push_back(y);
  }
  for (int i = 1; i <= 3; ++i) sort(g[i].begin(), g[i].end(), cmp);
  for (int i = 1; i <= 3; ++i) {
    if (g[i].size() == 0) continue;
    sum[i][1] = g[i][0];
    for (int j = 1; j < g[i].size(); ++j) sum[i][j + 1] = sum[i][j] + g[i][j];
  }
  long long s = 0;
  for (int i = 0; i <= min(m / 3, (int)g[3].size()); ++i) {
    s = sum[3][i];
    int bag = m - 3 * i;
    int l = 0, r = min((int)g[2].size(), bag / 2);
    while (l + 1 < r) {
      int lmid = (2 * l + r) / 3;
      int rmid = (2 * r + l) / 3;
      if (check(lmid, bag) > check(rmid, bag))
        r = rmid - 1;
      else
        l = lmid + 1;
    }
    for (int i = l; i <= r; ++i) ans = max(ans, s + check(i, bag));
  }
  printf("%lld\n", ans);
  return 0;
}
