#include <bits/stdc++.h>
using namespace std;
int t1, t2, n, m, a1[100005], a[100005], sum[100005], ans;
struct node {
  int a, b;
} s[100005];
int gets() {
  int v = 0;
  bool f = 0;
  char ch;
  while (!isdigit(ch)) {
    f |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch)) {
    v = (v << 3) + (v << 1) + ch - 48;
    ch = getchar();
  }
  return f ? -v : v;
}
bool cmp(const node &a, const node &b) {
  return (a.b == b.b && a.a < b.a) || a.b > b.b;
}
int main() {
  n = gets(), m = gets();
  for (int i = 1; i <= n; i++) s[i].a = gets(), s[i].b = gets();
  sort(1 + s, 1 + s + n, cmp);
  for (int i = 1; i <= n; i++) {
    t1 = s[i].a, t2 = s[i].b;
    a[t1] += t2, a1[t1]++;
    if (a[t1] > 0) sum[a1[t1]] += a[t1];
  }
  for (int i = 1; i <= n; i++) ans = max(ans, sum[i]);
  printf("%d\n", ans);
  return 0;
}
