#include <bits/stdc++.h>
using namespace std;
int n, m, s1[300010], s2[300010];
long long f[300010];
vector<int> v[4];
bool cmp(int x, int y) { return x > y; }
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    v[x].push_back(y);
  }
  for (int i = 1; i <= 3; i++) {
    sort(v[i].begin(), v[i].end(), cmp);
  }
  for (int i = 1; i <= m; i++) {
    if (s1[i - 1] < v[1].size()) {
      if (f[i - 1] + v[1][s1[i - 1]] > f[i]) {
        f[i] = f[i - 1] + v[1][s1[i - 1]];
        s1[i] = s1[i - 1] + 1;
        s2[i] = s2[i - 1];
      }
    }
    if (i > 1 && s2[i - 2] < v[2].size()) {
      if (f[i - 2] + v[2][s2[i - 2]] > f[i]) {
        f[i] = f[i - 2] + v[2][s2[i - 2]];
        s1[i] = s1[i - 2];
        s2[i] = s2[i - 2] + 1;
      }
    }
  }
  for (int i = 1; i <= m; i++) {
    f[i] = max(f[i], f[i - 1]);
  }
  long long s = 0, ans = 0;
  ans = max(ans, f[m]);
  for (int i = 0; i < v[3].size(); i++) {
    if ((i + 1) * 3 > m) {
      break;
    }
    s += v[3][i];
    ans = max(ans, s + f[m - 3 - i * 3]);
  }
  cout << ans << endl;
  return 0;
}
