#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 10;
int w[maxn], c[maxn];
vector<long long> adj[4];
long long f(int m, int t) {
  return adj[2][t] + adj[1][min((int)adj[1].size() - 1, (m - 2 * t))];
}
long long get(int m) {
  int lo = 0, hi = min(m / 2, (int)adj[2].size() - 1);
  while (lo < hi) {
    int mid = (lo + hi) / 2;
    if (f(m, mid) < f(m, mid + 1))
      lo = mid + 1;
    else
      hi = mid;
  }
  return f(m, lo);
}
int main() {
  int n, m;
  cin >> n >> m;
  adj[1].push_back(0);
  adj[2].push_back(0);
  adj[3].push_back(0);
  for (int i = 1; i <= n; ++i) {
    cin >> w[i] >> c[i];
    adj[w[i]].push_back(c[i]);
  }
  for (int i = 1; i <= 3; ++i) {
    sort(adj[i].begin(), adj[i].end());
    reverse(adj[i].begin() + 1, adj[i].end());
    for (int j = 1; j < adj[i].size(); ++j) {
      adj[i][j] += adj[i][j - 1];
    }
  }
  long long ans = 0;
  long long acc = 0;
  for (int i = 0; i < adj[3].size() && 3 * i <= m; ++i) {
    acc = adj[3][i];
    ans = max(ans, acc + get(m - 3 * i));
  }
  cout << ans << '\n';
  return 0;
}
