#include <bits/stdc++.h>
using namespace std;
int inf = 1000;
void did(vector<long long> &x) {
  sort(x.rbegin(), x.rend());
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  int n, m, s;
  long long r;
  cin >> n >> m;
  vector<vector<long long> > a(m);
  vector<long long> ans(n + 1);
  for (int i = 0; i < n; i++) {
    cin >> s >> r;
    a[s - 1].push_back(r);
  }
  long long res;
  for (int i = 0; i < m; i++) {
    did(a[i]);
    res = 0;
    for (int j = 0; j < a[i].size(); j++) {
      res += a[i][j];
      if (res > 0) ans[j + 1] += res;
    }
  }
  res = 0;
  for (int i = 0; i <= n; i++) {
    res = max(res, ans[i]);
  }
  cout << res;
  return 0;
}
