#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, m;
  cin >> n >> m;
  vector<vector<long long> > man(m + 1);
  for (int i = 0; i != n; ++i) {
    long long s, r;
    cin >> s >> r;
    man[s].push_back(r);
  }
  vector<long long> sum(n + 1, 0);
  for (int i = 0; i != man.size(); ++i) {
    sort(man[i].rbegin(), man[i].rend());
    long long now = 0;
    for (int h = 0; h != man[i].size(); ++h) {
      now += man[i][h];
      if (now < 0) break;
      sum[h] += now;
    }
  }
  long long mx = -1e16;
  for (int i = 0; i != sum.size(); ++i) {
    mx = max(sum[i], mx);
  }
  cout << max(0LL, mx) << endl;
}
