#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18, MAXN = 1e5 + 128, MOD = 1e9 + 7;
long long binpow(long long a, long long n) {
  long long res = 1;
  while (n) {
    if (n & 1) res *= a;
    a *= a;
    n >>= 1;
  }
  return res;
}
long long NOD(long long NOD1, long long NOD2) {
  while (NOD2) {
    NOD1 %= NOD2;
    swap(NOD1, NOD2);
  }
  return NOD1;
}
long long NOK(long long NOK1, long long NOK2) {
  return NOK1 * NOK2 / NOD(NOK1, NOK2);
}
long long n, m, sum = -1;
vector<long long> v[MAXN];
map<long long, long long> mp;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> m;
  for (long long x, y, i = 0; i < n; i++) {
    cin >> x >> y;
    v[x].push_back(y);
    sum = max(sum, y);
  }
  for (int i = 1; i <= m; i++) {
    sort(v[i].begin(), v[i].end());
    reverse(v[i].begin(), v[i].end());
  }
  sum = 0;
  for (int i = 1; i <= m; i++) {
    sum = 0;
    for (int j = 0; j < v[i].size(); j++) {
      if (sum + v[i][j] < 0) break;
      sum += v[i][j];
      mp[j + 1] += sum;
    }
  }
  sum = 0;
  for (auto it : mp) {
    sum = max(sum, it.second);
  }
  cout << sum;
}
