#include <bits/stdc++.h>
using namespace std;
struct node {
  int num;
  int v;
};
bool cmp1(node a, node b) { return a.v > b.v; }
node a[100005];
vector<int> ve[100005];
int main() {
  int n, m;
  while (cin >> n >> m) {
    for (int i = 0; i < 100005; i++) {
      ve[0].push_back(0);
      ve[i].push_back(0);
    }
    for (int i = 0; i < n; i++) {
      scanf("%d%d", &a[i].num, &a[i].v);
    }
    sort(a, a + n, cmp1);
    for (int i = 0; i < n; i++) {
      int x = ve[a[i].num].back();
      ve[a[i].num].push_back(x + a[i].v);
      ve[0][ve[a[i].num].size()] =
          max(ve[a[i].num].back() + ve[0][ve[a[i].num].size()],
              ve[0][ve[a[i].num].size()]);
    }
    int mx = 0;
    for (int i = 0; i <= ve[0].size(); i++) {
      mx = max(mx, ve[0][i]);
    }
    cout << mx << endl;
  }
}
