#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios_base::sync_with_stdio(false);
  long long n, m;
  cin >> n >> m;
  vector<vector<long long> > v(m);
  long long a, b;
  for (long long i = 0; i < n; i++) {
    cin >> a >> b;
    v[a - 1].push_back(b);
  }
  long long sz = 0;
  for (long long i = 0; i < m; i++) {
    sort(v[i].begin(), v[i].end(), greater<long long>());
    sz = max(sz, (long long)v[i].size());
  }
  vector<long long> mx(n + 5, 0);
  for (long long i = 0; i < m; i++) {
    long long cursum = 0;
    for (long long j = 0; j < v[i].size(); j++) {
      cursum += v[i][j];
      if (cursum < 0) {
        break;
      }
      mx[j + 1] += cursum;
    }
  }
  cout << *max_element(mx.begin(), mx.end());
}
