#include <bits/stdc++.h>
using namespace std;
const int mxn = 1e5 + 5;
long long f[mxn], s[mxn], t[mxn];
int n, m, cf, cs, ct;
int nf, ns;
signed main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    int x, y;
    cin >> x >> y;
    if (x == 1)
      f[++cf] = y;
    else if (x == 2)
      s[++cs] = y;
    else
      t[++ct] = y;
  }
  sort(f + 1, f + cf + 1);
  reverse(f + 1, f + cf + 1);
  sort(s + 1, s + cs + 1);
  reverse(s + 1, s + cs + 1);
  sort(t + 1, t + ct + 1);
  reverse(t + 1, t + ct + 1);
  for (int i = 1; i <= cf; ++i) f[i] += f[i - 1];
  for (int i = 1; i <= cs; ++i) s[i] += s[i - 1];
  for (int i = 1; i <= ct; ++i) t[i] += t[i - 1];
  if (m >= cf + cs * 2 + ct * 3) {
    cout << f[cf] + s[cs] + t[ct] << '\n';
    return 0;
  }
  long long mx = 0, ans = 0;
  for (int i = 0; i <= cs; ++i) {
    int tmp = m - i * 2;
    tmp = min(tmp, cf);
    if (tmp < 0) break;
    long long sm = s[i] + f[tmp];
    if (sm > mx) {
      mx = sm;
      nf = tmp;
      ns = i;
    }
  }
  ans = mx;
  for (int i = 1; i <= ct; ++i) {
    mx = 0;
    int tps = ns, tpf = nf;
    for (int cg = -5; cg <= 5; ++cg) {
      int ts = ns + cg, tf = m - i * 3 - ts * 2;
      ts = min(ts, cs), tf = min(tf, cf);
      if (ts < 0 or tf < 0) continue;
      long long sm = t[i] + s[ts] + f[tf];
      if (sm > mx) {
        mx = sm;
        tps = ts;
        tpf = tf;
      }
    }
    if (ans < mx) {
      ans = mx;
      ns = tps;
      nf = tpf;
    }
  }
  cout << ans << '\n';
}
