#include <bits/stdc++.h>
using namespace std;
int xm[4] = {-1, 1, 0, 0};
int ym[4] = {0, 0, -1, 1};
const int MOD = 1e9 + 7;
const int MAXN = 5e5 + 5;
const long long POW = 9973;
struct nd {
  long long c;
  int t1, t2;
};
nd dp[MAXN];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> ar[4];
  for (int i = 0; i < n; i++) {
    int w, c;
    cin >> w >> c;
    ar[w].push_back(c);
  }
  for (int i = 1; i <= 3; i++) sort(ar[i].begin(), ar[i].end(), greater<int>());
  int one = ar[1].size();
  int two = ar[2].size();
  for (int i = 0; i <= one + two * 2; i++) {
    if (i == 0)
      dp[i] = {0, 0, 0};
    else {
      dp[i] = dp[i - 1];
      if (i && dp[i - 1].t1 < one) {
        if (dp[i - 1].c + ar[1][dp[i - 1].t1] > dp[i].c) {
          dp[i] = {dp[i - 1].c + ar[1][dp[i - 1].t1], dp[i - 1].t1 + 1,
                   dp[i - 1].t2};
        }
      }
      if (i >= 2 && dp[i - 2].t2 < two) {
        if (dp[i - 2].c + ar[2][dp[i - 2].t2] > dp[i].c) {
          dp[i] = {dp[i - 2].c + ar[2][dp[i - 2].t2], dp[i - 2].t1,
                   dp[i - 2].t2 + 1};
        }
      }
    }
  }
  long long ans = 0;
  long long tot = 0;
  for (int i = 0; i <= ar[3].size(); i++) {
    int rm = m - 3 * i;
    if (rm < 0) break;
    ans = max(ans, tot + dp[min(rm, one + two * 2)].c);
    if (i < ar[3].size()) tot += ar[3][i];
  }
  cout << ans;
}
