#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  long long n, m;
  scanf("%lld %lld", &n, &m);
  vector<vector<long long>> subjects(m);
  for (long long i = 0; i < n; i++) {
    long long si, ri;
    scanf("%lld %lld", &si, &ri);
    si--;
    subjects[si].push_back(ri);
  }
  for (long long i = 0; i < m; i++) {
    sort(subjects[i].rbegin(), subjects[i].rend());
  }
  sort(subjects.rbegin(), subjects.rend(),
       [](const vector<long long>& a, const vector<long long>& b) {
         return a.size() < b.size();
       });
  for (long long i = 0; i < m; i++) {
    for (long long j = 1; j < subjects[i].size(); j++) {
      ;
      ;
      ;
      subjects[i][j] = subjects[i][j - 1] + subjects[i][j];
      ;
    }
  }
  long long total = 0;
  for (long long pos = 0; pos < n; pos++) {
    long long sum = 0;
    for (long long i = 0; i < m; i++) {
      if (!(subjects[i].size() > pos)) {
        break;
      }
      if (subjects[i].size() > pos && subjects[i][pos] > 0) {
        sum += subjects[i][pos];
        ;
        ;
      }
    };
    total = max(total, sum);
  }
  cout << total << endl;
}
