#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, m, sum, out = 0, a, b;
  vector<int> vec[100001];
  vector<int> vec2[100001];
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a >> b;
    vec[a].push_back(b);
  }
  for (int i = 1; i <= m; i++) {
    sort(vec[i].begin(), vec[i].end());
    sum = 0;
    for (int j = int(vec[i].size()) - 1; j >= 0; j--) {
      sum += vec[i][j];
      if (sum < 0) break;
      vec2[int(vec[i].size()) - j].push_back(sum);
    }
  }
  for (int i = 1; i <= n; i++) {
    sum = 0;
    for (int j = 0; j < vec2[i].size(); j++) sum += vec2[i][j];
    out = max(out, sum);
  }
  cout << out;
}
