#include <bits/stdc++.h>
using namespace std;
long long n, m;
long long a[300001], b[300001], c[300001];
long long p1[300001], p2[300001], p3[300001], dp[300001];
signed main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(false);
  cin >> n >> m;
  long long p = 0, q = 0, r = 0;
  for (long long i = 1; i <= n; i++) {
    long long w, cost;
    cin >> w >> cost;
    if (w == 1)
      a[++p] = cost;
    else if (w == 2)
      b[++q] = cost;
    else
      c[++r] = cost;
  }
  sort(a + 1, a + p + 1);
  reverse(a + 1, a + p + 1);
  sort(b + 1, b + q + 1);
  reverse(b + 1, b + q + 1);
  sort(c + 1, c + r + 1);
  reverse(c + 1, c + r + 1);
  for (long long i = 1; i < r + 1; i++) c[i] += c[i - 1];
  p1[1] = 1;
  dp[1] = a[1];
  dp[2] = max(a[1] + a[2], b[1]);
  if (dp[2] == a[1] + a[2]) {
    p1[2] = 2;
  } else
    p2[2] = 1;
  for (long long i = 3; i < m + 1; i++) {
    long long x = p1[i - 1] + 1, y = p2[i - 1];
    dp[i] = dp[i - 1] + a[p1[i - 1] + 1];
    if (dp[i] < dp[i - 2] + a[p1[i - 2] + 1] + a[p1[i - 2] + 2]) {
      x = p1[i - 2] + 2;
      y = p2[i - 1];
      dp[i] = dp[i - 2] + a[p1[i - 2] + 1] + a[p1[i - 2] + 2];
    }
    if (dp[i] < dp[i - 2] + b[p2[i - 2] + 1]) {
      x = p1[i - 2], y = p2[i - 2] + 1;
      dp[i] = dp[i - 2] + b[p2[i - 2] + 1];
    }
    p1[i] = x;
    p2[i] = y;
  }
  long long ans = 0;
  for (long long i = 1; i < m + 1; i++) {
    dp[i] = max(dp[i - 1], dp[i]);
  }
  for (long long i = 0; i < r + 1; i++) {
    long long rem = m - i * 3;
    if (rem < 0) break;
    ans = max(ans, c[i] + dp[rem]);
  }
  cout << ans;
}
