#include <bits/stdc++.h>
using namespace std;
const long long N = 100005;
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  {
    long long i, j, k, n, m, ans = 0, cnt = 0, sum = 0;
    cin >> n >> k;
    pair<long long, long long> a[n];
    vector<long long> v[4];
    for (i = 0; i < n; i++) {
      cin >> a[i].first >> a[i].second;
      v[a[i].first].push_back(a[i].second);
    }
    sort(v[1].rbegin(), v[1].rend());
    sort(v[2].rbegin(), v[2].rend());
    sort(v[3].rbegin(), v[3].rend());
    long long pref1[v[1].size() + 2];
    pref1[0] = 0;
    for (i = 0; i < v[1].size(); i++) {
      pref1[i + 1] = pref1[i] + v[1][i];
    }
    long long pref2[v[2].size() + 2];
    pref2[0] = 0;
    for (i = 0; i < v[2].size(); i++) {
      pref2[i + 1] = pref2[i] + v[2][i];
    }
    long long w = 0;
    for (i = 0; i <= v[3].size(); i++) {
      if (i > 0) {
        sum += v[3][i - 1];
        w += 3;
      }
      if (w > k) break;
      ans = max(ans, sum);
      if (w == k) break;
      long long left2 = 0, right2 = v[2].size();
      while (left2 <= right2) {
        long long mid = (left2 + right2) / 2;
        if (mid * 2 + w > k) {
          right2 = mid - 1;
          continue;
        }
        long long pos1 = min(k - (mid * 2 + w), (long long)v[1].size());
        long long temp = pref2[mid] + pref1[pos1];
        long long ss = 0;
        if (pos1 < v[1].size()) ss += v[1][pos1];
        if (pos1 + 1 < v[1].size()) ss += v[1][pos1 + 1];
        ans = max(ans, sum + pref2[mid] + pref1[pos1]);
        if (mid - 1 >= 0 and ss > v[2][mid - 1]) {
          right2 = mid - 1;
        } else {
          left2 = mid + 1;
        }
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
