#include <bits/stdc++.h>
using namespace std;
const int maxn = 101000;
int n, m;
vector<int> G[maxn];
vector<int> he[maxn];
int si[maxn];
bool cmp(int a, int b) { return a > b; }
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    G[u].push_back(v);
  }
  for (int i = 1; i <= m; i++) {
    sort(G[i].begin(), G[i].end(), cmp);
  }
  int s = 0;
  for (int i = 1; i <= m; i++) {
    if (G[i].size() == 0) continue;
    if (G[i].size() > s) s = G[i].size();
    for (int j = 0; j < G[i].size(); j++) {
      if (j != 0)
        he[i].push_back(he[i][j - 1] + G[i][j]);
      else
        he[i].push_back(G[i][j]);
    }
  }
  int ans = 0;
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= s; j++) {
      if (G[i].size() < j || he[i][j - 1] < 0) break;
      si[j] += he[i][j - 1];
    }
  }
  for (int j = 1; j <= s; j++) ans = max(si[j], ans);
  printf("%d\n", ans);
  return 0;
}
