#include <bits/stdc++.h>
using namespace std;
const int N = 100000 + 10;
const int M = 1000000007;
const double PI = atan(1) * 4;
const int oo = 1000000000;
int n, m;
vector<long long> v[4];
int main() {
  cin >> n >> m;
  for (int a, b, i = 0; i < n; ++i) {
    scanf("%d%d", &a, &b);
    v[a].push_back(b);
  }
  for (int i = 0; i < 4; ++i) {
    sort(v[i].rbegin(), v[i].rend());
    v[i].insert(v[i].begin(), 0);
    for (int j = 1; j < v[i].size(); ++j) v[i][j] += v[i][j - 1];
  }
  long long answer = 0;
  for (int i = 0; i < v[3].size() && i <= m / 3; ++i) {
    long long ans = v[3][i], bst = 0;
    int lo = 0, hi = v[2].size() - 1, md1, md2, lf = m - 3 * i;
    hi = min(hi, lf / 2);
    while (lo <= hi) {
      md1 = (2 * lo + hi) / 3;
      md2 = (lo + 2 * hi) / 3;
      long long an1 = v[2][md1] + v[1][min((int)v[1].size() - 1, lf - 2 * md1)];
      long long an2 = v[2][md2] + v[1][min((int)v[1].size() - 1, lf - 2 * md2)];
      bst = max(bst, max(an1, an2));
      if (an1 > an2)
        hi = md2 - 1;
      else
        lo = md1 + 1;
    }
    answer = max(answer, ans + bst);
  }
  cout << answer << endl;
}
