#include <bits/stdc++.h>
using namespace std;
const long long N = 1e9 + 5;
bool col2(pair<long long, long long> p1, pair<long long, long long> p2) {
  return p1.second > p2.second;
}
struct comp {
  bool operator()(long long const &x, long long const &y) { return x > y; }
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ;
  long long n, m;
  cin >> n >> m;
  vector<pair<long long, long long>> v(n);
  for (long long i = 0; i < n; i++) cin >> v[i].first >> v[i].second;
  map<long long, vector<long long>> mp;
  for (long long i = 0; i < n; i++) {
    mp[v[i].first].push_back(v[i].second);
  }
  vector<pair<long long, vector<long long>>> res;
  long long ck = 0;
  vector<long long> dummy(0);
  for (long long i = 0; i < (long long)mp.size(); i++) {
    res.push_back(make_pair(0, dummy));
  }
  for (auto i : mp) {
    for (long long j = 0; j < i.second.size(); j++)
      res[ck].second.push_back(i.second[j]);
    res[ck].first = i.second.size();
    sort(res[ck].second.begin(), res[ck].second.end());
    reverse(res[ck].second.begin(), res[ck].second.end());
    ck++;
  }
  sort(res.begin(), res.end());
  reverse(res.begin(), res.end());
  for (long long i = 0; i < res.size(); i++) {
    if (res[i].second.size() == 0) break;
    for (long long j = 1; j < res[i].second.size(); j++)
      res[i].second[j] += res[i].second[j - 1];
  }
  if (mp.size() == 1) {
    vector<long long> temp;
    for (auto i : mp) {
      for (long long j = 0; j < i.second.size(); j++)
        temp.push_back(i.second[j]);
    }
    sort(temp.begin(), temp.end());
    long long tans = 0;
    for (long long i = (long long)temp.size() - 1; i >= 0; i--) {
      if (temp[i] < 0) break;
      tans += temp[i];
    }
    cout << tans << endl;
    return 0;
  }
  vector<long long> ans;
  long long pt = 1;
  while (pt <= (long long)res[0].second.size()) {
    long long sum = 0;
    for (auto i : res) {
      if (i.second.size() < pt) break;
      if (i.second[pt - 1] > 0) sum += i.second[pt - 1];
    }
    ans.push_back(sum);
    pt++;
  }
  long long ans1 = *max_element(ans.begin(), ans.end());
  if (ans1 < 0)
    cout << 0 << endl;
  else
    cout << ans1 << endl;
}
