#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ii pair<ll, ll>
#define x first
#define y second

using namespace __gnu_pbds;
using namespace std;

typedef tree<ii,null_type,less<ii>,rb_tree_tag,tree_order_statistics_node_update> set_t;

///////////////////

const int N = 100*1000 + 10;
vector<ll> sub[N];

///////////////////

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ///////////////////

    int n, m;
    cin>>n>>m;
    for(int i = 0; i<n; i++)
    {
        ll s, r;
        cin>>s>>r;
        sub[s].push_back(r);
    }

    ll sz = 0;

    set<pair<vector<ll>, int> > subs;
    for(int i = 1; i<=m; i++)
    {
        sz = max(sz, (ll)sub[i].size());
        sort(sub[i].begin(), sub[i].end(), greater<ll>());
        if(sub[i].size() > 0)
            subs.insert({sub[i], i});
    }

    ll ans = 0;
    ll sums[m+1];
    memset(sums, 0, sizeof(sums));

    for(int siz = 1; siz<=sz; siz++)
    {
        ll sum = 0;
        set<pair<vector<ll>, int> > temp;
        for(auto to : subs)
        {
            auto it = to.first;
            if(it.size() < siz)
            {
                continue;
            }
            temp.insert(to);
            sums[to.second] += it[siz-1];
            if(sums[to.second] > 0)
            {
                sum += sums[to.second];
            }

        }
        if(subs.size() != temp.size())
            subs = temp;
        ans = max(sum, ans);

    }

    cout<<ans;

    ///////////////////

    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ii pair<ll, ll>
#define x first
#define y second

using namespace __gnu_pbds;
using namespace std;

typedef tree<ii,null_type,less<ii>,rb_tree_tag,tree_order_statistics_node_update> set_t;

///////////////////

const int N = 100*1000 + 10;
vector<ll> sub[N];

///////////////////

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ///////////////////

    int n, m;
    cin>>n>>m;
    for(int i = 0; i<n; i++)
    {
        ll s, r;
        cin>>s>>r;
        sub[s].push_back(r);
    }

    ll sz = 0;
    for(int i = 1; i<=m; i++)
    {
        sz = max(sz, (ll)sub[i].size());
        sort(sub[i].begin(), sub[i].end(), greater<ll>());
    }

    vector<ll> ans(sz+1, 0);

    for(int i = 1; i<=sz; i++)
    {
        ll curSum = 0;
        for(int j = 0; j<(int)sub[i].size(); j++)
        {
            curSum += sub[i][j];
            if(curSum > 0)
            {
                ans[j+1] += curSum;
            }
        }
    }

    ll mx = ans[1];
    for(int i = 1; i<=sz; i++)
    {
        mx = max(mx, ans[i]);
    }

    cout<<mx;

    ///////////////////

    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ii pair<ll, ll>
#define x first
#define y second

using namespace __gnu_pbds;
using namespace std;

typedef tree<ii,null_type,less<ii>,rb_tree_tag,tree_order_statistics_node_update> set_t;

///////////////////

const int N = 100*1000 + 10;
vector<ll> sub[N];

///////////////////

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ///////////////////

    int n, m;
    cin>>n>>m;
    for(int i = 0; i<n; i++)
    {
        ll s, r;
        cin>>s>>r;
        sub[s].push_back(r);
    }

    ll sz = 0;
    for(int i = 1; i<=m; i++)
    {
        sz = max(sz, (ll)sub[i].size());
        sort(sub[i].begin(), sub[i].end(), greater<ll>());
    }

    vector<ll> ans(sz+1, 0);

    for(int i = 1; i<=sz; i++)
    {
        ll curSum = 0;
        for(int j = 0; j<(int)sub[i].size(); j++)
        {
            curSum += sub[i][j];
            if(curSum > 0)
            {
                ans[j+1] += curSum;
            }
        }
    }

    ll mx = *max_element(ans.begin(), ans.end());

    cout<<mx;

    ///////////////////

    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ii pair<ll, ll>
#define x first
#define y second

using namespace __gnu_pbds;
using namespace std;

typedef tree<ii,null_type,less<ii>,rb_tree_tag,tree_order_statistics_node_update> set_t;

///////////////////

const int N = 100*1000 + 10;
vector<ll> sub[N];

///////////////////

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ///////////////////

    int n, m;
    cin>>n>>m;
    for(int i = 0; i<n; i++)
    {
        ll s, r;
        cin>>s>>r;
        sub[s].push_back(r);
    }

    ll sz = 0;

    set<pair<vector<ll>, int> > subs;
    for(int i = 1; i<=m; i++)
    {
        sz = max(sz, (ll)sub[i].size());
        sort(sub[i].begin(), sub[i].end(), greater<ll>());
        if(sub[i].size() > 0)
            subs.insert({sub[i], i});
    }

    ll ans = 0;
    ll sums[m+1];
    memset(sums, 0, sizeof(sums));

    for(int siz = 1; siz<=sz; siz++)
    {
        ll sum = 0;
        set<pair<vector<ll>, int> > temp;
        for(auto to : subs)
        {
            auto it = to.first;
            if(it.size() < siz)
            {
                continue;
            }
            temp.insert(to);
            sums[to.second] += it[siz-1];
            sum += sums[to.second];
        }
        subs = temp;
        ans = max(sum, ans);

    }

    cout<<ans;

    ///////////////////

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define fastio ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define t_times int t; cin >> t; while(t--)
#define parr(a, n) for(int __i = 0; __i < (int )n; __i++) cout << a[__i] << ' '; cout << '\n'
#define fr(i, p, n) for(int i = (int )p; i < (int )n; i++)
#define frn(i, p, n) for(int i = (int )p; i <= (int )n; i++)
#define rev(i, n, p) for(int i = (int )n; i >= (int )p; i--)
#define all(c) (c).begin(), (c).end()
#define sci(n) scanf("%d", &n)
#define scl(n) scanf("%lld", &n)
#define prin(n) printf("%d\n", n)
#define prln(n) printf("%lld\n", n)
#define mp make_pair
#define F first
#define S second
#define pb push_back
#define bp __builtin_popcount
typedef long long int LL;
typedef unsigned long long ULL;
typedef unsigned int uint;
typedef pair< int, int> ii;
typedef vector< int> vi;

const int mod = 1e9 + 7;
const LL INF = 9e18 + 2e17;
const int inf = 2e9;
const int N = 1e5 + 22;
const double eps = 1e-10;

