#include <bits/stdc++.h>
using namespace std;
vector<long long int> u[4], v[4];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int n, m, i, j, ans = 0;
  cin >> n >> m;
  for (i = 0; i < n; ++i) {
    long long int x, y;
    cin >> x >> y;
    v[x].push_back(y);
    u[x].push_back(y);
  }
  v[3].push_back(1e18);
  for (i = 1; i <= 3; ++i) {
    sort(v[i].begin(), v[i].end(), greater<long long int>());
    sort(u[i].begin(), u[i].end(), greater<long long int>());
  }
  v[3][0] = 0;
  for (i = 1; i < (long long int)(v[3].size()); ++i) v[3][i] += v[3][i - 1];
  for (i = 1; i < (long long int)(v[1].size()); i += 2) {
    v[2].push_back(v[1][i] + v[1][i - 1]);
  }
  for (i = 2; i < (long long int)(u[1].size()); i += 2) {
    u[2].push_back(u[1][i] + u[1][i - 1]);
  }
  sort(v[2].begin(), v[2].end(), greater<long long int>());
  sort(u[2].begin(), u[2].end(), greater<long long int>());
  for (i = 0; i < (long long int)(v[3].size()); ++i) {
    long long int r = m, val1 = 0, val2 = 0;
    r = r - 3 * i;
    if (r < 0) continue;
    long long int y = r;
    val1 = v[3][i];
    val2 = v[3][i];
    if ((long long int)(u[1].size()) && y > 0) {
      val1 = val1 + u[1][0];
      --y;
    }
    for (j = 0; j < min((y / 2), (long long int)(u[2].size())); ++j) {
      val1 = val1 + u[2][j];
    }
    for (j = 0; j < min((r / 2), (long long int)(v[2].size())); ++j) {
      val2 = val2 + v[2][j];
    }
    ans = max({ans, val1, val2});
  }
  cout << ans;
  return 0;
}
