#include <bits/stdc++.h>
using namespace std;
pair<int, int> f[3 * 110000];
long long sumx[110000], sumy[110000], sumz[110000];
int n, m, w, c, x[110000], y[110000], z[110000];
bool cmp(int a, int b) { return a > b; }
pair<int, int> add(pair<int, int> a, int k) {
  if (k == 1)
    a.first++;
  else
    a.second++;
  a.first = min(a.first, x[0]);
  a.second = min(a.second, y[0]);
  return a;
}
long long sum(pair<int, int> a) { return sumx[a.first] + sumy[a.second]; }
pair<int, int> max(pair<int, int> a, pair<int, int> b) {
  if (sum(a) >= sum(b))
    return a;
  else
    return b;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &w, &c);
    if (w == 1) x[++x[0]] = c;
    if (w == 2) y[++y[0]] = c;
    if (w == 3) z[++z[0]] = c;
  }
  sort(x + 1, x + x[0] + 1, cmp);
  sort(y + 1, y + y[0] + 1, cmp);
  sort(z + 1, z + z[0] + 1, cmp);
  for (int i = 1; i <= n; i++) {
    sumx[i] = sumx[i - 1] + x[i];
    sumy[i] = sumy[i - 1] + y[i];
    sumz[i] = sumz[i - 1] + z[i];
  }
  f[0] = pair<int, int>(0, 0);
  f[1] = add(f[0], 1);
  for (int i = 2; i <= m; i++) f[i] = max(add(f[i - 1], 1), add(f[i - 2], 2));
  long long ans = 0;
  for (int i = 0; 3 * i <= m; i++) ans = max(ans, sumz[i] + sum(f[m - 3 * i]));
  cout << ans;
  return 0;
}
