#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, r, s, range = 0;
  scanf("%d%d", &n, &m);
  vector<int> sub[m + 1];
  set<int> st;
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &s, &r);
    sub[s].push_back(r);
    st.insert(s);
  }
  for (auto it : st) {
    int sz = sub[it].size();
    range = max(range, sz);
    sort(sub[it].begin(), sub[it].end(), greater<int>());
    for (int i = 1; i < sz; i++) {
      sub[it][i] += sub[it][i - 1];
    }
  }
  int ans = 0;
  for (int i = 1; i <= range; i++) {
    set<int> t = st;
    int sum = 0;
    for (auto it : t) {
      if ((int)sub[it].size() < i) {
        st.erase(it);
      } else if (sub[it][i - 1] < 0) {
        st.erase(it);
      } else
        sum += sub[it][i - 1];
    }
    ans = max(ans, sum);
  }
  printf("%d\n", ans);
}
