#include <bits/stdc++.h>
using namespace std;
long long i, j, k, l, n, m, p, q, r, d, su, sx, sy,
    prod = 1, maxi, a, b, w, x, y, o, e, f, mini = 10000000000000, t, cnt, val;
string s, s1, s2;
long long ar[300010];
long long ans;
map<long long, vector<long long> > ma;
long long modularExponentiation(long long x, long long n) {
  if (n == 0)
    return 1;
  else if (n % 2 == 0)
    return modularExponentiation((x * x) % 998244353, n / 2);
  else
    return (x * modularExponentiation((x * x) % 998244353, (n - 1) / 2)) %
           998244353;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int ti = 1;
  while (ti--) {
    cin >> n >> m;
    for (long long i = 0; i < n; i++) {
      cin >> a >> b;
      ma[a].push_back(b);
    }
    for (auto i = ma.begin(); i != ma.end(); i++) {
      maxi = max(maxi, (long long)(*i).second.size());
      sort((*i).second.begin(), (*i).second.end(), greater<long long>());
      if ((*i).second[0] > 0) ar[0] += (*i).second[0];
      for (long long j = 1; j < (*i).second.size(); j++) {
        (*i).second[j] += (*i).second[j - 1];
        if ((*i).second[j] > 0) ar[j] += (*i).second[j];
      }
    }
    for (long long i = 0; i < n + 1; i++) ans = max(ans, ar[i]);
    cout << ans;
  }
  return 0;
}
