#include <bits/stdc++.h>
using namespace std;
vector<int> v[100005];
set<int> notend;
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    v[x].emplace_back(y);
  }
  for (int i = 1; i <= m; i++) {
    sort(v[i].begin(), v[i].end());
    reverse(v[i].begin(), v[i].end());
    for (int j = 1; j < v[i].size(); j++) {
      v[i][j] += v[i][j - 1];
    }
    notend.insert(i);
  }
  int ans = 0;
  for (int j = 0; !notend.empty(); j++) {
    int c = 0;
    queue<int> q;
    for (int i : notend) {
      if (j == v[i].size()) {
        q.push(i);
      } else {
        if (v[i][j] > 0) c += v[i][j];
      }
    }
    while (!q.empty()) {
      notend.erase(q.front());
      q.pop();
    }
    ans = max(ans, c);
  }
  printf("%d\n", ans);
}
