#include <bits/stdc++.h>
using namespace std;
clock_t start;
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
template <typename T1, typename T2, typename T3>
struct query {
  T1 ss;
  T2 x;
  T3 y;
};
template <typename T1, typename T2, typename T3>
bool operator<(query<T1, T2, T3>& o1, query<T1, T2, T3>& o2) {
  return o1.x < o2.x;
}
template <typename T1, typename T2, typename T3>
istream& operator>>(istream& in, query<T1, T2, T3>& q) {
  in >> q.ss >> q.x >> q.y;
  return in;
}
template <typename T1, typename T2, typename T3>
ostream& operator<<(ostream& out, query<T1, T2, T3>& q) {
  out << q.ss << " " << q.x << " " << q.y;
  return out;
}
void solve() {
  long long n, m, ans = 0;
  cin >> n >> m;
  vector<long long> v[3];
  for (long long i = 0; i < n; i++) {
    long long w, c;
    cin >> w >> c;
    w--;
    v[w].push_back(c);
  }
  v[0].resize(max(n, m + 1));
  v[1].resize(max(n, m + 1));
  v[2].resize(max(n, m + 1));
  sort((v[0]).rbegin(), (v[0]).rend());
  sort((v[1]).rbegin(), (v[1]).rend());
  sort((v[2]).rbegin(), (v[2]).rend());
  for (long long i = 1; i <= m; i++) {
    v[2][i] += v[2][i - 1];
  }
  pair<pair<long long, long long>, pair<long long, long long> > dp[m + 1];
  dp[0] = make_pair(make_pair(0, 0), make_pair(0, 0));
  for (long long j = 1; j <= m; j++) {
    long long x = v[0][dp[j - 1].first.second];
    long long ff = dp[j - 1].first.first + x;
    long long fs = dp[j - 1].first.second + 1;
    long long sf = dp[j - 1].second.first;
    if (j >= 2) {
      x = v[1][dp[j - 2].second.first];
      if (ff < dp[j - 2].first.first + x) {
        ff = dp[j - 2].first.first + x;
        fs = dp[j - 2].first.second;
        sf = dp[j - 2].second.first + 1;
      }
    }
    dp[j].first.first = ff;
    dp[j].first.second = fs;
    dp[j].second.first = sf;
    if ((m - j) / 3 - 1 >= 0)
      ans = max(ans, ff + v[2][(m - j) / 3 - 1]);
    else
      ans = max(ans, ff);
  }
  if (m / 3 > 0) ans = max(ans, v[2][m / 3 - 1]);
  cout << ans << '\n';
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  start = clock();
  cout << fixed << setprecision(20);
  while (t--) {
    solve();
  }
  double time_taken = double(clock() - start) / double(CLOCKS_PER_SEC);
}
