#include <bits/stdc++.h>
using namespace std;
const int MAXN = 300000 + 10;
const int INF = 0x7fffffff;
struct {
  long long v;
  long long s1, s2;
} dp[MAXN];
long long a[4][MAXN];
long long s[4][MAXN];
int num[4];
int cmp(int x, int y) { return x > y; }
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  num[1] = num[2] = num[3] = 0;
  for (int i = 1; i <= n; i++) {
    long long w, c;
    scanf("%lld%lld", &w, &c);
    a[w][++num[w]] = c;
  }
  for (int i = 1; i <= 3; i++) {
    sort(a[i] + 1, a[i] + num[i] + 1, cmp);
    for (int j = 1; j <= num[i]; j++) {
      s[i][j] = s[i][j - 1] + a[i][j];
    }
  }
  dp[0].v = dp[0].s1 = dp[0].s2 = 0;
  for (int i = 1; i <= m; i++) {
    dp[i] = dp[i - 1];
    if (dp[i - 1].v + a[1][dp[i - 1].s1 + 1] > dp[i].v) {
      dp[i].v = dp[i - 1].v + a[1][dp[i - 1].s1 + 1];
      dp[i].s1 = dp[i - 1].s1 + 1;
      dp[i].s2 = dp[i - 1].s2;
    }
    if (i >= 2 && dp[i - 2].v + a[2][dp[i - 2].s2 + 1] > dp[i].v) {
      dp[i].v = dp[i - 2].v + a[2][dp[i - 2].s2 + 1];
      dp[i].s1 = dp[i - 2].s1;
      dp[i].s2 = dp[i - 2].s2 + 1;
    }
  }
  long long res = 0;
  for (int i = 0; i <= num[3]; i++) {
    if (m >= i * 3) res = max(res, s[3][i] + dp[m - 3 * i].v);
  }
  printf("%I64d\n", res);
}
