#include <bits/stdc++.h>
using namespace std;
int n, m;
long long w[200009], c[200009], pre[10][200009];
vector<long long> v[10];
inline bool check(int mid, int curr1, int curr2) {
  if (v[1][curr1 - 2 * (mid - 1) - 1] + v[1][curr1 - 2 * (mid - 1) - 2] <=
      v[2][curr2 + (mid - 1)]) {
    return true;
  }
  return false;
}
inline long long calc_max(int i) {
  long long ans1 = 0, ans2 = 0;
  int req = m - (i * 3), curr1 = 0, curr2 = 0;
  if (v[1].size() == 0) {
    curr1 = 0;
  } else if (v[1].size() <= req) {
    curr1 = v[1].size();
    ans1 = pre[1][curr1 - 1];
  } else {
    curr1 = req;
    ans1 = pre[1][curr1 - 1];
  }
  req = req - curr1;
  if (v[2].size() == 0) {
    curr2 = 0;
  } else if (2 * (int)v[2].size() <= req) {
    curr2 = v[2].size();
    ans2 = pre[2][curr2 - 1];
  } else {
    curr2 = req / 2;
    ans2 = pre[2][curr2 - 1];
  }
  req = req - (curr2 * 2);
  if (curr1 == v[1].size() && curr2 == v[2].size()) {
    return ans1 + ans2;
  }
  if (curr1 == 0 && curr2 == 0) {
    return 0;
  }
  if (curr1 == 0 || curr2 == v[2].size()) {
    return ans1 + ans2;
  }
  if (req) {
    if (v[1][curr1 - 1] <= v[2][curr2]) {
      ans1 -= v[1][curr1 - 1];
      ans2 += v[2][curr2];
      curr2++;
      curr1--;
      req--;
    }
  }
  if (curr1 < 2 || curr2 == v[2].size()) {
    return ans1 + ans2;
  }
  if (v[1][curr1 - 1] + v[1][curr1 - 2] > v[2][curr2]) {
    return ans1 + ans2;
  }
  int low = 1, high = min((curr1 / 2), (int)v[2].size() - curr2);
  while (low < high) {
    int mid = low + ((high - low + 1) / 2);
    if (check(mid, curr1, curr2)) {
      low = mid;
    } else {
      high = mid - 1;
    }
  }
  if (check(low, curr1, curr2)) {
  } else {
  }
  ans1 = ans1 - (pre[1][curr1 - 1] - pre[1][curr1 - 2 * low - 1]);
  ans2 = ans2 + (pre[2][curr2 + (low - 1)] - pre[2][curr2 - 1]);
  return ans1 + ans2;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> w[i] >> c[i];
    v[w[i]].push_back(c[i]);
  }
  for (int i = 1; i <= 3; i++) {
    sort(v[i].begin(), v[i].end(), greater<long long>());
    if (v[i].size() >= 1) {
      pre[i][0] = v[i][0];
    }
    for (int j = 1; j < v[i].size(); j++) {
      pre[i][j] = pre[i][j - 1] + v[i][j];
    }
  }
  long long ans = 0, s = 0;
  for (int i = 0; i <= v[3].size(); i++) {
    if (m - (i * 3) >= 0) {
      if (i) {
        s = s + v[3][i - 1];
      }
      ans = max(ans, s + calc_max(i));
    }
  }
  cout << ans << "\n";
}
