#include <bits/stdc++.h>
const int maxn = 1e5 + 10;
const long long inf = 1e18;
const long long mod = 998244353;
using namespace std;
int cnt[maxn];
int pos[maxn];
long long sum[maxn];
vector<int> nu[maxn];
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  int u, v;
  for (int i = 1; i <= n; ++i) {
    scanf("%d %d", &u, &v);
    nu[u].push_back(v);
  }
  int t = 0;
  for (int i = 1; i <= m; ++i) {
    sort(nu[i].rbegin(), nu[i].rend());
    t = max(t, int(nu[i].size()));
  }
  long long maxx = 0;
  for (int i = 1; i <= m; ++i) {
    for (int j = 0; j < nu[i].size(); ++j) {
      if (j != 0) nu[i][j] += nu[i][j - 1];
    }
  }
  for (int i = 1; i <= m; ++i) {
    for (int j = 0; j < nu[i].size(); ++j) {
      if (nu[i][j] > 0) {
        sum[j] += nu[i][j];
      }
    }
  }
  for (int j = 0; j <= t; ++j) {
    maxx = max(maxx, sum[j]);
  }
  printf("%lld\n", maxx);
  return 0;
}
