#include <bits/stdc++.h>
using namespace std;
long long int dp[1000005];
long long int p1[1000005];
long long int p2[1000005];
vector<long long int> v[5];
int main() {
  long long int n, m;
  scanf("%lld%lld", &n, &m);
  for (long long int i = 1; i <= n; i++) {
    long long int w, c;
    scanf("%lld%lld", &w, &c);
    v[w].push_back(c);
  }
  v[1].resize(m + 2);
  v[2].resize(m + 2);
  v[3].resize(m + 2);
  sort(v[1].begin(), v[1].end(), greater<long long int>());
  sort(v[2].begin(), v[2].end(), greater<long long int>());
  sort(v[3].begin(), v[3].end(), greater<long long int>());
  dp[1] = v[1][0];
  p1[1] = 1;
  p2[1] = 0;
  long long int ans = dp[1];
  for (long long int i = 2; i <= m; i++) {
    dp[i] = dp[i - 2] + v[1][p1[i - 2]] + v[1][p1[i - 2] + 1];
    p1[i] = p1[i - 2] + 2;
    p2[i] = p2[i - 2];
    if (dp[i] < dp[i - 2] + v[2][p2[i - 2]]) {
      dp[i] = dp[i - 2] + v[2][p2[i - 2]];
      p2[i] = p2[i - 2] + 1;
      p1[i] = p1[i - 2];
    }
    ans = max(ans, dp[i]);
  }
  long long int ptr = 0;
  long long int sum = 0;
  for (long long int i = 3; i <= m; i += 3) {
    ans = max(ans, dp[m - i] + sum + v[3][ptr]);
    sum += v[3][ptr];
    ptr++;
  }
  cout << ans;
  return 0;
}
