#include <bits/stdc++.h>
using namespace std;
using LL = long long int;
using VL = vector<LL>;
using VI = vector<int>;
using VVL = vector<VL>;
using PLL = pair<LL, LL>;
using PII = pair<int, int>;
void _print() {}
template <typename T, typename... Args>
void _print(const T first_elem, const Args... args) {}
int main() {
  LL n, m;
  cin >> n >> m;
  vector<int> totalSum(n, 0);
  vector<vector<int>> preSum(m);
  for (int i = 0; i < n; i++) {
    LL subject, skill;
    cin >> subject >> skill;
    preSum[subject - 1].push_back(skill);
  }
  for (int i = 0; i < m; i++) {
    sort(preSum[i].begin(), preSum[i].end(),
         [](const int& a, const int& b) { return a > b; });
  }
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < preSum[i].size(); j++) {
      if (j) {
        preSum[i][j] += preSum[i][j - 1];
      }
      if (preSum[i][j] > 0) {
        totalSum[j] += preSum[i][j];
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < n; i++) ans = max(ans, totalSum[i]);
  cout << ans << endl;
  return 0;
}
