#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
vector<int> v[N];
long long sum[N];
int n, m;
bool cmp(int x, int y) { return x > y; }
int main() {
  int id, val;
  while (~scanf("%d%d", &n, &m)) {
    for (int i = 1; i <= n; i++) {
      scanf("%d%d", &id, &val);
      v[id].push_back(val);
      sum[i] = 0;
    }
    long long ans = 0, cnt;
    for (int i = 1; i <= m; i++) {
      sort(v[i].begin(), v[i].end(), cmp);
      cnt = 0;
      for (int j = 0; j < v[i].size(); j++) {
        cnt += v[i][j];
        if (cnt > 0) {
          sum[j] += cnt;
          ans = max(ans, sum[j]);
        } else
          break;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
