#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long n, m, s, r;
  cin >> n >> m;
  vector<long long> ar[m + 2];
  for (int i = 0; i < n; i++) {
    cin >> s >> r;
    ar[s].push_back(r);
  }
  for (int i = 0; i <= m; i++) {
    sort(ar[i].begin(), ar[i].end(), greater<long long>());
  }
  map<long long, long long> ans;
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j < ar[i].size(); j++) {
      ar[i][j] += ar[i][j - 1];
    }
  }
  long long mx = 0;
  for (int i = 1; i <= m; i++) {
    for (int j = 0; j < ar[i].size(); j++) {
      if (ar[i][j] > 0) {
        ans[j] += ar[i][j];
        mx = max(mx, ans[j]);
      }
    }
  }
  cout << mx << "\n";
  return 0;
}
