#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T>
using V = vector<T>;
template <class T, class U>
using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const ll MOD = 1000000007;
const ll HIGHINF = (ll)1e18;
const int INF = 1e9;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  V<V<int> > wc(3);
  vll w3;
  for (int i = 0; i < n; i++) {
    int w;
    ll c;
    cin >> w >> c;
    if (w == 3)
      w3.emplace_back(c);
    else
      wc[w].emplace_back(c);
  }
  sort(w3.begin(), w3.end());
  w3.emplace_back(0LL);
  reverse(w3.begin(), w3.end());
  for (int i = 1; i < w3.size(); i++) w3[i] += w3[i - 1];
  sort(wc[1].begin(), wc[1].end());
  reverse(wc[1].begin(), wc[1].end());
  sort(wc[2].begin(), wc[2].end());
  reverse(wc[2].begin(), wc[2].end());
  int i1 = 0, i2 = 0;
  V<P<ll, P<int, int> > > dp(m + 1, make_pair(0, make_pair(0, 0)));
  for (int i = 0; i < m; i++) {
    if (dp[i].second.first < wc[1].size() && i + 1 <= m &&
        dp[i + 1].first < dp[i].first + wc[1][dp[i].second.first]) {
      dp[i + 1].first = dp[i].first + wc[1][dp[i].second.first];
      dp[i + 1].second = make_pair(dp[i].second.first + 1, dp[i].second.second);
    }
    if (dp[i].second.second < wc[2].size() && i + 2 <= m &&
        dp[i + 2].first < dp[i].first + wc[2][dp[i].second.first]) {
      dp[i + 2].first = dp[i].first + wc[2][dp[i].second.second];
      dp[i + 2].second = make_pair(dp[i].second.first, dp[i].second.second + 1);
    }
  }
  ll ans = 0;
  for (int i = 0; i <= m; i++) {
    chmax(ans, dp[i].first + w3[min((m - i) / 3, (int)w3.size() - 1)]);
  }
  cout << ans << '\n';
  return 0;
}
