#include <bits/stdc++.h>
using namespace std;
const int maxn = 100100;
vector<int> a[maxn], s[maxn];
int b[maxn];
bool cmp(int a, int b) { return a > b; }
bool cmpb(int x, int y) { return a[x].size() > a[y].size(); }
int main(void) {
  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);
  }
  for (int i = 1; i <= m; i++) sort(a[i].begin(), a[i].end(), cmp);
  for (int i = 1; i <= m; i++) {
    int now = 0;
    for (int j = 0; j < a[i].size(); j++) {
      now += a[i][j];
      s[i].push_back(now);
    }
  }
  for (int i = 1; i <= m; i++) b[i] = i;
  sort(b + 1, b + m + 1, cmpb);
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int tmp = 0;
    for (int j = 1; j <= m; j++) {
      if (a[b[j]].size() < i) break;
      tmp += max(0, s[b[j]][i - 1]);
    }
    ans = max(ans, tmp);
  }
  printf("%d\n", ans);
  return 0;
}
