#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
const int maxs = 1e4 + 5;
int n, m;
vector<int> ds[maxn];
vector<int> ds2[maxn];
vector<int> ds3[maxn];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  int s, r;
  for (int i = 1; i <= n; i++) {
    cin >> s >> r;
    ds[s].push_back(r);
  }
  for (int i = 1; i <= m; i++) {
    sort(ds[i].begin(), ds[i].end());
  }
  int sum;
  for (int i = 1; i <= m; i++) {
    sum = 0;
    int sz = ds[i].size();
    for (int j = sz - 1; j >= 0; j--) {
      sum += ds[i][j];
      if (sum >= 0)
        ds2[i].push_back(sum);
      else
        break;
    }
  }
  for (int i = 1; i <= m; i++) {
    int sz = ds2[i].size();
    for (int j = 1; j <= sz; j++) {
      ds3[j].push_back(i);
    }
  }
  int ret = 0;
  for (int i = 1; i <= n; i++) {
    int ans = 0;
    int taken = ds3[i].size();
    for (int j = 0; j <= taken - 1; j++) {
      int id = ds3[i][j];
      ans += ds2[id][i - 1];
    }
    if (ret < ans) ret = ans;
  }
  cout << ret << "\n";
  return 0;
}
