#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int n;
struct data {
  ll cost;
  int cnt1, cnt2, cnt3;
} d[300010];
ll m, c1[100010], c2[100010], c3[100010], t1 = 0, t2 = 0, t3 = 0;
bool cmp(ll a, ll b) { return a > b; }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    int a;
    ll b;
    cin >> a >> b;
    if (a == 1) {
      ++t1;
      c1[t1] = b;
    }
    if (a == 2) {
      ++t2;
      c2[t2] = b;
    }
    if (a == 3) {
      ++t3;
      c3[t3] = b;
    }
  }
  d[0] = {0, 0, 0};
  sort(c1 + 1, c1 + 1 + t1, cmp);
  sort(c2 + 1, c2 + 1 + t2, cmp);
  sort(c3 + 1, c3 + 1 + t3, cmp);
  for (int i = 1; i <= m; ++i) {
    if (i >= 1) {
      if (d[i - 1].cnt1 < t1 &&
          d[i - 1].cost + c1[d[i - 1].cnt1 + 1] > d[i].cost) {
        d[i].cost = d[i - 1].cost + c1[d[i - 1].cnt1 + 1];
        d[i].cnt1 = d[i - 1].cnt1 + 1;
        d[i].cnt2 = d[i - 1].cnt2;
        d[i].cnt3 = d[i - 1].cnt3;
      }
    }
    if (i >= 2) {
      if (d[i - 2].cnt2 < t2 &&
          d[i - 2].cost + c2[d[i - 2].cnt2 + 1] > d[i].cost) {
        d[i].cost = d[i - 2].cost + c2[d[i - 2].cnt2 + 1];
        d[i].cnt1 = d[i - 2].cnt1;
        d[i].cnt2 = d[i - 2].cnt2 + 1;
        d[i].cnt3 = d[i - 2].cnt3;
      }
      if (d[i - 2].cnt1 > 0 && d[i - 2].cnt3 < t3) {
        if (d[i - 2].cost - c1[d[i - 2].cnt1] + c3[d[i - 2].cnt3 + 1] >
            d[i].cost) {
          d[i].cost = d[i - 2].cost - c1[d[i - 2].cnt1] + c3[d[i - 2].cnt3 + 1];
          d[i].cnt1 = d[i - 2].cnt1 - 1;
          d[i].cnt2 = d[i - 2].cnt2;
          d[i].cnt3 = d[i - 2].cnt3 + 1;
        }
      }
    }
    if (i >= 3) {
      if (d[i - 3].cnt3 < t3 &&
          d[i - 3].cost + c3[d[i - 3].cnt3 + 1] > d[i].cost) {
        d[i].cost = d[i - 3].cost + c3[d[i - 3].cnt3 + 1];
        d[i].cnt1 = d[i - 3].cnt1;
        d[i].cnt2 = d[i - 3].cnt2;
        d[i].cnt3 = d[i - 3].cnt3 + 1;
      }
    }
  }
  ll ans = 0;
  for (int i = 1; i <= m; ++i) ans = max(ans, d[i].cost);
  cout << ans;
  return 0;
}
