#include <bits/stdc++.h>
using namespace std;
vector<vector<long long> > vec(10);
long long w[123456], c[123456];
pair<long long, pair<int, int> > dp[312345];
int main() {
  std::ios::sync_with_stdio(false);
  long long n, m, i, j, haha = 0;
  cin >> n >> m;
  for (i = 0; i < n; i++) {
    cin >> w[i] >> c[i];
    vec[w[i]].push_back(c[i]);
  }
  for (i = 0; i < 4; i++) {
    sort(vec[i].begin(), vec[i].end());
    reverse(vec[i].begin(), vec[i].end());
  }
  dp[0] = make_pair(0, make_pair(0, 0));
  if (vec[1].empty()) {
    dp[1] = make_pair(0, make_pair(0, 0));
  } else {
    dp[1] = make_pair(vec[1][0], make_pair(1, 0));
  }
  for (i = 2; i < m + 2; i++) {
    dp[i] = dp[i - 1];
    if (dp[i - 1].second.first < vec[1].size()) {
      haha = dp[i - 1].second.first;
      if (dp[i].first < dp[i - 1].first + vec[1][haha]) {
        dp[i].first = dp[i - 1].first + vec[1][haha];
        dp[i].second =
            make_pair(dp[i - 1].second.first + 1, dp[i - 1].second.second);
      }
    }
    if (dp[i - 2].second.second < vec[2].size()) {
      haha = dp[i - 2].second.second;
      if (dp[i].first < dp[i - 2].first + vec[2][haha]) {
        dp[i].first = dp[i - 2].first + vec[2][haha];
        dp[i].second =
            make_pair(dp[i - 2].second.first, dp[i - 2].second.second + 1);
      }
    }
  }
  long long sumi = 0;
  j = 0;
  long long maxi = 0;
  while (m >= 0) {
    maxi = max(dp[m].first + sumi, maxi);
    if (j == vec[3].size()) break;
    sumi += vec[3][j];
    j++;
    m -= 3;
  }
  cout << maxi << endl;
}
