#include <bits/stdc++.h>
using namespace std;
vector<long long> v[100003];
long long cnt[100003];
int main() {
  long long n, m, s, r;
  scanf("%lld %lld", &n, &m);
  for (long long i = 1; i <= n; i++) {
    scanf("%lld %lld", &s, &r);
    v[s].push_back(r);
  }
  for (long long i = 1; i <= m; i++) {
    if (v[i].size() > 0) sort(v[i].begin(), v[i].end(), greater<long long>());
  }
  for (long long i = 1; i <= m; i++) {
    if (v[i].size() == 0) continue;
    long long sum = 0;
    for (long long j = 0; j < v[i].size(); j++) {
      sum += v[i][j];
      if (sum > 0) cnt[j + 1] += sum;
    }
  }
  long long ans = 0;
  for (long long i = 1; i <= n; i++) {
    ans = max(ans, cnt[i]);
  }
  cout << ans << endl;
  return 0;
}
