#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;
const int INF = 0x3f3f3f3f;
const int mod = 998244353;
int n, m;
struct Node {
  int idx;
  vector<int> v;
  bool friend operator<(Node c, Node d) {
    if (c.v.size() != d.v.size()) return !(c.v.size() < d.v.size());
    return c.idx < d.idx;
  }
} p[N + 10];
bool cmp(int x, int y) { return x > y; }
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    int s, w;
    cin >> s >> w;
    p[s].v.push_back(w);
  }
  sort(p + 1, p + 1 + m);
  for (int i = 1; i <= n; i++) {
    sort(p[i].v.begin(), p[i].v.end(), cmp);
    for (int j = 1, sz = (int)p[i].v.size(); j < sz; j++)
      p[i].v[j] += p[i].v[j - 1];
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int c = 0;
    for (int j = 1; j <= m; j++)
      if (p[j].v.size() < i)
        break;
      else
        c = max(c, c + p[j].v[i - 1]);
    ans = max(ans, c);
  }
  cout << ans;
  return 0;
}
