#include <bits/stdc++.h>
using namespace std;
int N = 998244353;
int i, j, x, y, z, m, n, k, f, ma = 0, ma2, bl, l, r, t, mi = 1e8, x2, y2, x3,
                               y3, a[200100], d[200000], p, ans;
string s, s1;
vector<int> v[101000];
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> m;
  for (i = 1; i <= n; i++) {
    cin >> x >> y;
    v[x].push_back(y);
  }
  for (i = 1; i <= m; i++) {
    sort(v[i].begin(), v[i].end());
  }
  for (i = 1; i <= m; i++) {
    x = 0;
    for (j = v[i].size() - 1; j >= 0; j--) {
      x += v[i][j];
      if (x < 0) break;
      d[v[i].size() - j] += x;
    }
  }
  for (i = 1; i <= n; i++) ma = max(ma, d[i]);
  cout << ma;
}
