#include <bits/stdc++.h>
using namespace std;
vector<int> v[100005];
int ans[100005];
int main() {
  int n, k;
  cin >> n >> k;
  int s, r;
  for (int i = 0; i < n; i++) {
    cin >> s >> r;
    v[s].push_back(r);
  }
  for (int i = 1; i <= k; i++) {
    sort(v[i].begin(), v[i].end(), greater<int>());
  }
  memset(ans, 0, sizeof(ans));
  int cur = 0, sz = 0;
  for (int i = 1; i <= k; i++) {
    cur = 0;
    sz = max(sz, (int)v[i].size());
    for (int j = 0; j < v[i].size(); j++) {
      cur += v[i][j];
      if (cur < 0) break;
      ans[j] += cur;
    }
  }
  cout << *max_element(ans, ans + sz) << endl;
  ;
  return 0;
}
