#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 29;
const long long LINF = 1LL << 60;
long long mod = 1e9 + 7;
char READ_DATA;
int SIGNAL_INPUT;
template <typename Type>
inline Type ru(Type &v) {
  SIGNAL_INPUT = 1;
  while ((READ_DATA = getchar()) < '0' || READ_DATA > '9')
    if (READ_DATA == '-')
      SIGNAL_INPUT = -1;
    else if (READ_DATA == EOF)
      return EOF;
  v = READ_DATA - '0';
  while ((READ_DATA = getchar()) >= '0' && READ_DATA <= '9')
    v = v * 10 + READ_DATA - '0';
  v *= SIGNAL_INPUT;
  return v;
}
inline long long modru(long long &v) {
  long long p = 0;
  SIGNAL_INPUT = 1;
  while ((READ_DATA = getchar()) < '0' || READ_DATA > '9')
    if (READ_DATA == '-')
      SIGNAL_INPUT = -1;
    else if (READ_DATA == EOF)
      return EOF;
  p = v = READ_DATA - '0';
  while ((READ_DATA = getchar()) >= '0' && READ_DATA <= '9') {
    v = (v * 10 + READ_DATA - '0') % mod;
    p = (p * 10 + READ_DATA - '0') % (mod - 1);
  }
  v *= SIGNAL_INPUT;
  return p;
}
template <typename A, typename B>
inline char ru(A &x, B &y) {
  if (ru(x) == EOF) return EOF;
  ru(y);
  return 2;
}
template <typename A, typename B, typename C>
inline char ru(A &x, B &y, C &z) {
  if (ru(x) == EOF) return EOF;
  ru(y);
  ru(z);
  return 3;
}
template <typename A, typename B, typename C, typename D>
inline char ru(A &x, B &y, C &z, D &w) {
  if (ru(x) == EOF) return EOF;
  ru(y);
  ru(z);
  ru(w);
  return 4;
}
struct Edge {
  int u, v, next;
  int w, cap, flow;
  Edge(int _u = 0, int _v = 0, int nxt = -1, long long _w = 1,
       long long _cap = 0) {
    u = _u;
    v = _v;
    w = _w;
    cap = _cap;
    flow = 0;
    next = nxt;
  }
  int operator<(const Edge &b) const { return w < b.w; }
};
const int maxn = 3e5 + 7;
double eps = 1e-6;
int n, m;
long long cnt1, cnt2, cnt3;
long long a[maxn], b[maxn], c[maxn];
long long dp[maxn], pre[maxn];
pair<int, int> f[maxn];
int cmp(const long long &a, const long long &b) { return a > b; }
int main() {
  ru(n, m);
  long long w, v;
  for (int i = 1; i <= n; ++i) {
    ru(w, v);
    if (w == 1)
      a[++cnt1] = v;
    else if (w == 2)
      b[++cnt2] = v;
    else
      c[++cnt3] = v;
  }
  sort(a + 1, a + cnt1 + 1, cmp);
  sort(b + 1, b + cnt2 + 1, cmp);
  sort(c + 1, c + cnt3 + 1, cmp);
  int x;
  if (cnt1) {
    dp[1] = a[1];
    f[1].first = 1;
  }
  for (int i = 2; i <= m; ++i) {
    x = 0;
    dp[i] = dp[i - 1];
    f[i] = f[i - 1];
    if (f[i - 1].first < cnt1) {
      dp[i] = dp[i - 1] + a[f[i - 1].first + 1];
      x = 1;
    }
    if (f[i - 2].second < cnt2 && dp[i] < dp[i - 2] + b[f[i - 2].second + 1]) {
      dp[i] = dp[i - 2] + b[f[i - 2].second + 1];
      x = 2;
    }
    if (x == 1) {
      f[i] = f[i - 1];
      f[i].first++;
    } else if (x == 2) {
      f[i] = f[i - 2];
      f[i].second++;
    }
  }
  for (int i = 1; i <= m / 3; ++i) pre[i] = pre[i - 1] + c[i];
  long long ans = 0;
  for (int i = 0; i <= m; ++i) {
    ans = max(ans, dp[i] + pre[(m - i) / 3]);
  }
  cout << ans;
}