bool cmp(ii A, ii B) {
    if(A.F != B.F) return A.F < B.F;
    return A.S > B.S;
}
void _solve() {
    int n, m;
    cin >> n >> m;
    ii sr[n];
    fr(i, 0, n) cin >> sr[i].F >> sr[i].S;
    sort(sr, sr + n, cmp);
    int c = 0;
    for(int i = 0; i < n; ) {
        int x = sr[i].F;
        while(i < n && x == sr[i].F) i++;
        c++;
    }
    vector< vi> row_sum(c);
    vector< int> tot_sum;
    int p = 0;
    for(int i = 0; i < n; ) {
        int x = sr[i].F, sum = 0;
        while(i < n && x == sr[i].F) {
            sum += sr[i].S;
            i++;
            if(sum <= 0) break;
            row_sum[p].pb(sum);
        }
        if(!row_sum[p].empty())
            p++;
    }
    //fr(i, 0, p) { parr(row_sum[i], row_sum[i].size());}

    ii row_sz[p];
    fr(i, 0, p) row_sz[i] = {row_sum[i].size(), i};
    sort(row_sz, row_sz + p);
    for(int y = 0, i = 0; y < p; ) {
        int x = row_sz[y].F;
        while(i < x) {
            int sum = 0;
            fr(j, y, p) sum += row_sum[row_sz[j].S][i];
            tot_sum.pb(sum);
            i++;
        }
        while(x == row_sz[y].F) y++;
    }
    //parr(tot_sum, tot_sum.size());
    int ans = 0;
    fr(i, 0, tot_sum.size()) ans = max(ans, tot_sum[i]);
    cout << ans;
}
int main() {
	fastio;

	//t_times
	_solve();
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define fastio ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define t_times int t; cin >> t; while(t--)
#define parr(a, n) for(int __i = 0; __i < (int )n; __i++) cout << a[__i] << ' '; cout << '\n'
#define fr(i, p, n) for(int i = (int )p; i < (int )n; i++)
#define frn(i, p, n) for(int i = (int )p; i <= (int )n; i++)
#define rev(i, n, p) for(int i = (int )n; i >= (int )p; i--)
#define all(c) (c).begin(), (c).end()
#define sci(n) scanf("%d", &n)
#define scl(n) scanf("%lld", &n)
#define prin(n) printf("%d\n", n)
#define prln(n) printf("%lld\n", n)
#define mp make_pair
#define F first
#define S second
#define pb push_back
#define bp __builtin_popcount
typedef long long int LL;
typedef unsigned long long ULL;
typedef unsigned int uint;
typedef pair< int, int> ii;
typedef vector< int> vi;

const int mod = 1e9 + 7;
const LL INF = 9e18 + 2e17;
const int inf = 2e9;
const int N = 2e3 + 22;
const double eps = 1e-10;

bool cmp(ii A, ii B) {
    if(A.F != B.F) return A.F < B.F;
    return A.S > B.S;
}
void _solve() {
    int n, m;
    cin >> n >> m;
    ii sr[n];
    fr(i, 0, n) cin >> sr[i].F >> sr[i].S;
    sort(sr, sr + n, cmp);
    int c = 0, d = 0;
    for(int i = 0; i < n; ) {
        int x = sr[i].F;
        while(i < n && x == sr[i].F) i++;
        c++;
    }
    vector< vi> row_sum(c);
    vector< int> tot_sum;
    int p = 0;
    for(int i = 0; i < n; ) {
        int x = sr[i].F, sum = 0;
        while(i < n && x == sr[i].F) {
            sum += sr[i++].S;
            if(sum <= 0) break;
            row_sum[p].pb(sum);
        }
        d = max(d, (int )row_sum[p].size());
        if(!row_sum[p].empty())
            p++;
    }
    //fr(i, 0, p) { parr(row_sum[i], row_sum[i].size());}
    fr(i, 0, d) {
        int sum = 0;
        fr(j, 0, p) {
            if(i < row_sum[j].size())
                sum += row_sum[j][i];
        }
        tot_sum.pb(sum);
    }
    //parr(tot_sum, d);
    int ans = 0;
    fr(i, 0, d) ans = max(ans, tot_sum[i]);
    cout << ans;
}
int main() {
	fastio;
	//t_times
	_solve();
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define fastio ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define t_times int t; cin >> t; while(t--)
#define parr(a, n) for(int __i = 0; __i < (int )n; __i++) cout << a[__i] << ' '; cout << '\n'
#define fr(i, p, n) for(int i = (int )p; i < (int )n; i++)
#define frn(i, p, n) for(int i = (int )p; i <= (int )n; i++)
#define rev(i, n, p) for(int i = (int )n; i >= (int )p; i--)
#define all(c) (c).begin(), (c).end()
#define sci(n) scanf("%d", &n)
#define scl(n) scanf("%lld", &n)
#define prin(n) printf("%d\n", n)
#define prln(n) printf("%lld\n", n)
#define mp make_pair
#define F first
#define S second
#define pb push_back
#define bp __builtin_popcount
typedef long long int LL;
typedef unsigned long long ULL;
typedef unsigned int uint;
typedef pair< int, int> ii;
typedef vector< int> vi;

const int mod = 1e9 + 7;
const LL INF = 9e18 + 2e17;
const int inf = 2e9;
const int N = 1e5 + 22;
const double eps = 1e-10;

bool cmp(ii A, ii B) {
    if(A.F != B.F) return A.F < B.F;
    return A.S > B.S;
}
void _solve() {
    int n, m;
    cin >> n >> m;
    ii sr[n];
    fr(i, 0, n) cin >> sr[i].F >> sr[i].S;
    sort(sr, sr + n, cmp);
    int c = 0;
    for(int i = 0; i < n; ) {
        int x = sr[i].F;
        while(i < n && x == sr[i].F) i++;
        c++;
    }
    vector< vi> row_sum(c);
    vector< int> tot_sum;
    int p = 0;
    for(int i = 0; i < n; ) {
        int x = sr[i].F, sum = 0;
        while(i < n && x == sr[i].F) {
            sum += sr[i].S;
            i++;
            if(sum <= 0) continue;
            row_sum[p].pb(sum);
        }
        if(!row_sum[p].empty())
            p++;
    }
    //fr(i, 0, p) { parr(row_sum[i], row_sum[i].size());}

    ii row_sz[p];
    fr(i, 0, p) row_sz[i] = {row_sum[i].size(), i};
    sort(row_sz, row_sz + p);
    for(int y = 0, i = 0; y < p; ) {
        int x = row_sz[y].F;
        while(i < x) {
            int sum = 0;
            fr(j, y, p) sum += row_sum[row_sz[j].S][i];
            tot_sum.pb(sum);
            i++;
        }
        while(x == row_sz[y].F) y++;
    }
    //parr(tot_sum, tot_sum.size());
    int ans = 0;
    fr(i, 0, tot_sum.size()) ans = max(ans, tot_sum[i]);
    cout << ans;
}
int main() {
	fastio;

	//t_times
	_solve();
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define fastio ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define t_times int t; cin >> t; while(t--)
#define parr(a, n) for(int __i = 0; __i < (int )n; __i++) cout << a[__i] << ' '; cout << '\n'
#define fr(i, p, n) for(int i = (int )p; i < (int )n; i++)
#define frn(i, p, n) for(int i = (int )p; i <= (int )n; i++)
#define rev(i, n, p) for(int i = (int )n; i >= (int )p; i--)
#define all(c) (c).begin(), (c).end()
#define sci(n) scanf("%d", &n)
#define scl(n) scanf("%lld", &n)
#define prin(n) printf("%d\n", n)
#define prln(n) printf("%lld\n", n)
#define mp make_pair
#define F first
#define S second
#define pb push_back
#define bp __builtin_popcount
typedef long long int LL;
typedef unsigned long long ULL;
typedef unsigned int uint;
typedef pair< int, int> ii;
typedef vector< int> vi;

const int mod = 1e9 + 7;
const LL INF = 9e18 + 2e17;
const int inf = 2e9;
const int N = 2e3 + 22;
const double eps = 1e-10;

bool cmp(ii A, ii B) {
    if(A.F != B.F) return A.F < B.F;
    return A.S > B.S;
}
void _solve() {
    int n, m;
    cin >> n >> m;
    ii sr[n];
    fr(i, 0, n) cin >> sr[i].F >> sr[i].S;
    sort(sr, sr + n, cmp);
    int c = 0, d = 0;
    for(int i = 0; i < n; ) {
        int x = sr[i].F, td = 0;
        while(i < n && x == sr[i].F) i++;
        c++;
    }
    vector< vi> row_sum(c);
    vector< int> tot_sum;
    int p = 0;
    for(int i = 0; i < n; ) {
        int x = sr[i].F, sum = 0;
        while(i < n && x == sr[i].F) {
            sum += sr[i++].S;
            if(sum <= 0) continue;
            row_sum[p].pb(sum);
        }
        d = max(d, (int )row_sum[p].size());
        if(!row_sum[p].empty())
            p++;
    }
    //fr(i, 0, p) { parr(row_sum[i], row_sum[i].size());}
    fr(i, 0, d) {
        int sum = 0;
        fr(j, 0, p) {
            if(i < row_sum[j].size())
                sum += row_sum[j][i];
        }
        tot_sum.pb(sum);
    }
    //parr(tot_sum, d);
    int ans = 0;
    fr(i, 0, d) ans = max(ans, tot_sum[i]);
    cout << ans;
}
int main() {
	fastio;

	//t_times
	_solve();
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define fastio ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define t_times int t; cin >> t; while(t--)
#define parr(a, n) for(int __i = 0; __i < (int )n; __i++) cout << a[__i] << ' '; cout << '\n'
#define fr(i, p, n) for(int i = (int )p; i < (int )n; i++)
#define frn(i, p, n) for(int i = (int )p; i <= (int )n; i++)
#define rev(i, n, p) for(int i = (int )n; i >= (int )p; i--)
#define all(c) (c).begin(), (c).end()
#define sci(n) scanf("%d", &n)
#define scl(n) scanf("%lld", &n)
#define prin(n) printf("%d\n", n)
#define prln(n) printf("%lld\n", n)
#define mp make_pair
#define F first
#define S second
#define pb push_back
#define bp __builtin_popcount
typedef long long int LL;
typedef unsigned long long ULL;
typedef unsigned int uint;
typedef pair< int, int> ii;
typedef vector< int> vi;

const int mod = 1e9 + 7;
const LL INF = 9e18 + 2e17;
const int inf = 2e9;
const int N = 2e3 + 22;
const double eps = 1e-10;

bool cmp(ii A, ii B) {
    if(A.F != B.F) return A.F < B.F;
    return A.S > B.S;
}
void _solve() {
    int n, m;
    cin >> n >> m;
    ii sr[n];
    fr(i, 0, n) cin >> sr[i].F >> sr[i].S;
    sort(sr, sr + n, cmp);
    int c = 0, d = 0;
    for(int i = 0; i < n; ) {
        int x = sr[i].F;
        while(i < n && x == sr[i].F) i++;
        c++;
    }
    vector< vi> row_sum(c);
    vector< int> tot_sum;
    int p = 0;
    for(int i = 0; i < n; ) {
        int x = sr[i].F, sum = 0;
        while(i < n && x == sr[i].F) {
            sum += sr[i++].S;
            if(sum <= 0) continue;
            row_sum[p].pb(sum);
        }
        d = max(d, (int )row_sum[p].size());
        if(!row_sum[p].empty())
            p++;
    }
    //fr(i, 0, p) { parr(row_sum[i], row_sum[i].size());}
    fr(i, 0, d) {
        int sum = 0;
        fr(j, 0, p) {
            if(i < row_sum[j].size())
                sum += row_sum[j][i];
        }
        tot_sum.pb(sum);
    }
    //parr(tot_sum, d);
    int ans = 0;
    fr(i, 0, d) ans = max(ans, tot_sum[i]);
    cout << ans;
}
int main() {
	fastio;
	//t_times
	_solve();
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define fastio ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define t_times int t; cin >> t; while(t--)
#define parr(a, n) for(int __i = 0; __i < (int )n; __i++) cout << a[__i] << ' '; cout << '\n'
#define fr(i, p, n) for(int i = (int )p; i < (int )n; i++)
#define frn(i, p, n) for(int i = (int )p; i <= (int )n; i++)
#define rev(i, n, p) for(int i = (int )n; i >= (int )p; i--)
#define all(c) (c).begin(), (c).end()
#define sci(n) scanf("%d", &n)
#define scl(n) scanf("%lld", &n)
#define prin(n) printf("%d\n", n)
#define prln(n) printf("%lld\n", n)
#define mp make_pair
#define F first
#define S second
#define pb push_back
#define bp __builtin_popcount
typedef long long int LL;
typedef unsigned long long ULL;
typedef unsigned int uint;
typedef pair< int, int> ii;
typedef vector< int> vi;

const int mod = 1e9 + 7;
const LL INF = 9e18 + 2e17;
const int inf = 2e9;
const int N = 2e3 + 22;
const double eps = 1e-10;

bool cmp(ii A, ii B) {
    if(A.F != B.F) return A.F < B.F;
    return A.S > B.S;
}
void _solve() {
    int n, m;
    cin >> n >> m;
    ii sr[n];
    fr(i, 0, n) cin >> sr[i].F >> sr[i].S;
    sort(sr, sr + n, cmp);
    int c = 0, d = 0;
    for(int i = 0; i < n; ) {
        int x = sr[i].F;
        while(i < n && x == sr[i].F) i++;
        c++;
    }
    vector< vi> row_sum(c);
    vector< int> tot_sum;
    int p = 0;
    for(int i = 0; i < n; ) {
        int x = sr[i].F, sum = 0;
        while(i < n && x == sr[i].F) {
            sum += sr[i++].S;
            if(sum <= 0) break;
            row_sum[p].pb(sum);
        }
        d = max(d, (int )row_sum[p].size());
        if(!row_sum[p].empty())
            p++;
    }
    //fr(i, 0, p) { parr(row_sum[i], row_sum[i].size());}
    fr(i, 0, d) {
        int sum = 0;
        fr(j, 0, p) {
            if(i < row_sum[j].size())
                sum += row_sum[j][i];
        }
        tot_sum.pb(sum);
    }
    //parr(tot_sum, d);
    int ans = 0;
    fr(i, 0, d) ans = max(ans, tot_sum[i]);
    cout << ans;
}
int main() {
	fastio;

	//t_times
	_solve();
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define fastio ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define t_times int t; cin >> t; while(t--)
#define parr(a, n) for(int __i = 0; __i < (int )n; __i++) cout << a[__i] << ' '; cout << '\n'
#define fr(i, p, n) for(int i = (int )p; i < (int )n; i++)
#define frn(i, p, n) for(int i = (int )p; i <= (int )n; i++)
#define rev(i, n, p) for(int i = (int )n; i >= (int )p; i--)
#define all(c) (c).begin(), (c).end()
#define sci(n) scanf("%d", &n)
#define scl(n) scanf("%lld", &n)
#define prin(n) printf("%d\n", n)
#define prln(n) printf("%lld\n", n)
#define mp make_pair
#define F first
#define S second
#define pb push_back
#define bp __builtin_popcount
typedef long long int LL;
typedef unsigned long long ULL;
typedef unsigned int uint;
typedef pair< int, int> ii;
typedef vector< int> vi;

const int mod = 1e9 + 7;
const LL INF = 9e18 + 2e17;
const int inf = 2e9;
const int N = 2e3 + 22;
const double eps = 1e-10;

bool cmp(ii A, ii B) {
    if(A.F != B.F) return A.F < B.F;
    return A.S > B.S;
}
void _solve() {
    int n, m;
    cin >> n >> m;
    ii sr[n];
    fr(i, 0, n) cin >> sr[i].F >> sr[i].S;
    sort(sr, sr + n, cmp);
    int c = 0, d = 0;
    for(int i = 0; i < n; ) {
        int x = sr[i].F, td = 0;
        while(x == sr[i].F) i++;
        c++;
    }
    vector< vi> row_sum(c);
    vector< int> tot_sum;
    int p = 0;
    for(int i = 0; i < n; ) {
        int x = sr[i].F, sum = 0;
        while(x == sr[i].F) {
            sum += sr[i++].S;
            if(sum <= 0) break;
            row_sum[p].pb(sum);
        }
        d = max(d, (int )row_sum[p].size());
        if(!row_sum[p].empty())
            p++;
    }
    //fr(i, 0, p) { parr(row_sum[i], row_sum[i].size());}
    fr(i, 0, d) {
        int sum = 0;
        fr(j, 0, p) {
            if(i < row_sum[j].size())
                sum += row_sum[j][i];
        }
        tot_sum.pb(sum);
    }
    //parr(tot_sum, d);
    int ans = 0;
    fr(i, 0, d) ans = max(ans, tot_sum[i]);
    cout << ans;
}
int main() {
	fastio;

	//t_times
	_solve();
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define fastio ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define t_times int t; cin >> t; while(t--)
#define parr(a, n) for(int __i = 0; __i < (int )n; __i++) cout << a[__i] << ' '; cout << '\n'
#define fr(i, p, n) for(int i = (int )p; i < (int )n; i++)
#define frn(i, p, n) for(int i = (int )p; i <= (int )n; i++)
#define rev(i, n, p) for(int i = (int )n; i >= (int )p; i--)
#define all(c) (c).begin(), (c).end()
#define sci(n) scanf("%d", &n)
#define scl(n) scanf("%lld", &n)
#define prin(n) printf("%d\n", n)
#define prln(n) printf("%lld\n", n)
#define mp make_pair
#define F first
#define S second
#define pb push_back
#define bp __builtin_popcount
typedef long long int LL;
typedef unsigned long long ULL;
typedef unsigned int uint;
typedef pair< int, int> ii;
typedef vector< int> vi;

const int mod = 1e9 + 7;
const LL INF = 9e18 + 2e17;
const int inf = 2e9;
const int N = 1e5 + 22;
const double eps = 1e-10;

ii sr[N];

bool cmp(ii A, ii B) {
    if(A.F != B.F) return A.F < B.F;
    return A.S > B.S;
}
void _solve() {
    int n, m;
    cin >> n >> m;

    fr(i, 0, n) cin >> sr[i].F >> sr[i].S;
    sort(sr, sr + n, cmp);
    int c = 0, d = 0;
    for(int i = 0; i < n; ) {
        int x = sr[i].F;
        while(i < n && x == sr[i].F) i++;
        c++;
    }
    vector< vi> row_sum(c);
    vector< int> tot_sum;
    int p = 0;
    for(int i = 0; i < n; ) {
        int x = sr[i].F, sum = 0;
        while(i < n && x == sr[i].F) {
            sum += sr[i++].S;
            if(sum <= 0) break;
            row_sum[p].pb(sum);
        }
        d = max(d, (int )row_sum[p].size());
        if(!row_sum[p].empty())
            p++;
    }
    //fr(i, 0, p) { parr(row_sum[i], row_sum[i].size());}
    fr(i, 0, d) {
        int sum = 0;
        fr(j, 0, p) {
            if(i < row_sum[j].size())
                sum += row_sum[j][i];
        }
        tot_sum.pb(sum);
    }
    //parr(tot_sum, d);
    int ans = 0;
    fr(i, 0, d) ans = max(ans, tot_sum[i]);
    cout << ans;
}
int main() {
	fastio;

	//t_times
	_solve();
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define fastio ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define t_times int t; cin >> t; while(t--)
#define parr(a, n) for(int __i = 0; __i < (int )n; __i++) cout << a[__i] << ' '; cout << '\n'
#define fr(i, p, n) for(int i = (int )p; i < (int )n; i++)
#define frn(i, p, n) for(int i = (int )p; i <= (int )n; i++)
#define rev(i, n, p) for(int i = (int )n; i >= (int )p; i--)
#define all(c) (c).begin(), (c).end()
#define sci(n) scanf("%d", &n)
#define scl(n) scanf("%lld", &n)
#define prin(n) printf("%d\n", n)
#define prln(n) printf("%lld\n", n)
#define mp make_pair
#define F first
#define S second
#define pb push_back
#define bp __builtin_popcount
typedef long long int LL;
typedef unsigned long long ULL;
typedef unsigned int uint;
typedef pair< int, int> ii;
typedef vector< int> vi;

const int mod = 1e9 + 7;
const LL INF = 9e18 + 2e17;
const int inf = 2e9;
const int N = 2e3 + 22;
const double eps = 1e-10;

bool cmp(ii A, ii B) {
    if(A.F != B.F) return A.F < B.F;
    return A.S > B.S;
}
void _solve() {
    int n, m;
    cin >> n >> m;
    ii sr[n];
    fr(i, 0, n) cin >> sr[i].F >> sr[i].S;
    sort(sr, sr + n, cmp);
    int c = 0, d = 0;
    for(int i = 0; i < n; ) {
        int x = sr[i].F, td = 0;
        while(i < n && x == sr[i].F) i++;
        c++;
    }
    vector< vi> row_sum(c);
    vector< int> tot_sum;
    int p = 0;
    for(int i = 0; i < n; ) {
        int x = sr[i].F, sum = 0;
        while(i < n && x == sr[i].F) {
            sum += sr[i++].S;
            if(sum <= 0) break;
            row_sum[p].pb(sum);
        }
        d = max(d, (int )row_sum[p].size());
        if(!row_sum[p].empty())
            p++;
    }
    //fr(i, 0, p) { parr(row_sum[i], row_sum[i].size());}
    fr(i, 0, d) {
        int sum = 0;
        fr(j, 0, p) {
            if(i < row_sum[j].size())
                sum += row_sum[j][i];
        }
        tot_sum.pb(sum);
    }
    //parr(tot_sum, d);
    int ans = 0;
    fr(i, 0, d) ans = max(ans, tot_sum[i]);
    cout << ans;
}
int main() {
	fastio;

	//t_times
	_solve();
	return 0;
}


/*input
5 2
1 -1
1 -5
2 -1
2 -1
1 -10
*/

#include <iostream>
#include <climits>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

typedef long long int ll;
#define MOD 1000000007

int main()
{
	ios_base::sync_with_stdio(false);

	int n,m;
	cin>>n>>m;

	vector< vector<int> > sl(m+1);

	for(int i=0;i<n;i++)
	{
		int s,l;
		cin>>s>>l;

		sl[s].push_back(l);
	}

	for(int i=0;i<=m;i++)
		sort(sl[i].rbegin(), sl[i].rend());

	int val[n+1] = {0};

	for(int i=0;i<=m;i++)
	{
		int cur = 0;
		for(int j=0;j<sl[i].size();j++)
		{
			cur += sl[i][j];

			val[j+1] += cur;
		}
	}

	int ans = 0;

	for(int i=0;i<=m;i++)
		ans = max(ans, val[i]);

	cout<<ans;

	// your code goes here
	return 0;
}


/*input
1 100000
100000 10000
*/

#include <iostream>
#include <climits>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

typedef long long int ll;
#define MOD 1000000007

int main()
{
	ios_base::sync_with_stdio(false);

	int n,m;
	cin>>n>>m;

	vector< vector<int> > sl(m+1, vector<int>());

	for(int i=0;i<n;i++)
	{
		int s,l;
		cin>>s>>l;

		sl[s].push_back(l);
	}

	for(int i=0;i<=m;i++)
		sort(sl[i].rbegin(), sl[i].rend());

	int val[n+1];
	memset(val,0,sizeof(val));

	for(int i=0;i<=m;i++)
	{
		int cur = 0;
		for(int j=0;j<sl[i].size();j++)
		{
			cur += sl[i][j];

			if(cur<0)
				break;

			val[j+1] += cur;
		}
	}

	int ans = 0;

	for(int i=0;i<=n;i++)
		ans = max(ans, val[i]);

	cout<<ans;

	// your code goes here
	return 0;
}


/*input
1 100000
100000 10000
*/

#include <iostream>
#include <climits>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

typedef long long int ll;
#define MOD 1000000007

int main()
{
	ios_base::sync_with_stdio(false);

	int n,m;
	cin>>n>>m;

	vector< vector<int> > sl(m+1);

	for(int i=0;i<n;i++)
	{
		int s,l;
		cin>>s>>l;

		sl[s].push_back(l);
	}

	for(int i=0;i<=m;i++)
		sort(sl[i].rbegin(), sl[i].rend());

	int val[n+1];
	memset(val,0,sizeof(val));

	for(int i=0;i<=m;i++)
	{
		int cur = 0;
		for(int j=0;j<sl[i].size();j++)
		{
			cur += sl[i][j];

			if(cur<0)
				break;

			val[j+1] += cur;
		}
	}

	int ans = 0;

	for(int i=0;i<=n;i++)
		ans = max(ans, val[i]);

	cout<<ans;

	// your code goes here
	return 0;
}


/*input
6 2
1 6
1 -5
2 6
2 6
1 -7
2 -7
*/

#include <iostream>
#include <climits>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

typedef long long int ll;
#define MOD 1000000007

int main()
{
	ios_base::sync_with_stdio(false);

	int n,m;
	cin>>n>>m;

	vector< vector<int> > sl(m+1, vector<int>());

	for(int i=0;i<n;i++)
	{
		int s,l;
		cin>>s>>l;

		sl[s].push_back(l);
	}

	for(int i=0;i<=m;i++)
		sort(sl[i].rbegin(), sl[i].rend());

	int val[n+1];
	memset(val,0,sizeof(val));
	int ans = 0;

	for(int i=0;i<=m;i++)
	{
		int cur = 0;
		for(int j=0;j<sl[i].size();j++)
		{
			cur += sl[i][j];

			if(cur<0)
				break;

			val[j+1] += cur;
			ans = max(ans, val[j+1]);
		}
	}

	cout<<ans;

	// your code goes here
	return 0;
}


/*input
5 2
1 -1
1 -5
2 -1
2 -1
1 -10
*/

#include <iostream>
#include <climits>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

typedef long long int ll;
#define MOD 1000000007

int main()
{
	ios_base::sync_with_stdio(false);

	int n,m;
	cin>>n>>m;

	vector< vector<int> > sl(m+1);

	for(int i=0;i<n;i++)
	{
		int s,l;
		cin>>s>>l;

		sl[s].push_back(l);
	}

	for(int i=0;i<=m;i++)
		sort(sl[i].rbegin(), sl[i].rend());

	int val[m+1] = {0};

	for(int i=0;i<=m;i++)
	{
		int cur = 0;
		for(int j=0;j<sl[i].size();j++)
		{
			cur += sl[i][j];

			if(cur<0)
				break;

			val[j+1] += cur;
		}
	}

	int ans = 0;

	for(int i=0;i<=m;i++)
		ans = max(ans, val[i]);

	cout<<ans;

	// your code goes here
	return 0;
}


/*input
6 2
1 6
1 -5
2 6
2 6
1 -7
2 -7
*/

#include <iostream>
#include <climits>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

typedef long long int ll;
#define MOD 1000000007

int main()
{
	ios_base::sync_with_stdio(false);

	int n,m;
	cin>>n>>m;

	vector< vector<int> > sl(m+1, vector<int>());

	for(int i=0;i<n;i++)
	{
		int s,l;
		cin>>s>>l;

		sl[s].push_back(l);
	}

	for(int i=0;i<=m;i++)
		sort(sl[i].rbegin(), sl[i].rend());

	int val[m+1];
	memset(val,0,sizeof(val));
	int ans = 0;

	for(int i=0;i<=m;i++)
	{
		int cur = 0;
		for(int j=0;j<sl[i].size();j++)
		{
			cur += sl[i][j];

			if(cur<0)
				break;

			val[j+1] += cur;
			ans = max(ans, val[j+1]);
		}
	}

	cout<<ans;

	// your code goes here
	return 0;
}


/*input
5 2
1 -1
1 -5
2 -1
2 -1
1 -10
*/

#include <iostream>
#include <climits>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

typedef long long int ll;
#define MOD 1000000007

int main()
{
	ios_base::sync_with_stdio(false);

	int n,m;
	cin>>n>>m;

	vector< vector<int> > sl(m+1, vector<int>());

	for(int i=0;i<n;i++)
	{
		int s,l;
		cin>>s>>l;

		sl[s].push_back(l);
	}

	for(int i=0;i<=m;i++)
		sort(sl[i].rbegin(), sl[i].rend());

	int val[m+1] = {0};
	int ans = 0;

	for(int i=0;i<=m;i++)
	{
		int cur = 0;
		for(int j=0;j<sl[i].size();j++)
		{
			cur += sl[i][j];

			if(cur<0)
				break;

			val[j+1] += cur;
			ans = max(ans, val[j+1]);
		}
	}

	cout<<ans;

	// your code goes here
	return 0;
}


/*input
5 2
1 -1
1 -5
2 -1
2 -1
1 -10
*/

#include <iostream>
#include <climits>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

typedef long long int ll;
#define MOD 1000000007

int main()
{
	ios_base::sync_with_stdio(false);

	int n,m;
	cin>>n>>m;

	vector< vector<int> > sl(m+1);

	for(int i=0;i<n;i++)
	{
		int s,l;
		cin>>s>>l;

		sl[s].push_back(l);
	}

	for(int i=0;i<=m;i++)
		sort(sl[i].rbegin(), sl[i].rend());

	int val[m+1] = {0};

	for(int i=0;i<=m;i++)
	{
		int cur = 0;
		for(int j=0;j<sl[i].size();j++)
		{
			cur += sl[i][j];

			val[j+1] += cur;
		}
	}

	int ans = 0;

	for(int i=0;i<=m;i++)
		ans = max(ans, val[i]);

	cout<<ans;

	// your code goes here
	return 0;
}


/*input
5 2
1 -1
1 -5
2 -1
2 -1
1 -10
*/

#include <iostream>
#include <climits>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

typedef long long int ll;
#define MOD 1000000007

int main()
{
	ios_base::sync_with_stdio(false);

	int n,m;
	cin>>n>>m;

	vector< vector<int> > sl(m+1);

	for(int i=0;i<n;i++)
	{
		int s,l;
		cin>>s>>l;

		sl[s].push_back(l);
	}

	for(int i=0;i<=m;i++)
		sort(sl[i].rbegin(), sl[i].rend());

	int val[n+1] = {0};

	for(int i=0;i<=m;i++)
	{
		int cur = 0;
		for(int j=0;j<sl[i].size();j++)
		{
			cur += sl[i][j];

			if(cur<0)
				break;

			val[j+1] += cur;
		}
	}

	int ans = 0;

	for(int i=0;i<=m;i++)
		ans = max(ans, val[i]);

	cout<<ans;

	// your code goes here
	return 0;
}


/*input
6 2
1 6
1 -5
2 6
2 6
1 -7
2 -7
*/

#include <iostream>
#include <climits>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

typedef long long int ll;
#define MOD 1000000007

int main()
{
	ios_base::sync_with_stdio(false);

	int n,m;
	cin>>n>>m;

	vector< vector<int> > sl(m+1, vector<int>());

	for(int i=0;i<n;i++)
	{
		int s,l;
		cin>>s>>l;

		sl[s].push_back(l);
	}

	for(int i=0;i<=m;i++)
		sort(sl[i].rbegin(), sl[i].rend());

	int val[m+1];
	memset(val,0,sizeof(val));
	int ans = 0;

	for(int i=0;i<=m;i++)
	{
		int cur = 0;
		for(int j=0;j<sl[i].size();j++)
		{
			cur += sl[i][j];

			if(cur<0)
				break;

			val[j+1] += cur;
			ans = max(ans, val[j+1]);
		}
	}

	cout<<ans;;

	// your code goes here
	return 0;
}


/*input
5 2
1 -1
1 -5
2 -1
2 -1
1 -10
*/

#include <iostream>
#include <climits>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

typedef long long int ll;
#define MOD 1000000007

int main()
{
	ios_base::sync_with_stdio(false);

	int n,m;
	cin>>n>>m;

	vector< vector<int> > sl(m+1);

	for(int i=0;i<n;i++)
	{
		int s,l;
		cin>>s>>l;

		sl[s].push_back(l);
	}

	for(int i=0;i<=m;i++)
		sort(sl[i].rbegin(), sl[i].rend());

	int val[n+1];
	memset(val,0,sizeof(val));

	for(int i=0;i<=m;i++)
	{
		int cur = 0;
		for(int j=0;j<sl[i].size();j++)
		{
			cur += sl[i][j];

			if(cur<0)
				break;

			val[j+1] += cur;
		}
	}

	int ans = 0;

	for(int i=0;i<=m;i++)
		ans = max(ans, val[i]);

	cout<<ans;

	// your code goes here
	return 0;
}


#include <stdio.h>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long int ld;
vector<ld> kk[111111],kk2[111111],kk3[111111];
ld sum[111111];
bool tmr(int a,int b){
	return a>b?true:false;
}
int main(void){
	int n,m;
	scanf("%d%d",&n,&m);
	for(int e=0;e<n;e++){
		int s,r;
		scanf("%d%d",&s,&r);
		kk[s].push_back(r);
	}
	for(int e=1;e<=m;e++) sort(kk[e].begin(),kk[e].end(),tmr);
	for(int e=1;e<=m;e++){
		ld now =0;
		for(int p=0;p<kk[e].size();p++){
			now+=kk[e][p];
			kk2[e].push_back(now);
		}
	}
	vector<pair<int,int> > rr;
	for(int e=1;e<=m;e++) rr.push_back(make_pair(kk2[e].size(),e));
	for(int e=0;e<rr.size();e++){
		int now = rr[e].second;
		for(int p=0;p<kk2[now].size();p++) kk3[e].push_back(kk2[now][p]);
	}
	int ll = 0;
	ld mm = 0;
	for(int e=0;e<rr.size();e++){
		if(e) {
			if(rr[e].first==rr[e-1].first)  continue;
		}
		if(rr[e].first==0) continue;
		ld tot=0;
		for(int p=e;p<rr.size();p++){
			tot+=kk3[p][rr[e].first-1];
		}
		mm=max(mm,tot);
	}
	cout << mm;
}


#include <stdio.h>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long int ld;
vector<ld> kk[111111];
ld sum[111111],mm;
bool tmr(ld a,ld b){
	return a>b;
}
int main(void){
	int n,m;
	scanf("%d%d",&n,&m);
	for(int e=0;e<n;e++){
		int s,r;
		scanf("%d%d",&s,&r);
		kk[s].push_back(r);
	}
	for(int e=1;e<=m;e++) sort(kk[e].begin(),kk[e].end(),tmr);
	for(int e=1;e<=m;e++){
		ld now = 0;
		for(int p=0;p<kk[e].size();p++){
			now+=kk[e][p];
			if(now>0) sum[p+1]+=now;
		}
	}
	for(int e=1;e<=m;e++) mm=max(mm,sum[e]);
	printf("%lld",mm);
}


#include <stdio.h>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long int ld;
vector<ld> kk[111111],kk2[111111],kk3[111111];
ld sum[111111];
bool tmr(ld a,ld b){
	return a>b;
}
int main(void){
	int n,m;
	scanf("%d%d",&n,&m);
	for(int e=0;e<n;e++){
		int s,r;
		scanf("%d%d",&s,&r);
		kk[s].push_back(r);
	}
	for(int e=1;e<=m;e++) sort(kk[e].begin(),kk[e].end(),tmr);
	for(int e=1;e<=m;e++){
		ld now =0;
		for(int p=0;p<kk[e].size();p++){
			now+=kk[e][p];
			kk2[e].push_back(now);
		}
	}
	ld mm = 0;
	vector<int> nu;
	for(int e=1;e<=m;e++) if(kk2[e].size()>=300) nu.push_back(e);
	for(int e=1;e<=min(300,m);e++){
		ld tot = 0;
		for(int p=1;p<=m;p++){
			if(kk2[p].size()>=e) if(kk2[p][e-1]>0) tot+=kk2[p][e-1];
		}
		mm=max(mm,tot);
	}
	for(int e=300;e<=m;e++){
		ld tot=0;
		for(int p=0;p<nu.size();p++){
			int num=nu[p];
			if(kk2[num].size()>=e) if(kk2[num][e-1]>0) tot+=kk2[num][e-1];
		}
		mm=max(mm,tot);
	}
	printf("%I64d",mm);
}


#include <stdio.h>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long int ld;
vector<ld> kk[111111],kk2[111111],kk3[111111];
ld sum[111111];
bool tmr(ld a,ld b){
	return a>b;
}
int main(void){
	int n,m;
	scanf("%d%d",&n,&m);
	for(int e=0;e<n;e++){
		int s,r;
		scanf("%d%d",&s,&r);
		kk[s].push_back(r);
	}
	for(int e=1;e<=m;e++) sort(kk[e].begin(),kk[e].end(),tmr);
	for(int e=1;e<=m;e++){
		ld now =0;
		for(int p=0;p<kk[e].size();p++){
			now+=kk[e][p];
			kk2[e].push_back(now);
		}
	}
	ld mm = 0;
	vector<int> nu;
	for(int e=1;e<=m;e++) if(kk2[e].size()>=300) nu.push_back(e);
	for(int e=1;e<=min(300,m);e++){
		ld tot = 0;
		for(int p=1;p<=m;p++){
			if(kk2[p].size()>=e) if(kk2[p][e-1]>0) tot+=kk2[p][e-1];
		}
		mm=max(mm,tot);
	}
	for(int e=300;e<=m;e++){
		ld tot=0;
		for(int p=0;p<nu.size();p++){
			int num=nu[p];
			if(kk2[num].size()>=e) if(kk2[num][e-1]>0) tot+=kk2[num][e-1];
		}
		mm=max(mm,tot);
	}
	printf("%lld",mm);
}


#include <stdio.h>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long int ld;
vector<ld> kk[111111],kk2[111111],kk3[111111];
ld sum[111111];
bool tmr(ld a,ld b){
	return a>b;
}
int main(void){
	int n,m;
	scanf("%d%d",&n,&m);
	for(int e=0;e<n;e++){
		int s,r;
		scanf("%d%d",&s,&r);
		kk[s].push_back(r);
	}
	for(int e=1;e<=m;e++) sort(kk[e].begin(),kk[e].end(),tmr);
	for(int e=1;e<=m;e++){
		ld now =0;
		for(int p=0;p<kk[e].size();p++){
			now+=kk[e][p];
			kk2[e].push_back(now);
		}
	}
	ld mm = 0;
	vector<int> nu;
	for(int e=1;e<=m;e++) if(kk2[e].size()>=300) nu.push_back(e);
	for(int e=1;e<=min(300,m);e++){
		ld tot = 0;
		for(int p=1;p<=m;p++){
			if(kk2[p].size()>=e) tot+=kk2[p][e-1];
		}
		mm=max(mm,tot);
	}
	for(int e=300;e<=m;e++){
		ld tot=0;
		for(int p=0;p<nu.size();p++){
			int num=nu[p];
			if(kk2[num].size()>=e) if(kk2[num][e-1]>0) tot+=kk2[num][e-1];
		}
		mm=max(mm,tot);
	}
	printf("%I64d",mm);
}


#include <stdio.h>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long int ld;
vector<ld> kk[111111],kk2[111111],kk3[111111];
ld sum[111111];
bool tmr(int a,int b){
	return a>b?true:false;
}
int main(void){
	int n,m;
	scanf("%d%d",&n,&m);
	for(int e=0;e<n;e++){
		int s,r;
		scanf("%d%d",&s,&r);
		kk[s].push_back(r);
	}
	for(int e=1;e<=m;e++) sort(kk[e].begin(),kk[e].end(),tmr);
	for(int e=1;e<=m;e++){
		ld now =0;
		for(int p=0;p<kk[e].size();p++){
			now+=kk[e][p];
			kk2[e].push_back(now);
		}
	}
	vector<pair<int,int> > rr;
	for(int e=1;e<=m;e++) rr.push_back(make_pair(kk2[e].size(),e));
	sort(rr.begin(),rr.end());
	for(int e=0;e<rr.size();e++){
		int now = rr[e].second;
		for(int p=0;p<kk2[now].size();p++) kk3[e].push_back(kk2[now][p]);
	}
	int ll = 0;
	ld mm = 0;
	for(int e=0;e<rr.size();e++){
		if(e) {
			if(rr[e].first==rr[e-1].first)  continue;
		}
		if(rr[e].first==0) continue;
		ld tot=0;
		for(int p=e;p<rr.size();p++){
			tot+=kk3[p][rr[e].first-1];
		}
		mm=max(mm,tot);
	}
	cout << mm;
}


#include <stdio.h>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long int ld;
vector<ld> kk[111111],kk2[111111],kk3[111111];
ld sum[111111];
bool tmr(int a,int b){
	return a>b?true:false;
}
int main(void){
	int n,m;
	scanf("%d%d",&n,&m);
	for(int e=0;e<n;e++){
		int s,r;
		scanf("%d%d",&s,&r);
		kk[s].push_back(r);
	}
	for(int e=1;e<=m;e++) sort(kk[e].begin(),kk[e].end(),tmr);
	for(int e=1;e<=m;e++){
		ld now =0;
		for(int p=0;p<kk[e].size();p++){
			now+=kk[e][p];
			kk2[e].push_back(now);
		}
	}
	vector<pair<int,int> > rr;
	for(int e=1;e<=m;e++) rr.push_back(make_pair(kk2[e].size(),e));
	sort(rr.begin(),rr.end());
	for(int e=0;e<rr.size();e++){
		int now = rr[e].second;
		for(int p=0;p<kk2[now].size();p++) kk3[e].push_back(kk2[now][p]);
	}
	int ll = 0;
	ld mm = 0;
	for(int e=0;e<rr.size();e++){
		if(e) {
			if(rr[e].first==rr[e-1].first)  continue;
		}
		if(rr[e].first==0) continue;
		ld tot=0;
		for(int p=e;p<rr.size();p++){
			if(kk3[p][rr[e].first-1]<0) continue;
			tot+=kk3[p][rr[e].first-1];
		}
		mm=max(mm,tot);
	}
	cout << mm;
}


#include <stdio.h>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long int ld;
vector<ld> kk[111111],kk2[111111],kk3[111111];
ld sum[111111];
bool tmr(ld a,ld b){
	return a>b;
}
int main(void){
	int n,m;
	scanf("%d%d",&n,&m);
	for(int e=0;e<n;e++){
		int s,r;
		scanf("%d%d",&s,&r);
		kk[s].push_back(r);
	}
	for(int e=1;e<=m;e++) sort(kk[e].begin(),kk[e].end(),tmr);
	for(int e=1;e<=m;e++){
		ld now =0;
		for(int p=0;p<kk[e].size();p++){
			now+=kk[e][p];
			kk2[e].push_back(now);
		}
	}
	ld mm = 0;
	vector<int> nu;
	for(int e=1;e<=m;e++) if(kk2[e].size()>=300) nu.push_back(e);
	for(int e=1;e<=min(300,n);e++){
		ld tot = 0;
		for(int p=1;p<=m;p++){
			if(kk2[p].size()>=e) if(kk2[p][e-1]>0) tot+=kk2[p][e-1];
		}
		mm=max(mm,tot);
	}
	for(int e=300;e<=n;e++){
		ld tot=0;
		for(int p=0;p<nu.size();p++){
			int num=nu[p];
			if(kk2[num].size()>=e) if(kk2[num][e-1]>0) tot+=kk2[num][e-1];
		}
		mm=max(mm,tot);
	}
	printf("%I64d",mm);
}
close


#include <stdio.h>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long int ld;
vector<ld> kk[111111],kk2[111111],kk3[111111];
ld sum[111111];
bool tmr(int a,int b){
	return a>b?true:false;
}
int main(void){
	int n,m;
	scanf("%d%d",&n,&m);
	for(int e=0;e<n;e++){
		int s,r;
		scanf("%d%d",&s,&r);
		kk[s].push_back(r);
	}
	for(int e=1;e<=m;e++) sort(kk[e].begin(),kk[e].end(),tmr);
	for(int e=1;e<=m;e++){
		ld now =0;
		for(int p=0;p<kk[e].size();p++){
			now+=kk[e][p];
			kk2[e].push_back(now);
		}
	}
	ld mm = 0;
	vector<int> nu;
	for(int e=1;e<=m;e++) if(kk2[e].size()>=300) nu.push_back(e);
	for(int e=1;e<=min(300,m);e++){
		ld tot = 0;
		for(int p=1;p<=m;p++){
			if(kk2[p].size()>=e) tot+=kk2[p][e-1];
		}
		mm=max(mm,tot);
	}
	for(int e=300;e<=m;e++){
		ld tot=0;
		for(int p=0;p<nu.size();p++){
			int num=nu[p];
			if(kk2[num].size()>=e) tot+=kk2[num][e-1];
		}
		mm=max(mm,tot);
	}
	printf("%I64d\n",mm);
}


#include <stdio.h>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long int ld;
vector<int> kk[111111];
int sum[111111],mm;
bool tmr(int a,int b){
	return a>b;
}
int main(void){
	int n,m;
	scanf("%d%d",&n,&m);
	for(int e=0;e<n;e++){
		int s,r;
		scanf("%d%d",&s,&r);
		kk[s].push_back(r);
	}
	for(int e=1;e<=m;e++) sort(kk[e].begin(),kk[e].end(),tmr);
	for(int e=1;e<=m;e++){
		int now = 0;
		for(int p=0;p<kk[e].size();p++){
			now+=kk[e][p];
			if(now>0) sum[p+1]+=now;
		}
	}
	for(int e=1;e<=m;e++) mm=max(mm,sum[e]);
	printf("%d",mm);
}


#include <stdio.h>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long int ld;
vector<ld> kk[111111],kk2[111111];

bool tmr(ld a,ld b){
	return a>b;
}
int main(void){
	int n,m;
	scanf("%d%d",&n,&m);
	for(int e=0;e<n;e++){
		int s,r;
		scanf("%d%d",&s,&r);
		kk[s].push_back(r);
	}
	for(int e=1;e<=m;e++) sort(kk[e].begin(),kk[e].end(),tmr);
	for(int e=1;e<=m;e++){
		ld now =0;
		for(int p=0;p<kk[e].size();p++){
			now+=kk[e][p];
			kk2[e].push_back(now);
		}
	}
	ld mm = 0;
	vector<int> nu;
	for(int e=1;e<=m;e++) if(kk2[e].size()>=300) nu.push_back(e);
	for(int e=1;e<=min(300,m);e++){
		ld tot = 0;
		for(int p=1;p<=m;p++){
			if(kk2[p].size()>=e) if(kk2[p][e-1]>0) tot+=kk2[p][e-1];
		}
		mm=max(mm,tot);
	}
	for(int e=200;e<=m;e++){
		ld tot=0;
		for(int p=0;p<nu.size();p++){
			int num=nu[p];
			if(kk2[num].size()>=e) if(kk2[num][e-1]>0) tot+=kk2[num][e-1];
		}
		mm=max(mm,tot);
	}
	printf("%lld",mm);
}


#include <stdio.h>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long int ld;
vector<ld> kk[111111],kk2[111111],kk3[111111];
ld sum[111111];
bool tmr(ld a,ld b){
	return a>b;
}
int main(void){
	int n,m;
	scanf("%d%d",&n,&m);
	for(int e=0;e<n;e++){
		int s,r;
		scanf("%d%d",&s,&r);
		kk[s].push_back(r);
	}
	for(int e=1;e<=m;e++) sort(kk[e].begin(),kk[e].end(),tmr);
	for(int e=1;e<=m;e++){
		ld now =0;
		for(int p=0;p<kk[e].size();p++){
			now+=kk[e][p];
			kk2[e].push_back(now);
		}
	}
	ld mm = 0;
	vector<int> nu;
	for(int e=1;e<=m;e++) if(kk2[e].size()>=300) nu.push_back(e);
	for(int e=1;e<=min(300,n);e++){
		ld tot = 0;
		for(int p=1;p<=m;p++){
			if(kk2[p].size()>=e) if(kk2[p][e-1]>0) tot+=kk2[p][e-1];
		}
		mm=max(mm,tot);
	}
	for(int e=300;e<=n;e++){
		ld tot=0;
		for(int p=0;p<nu.size();p++){
			int num=nu[p];
			if(kk2[num].size()>=e) if(kk2[num][e-1]>0) tot+=kk2[num][e-1];
		}
		mm=max(mm,tot);
	}
	printf("%I64d",mm);
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 5e5 + 10;
const ll P = 1e12 + 3;
int n, m;
vector<int> sp[N];
int T[N];
int main()
{
	scanf("%d%d", &n, &m);
	int x, y;
	for(int i = 1; i <= n; ++i)
	{
		scanf("%d%d", &x, &y);
		sp[x].push_back(y);
	}
	for(int i = 1; i <= m; ++i) sort(sp[i].begin(), sp[i].end());
	for(int i = 1; i <= m; ++i)
	{
		if(sp[i].size() == 0) continue;
		T[1] += sp[i].back();
		int cnt = 1;
		for(int j = sp[i].size() - 2; j >= 0; --j)
		{
			sp[i][j] += sp[i][j + 1];
			T[++cnt] += sp[i][j];
		}
	}
	int ans = 0;
	for(int i = 1; i <= m; ++i) ans = max(T[i], ans);
	printf("%d\n", ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 5e5 + 10;
const ll P = 1e12 + 3;
ll n, m;
ll x, y;
priority_queue<ll> q[N];
ll T[N];
int main()
{
	scanf("%lld%lld", &n, &m);
	for(int i = 1; i <= n; ++i)
	{
		scanf("%lld%lld", &x, &y);
		q[x].push(y);
	}
	for(int i = 1; i <= m; ++i)
	{
		ll cnt = 0;
		ll sum = 0;
		while(q[i].size())
		{
			ll now = q[i].top(); q[i].pop();
			sum += now;
			if(sum > 0)
			T[++cnt] += sum;
		}
	}
	ll ans = 0;
	for(int i = 1; i <= m; ++i) ans = max(ans, T[i]);
	printf("%lld", ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 5e5 + 10;
const ll P = 1e12 + 3;
ll n, m;
ll x, y;
priority_queue<ll> q[N];
ll T[N];
int main()
{
	scanf("%lld%lld", &n, &m);
	for(int i = 1; i <= n; ++i)
	{
		scanf("%lld%lld", &x, &y);
		q[x].push(y);
	}
	for(int i = 1; i <= m; ++i)
	{
		ll cnt = 0;
		ll sum = 0;
		while(q[i].size())
		{
			ll now = q[i].top(); q[i].pop();
			sum += now;
			if(sum < 0) break;
			T[++cnt] += sum;
		}
	}
	ll ans = 0;
	for(int i = 1; i <= m; ++i) ans = max(ans, T[i]);
	printf("%lld", ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 5e5 + 10;
const ll P = 1e12 + 3;
int n, m;
vector<int> sp[N];
int T[N];
int main()
{
	scanf("%d%d", &n, &m);
	int x, y;
	for(int i = 1; i <= n; ++i)
	{
		scanf("%d%d", &x, &y);
		if(y < 0) continue;
		sp[x].push_back(y);
	}
	for(int i = 1; i <= m; ++i) sort(sp[i].begin(), sp[i].end());
	for(int i = 1; i <= m; ++i)
	{
		if(sp[i].size() == 0) continue;
		T[1] += sp[i].back();
		int cnt = 1;
		for(int j = sp[i].size() - 2; j >= 0; --j)
		{
			sp[i][j] += sp[i][j + 1];
			T[++cnt] += max(0, sp[i][j]);
		}
	}
	int ans = 0;
	for(int i = 1; i <= m; ++i) ans = max(T[i], ans);
	printf("%d\n", ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 5e5 + 10;
const ll P = 1e12 + 3;
ll n, m;
ll x, y;
priority_queue<ll> q[N];
ll T[N];
int main()
{
	scanf("%lld%lld", &n, &m);
	for(int i = 1; i <= n; ++i)
	{
		scanf("%lld%lld", &x, &y);
		q[x].push(y);
	}
	for(int i = 1; i <= m; ++i)
	{
		ll cnt = 0;
		ll sum = 0;
		while(q[i].size())
		{
			ll now = q[i].top(); q[i].pop();
			sum += now;
			if(sum < 0) break;
			T[++cnt] += sum;
		}
	}
	ll ans = 0;
	for(int i = 1; i <= n; ++i) ans = max(ans, T[i]);
	printf("%lld", ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 5e5 + 10;
const ll P = 1e12 + 3;
int n, m;
vector<int> sp[N];
int T[N];
int main()
{
	scanf("%d%d", &n, &m);
	int x, y;
	for(int i = 1; i <= n; ++i)
	{
		scanf("%d%d", &x, &y);
		if(y < 0) continue;
		sp[x].push_back(y);
	}
	for(int i = 1; i <= m; ++i) sort(sp[i].begin(), sp[i].end());
	for(int i = 1; i <= m; ++i)
	{
		if(sp[i].size() == 0) continue;
		T[1] += max(0, sp[i].back());
		int cnt = 1;
		for(int j = sp[i].size() - 2; j >= 0; --j)
		{
			sp[i][j] += sp[i][j + 1];
			T[++cnt] += max(0, sp[i][j]);
		}
	}
	int ans = 0;
	for(int i = 1; i <= m; ++i) ans = max(T[i], ans);
	printf("%d\n", ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 5e5 + 10;
const ll P = 1e12 + 3;
int n, m;
vector<int> sp[N];
int T[N];
int main()
{
	scanf("%d%d", &n, &m);
	int x, y;
	for(int i = 1; i <= n; ++i)
	{
		scanf("%d%d", &x, &y);
		sp[x].push_back(y);
	}
	for(int i = 1; i <= m; ++i) sort(sp[i].begin(), sp[i].end());
	for(int i = 1; i <= m; ++i)
	{
		if(sp[i].size() == 0) continue;
		T[1] += max(0, sp[i].back());
		int cnt = 1;
		for(int j = sp[i].size() - 2; j >= 0; --j)
		{
			sp[i][j] += sp[i][j + 1];
			T[++cnt] += max(0, sp[i][j]);
		}
	}
	int ans = 0;
	for(int i = 1; i <= n; ++i) ans = max(T[i], ans);
	printf("%d\n", ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 5e5 + 10;
const ll P = 1e12 + 3;
int n, m;
vector<int> sp[N];
int T[N];
int main()
{
	scanf("%d%d", &n, &m);
	int x, y;
	for(int i = 1; i <= n; ++i)
	{
		scanf("%d%d", &x, &y);
		sp[x].push_back(y);
	}
	for(int i = 1; i <= m; ++i) sort(sp[i].begin(), sp[i].end());
	for(int i = 1; i <= m; ++i)
	{
		if(sp[i].size() == 0) continue;
		T[1] += max(0, sp[i].back());
		int cnt = 1;
		for(int j = sp[i].size() - 2; j >= 0; --j)
		{
			sp[i][j] += sp[i][j + 1];
			T[++cnt] += max(0, sp[i][j]);
		}
	}
	int ans = 0;
	for(int i = 1; i <= m; ++i) ans = max(T[i], ans);
	printf("%d\n", ans);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define INF (2 << 31)
#define INFLL ((ll)(1e18)+7)
#define endl "\n"
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))

typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pii;
typedef vector<pii> vii;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    ll n, m;
    cin >> n >> m;
    vvi cum(100000, vi());

    for (int i = 0; i < n; i++) {
        ll s, r;
        cin >> s >> r;
        cum[s].push_back(r);
    }

    for (int i = 0; i < 100000; i++) {
        sort(cum[i].begin(), cum[i].end(), greater<ll>());
    }

    for (int i = 0; i < 100000; i++) {
        for (int j = 1; j < (int)cum[i].size(); j++) {
            cum[i][j] += cum[i][j-1];
        }
    }

    // TODO: TLE
    vvi q(100001, vi());
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j < 100000; j++) {
            if ((int)cum[j].size() >= i)
                q[i].push_back(cum[j][i-1]);
        }
        if (q[i].empty()) break;
    }

    for (int i = 0; i < 100001; i++) {
        sort(q[i].begin(), q[i].end(), greater<ll>());
    }

    for (int i = 0 ;i  <100001; i++) {
        for (int j = 1; j < (int)q[i].size(); j++) {
            q[i][j] += q[i][j-1];
        }
    }

    ll best = 0;

    for (vi a: q) {
        if (!a.empty())
        best = max(best, a[a.size()-1]);
    }

    /*
    for (int i = 1; i <= n; i++) {

        vi factors;
        for (int f = 1; f <= sqrt(i); f++) {
            if (i%f == 0) {
                factors.push_back(f);
                factors.push_back(i/f);
            }
        }

        for (ll j: factors) {
            if (j <= m)
                if (j-1 < (int)q[i/j].size())
                    best = max(best, q[i/j][j-1]);
        }

    }
    */

    cout << best << endl;

}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define INF (2 << 31)
#define INFLL ((ll)(1e18)+7)
#define endl "\n"
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))

typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pii;
typedef vector<pii> vii;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    ll n, m;
    cin >> n >> m;
    vvi cum(100000, vi());

    for (int i = 0; i < n; i++) {
        ll s, r;
        cin >> s >> r;
        cum[s].push_back(r);
    }

    for (int i = 0; i < 100000; i++) {
        sort(cum[i].begin(), cum[i].end(), greater<ll>());
    }

    for (int i = 0; i < 100000; i++) {
        for (int j = 1; j < (int)cum[i].size(); j++) {
            cum[i][j] += max(cum[i][j-1], 0);
        }
    }

    // TODO: TLE
    vvi q(100001, vi());
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j < 100000; j++) {
            if ((int)cum[j].size() >= i)
                q[i].push_back(cum[j][i-1]);
        }
    }

    for (int i = 0; i < 100001; i++) {
        sort(q[i].begin(), q[i].end(), greater<ll>());
    }

    for (int i = 0 ;i  <100001; i++) {
        for (int j = 1; j < (int)q[i].size(); j++) {
            q[i][j] += max(0,q[i][j-1]);
        }
    }

    ll best = 0;

    for (vi a: q) {
        for (auto i: a)
            best = max(best,i);
    }

    /*
    for (int i = 1; i <= n; i++) {

        vi factors;
        for (int f = 1; f <= sqrt(i); f++) {
            if (i%f == 0) {
                factors.push_back(f);
                factors.push_back(i/f);
            }
        }

        for (ll j: factors) {
            if (j <= m)
                if (j-1 < (int)q[i/j].size())
                    best = max(best, q[i/j][j-1]);
        }

    }
    */

    cout << best << endl;

}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define INF (2 << 31)
#define INFLL ((ll)(1e18)+7)
#define endl "\n"
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))

typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pii;
typedef vector<pii> vii;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    ll n, m;
    cin >> n >> m;
    vvi cum(100001, vi());

    for (int i = 0; i < n; i++) {
        ll s, r;
        cin >> s >> r;
        cum[s].push_back(r);
    }

    for (int i = 0; i <= m; i++) {
        sort(cum[i].begin(), cum[i].end(), greater<ll>());
    }

    for (int i = 0; i <= m; i++) {
        for (int j = 1; j < (int)cum[i].size(); j++) {
            cum[i][j] += cum[i][j-1];
        }
    }

    vvi q(100001, vi());
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j <= m; j++) {
            if ((int)cum[j].size() >= i)
                q[i].push_back(cum[j][i-1]);
        }
        if (q[i].empty()) break;
    }

    for (int i = 0; i < 100001; i++) {
        sort(q[i].begin(), q[i].end(), greater<ll>());
    }

    for (int i = 0 ;i  <100001; i++) {
        for (int j = 1; j < (int)q[i].size(); j++) {
            q[i][j] += max(0,q[i][j-1]);
        }
    }

    ll best = 0;

    for (vi a: q) {
        for (auto i: a)
            best = max(best,i);
    }

    /*
    for (int i = 1; i <= n; i++) {

        vi factors;
        for (int f = 1; f <= sqrt(i); f++) {
            if (i%f == 0) {
                factors.push_back(f);
                factors.push_back(i/f);
            }
        }

        for (ll j: factors) {
            if (j <= m)
                if (j-1 < (int)q[i/j].size())
                    best = max(best, q[i/j][j-1]);
        }

    }
    */

    cout << best << endl;

}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define INF (2 << 31)
#define INFLL ((ll)(1e18)+7)
#define endl "\n"
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))

typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pii;
typedef vector<pii> vii;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    ll n, m;
    cin >> n >> m;
    vvi cum(100000, vi());

    for (int i = 0; i < n; i++) {
        ll s, r;
        cin >> s >> r;
        cum[s].push_back(r);
    }

    for (int i = 0; i < 100000; i++) {
        sort(cum[i].begin(), cum[i].end(), greater<ll>());
    }

    for (int i = 0; i < 100000; i++) {
        for (int j = 1; j < (int)cum[i].size(); j++) {
            cum[i][j] += max(cum[i][j-1], 0);
        }
    }

    // TODO: TLE
    vvi q(100001, vi());
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j < 100000; j++) {
            if ((int)cum[j].size() >= i)
                q[i].push_back(cum[j][i-1]);
        }
    }

    for (int i = 0; i < 100001; i++) {
        sort(q[i].begin(), q[i].end(), greater<ll>());
    }

    for (int i = 0 ;i  <100001; i++) {
        for (int j = 1; j < (int)q[i].size(); j++) {
            q[i][j] += q[i][j-1];
        }
    }

    ll best = 0;

    for (vi a: q) {
        for (auto i: a)
            best = max(best,i);
    }

    /*
    for (int i = 1; i <= n; i++) {

        vi factors;
        for (int f = 1; f <= sqrt(i); f++) {
            if (i%f == 0) {
                factors.push_back(f);
                factors.push_back(i/f);
            }
        }

        for (ll j: factors) {
            if (j <= m)
                if (j-1 < (int)q[i/j].size())
                    best = max(best, q[i/j][j-1]);
        }

    }
    */

    cout << best << endl;

}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define INF (2 << 31)
#define INFLL ((ll)(1e18)+7)
#define endl "\n"
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))

typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pii;
typedef vector<pii> vii;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    ll n, m;
    cin >> n >> m;
    vvi cum(100000, vi());

    for (int i = 0; i < n; i++) {
        ll s, r;
        cin >> s >> r;
        cum[s].push_back(r);
    }

    for (int i = 0; i < 100000; i++) {
        sort(cum[i].begin(), cum[i].end(), greater<ll>());
    }

    for (int i = 0; i < 100000; i++) {
        for (int j = 1; j < (int)cum[i].size(); j++) {
            cum[i][j] += cum[i][j-1];
        }
    }

    // TODO: TLE
    vvi q(100001, vi());
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j < 100000; j++) {
            if ((int)cum[j].size() >= i)
                q[i].push_back(cum[j][i-1]);
        }
        if (q[i].empty()) break;
    }

    for (int i = 0; i < 100001; i++) {
        sort(q[i].begin(), q[i].end(), greater<ll>());
    }

    for (int i = 0 ;i  <100001; i++) {
        for (int j = 1; j < (int)q[i].size(); j++) {
            q[i][j] += q[i][j-1];
        }
    }

    ll best = 0;

    for (vi a: q) {
        for (auto i: a)
            best = max(best,i);
    }

    /*
    for (int i = 1; i <= n; i++) {

        vi factors;
        for (int f = 1; f <= sqrt(i); f++) {
            if (i%f == 0) {
                factors.push_back(f);
                factors.push_back(i/f);
            }
        }

        for (ll j: factors) {
            if (j <= m)
                if (j-1 < (int)q[i/j].size())
                    best = max(best, q[i/j][j-1]);
        }

    }
    */

    cout << best << endl;

}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define INF (2 << 31)
#define INFLL ((ll)(1e18)+7)
#define endl "\n"
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))

typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pii;
typedef vector<pii> vii;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    ll n, m;
    cin >> n >> m;
    vvi cum(100001, vi());

    for (int i = 0; i < n; i++) {
        ll s, r;
        cin >> s >> r;
        cum[s].push_back(r);
    }

    for (int i = 0; i < m; i++) {
        sort(cum[i].begin(), cum[i].end(), greater<ll>());
    }

    for (int i = 0; i < m; i++) {
        for (int j = 1; j < (int)cum[i].size(); j++) {
            cum[i][j] += cum[i][j-1];
        }
    }

    vvi q(100001, vi());
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j < m; j++) {
            if ((int)cum[j].size() >= i)
                q[i].push_back(cum[j][i-1]);
        }
        if (q[i].empty()) break;
    }

    for (int i = 0; i < 100001; i++) {
        sort(q[i].begin(), q[i].end(), greater<ll>());
    }

    for (int i = 0 ;i  <100001; i++) {
        for (int j = 1; j < (int)q[i].size(); j++) {
            q[i][j] += max(0,q[i][j-1]);
        }
    }

    ll best = 0;

    for (vi a: q) {
        for (auto i: a)
            best = max(best,i);
    }

    /*
    for (int i = 1; i <= n; i++) {

        vi factors;
        for (int f = 1; f <= sqrt(i); f++) {
            if (i%f == 0) {
                factors.push_back(f);
                factors.push_back(i/f);
            }
        }

        for (ll j: factors) {
            if (j <= m)
                if (j-1 < (int)q[i/j].size())
                    best = max(best, q[i/j][j-1]);
        }

    }
    */

    cout << best << endl;

}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define INF (2 << 31)
#define INFLL ((ll)(1e18)+7)
#define endl "\n"
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))

typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pii;
typedef vector<pii> vii;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    ll n, m;
    cin >> n >> m;
    vvi cum(100000, vi());

    for (int i = 0; i < n; i++) {
        ll s, r;
        cin >> s >> r;
        cum[s].push_back(r);
    }

    for (int i = 0; i < 100000; i++) {
        sort(cum[i].begin(), cum[i].end(), greater<ll>());
    }

    for (int i = 0; i < 100000; i++) {
        for (int j = 1; j < (int)cum[i].size(); j++) {
            cum[i][j] += cum[i][j-1];
        }
    }

    vvi q(100001, vi());
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j < 100000; j++) {
            if ((int)cum[j].size() >= i)
                q[i].push_back(cum[j][i-1]);
        }
        if (q[i].empty()) break;
    }

    for (int i = 0; i < 100001; i++) {
        sort(q[i].begin(), q[i].end(), greater<ll>());
    }

    for (int i = 0 ;i  <100001; i++) {
        for (int j = 1; j < (int)q[i].size(); j++) {
            q[i][j] += q[i][j-1];
        }
    }

    ll best = 0;

    for (int i = 1; i <= n; i++) {

        vi factors;
        for (int f = 1; f <= sqrt(i); f++) {
            if (i%f == 0) {
                factors.push_back(f);
                factors.push_back(i/f);
            }
        }

        for (ll j: factors) {
            if (j-1 < (int)q[i/j].size())
                best = max(best, q[i/j][j-1]);
        }

    }

    cout << best << endl;

}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define INF (2 << 31)
#define INFLL ((ll)(1e18)+7)
#define endl "\n"
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))

typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pii;
typedef vector<pii> vii;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    ll n, m;
    cin >> n >> m;
    vvi cum(100000, vi());

    for (int i = 0; i < n; i++) {
        ll s, r;
        cin >> s >> r;
        cum[s].push_back(r);
    }

    for (int i = 0; i < 100000; i++) {
        sort(cum[i].begin(), cum[i].end(), greater<ll>());
    }

    for (int i = 0; i < 100000; i++) {
        for (int j = 1; j < (int)cum[i].size(); j++) {
            cum[i][j] += cum[i][j-1];
        }
    }

    vvi q(100001, vi());
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j < 100000; j++) {
            if ((int)cum[j].size() >= i)
                q[i].push_back(cum[j][i-1]);
        }
        if (q[i].empty()) break;
    }

    for (int i = 0; i < 100001; i++) {
        sort(q[i].begin(), q[i].end(), greater<ll>());
    }

    for (int i = 0 ;i  <100001; i++) {
        for (int j = 1; j < (int)q[i].size(); j++) {
            q[i][j] += q[i][j-1];
        }
    }

    ll best = 0;

    for (int i = 1; i <= n; i++) {

        priority_queue<ll, vi, greater<ll>> factors;
        for (int f = 1; f <= sqrt(i); f++) {
            if (i%f == 0) {
                factors.push(f);
                factors.push(i/f);
            }
        }

        while (!factors.empty()) {
            ll j = factors.top(); factors.pop();
            if (i-1 < (int)q[i/j].size())
                best = max(best, q[i/j][i-1]);
        }

    }

    cout << best << endl;

}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define INF (2 << 31)
#define INFLL ((ll)(1e18)+7)
#define endl "\n"
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))

typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pii;
typedef vector<pii> vii;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    ll n, m;
    cin >> n >> m;
    vvi cum(100000, vi());

    for (int i = 0; i < n; i++) {
        ll s, r;
        cin >> s >> r;
        cum[s].push_back(r);
    }

    for (int i = 0; i < 100000; i++) {
        sort(cum[i].begin(), cum[i].end(), greater<ll>());
    }

    for (int i = 0; i < 100000; i++) {
        for (int j = 1; j < (int)cum[i].size(); j++) {
            cum[i][j] += cum[i][j-1];
        }
    }

    // TODO: TLE
    vvi q(100001, vi());
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j < 100000; j++) {
            if ((int)cum[j].size() >= i)
                q[i].push_back(cum[j][i-1]);
        }
        if (q[i].empty()) break;
    }

    for (int i = 0; i < 100001; i++) {
        sort(q[i].begin(), q[i].end(), greater<ll>());
    }

    for (int i = 0 ;i  <100001; i++) {
        for (int j = 1; j < (int)q[i].size(); j++) {
            q[i][j] += max(0,q[i][j-1]);
        }
    }

    ll best = 0;

    for (vi a: q) {
        for (auto i: a)
            best = max(best,i);
    }

    /*
    for (int i = 1; i <= n; i++) {

        vi factors;
        for (int f = 1; f <= sqrt(i); f++) {
            if (i%f == 0) {
                factors.push_back(f);
                factors.push_back(i/f);
            }
        }

        for (ll j: factors) {
            if (j <= m)
                if (j-1 < (int)q[i/j].size())
                    best = max(best, q[i/j][j-1]);
        }

    }
    */

    cout << best << endl;

}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define INF (2 << 31)
#define INFLL ((ll)(1e18)+7)
#define endl "\n"
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))

typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pii;
typedef vector<pii> vii;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    ll n, m;
    cin >> n >> m;
    vvi cum(100001, vi());

    for (int i = 0; i < n; i++) {
        ll s, r;
        cin >> s >> r;
        cum[s].push_back(r);
    }

    for (int i = 0; i <= m; i++) {
        sort(cum[i].begin(), cum[i].end(), greater<ll>());
    }

    for (int i = 0; i <= m; i++) {
        for (int j = 1; j < (int)cum[i].size(); j++) {
            cum[i][j] += cum[i][j-1];
        }
    }

    vvi q(100001, vi());

    for (int i = 0; i <= m; i++)
    {
        for (int j = 0; j < (int)cum[i].size(); j++) {
            q[j].push_back(cum[i][j]);
        }
    }

    for (int i = 0; i < 100001; i++) {
        sort(q[i].begin(), q[i].end(), greater<ll>());
    }

    for (int i = 0 ;i  <100001; i++) {
        for (int j = 1; j < (int)q[i].size(); j++) {
            q[i][j] += max(0,q[i][j-1]);
        }
    }

    ll best = 0;

    for (int i = 0; i <= m; i++) {
        vi a = q[i];
        for (auto i: a)
            best = max(best,i);
    }

    /*
    for (int i = 1; i <= n; i++) {

        vi factors;
        for (int f = 1; f <= sqrt(i); f++) {
            if (i%f == 0) {
                factors.push_back(f);
                factors.push_back(i/f);
            }
        }

        for (ll j: factors) {
            if (j <= m)
                if (j-1 < (int)q[i/j].size())
                    best = max(best, q[i/j][j-1]);
        }

    }
    */

    cout << best << endl;

}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define INF (2 << 31)
#define INFLL ((ll)(1e18)+7)
#define endl "\n"
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))

typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pii;
typedef vector<pii> vii;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    ll n, m;
    cin >> n >> m;
    vvi cum(100000, vi());

    for (int i = 0; i < n; i++) {
        ll s, r;
        cin >> s >> r;
        cum[s].push_back(r);
    }

    for (int i = 0; i < 100000; i++) {
        sort(cum[i].begin(), cum[i].end(), greater<ll>());
    }

    for (int i = 0; i < 100000; i++) {
        for (int j = 1; j < (int)cum[i].size(); j++) {
            cum[i][j] += cum[i][j-1];
        }
    }

    // TODO: TLE
    vvi q(100001, vi());
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j < 100000; j++) {
            if ((int)cum[j].size() >= i)
                q[i].push_back(cum[j][i-1]);
        }
    }

    for (int i = 0; i < 100001; i++) {
        sort(q[i].begin(), q[i].end(), greater<ll>());
    }

    for (int i = 0 ;i  <100001; i++) {
        for (int j = 1; j < (int)q[i].size(); j++) {
            q[i][j] += q[i][j-1];
        }
    }

    ll best = 0;

    for (vi a: q) {
        for (auto i: a)
            best = max(best,i);
    }

    /*
    for (int i = 1; i <= n; i++) {

        vi factors;
        for (int f = 1; f <= sqrt(i); f++) {
            if (i%f == 0) {
                factors.push_back(f);
                factors.push_back(i/f);
            }
        }

        for (ll j: factors) {
            if (j <= m)
                if (j-1 < (int)q[i/j].size())
                    best = max(best, q[i/j][j-1]);
        }

    }
    */

    cout << best << endl;

}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define INF (2 << 31)
#define INFLL ((ll)(1e18)+7)
#define endl "\n"
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))

typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pii;
typedef vector<pii> vii;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    ll n, m;
    cin >> n >> m;
    vvi cum(100001, vi());

    for (int i = 0; i < n; i++) {
        ll s, r;
        cin >> s >> r;
        cum[s].push_back(r);
    }

    for (int i = 0; i <= m; i++) {
        sort(cum[i].begin(), cum[i].end(), greater<ll>());
    }

    for (int i = 0; i <= m; i++) {
        for (int j = 1; j < (int)cum[i].size(); j++) {
            cum[i][j] += cum[i][j-1];
        }
    }

    vvi q(100001, vi());

    for (int i = 0; i <= m; i++)
    {
        for (int j = 0; j < (int)cum[i].size(); j++) {
            q[j].push_back(cum[i][j]);
        }
    }

    for (int i = 0; i < 100001; i++) {
        sort(q[i].begin(), q[i].end(), greater<ll>());
    }

    for (int i = 0 ;i  <100001; i++) {
        for (int j = 1; j < (int)q[i].size(); j++) {
            q[i][j] += max(0,q[i][j-1]);
        }
    }

    ll best = 0;

    for (auto a: q) {
        for (auto i: a)
            best = max(best,i);
    }

    /*
    for (int i = 1; i <= n; i++) {

        vi factors;
        for (int f = 1; f <= sqrt(i); f++) {
            if (i%f == 0) {
                factors.push_back(f);
                factors.push_back(i/f);
            }
        }

        for (ll j: factors) {
            if (j <= m)
                if (j-1 < (int)q[i/j].size())
                    best = max(best, q[i/j][j-1]);
        }

    }
    */

    cout << best << endl;

}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define INF (2 << 31)
#define INFLL ((ll)(1e18)+7)
#define endl "\n"
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))

typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pii;
typedef vector<pii> vii;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    ll n, m;
    cin >> n >> m;
    vvi cum(100001, vi());

    for (int i = 0; i < n; i++) {
        ll s, r;
        cin >> s >> r;
        cum[s].push_back(r);
    }

    for (int i = 0; i < 100001; i++) {
        sort(cum[i].begin(), cum[i].end(), greater<ll>());
    }

    for (int i = 0; i < 100001; i++) {
        for (int j = 1; j < (int)cum[i].size(); j++) {
            cum[i][j] += cum[i][j-1];
        }
    }

    vvi q(100001, vi());
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j < 100001; j++) {
            if ((int)cum[j].size() >= i)
                q[i].push_back(cum[j][i-1]);
        }
        if (q[i].empty()) break;
    }

    for (int i = 0; i < 100001; i++) {
        sort(q[i].begin(), q[i].end(), greater<ll>());
    }

    for (int i = 0 ;i  <100001; i++) {
        for (int j = 1; j < (int)q[i].size(); j++) {
            q[i][j] += max(0,q[i][j-1]);
        }
    }

    ll best = 0;

    for (vi a: q) {
        for (auto i: a)
            best = max(best,i);
    }

    /*
    for (int i = 1; i <= n; i++) {

        vi factors;
        for (int f = 1; f <= sqrt(i); f++) {
            if (i%f == 0) {
                factors.push_back(f);
                factors.push_back(i/f);
            }
        }

        for (ll j: factors) {
            if (j <= m)
                if (j-1 < (int)q[i/j].size())
                    best = max(best, q[i/j][j-1]);
        }

    }
    */

    cout << best << endl;

}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define INF (2 << 31)
#define INFLL ((ll)(1e18)+7)
#define endl "\n"
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))

typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pii;
typedef vector<pii> vii;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    ll n, m;
    cin >> n >> m;
    vvi cum(100000, vi());

    for (int i = 0; i < n; i++) {
        ll s, r;
        cin >> s >> r;
        cum[s].push_back(r);
    }

    for (int i = 0; i < 100000; i++) {
        sort(cum[i].begin(), cum[i].end(), greater<ll>());
    }

    for (int i = 0; i < 100000; i++) {
        for (int j = 1; j < (int)cum[i].size(); j++) {
            cum[i][j] += cum[i][j-1];
        }
    }

    vvi q(100001, vi());
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j < 100000; j++) {
            if ((int)cum[j].size() >= i)
                q[i].push_back(cum[j][i-1]);
        }
        if (q[i].empty()) break;
    }

    for (int i = 0; i < 100001; i++) {
        sort(q[i].begin(), q[i].end(), greater<ll>());
    }

    for (int i = 0 ;i  <100001; i++) {
        for (int j = 1; j < (int)q[i].size(); j++) {
            q[i][j] += q[i][j-1];
        }
    }

    ll best = 0;

    for (int i = 1; i <= n; i++) {

        priority_queue<ll, vi, greater<ll>> factors;
        for (int f = 1; f <= sqrt(i); f++) {
            if (i%f == 0) {
                factors.push(f);
                factors.push(i/f);
            }
        }

        while (!factors.empty()) {
            ll j = factors.top(); factors.pop();
            if (!q[j].empty())
                best = max(best, q[j][min(i-1, (int)q[j].size()-1)]);
        }

    }

    cout << best << endl;

}


#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define INF (2 << 31)
#define INFLL ((ll)(1e18)+7)
#define endl "\n"
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))

typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pii;
typedef vector<pii> vii;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    ll n, m;
    cin >> n >> m;
    vvi cum(100000, vi());

    for (int i = 0; i < n; i++) {
        ll s, r;
        cin >> s >> r;
        cum[s].push_back(r);
    }

    for (int i = 0; i < 100000; i++) {
        sort(cum[i].begin(), cum[i].end(), greater<ll>());
    }

    for (int i = 0; i < 100000; i++) {
        for (int j = 1; j < (int)cum[i].size(); j++) {
            cum[i][j] += cum[i][j-1];
        }
    }

    vvi q(100001, vi());
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j < 100000; j++) {
            if ((int)cum[j].size() >= i)
                q[i].push_back(cum[j][i-1]);
        }
        if (q[i].empty()) break;
    }

    for (int i = 0; i < 100001; i++) {
        sort(q[i].begin(), q[i].end(), greater<ll>());
    }

    for (int i = 0 ;i  <100001; i++) {
        for (int j = 1; j < (int)q[i].size(); j++) {
            q[i][j] += q[i][j-1];
        }
    }

    ll best = 0;

    for (int i = 1; i <= n; i++) {

        vi factors;
        for (int f = 1; f <= sqrt(i); f++) {
            if (i%f == 0) {
                factors.push_back(f);
                factors.push_back(i/f);
            }
        }

        for (ll j: factors) {
            if (j <= m)
                if (j-1 < (int)q[i/j].size())
                    best = max(best, q[i/j][j-1]);
        }

    }

    cout << best << endl;

}


#include<bits/stdc++.h>
#include<map>
#define MOD 1000000007
#define NO_OF_CHARS 256
#define MAXN 100005
#define mp make_pair
#define vi vector<int>
#define pb push_back
using namespace std ;

int main(){
   int n , m ;
    cin >> n >> m ;
   int x , y ;
   int count[100005];
   long long ans = 0 ;
   map<long, long> umap ;
   map<long, long> sum  ;
   memset( count , 0, sizeof(count));
   for(int i=0 ; i<n ;i++){
   	  cin >> x >> y ;
   	  if( y>=0){
   	  count[x]++ ;
   	  sum[x]+= y ;
   	  umap[count[x]] += sum[x] ;
   } }
   map<long, long>::iterator it ;
    for( it = umap.begin(); it != umap.end(); it++){
           if( ans <= it->second)
            ans = it->second ;
      }
        cout << ans  ;
	return 0 ;
}


#include<bits/stdc++.h>
#include<map>
#define MOD 1000000007
#define NO_OF_CHARS 256
#define MAXN 100005
#define mp make_pair
#define vi vector<int>
#define pb push_back
using namespace std ;

int main(){
   int n , m ;
    cin >> n >> m ;
   int x , y ;
   int count[100005];
   long long ans = 0 ;
   map<long, long> umap ;
   map<long, long> sum  ;
   memset( count , 0, sizeof(count));
   for(int i=0 ; i<n ;i++){
   	  cin >> x >> y ;
   	  count[x]++ ;
   	  if( y >= 0)
   	  sum[x]+= y ;
   	  umap[count[x]] += sum[x] ;
   }
   map<long, long>::iterator it ;
    for( it = umap.begin(); it != umap.end(); it++){
           if( ans <= it->second)
            ans = it->second ;
      }
        cout << ans  ;
	return 0 ;
}


#include<bits/stdc++.h>
#include<map>
#define MOD 1000000007
#define NO_OF_CHARS 256
#define MAXN 100005
#define mp make_pair
#define vi vector<int>
#define pb push_back
using namespace std ;

int main(){
   int n , m ;
    cin >> n >> m ;
   int x , y ;
   int count[100005];
   long long ans = 0 ;
   map<long, long> umap ;
   map<long, long> sum  ;
   memset( count , 0, sizeof(count));
   for(int i=0 ; i<n ;i++){
   	  cin >> x >> y ;

   	  count[x]++ ;
   	  if( y>=0){
   	  sum[x]+= y ;
   	  umap[count[x]] += sum[x] ;
   } }
   map<long, long>::iterator it ;
    for( it = umap.begin(); it != umap.end(); it++){
           if( ans <= it->second)
            ans = it->second ;
      }
        cout << ans  ;
	return 0 ;
}


#include<bits/stdc++.h>
#include<map>
#define MOD 1000000007
#define NO_OF_CHARS 256
#define MAXN 100005
#define mp make_pair
#define vi vector<int>
#define pb push_back
using namespace std ;

int main(){
   int n , m ;
    cin >> n >> m ;
   int x , y ;
   int count[100005];
   long long ans = 0 ;
   map<long, long> umap ;
   map<long, long> sum  ;
   memset( count , 0, sizeof(count));
   for(int i=0 ; i<n ;i++){
   	  cin >> x >> y ;
   	  count[x]++ ;
   	  sum[x]+= y ;
   	  umap[count[x]] += sum[x] ;
   }
   map<long, long>::iterator it ;
    for( it = umap.begin(); it != umap.end(); it++){
           if( ans <= it->second)
            ans = it->second ;
      }
        cout << ans  ;
	return 0 ;
}


#include<bits/stdc++.h>
#include<map>
#define MOD 1000000007
#define NO_OF_CHARS 256
#define MAXN 100005
#define mp make_pair
#define vi vector<int>
#define pb push_back
using namespace std ;

int main(){
   int n , m ;
    cin >> n >> m ;
   int x , y ;
   int count[100005];
   long long ans = 0 ;
   map<long, long> umap ;
   map<long, long> sum  ;
   memset( count , 0, sizeof(count));
   for(int i=0 ; i<n ;i++){
   	  cin >> x >> y ;

   	  count[x]++ ;
   	  sum[x] += y;
   	  if( y >=0){
   	  umap[count[x]] += sum[x] ;
   } }
   map<long, long>::iterator it ;
    for( it = umap.begin(); it != umap.end(); it++){
           if( ans <= it->second)
            ans = it->second ;
      }
        cout << ans  ;
	return 0 ;
}


#include<bits/stdc++.h>
#include<map>
#define MOD 1000000007
#include <iterator>
#define NO_OF_CHARS 256
#define MAXN 100005
#define mp make_pair
#define vi vector<int>
#define pb push_back
using namespace std ;

