#include <bits/stdc++.h>
using namespace std;
const int MAX = 4 * 1e5;
long long int dp[MAX];
pair<int, int> cnts[MAX];
vector<int> first, second, third;
void sortRev(vector<int> &arr) {
  sort(arr.begin(), arr.end());
  reverse(arr.begin(), arr.end());
}
int main() {
  int n, m, w, c;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &w, &c);
    if (w == 1) {
      first.push_back(c);
    } else if (w == 2) {
      second.push_back(c);
    } else if (w == 3) {
      third.push_back(c);
    }
  }
  sortRev(first);
  sortRev(second);
  sortRev(third);
  dp[0] = 0;
  for (int i = 1; i <= m; i++) {
    dp[i] = dp[i - 1];
    cnts[i] = cnts[i - 1];
    if (cnts[i - 1].first < first.size()) {
      cnts[i] = cnts[i - 1];
      dp[i] = dp[i - 1] + first[cnts[i].first];
      cnts[i].first++;
    }
    if (i >= 2) {
      if (cnts[i - 2].second < second.size()) {
        long long int ones = 0;
        int last = cnts[i].first;
        if (last >= 2) {
          ones += first[last - 1] + first[last - 2];
        } else if (last >= 1) {
          ones += first[last - 1];
        }
        if ((dp[i - 2] + second[cnts[i - 2].second]) >= dp[i]) {
          cnts[i] = cnts[i - 2];
          dp[i] = dp[i - 2] + second[cnts[i - 2].second];
          cnts[i].second++;
        }
      }
    }
  }
  long long int curr = 0, ans = dp[m];
  for (int i = 0; i < third.size(); i++) {
    curr += third[i];
    int x = m - (3 * (i + 1));
    if (x >= 0) {
      ans = max(ans, dp[x] + curr);
    }
  }
  printf("%lld\n", ans);
  return 0;
}
