#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<pair<int, int> > a;
  for (int i = 0; i < n; ++i) {
    int x, y;
    cin >> x >> y;
    a.push_back(make_pair(x, y));
  }
  sort(a.begin(), a.end());
  reverse(a.begin(), a.end());
  vector<int> cont(m + 1, 0);
  vector<int> cant(1e6, 0);
  vector<int> sum(1e6, 0);
  int resp = 0;
  for (int i = 0; i < n; ++i) {
    int f = a[i].first, s = a[i].second;
    cont[f] += s;
    cant[f]++;
    if (cont[f] > 0) sum[cant[f]] += cont[f];
  }
  for (int i = 0; i < 1e6; ++i) {
    resp = max(resp, sum[i]);
  }
  cout << resp << '\n';
  return 0;
}
