#include <bits/stdc++.h>
using namespace std;
const long long N = (2e5 + 5);
const long long MOD = 10243;
const long long INF = 1e16;
const long long LOG = 29;
long long binpow(long long a, long long b) {
  a %= MOD;
  long long res = 1;
  while (b > 0) {
    if (b & 1) res = (res * a) % MOD;
    a = (a * a) % MOD;
    b >>= 1;
  }
  res %= MOD;
  return res;
}
struct gg {
  long long ans;
  long long one;
  long long two;
};
gg dp[300005];
vector<long long> w[4];
void solve() {
  long long n, m;
  cin >> n >> m;
  long long lol[n + 1], c[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> lol[i] >> c[i];
    w[lol[i]].push_back(c[i]);
  }
  for (int i = 1; i <= 3; i++) {
    sort(w[i].begin(), w[i].end(), greater<long long>());
  }
  dp[0].ans = 0;
  dp[0].one = 0;
  dp[0].two = 0;
  for (int i = 1; i <= m; i++) {
    dp[i] = {0, 0, 0};
  }
  if (m == 1) {
    long long i = 0;
    long long wp = dp[i].one;
    if (wp >= w[1].size())
      wp = 0;
    else
      wp = w[1][wp];
    long long x = 1;
    if (wp == 0) x = 0;
    if (dp[i].ans + wp > dp[i + 1].ans) {
      dp[i + 1].ans = dp[i].ans + wp;
      dp[i + 1].one = dp[i].one + x;
      dp[i + 1].two = dp[i].two;
    }
  }
  for (int i = 0; i <= m - 2; i++) {
    long long wp = dp[i].one;
    if (wp >= w[1].size())
      wp = 0;
    else
      wp = w[1][wp];
    long long x = 1;
    if (wp == 0) x = 0;
    if (dp[i].ans + wp > dp[i + 1].ans) {
      dp[i + 1].ans = dp[i].ans + wp;
      dp[i + 1].one = dp[i].one + x;
      dp[i + 1].two = dp[i].two;
    }
    long long o = dp[i].one;
    long long t = dp[i].two;
    if ((o + 1) >= w[1].size() && t >= w[2].size()) {
      if (dp[i + 2].ans < dp[i].ans) {
        dp[i + 2] = dp[i];
      }
      continue;
    }
    if ((o + 1) >= w[1].size()) {
      if (dp[i].ans + w[2][t] > dp[i + 2].ans) {
        dp[i + 2].ans = dp[i].ans + w[2][t];
        dp[i + 2].two = dp[i].two + 1;
        dp[i + 2].one = dp[i].one;
      }
    } else if (t >= w[2].size()) {
      if (dp[i].ans + w[1][o] + w[1][o + 1] > dp[i + 2].ans) {
        dp[i + 2].ans = dp[i].ans + w[1][o] + w[1][o + 1];
        dp[i + 2].two = dp[i].two;
        dp[i + 2].one = dp[i].one + 2;
      }
    } else {
      if (w[1][o] + w[1][o + 1] > w[2][t]) {
        if (dp[i].ans + w[1][o] + w[1][o + 1] > dp[i + 2].ans) {
          dp[i + 2].ans = dp[i].ans + w[1][o] + w[1][o + 1];
          dp[i + 2].two = dp[i].two;
          dp[i + 2].one = dp[i].one + 2;
        }
      } else {
        if (dp[i].ans + w[2][t] > dp[i + 2].ans) {
          dp[i + 2].ans = dp[i].ans + w[2][t];
          dp[i + 2].two = dp[i].two + 1;
          dp[i + 2].one = dp[i].one;
        }
      }
    }
  }
  long long ans = 0;
  long long sum = 0;
  for (long long x = 0; x < w[3].size(); x++) {
    if (3 * (x + 1) > m) break;
    sum += w[3][x];
    ans = max(ans, sum + dp[m - 3 * (x + 1)].ans);
  }
  for (int i = 1; i <= m; i++) {
    ans = max(ans, dp[i].ans);
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  long long t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
