#include <bits/stdc++.h>
using namespace std;
int n, m, s, r, f[100001], ans;
vector<int> d[100001];
bool cmp(int x, int y) { return x > y; }
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%d%d", &s, &r), d[s].push_back(r);
  for (int i = 1; i <= m; i++) {
    int s = 0;
    sort(d[i].begin(), d[i].end(), cmp);
    for (int j = 0; j < d[i].size(); j++) {
      s += d[i][j];
      f[j + 1] += s > 0 ? s : 0;
    }
  }
  for (int i = 1; i <= n; i++) ans = max(ans, f[i]);
  printf("%d\n", ans);
  return 0;
}