int main(){
   int n , m ;
   cin >> n >> m ;
   int x , y ;
   int count[100005];
   long long ans = 0 ;
   map<long, long> umap ;
   map<long, long> sum  ;
   memset( count , 0, sizeof(count));
   for(int i=0 ; i<n ;i++){
   	  cin >> x >> y ;
   	  count[x]++ ;
   	  if( y >=0)
   	  sum[x]+= y ;
   	  umap[count[x]] += sum[x] ;
   }
      map<long, long>::iterator it ;
    for( it = umap.begin(); it != umap.end(); ++it){
           if( ans <= it->second)
            ans = it->second ;
      }
        cout << ans  ;
	return 0 ;
}


#include<bits/stdc++.h>
#include<map>
#define MOD 1000000007
#define NO_OF_CHARS 256
#define MAXN 100005
#define mp make_pair
#define vi vector<int>
#define pb push_back
using namespace std ;

int main(){
   int n , m ;
    cin >> n >> m ;
   int x , y ;
   int count[100005];
   long long ans = 0 ;
   map<long, long> umap ;
   map<long, long> sum  ;
   memset( count , 0, sizeof(count));
   for(int i=0 ; i<n ;i++){
   	  cin >> x >> y ;
   	  if( y >=0){
   	  count[x]++ ;
   	  sum[x] += y;
   	  umap[count[x]] += sum[x] ;
   } }
   map<long, long>::iterator it ;
    for( it = umap.begin(); it != umap.end(); it++){
           if( ans <= it->second)
            ans = it->second ;
      }
        cout << ans  ;
	return 0 ;
}


#include<bits/stdc++.h>
using namespace std;

#define fore(i, l, r) for(int i = int(l); i < int(r); i++)
#define sz(a) int((a).size())

int n, m;
vector<int> s, r;

inline bool read() {
	if(!(cin >> n >> m))
		return false;
	s.assign(n, 0);
	r.assign(n, 0);

	fore(i, 0, n) {
		assert(cin >> s[i] >> r[i]);
		s[i]--;
	}
	return true;
}

vector< vector<int> > subs;

inline void solve() {
	subs.assign(m + 1, vector<int>());

	fore(i, 0, n)
		subs[s[i]].push_back(r[i]);

	fore(id, 0, sz(subs)) {
		sort(subs[id].begin(), subs[id].end());
		reverse(subs[id].begin(), subs[id].end());
	}

	vector<int> mx(n + 5, 0);
	fore(id, 0, sz(subs)) {
		int curSum = 0;
		fore(i, 0, sz(subs[id])) {
			curSum += subs[id][i];
			if(curSum < 0)
				break;

			mx[i + 1] += curSum;
		}
	}

	cout << *max_element(mx.begin(), mx.end()) << endl;
}

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	int tt = clock();
#endif
	cout << fixed << setprecision(15);

	if(read()) {
		solve();

#ifdef _DEBUG
		cerr << "TIME = " << clock() - tt << endl;
		tt = clock();
#endif
	}
	return 0;
}


#include<bits/stdc++.h>
#include<map>
#define MOD 1000000007
#define NO_OF_CHARS 256
#define MAXN 100005
#define  mp make_pair
#define vi vector<int>
#define pb push_back
using namespace std ;

int main(){
   int n , m ;
    cin >> n >> m
   vector<pair<int , int>> input ;
   int x , y ;
   int count[100005]; long long ans = 0 ;
   map<long, long> umap ;
   map<long, long> sum = 0 ;
   memset( count , 0, sizeof(count));
   for(int i=0 ; i<n ;i++){
   	  cin >> x >> y ;
   	  input.push_back({x,y});
   	  if(y >=0){
	  count[x]++ ;
   	  sum[x]+= y
   	  umap[count[x]] += sum[x] ;
   } }
   map<long, long>::iterator it
    for( auto it = umap.begin(); it != umap.end(); it++)
       ans = max( ans , it->second);
        cout << ans  ;
	return 0 ;
}


#include<bits/stdc++.h>
#include<map>
#define MOD 1000000007
#define NO_OF_CHARS 256
#define MAXN 100005
#define mp make_pair
#define vi vector<int>
#define pb push_back
using namespace std ;

int main(){
   int n , m ;
    cin >> n >> m ;
   int x , y ;
   int count[100005];
   long long ans = 0 ;
   map<long, long> umap ;
   map<long, long> sum  ;
   memset( count , 0, sizeof(count));
   for(int i=0 ; i<n ;i++){
   	  cin >> x >> y ;
   	  count[x]++ ;
   	  sum[x] += y;
   	  umap[count[x]] += sum[x] ;
   }
   map<long, long>::iterator it ;
    for(it = umap.begin(); it != umap.end(); it++){
           if( ans <= it->second)
            ans = it->second ;
            if( it->second < 0){
               cout << "0" ;
               return 0 ;
            }

      }
        cout << ans  ;
	return 0 ;
}


#include<bits/stdc++.h>
using namespace std;
using ll = long long;

//ll sum[10001][10001];

bool cmp(pair<ll,ll>a,pair<ll,ll>b)
{
	//if(a.first!=b.first)return a.first<b.first;
	return a.second>b.second;
}

bool cmp2(vector<ll> a,vector<ll> b)
{
	if(a.size()!=b.size()) a.size()>b.size();
	return a.back()>b.back();

}

int main()
{
	ll n,m;
	cin >> n >> m;
	vector<pair<ll,ll>>vp(n);
	for(ll i = 0;i<n;i++)
	{
		ll s,r;
		cin >> s >> r;
		s--;
		vp[i] = make_pair(s,r);
	}
	sort(vp.begin(),vp.end(),cmp);
	vector<vector<ll>> g(m,vector<ll>(1));
	ll mxsz=0;
	for(ll i =0;i<n;i++)
	{
		//if(vp[i].second>0)
		g[vp[i].first].push_back(vp[i].second+g[vp[i].first].back());
		mxsz = max(mxsz,(ll)g[vp[i].first].size());
	}
	//cout<<1<<endl;
	sort(g.begin(),g.end(),cmp2);
	ll ans = 0;
	for(ll i = 0;i<mxsz;i++)
	{
		ll now = 0;
		vector<ll> hoge;
		for(ll j = 0;j<m;j++)
		{

			if(i<g[j].size())
			{
				if(g[j][i]>0)
				now += g[j][i];
			}

			if(g[j].size()<i)break;

		}
		ans = max(ans,now);
	}
	cout<<max(0LL,ans)<<endl;
}


#include<bits/stdc++.h>
using namespace std;
using ll = long long;

//ll sum[10001][10001];

bool cmp(pair<ll,ll>a,pair<ll,ll>b)
{
	//if(a.first!=b.first)return a.first<b.first;
	return a.second>b.second;
}

bool cmp2(vector<ll> &a,vector<ll> &b)
{
	if(a.size()!=b.size()) a.size()>=b.size();
	return a.back()>b.back();

}

int main()
{
	ll n,m;
	cin >> n >> m;
	vector<pair<ll,ll>>vp(n);
	for(ll i = 0;i<n;i++)
	{
		ll s,r;
		cin >> s >> r;
		s--;
		vp[i] = make_pair(s,r);
	}
	sort(vp.begin(),vp.end(),cmp);
	vector<vector<ll>> g(m,vector<ll>(1));
	ll mxsz=0;
	for(ll i =0;i<n;i++)
	{
		//if(vp[i].second>0)
		g[vp[i].first].push_back(vp[i].second+g[vp[i].first].back());
		mxsz = max(mxsz,(ll)g[vp[i].first].size());
	}
	//cout<<1<<endl;
	sort(g.begin(),g.end(),cmp2);
	//swap(g[0],g[1]);
	//for(auto i:g)cout<<i.size()<<endl;
	ll ans = 0;
	for(ll i = 0;i<mxsz;i++)
	{
		ll now = 0;
		vector<ll> hoge;
		for(ll j = 0;j<m;j++)
		{

			if(i<g[j].size())
			{
				if(g[j][i]>0)
				now += g[j][i];
			}

			if(g[j].size()<i)break;

		}
		ans = max(ans,now);
	}
	cout<<max(0LL,ans)<<endl;
}


#include<bits/stdc++.h>
using namespace std;
using ll = long long;

//ll sum[10001][10001];

bool cmp(pair<ll,ll>a,pair<ll,ll>b)
{
	if(a.first!=b.first)return a.first<b.first;
	return a.second>b.second;
}

bool cmp2(vector<ll> a,vector<ll> b)
{
	if(a.size()!=b.size()) a.size()>b.size();
	return a.back()>b.back();

}

int main()
{
	ll n,m;
	cin >> n >> m;
	vector<pair<ll,ll>>vp(n);
	for(ll i = 0;i<n;i++)
	{
		ll s,r;
		cin >> s >> r;
		s--;
		vp[i] = make_pair(s,r);
	}
	sort(vp.begin(),vp.end(),cmp);
	vector<vector<ll>> g(m,vector<ll>(1));
	ll mxsz=0;
	for(ll i =0;i<n;i++)
	{
		g[vp[i].first].push_back(vp[i].second+g[vp[i].first].back());
		mxsz = max(mxsz,(ll)g[vp[i].first].size());
	}
	//cout<<1<<endl;
	sort(g.begin(),g.end(),cmp2);
	ll ans = 0;
	for(ll i = 0;i<mxsz;i++)
	{
		ll now = 0;
		for(ll j = 0;j<m;j++)
		{

			if(i<g[j].size())
			{
				if(g[j][i]>0)
				now += g[j][i];
			}
			else
			{
				break;
			}

		}
		ans = max(ans,now);
	}
	cout<<max(0LL,ans)<<endl;
}


import java.util.*;
import java.io.*;
import java.math.*;

public class Main {
    static PrintWriter out;
    static Reader in;
    public static void main(String[] args) throws IOException {
        //out = new PrintWriter(new File("out.txt"));
        //PrintWriter out = new PrintWriter(System.out);
        //in = new Reader(new FileInputStream("in.txt"));
        //Reader in = new Reader();
        input_output();
        Main solver = new Main();
        solver.solve();
        out.flush();
        out.close();

    }

    static int INF = (int)2e9+5;
    static int maxn = (int)1e5+5;
    static int mod=(int)1e9+7 ;
    static int n, m, t, q, k;

    void solve() throws IOException{
        n = in.nextInt();
        m = in.nextInt();

        int x = in.nextInt();
        k = in.nextInt();
        int y = in.nextInt();

        int[] arr = new int[n];
        for (int i = 0; i < n; i++) arr[i] = in.nextInt();
        int[] tar = new int[m];
        for (int i = 0; i < m; i++) tar[i] = in.nextInt();

        long ans = 0;

        ArrayList<Integer> list = new ArrayList<>();
        long tmp = 0;
        int cur = 0, max = 0;
        for (int i = 0; i < n; i++) {
            if (cur < m && tar[cur] == arr[i]) {
                if (list.size() == 0) {
                    max = tar[cur];
                    cur++;
                    continue;
                } else {
                    max = Math.max(max, tar[cur]);
                    tmp = func(x, k, y, max, list);
                    if (tmp == -1) {
                        out.println(-1);
                        return;
                    } else {
                        ans += tmp;
                    }
                    max = tar[cur];
                    cur++;
                    list.clear();
                    continue;
                }
            } else {
                list.add(arr[i]);
            }
        }
        if (list.size() != 0) {
            tmp = func(x, k, y, max, list);
            if (tmp == -1) {
                out.println(-1);
                return;
            } else ans+= tmp;
        }

        if (cur != m) out.println(-1);
        else out.println(ans);
    }

    //<>

    static long func(int x, int k, int y, int max, ArrayList<Integer> arr) {
        int size = arr.size();
        int top = 0;
        if (size == 0) return 0;
        for (int i = 0; i < size; i++) top = Math.max(top, arr.get(i));

        if ((long)x < (long)y*k) {
            if (size < k) {
                if (top > max) return -1;
                else return (long)y*size;
            }
            int left = 0;
            int i = 0;
            for (i = 0; i < size-k; i++) {
                if (arr.get(i) > max) {
                    i+=k-1;
                } else {
                    left++;
                }
            }
            int remain = size-i;
            left += remain;
            for (; i < size; i++) {
                if (arr.get(i) > max) {
                    left-=k;
                    break;
                }
            }

            if (left < size%k) return -1;
            else return (long)x*(size/k)+(long)y*(size%k);
        } else {
            if (top > max) {
                if (size < k) return -1;
                else return x+(long)(size-k)*(y);
            } else {
                return (long)size*y;
            }
        }
    }

    static class Reader {

        private InputStream mIs;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

        public Reader() {
            this(System.in);
        }

        public Reader(InputStream is) {
            mIs = is;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();

        }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = mIs.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public String nextLine() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isEndOfLine(c));
            return res.toString();
        }

        public String next() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        public long nextLong() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public boolean isEndOfLine(int c) {
            return c == '\n' || c == '\r' || c == -1;
        }

    }
    static void input_output() throws IOException {
        File f = new File("in.txt");
        if(f.exists() && !f.isDirectory()) {
            in = new Reader(new FileInputStream("in.txt"));
        } else in = new Reader();
        f = new File("out.txt");
        if(f.exists() && !f.isDirectory()) {
            out = new PrintWriter(new File("out.txt"));
        } else out = new PrintWriter(System.out);
    }
}