#include <bits/stdc++.h>
using namespace std;
vector<int> people[100000 + 6];
int n, m;
int object, grade;
int maxn;
int sum;
int length;
int ans[100000 + 6];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &object, &grade);
    people[object].push_back(grade);
    if (grade > maxn) {
      maxn = grade;
    }
  }
  if (maxn <= 0) {
    printf("0\n");
  } else {
    for (int i = 1; i <= m; i++) {
      length = people[i].size();
      int sum1 = 0;
      sort(people[i].begin(), people[i].end());
      reverse(people[i].begin(), people[i].end());
      for (int j = 0; j < length; j++) {
        sum1 += people[i][j];
        if (sum1 > 0) {
          ans[j + 1] += sum1;
        }
      }
    }
    for (int i = 1; i <= n; i++) {
      sum = max(sum, ans[i]);
    }
    cout << sum << endl;
  }
  return 0;
}
