#include <bits/stdc++.h>
using namespace std;
const int N = 300010;
int n, m;
int arr[4][N];
int idx[4];
long long best[N], ans = 0;
pair<int, int> best2[N];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    int w, c;
    scanf("%d%d", &w, &c);
    arr[w][idx[w]++] = c;
  }
  for (int i = 0; i < 4; i++) {
    sort(arr[i], arr[i] + idx[i]);
    reverse(arr[i], arr[i] + idx[i]);
  }
  best[1] = arr[1][0];
  ans = max(ans, best[1]);
  best2[1] = make_pair(0, -1);
  best2[0] = make_pair(-1, -1);
  for (int i = 2; i <= m; i++) {
    best[i] = best[i - 1] + arr[1][best2[i - 1].first + 1];
    best2[i] = best2[i - 1];
    best2[i].first++;
    if (best[i - 2] + arr[2][best2[i - 2].second + 1] > best[i]) {
      best[i] = best[i - 2] + arr[2][best2[i - 2].second + 1];
      best2[i] = best2[i - 2];
      best2[i].second++;
    }
    ans = max(ans, best[i]);
  }
  long long sum = 0;
  int i = 0;
  int cur = 0;
  while (cur + 3 <= m) {
    cur += 3;
    sum += arr[3][i++];
    ans = max(ans, sum + best[m - cur]);
  }
  cout << ans << endl;
  return 0;
}
