#include <bits/stdc++.h>
using namespace std;
int cnt[4];
vector<int> arr[4];
vector<int> sorted[2];
long long sum0[100010];
long long sum1[100010];
long long mx[300010];
int main() {
  int n, m, w, c;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> w >> c;
    arr[w].push_back(c);
    cnt[w]++;
  }
  for (int i = 1; i <= 3; i++)
    sort(arr[i].begin(), arr[i].end(), greater<int>());
  for (auto o : arr[2]) sorted[0].push_back(o), sorted[1].push_back(o);
  for (int i = 0; i + 1 < arr[1].size(); i += 2)
    sorted[0].push_back(arr[1][i] + arr[1][i + 1]);
  for (int i = 1; i + 1 < arr[1].size(); i += 2)
    sorted[1].push_back(arr[1][i] + arr[1][i + 1]);
  sort(sorted[0].begin(), sorted[0].end(), greater<int>());
  sort(sorted[1].begin(), sorted[1].end(), greater<int>());
  for (int i = 1; i <= sorted[0].size(); i++)
    sum0[i] = sum0[i - 1] + sorted[0][i - 1];
  for (int i = 1; i <= sorted[1].size(); i++)
    sum1[i] = sum1[i - 1] + sorted[1][i - 1];
  for (int i = 1; i <= 300000; i++) {
    long long v = 0;
    if (i & 1) {
      if (cnt[1] != 0)
        if (i / 2 <= sorted[1].size()) v = sum1[i / 2] + arr[1][0];
    } else {
      if (i / 2 <= sorted[0].size()) v = sum0[i / 2];
    }
    mx[i] = max(mx[i - 1], v);
  }
  long long ans = 0, sum = 0;
  for (int i = 0; i <= cnt[3]; i++) {
    if (m >= i * 3) ans = max(ans, sum + mx[m - i * 3]);
    if (i < cnt[3]) sum += arr[3][i];
  }
  cout << ans;
  return 0;
}
