#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 69;
int n, m, cnt;
long long f[N], nex[N], res;
vector<long long> a[N];
bool cmp(int a, int b) { return a > b; }
void init() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    int s, r;
    cin >> s >> r;
    if (!nex[s]) nex[s] = ++cnt;
    a[nex[s]].push_back(r);
  }
  for (int i = 1; i <= cnt; i++) sort(a[i].begin(), a[i].end(), cmp);
}
long long val(long long n) { return max(0LL, n); }
void calc() {
  for (int i = 1; i <= cnt; i++) {
    f[0] += val(a[i][0]);
    for (int j = 1; j < a[i].size(); j++) {
      a[i][j] += a[i][j - 1];
      f[j] += val(a[i][j]);
    }
  }
  for (int i = 0; i <= n; i++) res = max(res, f[i]);
  cout << res;
}
int main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  init();
  calc();
}
