#include <bits/stdc++.h>
const int N = 100005;
using namespace std;
int n, m, sz[3];
long long d[3][N], f[N << 2];
void solve() {
  for (int i = 0; i <= 2; ++i) sort(d[i] + 1, d[i] + sz[i] + 1);
  for (int i = 0; i <= 2; ++i) reverse(d[i] + 1, d[i] + sz[i] + 1);
  for (int i = 0; i <= 2; ++i) {
    for (int j = 2; j <= sz[i]; ++j) d[i][j] += d[i][j - 1];
  }
  int top0 = 2, top1 = 1;
  for (int i = 2; i <= m; i += 2) {
    f[i] = f[i - 2];
    if (top0 <= sz[0] && (top1 > sz[1] || d[0][top0] - d[0][top0 - 2] >=
                                              d[1][top1] - d[1][top1 - 1]))
      f[i] = f[i - 2] + d[0][top0] - d[0][top0 - 2], top0 += 2;
    else if (top1 <= sz[1])
      f[i] = f[i - 2] + d[1][top1] - d[1][top1 - 1], ++top1;
  }
  f[1] = d[0][1];
  top0 = 3, top1 = 1;
  for (int i = 3; i <= m; i += 2) {
    f[i] = f[i - 2];
    if (top0 <= sz[0] && (top1 > sz[1] || d[0][top0] - d[0][top0 - 2] >=
                                              d[1][top1] - d[1][top1 - 1]))
      f[i] = f[i - 2] + d[0][top0] - d[0][top0 - 2], top0 += 2;
    else if (top1 <= sz[1])
      f[i] = f[i - 2] + d[1][top1] - d[1][top1 - 1], ++top1;
  }
  long long ans = 0;
  for (int i = 0; i <= m; ++i) {
    int x = (m - i) / 3;
    x = min(x, sz[2]);
    ans = max(ans, f[i] + d[2][x]);
  }
  printf("%I64d\n", ans);
}
int main() {
  int x, y;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d", &x, &y);
    --x;
    ++sz[x];
    d[x][sz[x]] = y;
  }
  solve();
  return 0;
}
