#include <bits/stdc++.h>
using namespace std;
long long ans[100004] = {0};
vector<long long> v[100005];
int main() {
  long long n, m, i, j, ms = 0, sum = 0, res = 0;
  scanf("%lld %lld", &n, &m);
  for (i = 0; i < n; i++) {
    long long a, b;
    scanf("%lld %lld", &a, &b);
    v[a].push_back(b);
  }
  for (i = 1; i <= m; i++) {
    sum = 0;
    sort(v[i].begin(), v[i].end());
    long long si = v[i].size();
    ms = max(ms, si);
    for (j = si - 1; j >= 0; j--) {
      sum += v[i][j];
      if (sum > 0)
        ans[si - j] += sum;
      else
        break;
    }
  }
  for (i = 1; i <= ms; i++) res = max(res, ans[i]);
  printf("%lld\n", res);
  return 0;
}
