#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
vector<long long> v[100005];
long long n, ans[100005];
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  long long m;
  cin >> n >> m;
  long long x, y;
  for (long long i = 1; i <= n; i++) {
    cin >> x >> y;
    v[x].push_back(y);
  }
  long long sum = 0;
  for (long long i = 1; i <= m; i++) {
    sort(v[i].begin(), v[i].end(), greater<long long>());
    sum = 0;
    for (long long j = 0; j < v[i].size(); j++) {
      sum += v[i][j];
      if (sum > 0)
        ans[j + 1] += sum;
      else
        break;
    }
  }
  long long maxi = 0;
  for (long long i = 1; i <= n; i++) {
    maxi = max(maxi, ans[i]);
  }
  cout << maxi;
  return 0;
}
