#include <bits/stdc++.h>
using namespace std;
inline void read(long long &x) {
  char ch;
  bool flag = false;
  for (ch = getchar(); !isdigit(ch); ch = getchar())
    if (ch == '-') flag = true;
  for (x = 0; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
    ;
  x = flag ? -x : x;
}
inline void write(long long x) {
  static const long long maxlen = 100;
  static char s[maxlen];
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (!x) {
    putchar('0');
    return;
  }
  long long len = 0;
  for (; x; x /= 10) s[len++] = x % 10 + '0';
  for (long long i = len - 1; i >= 0; --i) putchar(s[i]);
}
const long long MAXN = 500050;
long long n, m;
vector<long long> sum[MAXN];
long long num[MAXN];
bool cmp(long long a, long long b) { return sum[a].size() < sum[b].size(); }
int main() {
  read(n);
  read(m);
  for (long long i = 1; i <= n; i++) {
    long long a, b;
    read(a);
    read(b);
    sum[a].push_back(b);
  }
  for (long long i = 1; i <= m; i++) {
    sort(sum[i].begin(), sum[i].end());
    reverse(sum[i].begin(), sum[i].end());
    for (long long j = 1; j < sum[i].size(); j++)
      sum[i][j] = sum[i][j] + sum[i][j - 1];
    num[i] = i;
  }
  sort(num + 1, num + m + 1, cmp);
  long long ans = 0;
  long long head = 1;
  long long now = 0;
  for (long long i = 1; i <= n; i++) {
    while ((head <= m) && (sum[num[head]].size() < i)) head++;
    now = 0;
    for (long long j = head; j <= m; j++)
      if (sum[num[j]][i - 1] >= 0) now += sum[num[j]][i - 1];
    ans = max(ans, now);
  }
  write(ans);
  puts("");
  return 0;
}
