#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int cnt[N];
vector<int> a[N];
vector<pair<int, int> > b[N];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    int s, r;
    scanf("%d%d", &s, &r);
    a[s].push_back(r);
  }
  int ans = 0;
  for (int i = 1; i <= m; i++) {
    sort(a[i].begin(), a[i].end());
    reverse(a[i].begin(), a[i].end());
    for (int j = 0; j < a[i].size(); j++) b[j].emplace_back(i, a[i][j]);
  }
  for (int i = 0; i < n; i++) {
    int tmp = 0;
    for (auto item : b[i]) {
      int s = item.first, r = item.second;
      cnt[s] += r;
      if (cnt[s] >= 0) tmp += cnt[s];
    }
    ans = max(tmp, ans);
  }
  printf("%d\n", ans);
  return 0;
}
