#include <bits/stdc++.h>
using namespace std;
int s[100005], r[100005];
vector<int> v[100005];
vector<int> ans[100005];
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) cin >> s[i] >> r[i];
  for (int i = 1; i <= n; i++) v[s[i]].push_back(r[i]);
  for (int i = 1; i <= 100000; i++) {
    if (v[i].size() > 0) sort((v[i]).begin(), (v[i]).end());
  }
  int cnt, sum, res = 0;
  for (int i = 1; i <= 100000; i++) {
    cnt = 1;
    sum = 0;
    for (int j = v[i].size() - 1; j >= 0; j--) {
      sum += v[i][j];
      ans[cnt].push_back(sum);
      cnt++;
    }
  }
  for (int i = 1; i <= 100000; i++) {
    if (ans[i].size() > 0) sort((ans[i]).begin(), (ans[i]).end());
  }
  for (int i = 1; i <= 100000; i++) {
    sum = 0;
    for (int j = ans[i].size() - 1; j >= 0; j--) {
      if (ans[i][j] < 0) break;
      sum += ans[i][j];
    }
    res = max(res, sum);
  }
  cout << res;
}
