#include <bits/stdc++.h>
using namespace std;
const long long LLMAX = 2e18;
const int MOD = 1e9 + 7;
const int MAXN = 1e5 + 10;
template <class T>
inline void read(T &DataIn) {
  DataIn = 0;
  T Flag = 0;
  char c = getchar();
  while (!isdigit(c)) {
    Flag |= c == '-';
    c = getchar();
  }
  while (isdigit(c)) {
    DataIn = DataIn * 10 + c - '0';
    c = getchar();
  }
  DataIn = Flag ? -DataIn : DataIn;
}
template <class T>
inline void write(T DataOut, char EndChar = '\n') {
  T lenth = 0, number[30];
  if (DataOut == 0) {
    putchar(48);
    putchar(EndChar);
    return;
  }
  while (DataOut > 0) {
    number[++lenth] = DataOut % 10;
    DataOut /= 10;
  }
  for (int i = lenth; i >= 1; i--) putchar(number[i] + 48);
  putchar(EndChar);
}
long long __gcd(long long a, long long b) {
  return b == 0 ? a : __gcd(b, a % b);
}
priority_queue<int, vector<int>, greater<int> > qu;
priority_queue<long long, vector<long long>, less<long long> > q1[MAXN],
    q2[MAXN];
int main(void) {
  FILE *fin = NULL, *fout = NULL;
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long n, m, ans = 0;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    long long s, r;
    cin >> s >> r;
    q1[s].push(r);
  }
  for (int i = 1; i <= m; i++) {
    long long sum = 0, k = 1;
    while (!q1[i].empty()) {
      sum += q1[i].top();
      q1[i].pop();
      q2[k++].push(sum);
    }
  }
  for (int i = 1; i <= n; i++) {
    long long sum = 0;
    while (!q2[i].empty()) {
      if (q2[i].top() > 0)
        sum += q2[i].top(), q2[i].pop();
      else
        break;
    }
    ans = max(ans, sum);
  }
  cout << ans << endl;
  return 0;
}
