#include <bits/stdc++.h>
using namespace std;
struct str {
  long long sum, tp1, tp2;
};
long long n, m, ans, sum[300100];
str dp[300100];
vector<long long> v[4];
bool comp(long long x, long long y) { return x > y; }
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    long long type, x;
    scanf("%lld%lld", &type, &x);
    v[type].push_back(x);
  }
  sort(v[1].begin(), v[1].end(), comp);
  sort(v[2].begin(), v[2].end(), comp);
  sort(v[3].begin(), v[3].end(), comp);
  for (int i = 1; i <= (int)v[3].size(); ++i) sum[i] = sum[i - 1] + v[3][i - 1];
  for (int i = 1; i <= m; ++i) {
    if (i == 1) {
      if (!v[1].empty()) dp[i] = {v[1][0], 1, 0};
      continue;
    }
    dp[i] = (dp[i - 2].sum > dp[i - 1].sum ? dp[i - 2] : dp[i - 1]);
    if (v[1].size() > dp[i - 1].tp1 &&
        dp[i - 1].sum + v[1][dp[i - 1].tp1] > dp[i].sum)
      dp[i] = {dp[i - 1].sum + v[1][dp[i - 1].tp1], dp[i - 1].tp1 + 1,
               dp[i - 1].tp2};
    if (v[2].size() > dp[i - 2].tp2 &&
        dp[i - 2].sum + v[2][dp[i - 2].tp2] > dp[i].sum)
      dp[i] = {dp[i - 2].sum + v[2][dp[i - 2].tp2], dp[i - 2].tp1,
               dp[i - 2].tp2 + 1};
  }
  for (int i = 0; i * 3 <= m; ++i) ans = max(ans, sum[i] + dp[m - i * 3].sum);
  cout << ans;
}
