#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
long long int memo[300005];
long long int sum[300005][3];
vector<int> v[4];
int first[300005];
int second[300005];
int main() {
  ios_base::sync_with_stdio(0);
  int n, m;
  int w, c;
  while (cin >> n >> m) {
    for (int i = 0; i < n; ++i) {
      cin >> w >> c;
      v[w].push_back(c);
    }
    for (int i = 1; i <= 3; ++i) sort(v[i].rbegin(), v[i].rend());
    memset(sum, 0, sizeof(sum));
    for (int i = 0; i < 3; ++i) {
      for (int j = 1; j <= m; ++j) {
        sum[j][i] = sum[j - 1][i];
        if (j <= v[i + 1].size()) sum[j][i] = v[i + 1][j - 1] + sum[j - 1][i];
      }
    }
    memset(memo, 0, sizeof(memo));
    memset(first, 0, sizeof(first));
    memset(second, 0, sizeof(second));
    memo[1] = sum[1][0];
    first[1] = 1;
    for (int j = 2; j <= m; ++j) {
      if (sum[first[j - 1] + 1][0] + sum[second[j - 1]][1] >
          sum[first[j - 2]][0] + sum[second[j - 2] + 1][1]) {
        first[j] = first[j - 1] + 1;
        second[j] = second[j - 1];
        memo[j] = sum[first[j]][0] + sum[second[j - 1]][1];
      } else {
        first[j] = first[j - 2];
        second[j] = second[j - 2] + 1;
        memo[j] = sum[first[j - 2]][0] + sum[second[j]][1];
      }
    }
    long long int mx = 0;
    for (int i = 0; i <= m; ++i) {
      mx = max(mx, memo[i] + sum[(m - i) / 3][2]);
    }
    cout << mx << "\n";
    for (int i = 1; i <= 3; ++i) v[i].clear();
  }
  return 0;
}
