#include <bits/stdc++.h>
using namespace std;
const int maxn = 300200;
struct Node {
  int a, b;
  long long v;
} dp[maxn];
long long n, w, a[4][maxn], sum[maxn];
int a1, b1, c1;
int main() {
  int x;
  long long y;
  scanf("%lld%lld", &n, &w);
  a1 = b1 = c1 = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d%lld", &x, &y);
    if (x == 1) a[1][++a1] = y;
    if (x == 2) a[2][++b1] = y;
    if (x == 3) a[3][++c1] = y;
  }
  sort(a[1] + 1, a[1] + a1 + 1, greater<long long>());
  sort(a[2] + 1, a[2] + b1 + 1, greater<long long>());
  sort(a[3] + 1, a[3] + c1 + 1, greater<long long>());
  for (int i = 1; i <= c1; i++) sum[i] = sum[i - 1] + a[3][i];
  dp[0].a = 0;
  dp[0].b = 0;
  dp[0].v = 0;
  for (int i = 1; i <= w; i++) {
    dp[i] = dp[i - 1];
    if (dp[i - 1].a < a1 && dp[i - 1].v + a[1][dp[i - 1].a + 1] > dp[i].v) {
      dp[i] = dp[i - 1];
      dp[i].v += a[1][dp[i - 1].a + 1];
      dp[i].a++;
    }
    if (i >= 2 && dp[i - 2].b < b1 &&
        dp[i - 2].v + a[2][dp[i - 2].b + 1] > dp[i].v) {
      dp[i] = dp[i - 2];
      dp[i].v += a[2][dp[i - 2].b + 1];
      dp[i].b++;
    }
  }
  long long cnt = dp[w].v;
  for (int i = 1; i <= c1; i++) {
    if (w >= i * 3) cnt = max(cnt, sum[i] + dp[w - 3 * i].v);
  }
  printf("%lld\n", cnt);
  return 0;
}
