#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
const long long mod = 1e9 + 7;
vector<int> co[maxn];
long long dqs[maxn];
bool cmp(int a, int b) { return a > b; }
int main() {
  int n, m, a, b;
  long long ans = 0;
  cin >> n >> m;
  memset(dqs, 0, sizeof dqs);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &a, &b);
    co[a].push_back(b);
  }
  for (int i = 1; i <= m; i++) {
    sort(co[i].begin(), co[i].end(), cmp);
    int len = co[i].size(), sum = 0;
    for (int j = 0; j < len; j++) {
      if (sum + co[i][j] < 0) break;
      dqs[j + 1] += co[i][j] + sum;
      sum += co[i][j];
    }
  }
  for (int i = 1; i <= n; i++) {
    ans = max(ans, dqs[i]);
  }
  cout << ans << endl;
}
