#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;
    for (long long i = 0; i < n; i++) {
      long long s, r;
      cin >> s >> r;
      v[s].emplace_back(r);
    }
    for (long long i = 1; i < nax; i++) {
      if (v[i].size()) sort(v[i].rbegin(), v[i].rend());
    }
    long long presum[nax];
    memset(presum, 0, sizeof(presum));
    for (long long i = 1; i < nax; i++) {
      long long cur = 0;
      for (long long j = 0; j < v[i].size(); j++) {
        cur += v[i][j];
        if (cur < 0) break;
        presum[j + 1] += cur;
      }
    }
    long long maxi = 0;
    for (long long i = 1; i < nax; i++) {
      maxi = max(maxi, presum[i]);
    }
    cout << maxi;
  }
  return 0;
}
