#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 5;
int n, m;
multiset<int> subject[MAX];
vector<vector<int>> sumSubject;
int sum[MAX];
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  int s, r;
  for (int i = 0; i < n; ++i) {
    cin >> s >> r;
    subject[s - 1].insert(r);
  }
  sumSubject.resize(m);
  for (int i = 0; i < m; ++i) {
    sumSubject[i].push_back(0);
    for (auto rit = subject[i].rbegin(); rit != subject[i].rend(); ++rit)
      sumSubject[i].push_back(sumSubject[i].back() + *rit);
  }
  for (int i = 0; i < m; ++i)
    for (int j = 1; j < (int)sumSubject[i].size(); ++j)
      if (sumSubject[i][j] > 0) sum[j] += sumSubject[i][j];
  int ans = 0;
  for (int i = 1; i <= n; ++i) ans = max(ans, sum[i]);
  cout << ans << '\n';
  return 0;
}
