#include <bits/stdc++.h>
using namespace std;
int N, M;
vector<long long> one, two, three;
long long dp[1000000];
vector<long long> pre1, pre2, pre3;
long long ans;
long long score(int tot, int a2) { return pre2[a2] + pre1[tot - 2 * a2]; }
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    int a, b;
    cin >> a >> b;
    if (a == 1) {
      one.push_back(b);
    }
    if (a == 2) {
      two.push_back(b);
    }
    if (a == 3) {
      three.push_back(b);
    }
  }
  sort(one.begin(), one.end());
  reverse(one.begin(), one.end());
  sort(two.begin(), two.end());
  reverse(two.begin(), two.end());
  sort(three.begin(), three.end());
  reverse(three.begin(), three.end());
  pre1.push_back(0);
  for (int i = 0; i < one.size(); i++) {
    pre1.push_back(pre1.back() + one[i]);
  }
  pre2.push_back(0);
  for (int i = 0; i < two.size(); i++) {
    pre2.push_back(pre2.back() + two[i]);
  }
  pre3.push_back(0);
  for (int i = 0; i < three.size(); i++) {
    pre3.push_back(pre3.back() + three[i]);
  }
  for (int i = 0; i <= one.size() + 2 * two.size(); i++) {
    dp[i] = -1000000000000000001ll;
    int small = max(0, ((int)(i - one.size() + 1) / 2));
    int big = min(i / 2, (int)(two.size()));
    while (big - small > 2) {
      int lt = (2 * small + big) / 3;
      int rt = (2 * big + small) / 3;
      if (score(i, lt) > score(i, rt)) {
        big = rt;
      }
      if (score(i, lt) < score(i, rt)) {
        small = lt;
      }
      if (score(i, lt) == score(i, rt)) {
        small = lt;
        big = rt;
      }
    }
    for (int j = small; j <= big; j++) {
      dp[i] = max(dp[i], score(i, j));
    }
  }
  for (int i = 1; i <= M; i++) {
    dp[i] = max(dp[i], dp[i - 1]);
  }
  for (int i = 0; i <= three.size() && 3 * i <= M; i++) {
    ans = max(ans, pre3[i] + dp[M - 3 * i]);
  }
  cout << ans << '\n';
  return 0;
}
