#include <bits/stdc++.h>
using namespace std;
vector<long long> v[100005];
vector<long long> vs[100005];
bool comp(vector<long long> a, vector<long long> b) {
  return a.size() > b.size();
}
bool comp2(long long a, long long b) { return a > b; }
int main() {
  ios::sync_with_stdio(false);
  int n, m;
  long long s, r, ans = 0;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> s >> r;
    v[s].push_back(r);
  }
  sort(v, v + m + 2, comp);
  for (int i = 0; v[i].size() > 0; i++) {
    sort(v[i].begin(), v[i].end(), comp2);
    s = 0;
    for (int j = 0; j < v[i].size(); j++) {
      s += v[i][j];
      vs[i].push_back(s);
    }
  }
  for (int i = 0; i < v[0].size(); i++) {
    s = 0;
    for (int j = 0; v[j].size() > i; j++) {
      if (vs[j][i] > 0) s += vs[j][i];
    }
    ans = max(ans, s);
  }
  cout << ans;
  return 0;
}
