#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 1e5 + 5;
vector<int> a[maxn];
int res[maxn];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    int q, w;
    scanf("%d%d", &q, &w);
    a[q].push_back(w);
  }
  int ms = 0;
  for (int i = 1; i <= m; i++) {
    int sump = a[i].size();
    sort(a[i].begin(), a[i].end());
    ms = max(sump, ms);
    int sum = 0;
    for (int j = sump - 1; j >= 0; j--) {
      sum += a[i][j];
      if (sum >= 0)
        res[sump - j] += sum;
      else
        break;
    }
  }
  int max1 = 0;
  for (int i = 1; i <= ms; i++) max1 = max(res[i], max1);
  printf("%d\n", max1);
}
