#include <bits/stdc++.h>
using namespace std;
const long long maxn = 100005;
struct lol {
  long long ix, s;
  lol(long long x, long long y) { ix = x, s = y; }
};
bool cmp(lol x, lol y) { return x.s > y.s; }
long long last[maxn];
vector<lol> ms;
vector<long long> subs[maxn];
int main() {
  long long n, m, s, r, mx = 0;
  scanf("%lld %lld", &n, &m);
  for (long long i = 0; i < n; i++) {
    scanf("%lld %lld", &s, &r);
    subs[s].push_back(r);
  }
  for (long long i = 1; i <= m; i++) {
    sort(subs[i].begin(), subs[i].end(), greater<long long>());
    mx = max((long long)subs[i].size(), mx);
    if (!subs[i].empty()) {
      ms.push_back(lol(i, subs[i].size()));
    }
  }
  sort(ms.begin(), ms.end(), cmp);
  long long ans = 0;
  for (long long i = 0; i < mx; i++) {
    long long sum = 0;
    for (auto j : ms) {
      if (i < j.s) {
        last[j.ix] += subs[j.ix][i];
        if (last[j.ix] > 0) sum += last[j.ix];
      } else {
        break;
      }
    }
    ans = max(ans, sum);
  }
  printf("%lld\n", (long long)max(0LL, ans));
  return 0;
}
