#include <bits/stdc++.h>
using namespace std;
long long xx[100010];
int main() {
  long long n, m;
  scanf("%lld %lld", &n, &m);
  vector<vector<long long>> vt;
  vt.resize(m + 1);
  for (int i = 0; i < n; i++) {
    long long q, w;
    scanf("%lld %lld", &q, &w);
    vt[q].push_back(w);
  }
  for (int i = 1; i <= m; i++) sort(vt[i].begin(), vt[i].end());
  for (int i = 1; i <= m; i++) {
    long long cnt = 1;
    long long x = 0;
    for (int y = vt[i].size() - 1; y >= 0; y--) {
      x += vt[i][y];
      if (x > 0) xx[cnt] += x;
      cnt++;
    }
  }
  long long dap = 0;
  for (int i = 1; i <= n; i++) {
    dap = max(xx[i], dap);
  }
  printf("%lld", dap);
}
