#include <bits/stdc++.h>
using namespace std;
const long long MAXN = (long long)((1e5) + 10);
const long long dx[4] = {-1, 1, 0, 0};
const long long dy[4] = {0, 0, -1, 1};
const long long nax = (long long)(1e5 + 10);
vector<long long> v[nax], kon[nax];
signed main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL);
  long long t = 1;
  while (t--) {
    ios_base::sync_with_stdio(false), cin.tie(NULL);
    long long n, m;
    cin >> n >> m;
    unordered_map<long long, long long> mpp;
    for (long long i = 0; i < n; i++) {
      long long s, r;
      cin >> s >> r;
      if (v[s].size() == 0) {
        v[s].emplace_back(r);
      } else {
        v[s].emplace_back(r);
      }
      mpp[s]++;
    }
    for (long long i = 1; i < nax; i++) {
      sort(v[i].begin(), v[i].end());
      reverse(v[i].begin(), v[i].end());
      for (long long j = 0; j < v[i].size(); j++) {
        if (j != 0) {
          v[i][j] += v[i][j - 1];
        }
      }
    }
    long long maxi = 0;
    for (auto it : mpp) {
      maxi = max(it.second, maxi);
    }
    long long skill = 0;
    for (long long i = 1; i < nax; i++) {
      if (i == 1) {
        for (long long ii = 1; ii < nax; ii++) {
          long long sz = v[ii].size();
          if (sz > 0) {
            kon[i].emplace_back(ii);
          }
        }
      } else {
        long long len = kon[i - 1].size();
        for (auto it : kon[i - 1]) {
          if (v[it].size() >= i) {
            kon[i].emplace_back(it);
          }
        }
      }
    }
    for (long long i = 1; i < nax; i++) {
      long long sum = 0;
      for (auto it : kon[i]) {
        if (v[it][i - 1] >= 0) sum += v[it][i - 1];
      }
      skill = max(skill, sum);
    }
    cout << skill;
  }
  return 0;
}
