#include <bits/stdc++.h>
using namespace std;
const int inf = (int)1e9;
const int mod = inf + 7;
int n, m;
long long second[300300], r[300300], tk[300300], mk[300300];
vector<long long> g[300300], L[300300];
pair<long long, long long> a[300300];
int main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    cin >> second[i] >> r[i];
    g[second[i]].push_back(r[i]);
  }
  for (int i = 1; i <= m; ++i) {
    sort((g[i]).begin(), (g[i]).end());
    reverse((g[i]).begin(), (g[i]).end());
  }
  for (int i = 1; i <= m; ++i) {
    long long curs = 0;
    for (int j = 0; j < (int)g[i].size(); ++j) {
      curs += g[i][j];
      L[i].push_back(curs);
    }
    a[i] = make_pair(1ll * (int)g[i].size(), i);
  }
  sort(a + 1, a + m + 1);
  long long mx = 0ll;
  for (int len = 1; len <= n; ++len) {
    long long cs = 0;
    for (int j = m; j >= 1; --j) {
      if (a[j].first < len) break;
      if (L[a[j].second][len - 1] > 0) cs += L[a[j].second][len - 1];
    }
    mx = max(mx, cs);
  }
  cout << mx;
  return 0;
}
