#include <bits/stdc++.h>
using namespace std;
typedef struct status {
  long long val;
  long long cnt1;
  long long cnt2;
} status;
vector<long long> v[4];
status s[300001];
int main() {
  int n, m, w;
  long long c, ans, add;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%d%I64d", &w, &c);
    v[w].push_back(c);
  }
  for (int i = 1; i <= 3; i++) {
    sort(v[i].begin(), v[i].end());
    reverse(v[i].begin(), v[i].end());
  }
  for (int i = 1; i <= m; i++) {
    if (s[i - 1].cnt1 < v[1].size()) {
      s[i].val = s[i - 1].val + v[1][s[i - 1].cnt1];
      s[i].cnt1 = s[i - 1].cnt1 + 1;
      s[i].cnt2 = s[i - 1].cnt2;
    }
    if (i >= 2 && s[i - 2].cnt2 < v[2].size() &&
        s[i].val < s[i - 2].val + v[2][s[i - 2].cnt2]) {
      s[i].val = s[i - 2].val + v[2][s[i - 2].cnt2];
      s[i].cnt1 = s[i - 2].cnt1;
      s[i].cnt2 = s[i - 2].cnt2 + 1;
    }
  }
  for (int i = 1; i <= m; i++)
    if (s[i].val < s[i - 1].val) s[i].val = s[i - 1].val;
  ans = 0;
  add = 0;
  for (int i = 0; i <= v[3].size() && m - 3 * i >= 0; i++) {
    if (s[m - 3 * i].val + add > ans) ans = s[m - 3 * i].val + add;
    if (i < v[3].size()) add += v[3][i];
  }
  printf("%I64d\n", ans);
  return 0;
}
