#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 2;
int n, m, s[N], r[N], cnt[N];
vector<pair<int, int> > v;
vector<int> g[N], sum[N];
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d %d", s + i, r + i);
    cnt[s[i]]++;
    g[s[i]].push_back(r[i]);
  }
  for (int i = 1; i <= m; i++) {
    if (cnt[i]) {
      v.push_back({cnt[i], i});
    }
    sort(g[i].rbegin(), g[i].rend());
    for (int j = 0; j < g[i].size(); j++) {
      sum[i].push_back(0);
      sum[i].back() += g[i][j];
      if (j > 0) sum[i].back() += sum[i][j - 1];
    }
  }
  sort(v.rbegin(), v.rend());
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int res = 0;
    for (int j = 0; j < v.size(); j++) {
      if (v[j].first < i) break;
      int id = v[j].second;
      res += max(0, sum[id][i - 1]);
    }
    ans = max(ans, res);
  }
  cout << ans << endl;
}
