#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
const int maxm = 2000005;
const int inf = 0x3f3f3f3f;
const int mod = 1000000000 + 7;
void init() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
};
int n, m;
vector<int> v[100005];
vector<int> vv[100005];
int sum[100005];
int to[100005];
int cnt = 0;
bool cmp(int x, int y) { return x > y; }
int main() {
  init();
  cin >> n >> m;
  int mx = 0;
  int top = 0;
  for (int i = 1; i <= n; i++) {
    int id, score;
    cin >> id >> score;
    if (sum[id] == 0) to[++cnt] = id;
    v[id].push_back(score);
    sum[id]++;
    mx = max(mx, sum[id]);
  }
  for (int kk = 1; kk <= cnt; kk++) {
    int i = to[kk];
    sort(v[i].begin(), v[i].end(), cmp);
    if (v[i].size()) vv[i].push_back(v[i][0]);
    for (int j = 1; j < v[i].size(); j++) {
      int k = vv[i][j - 1];
      if (k <= 0) break;
      vv[i].push_back(k + v[i][j]);
    }
  }
  int ans = 0;
  for (int i = 1; i <= mx; i++) {
    int tmp = 0;
    for (int k = 1; k <= cnt; k++) {
      int j = to[k];
      if (sum[j] < i) continue;
      if (i > vv[j].size()) continue;
      if (vv[j][i - 1] < 0) continue;
      tmp += vv[j][i - 1];
    }
    ans = max(ans, tmp);
  }
  cout << ans << endl;
}
