#include <bits/stdc++.h>
using namespace std;
string convertstring(long long n) {
  stringstream ss;
  ss << n;
  return ss.str();
}
const int N = 300002;
pair<int, long long> a[N];
std::vector<long long> v[4];
long long dp[N];
std::vector<long long> oddtimes, eventimes;
int main() {
  int n, m, i;
  long long maxi = 0;
  scanf("%d", &n);
  scanf("%d", &m);
  for (i = (int)0; i < (int)n; i++) {
    scanf("%d", &a[i].first);
    scanf("%lld", &a[i].second);
    v[a[i].first].push_back(a[i].second);
  }
  for (i = (int)1; i < (int)4; i++) {
    sort(v[i].begin(), v[i].end());
    reverse(v[i].begin(), v[i].end());
  }
  if (v[1].size() >= 1) maxi = v[1][0];
  oddtimes = v[2];
  eventimes = v[2];
  for (i = 1; i < v[1].size(); i += 2) {
    eventimes.push_back(v[1][i] + v[1][i - 1]);
  }
  for (i = 2; i < v[1].size(); i += 2) {
    oddtimes.push_back(v[1][i] + v[1][i - 1]);
  }
  sort(oddtimes.begin(), oddtimes.end());
  sort(eventimes.begin(), eventimes.end());
  reverse(oddtimes.begin(), oddtimes.end());
  reverse(eventimes.begin(), eventimes.end());
  for (i = 1; i < oddtimes.size(); i++) {
    oddtimes[i] += oddtimes[i - 1];
  }
  for (i = 1; i < eventimes.size(); i++) {
    eventimes[i] += eventimes[i - 1];
  }
  for (i = 1; i <= m; i++) {
    if (i % 2) {
      dp[i] = maxi;
      int p = min((i / 2) - 1, (int)oddtimes.size() - 1);
      if (p >= 0) dp[i] = maxi + oddtimes[p];
    } else {
      int p = min((i / 2) - 1, (int)eventimes.size() - 1);
      if (p >= 0) dp[i] = eventimes[p];
    }
    dp[i] = max(dp[i], dp[i - 1]);
  }
  long long ans = dp[m], wt = 0, cost = 0;
  for (i = 0; i < v[3].size(); i++) {
    wt += 3;
    if (wt > m) break;
    cost += v[3][i];
    ans = max(ans, cost + dp[m - wt]);
  }
  cout << ans << endl;
  return 0;
}
