#include <bits/stdc++.h>
using namespace std;
int N, M;
vector<vector<int> > C;
vector<vector<long long> > psum;
int main() {
  scanf("%d %d", &N, &M);
  C.resize(3);
  for (int i = 0; i < N; i++) {
    int w, c;
    scanf("%d %d", &w, &c);
    C[--w].push_back(c);
  }
  psum.resize(3);
  for (int i = 0; i < 3; i++) {
    sort(C[i].begin(), C[i].end());
    if (i == 2) reverse(C[i].begin(), C[i].end());
    psum[i].resize(C[i].size());
    for (int j = 0; j < C[i].size(); j++) {
      psum[i][j] = C[i][j];
      if (j) psum[i][j] += psum[i][j - 1];
    }
  }
  long long ans = 0;
  int W = M - min((int)C[2].size(), M / 3) * 3;
  long long cost = 0;
  for (int i = min((int)C[2].size(), M / 3); i >= 0; i--) {
    long long tmp = i == 0 ? 0 : psum[2][i - 1];
    while (W > 1) {
      if (C[0].size() == 0 && C[1].size() == 0)
        break;
      else if (C[0].size() == 0) {
        W -= 2;
        cost += C[1].back();
        C[1].pop_back();
      } else if (C[1].size() == 0) {
        W--;
        cost += C[0].back();
        C[0].pop_back();
      } else {
        int sum = 0;
        for (int i = 0; i < min(2, (int)C[0].size()); i++)
          sum += C[0][C[0].size() - 1 - i];
        if (sum < C[1].back()) {
          W -= 2;
          cost += C[1].back();
          C[1].pop_back();
        } else {
          W--;
          cost += C[0].back();
          C[0].pop_back();
        }
      }
    }
    if (W == 1 && C[0].size())
      ans = max(ans, C[0].back() + cost + tmp);
    else
      ans = max(ans, cost + tmp);
    W += 3;
  }
  printf("%lld", ans);
}
