#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios_base::sync_with_stdio(false);
  int64_t n, m;
  cin >> n >> m;
  vector<vector<int64_t> > v(m);
  for (int64_t i = 0; i < n; ++i) {
    int64_t a, b;
    cin >> a >> b;
    v[a - 1].push_back(b);
  }
  vector<vector<int64_t> > d(n);
  for (int64_t i = 0; i < m; ++i) {
    sort(v[i].rbegin(), v[i].rend());
    int64_t p = 0;
    for (int64_t j = 0; j < v[i].size(); ++j) {
      p += v[i][j];
      if (p > 0) d[j].push_back(p);
    }
  }
  int64_t ans = 0;
  for (int64_t i = 0; i < n; ++i) {
    sort(d[i].rbegin(), d[i].rend());
    int64_t p = 0;
    for (int64_t j = 0; j < d[i].size(); ++j) {
      p += d[i][j];
    }
    ans = max(ans, p);
  }
  cout << ans;
}
