#include <bits/stdc++.h>
using namespace std;
vector<int> abil[100100];
int coun[100100];
vector<pair<int, int> > st;
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++) {
    int s, r;
    scanf("%d %d", &s, &r);
    s--;
    abil[s].push_back(r);
    coun[s]++;
  }
  for (int i = 0; i < m; i++) {
    sort(abil[i].begin(), abil[i].end(), greater<int>());
    int sum = 0;
    for (int j = 0; j < (int)abil[i].size(); j++) {
      abil[i][j] = abil[i][j] + sum;
      sum = abil[i][j];
    }
    st.push_back({coun[i], i});
  }
  sort(st.begin(), st.end(), greater<pair<int, int> >());
  long long maxV = 0;
  for (int i = 1; i <= n; i++) {
    long long val = 0;
    for (int j = 0; j < m; j++) {
      int ch = st[j].second;
      if (abil[ch].size() < i)
        break;
      else if (abil[ch][i - 1] > 0)
        val += abil[ch][i - 1];
    }
    maxV = max(maxV, val);
  }
  printf("%lld", maxV);
}
