#include <bits/stdc++.h>

using namespace std;
int main() {
	long long int n,i,j,cnt,mx;
	scanf("%lld",&n);
	long long int a[n];
	for(i=0;i<n;i++)
	{
	    scanf("%lld",&a[i]);
	}
	cnt=1;
	mx=0;
	for(i=0;i<n-1;i++)
	{
	    if(a[i]<a[i+1])
	    {
	        cnt++;
	    }
	    else if(a[i]>=a[i+1]){
	        cnt=1;
	    }
	    if(mx<cnt)mx=cnt;
	}
	printf("%lld\n",mx);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
int main() {
	int n,i,j,cnt,mx;
	scanf("%d",&n);
	long long int a[n];
	for(i=0;i<n;i++)
	{
	    scanf("%lld",&a[i]);
	}
	cnt=1;
	mx=0;
	for(i=0;i<n-1;i++)
	{
	    if(a[i]<a[i+1]) cnt++;
	    else {
	        if(mx<cnt)mx=cnt;
	        cnt=1;
	    }
	}
	printf("%d\n",cnt);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
int main() {
	long long int n,i,j,cnt,mx;
	scanf("%lld",&n);
	long long int a[n];
	for(i=0;i<n;i++)
	{
	    scanf("%lld",&a[i]);
	}
	cnt=1;
	mx=0;
	for(i=0;i<n-1;i++)
	{
	    if(a[i]<a[i+1])
	    {
	        cnt++;
	    }
	    else if(a[i]>=a[i+1]){
	        if(mx<cnt)mx=cnt;
	        cnt=1;
	    }
	}
	printf("%lld\n",mx);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    long long a[100000],i,n,k=1,max=0;
    cin>>n;
    for(i=0;i<n;i++)
        cin>>a[i];
        if(n==1)
            max=1;
        else
        for(i=0;i<n-1;i++)
        {
            if(a[i+1]>a[i])
                k++;
            else
                k=1;
                if(k>max)
                    max=k;
        }
            cout<<max;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    long long a[100000],i,n,k=1,max=0;
    cin>>n;
    for(i=0;i<n;i++)
        cin>>a[i];
        for(i=0;i<n-1;i++)
        {
            if(a[i+1]>a[i])
                k++;
            else
                k=1;
                if(k>max)
                    max=k;
        }
            cout<<max;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    long long a[100000],i,n,k=1,max=0;
    cin>>n;
    for(i=0;i<n;i++)
        cin>>a[i];
        for(i=0;i<n;i++)
        {
            if(a[i+1]>a[i])
                k++;
            else
                k=1;
                if(k>max)
                    max=k;
        }
            cout<<max;

    return 0;
}


<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
	<FileVersion major="1" minor="6" />
	<Project>
		<Option title="nsbjsb" />
		<Option pch_mode="2" />
		<Option compiler="gcc" />
		<Build>
			<Target title="Debug">
				<Option output="bin/Debug/nsbjsb" prefix_auto="1" extension_auto="1" />
				<Option object_output="obj/Debug/" />
				<Option type="1" />
				<Option compiler="gcc" />
				<Compiler>
					<Add option="-g" />
				</Compiler>
			</Target>
			<Target title="Release">
				<Option output="bin/Release/nsbjsb" prefix_auto="1" extension_auto="1" />
				<Option object_output="obj/Release/" />
				<Option type="1" />
				<Option compiler="gcc" />
				<Compiler>
					<Add option="-O2" />
				</Compiler>
				<Linker>
					<Add option="-s" />
				</Linker>
			</Target>
		</Build>
		<Compiler>
			<Add option="-Wall" />
			<Add option="-fexceptions" />
		</Compiler>
		<Unit filename="main.cpp" />
		<Extensions>
			<code_completion />
			<envvars />
			<debugger />
			<lib_finder disable_auto="1" />
		</Extensions>
	</Project>
</CodeBlocks_project_file>


#include <bits/stdc++.h>

using namespace std;

int main()
{
    long long a[100000],i,n,k=1,max=0;
    cin>>n;
    for(i=0;i<n;i++)
        cin>>a[i];
        if(n==1)
            cout<<"1";
        else
        for(i=0;i<n-1;i++)
        {
            if(a[i+1]>a[i])
                k++;
            else
                k=1;
                if(k>max)
                    max=k;
        }
            cout<<max;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    long long a[100000],i,n,k=1,max=0;
    cin>>n;
    for(i=0;i<n;i++)
        cin>>a[i];
        for(i=0;i<n-1;i++)
        {
            if(a[i+1]>a[i])
                k++;
            else
                k=1;
                if(k>max)
                    max=k;
        }
            cout<<max;

    return 0;
}
#include <bits/stdc++.h>

using namespace std;

int main()
{
    long long a[100000],i,n,k=1,max=0;
    cin>>n;
    for(i=0;i<n;i++)
        cin>>a[i];
        for(i=0;i<n-1;i++)
        {
            if(a[i+1]>a[i])
                k++;
            else
                k=1;
                if(k>max)
                    max=k;
        }
            cout<<max;

    return 0;
}


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

int main()
{
	long long n,i,j,max=0,count,flag=0;
	cin>>n;
	long long a[n];
	for(i=0;i<n;i++)
	cin>>a[i];
	for(i=0;i<n-1;i++)
	{
		count=1;
		for(j=i+1;j<n;j++)
		{
			if(a[j]>a[j-1])
			count++;
			else
			break;
		}
	//	cout<<count;
		if(count>max)
		max=count;
	}
	cout<<max;

}


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

int main()
{
	long long n,i,j,max,count,flag=0;
	cin>>n;
	long long a[n];
	for(i=0;i<n;i++)
	cin>>a[i];
	for(i=0;i<n-1;i++)
	{
		count=1;
		for(j=i+1;j<n;j++)
		{
			if(a[j]>a[j-1])
			count++;
			else
			break;
		}
	//	cout<<count;
		if(count>max)
		max=count;
	}
	cout<<max;

}


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

int main()
{
	long long n,i,j,max=1,count,flag=0;
	cin>>n;
	long long a[n];
	for(i=0;i<n;i++)
	cin>>a[i];
	for(i=0;i<n-1;i++)
	{
		count=0;
		for(j=i+1;j<n;j++)
		{
			if(a[j]>a[j-1])
			count++;
			else
			break;
		}
	//	cout<<count;
		if(count>max)
		max=count;
	}
	cout<<max;;

}


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

int main()
{
	long long n,i,j,max,count,flag=0;
	cin>>n;
	long long a[n];
	for(i=0;i<n;i++)
	cin>>a[i];
	for(i=0;i<n-1;i++)
	{
		count=1;
		for(j=i+1;j<n;j++)
		{
			if(a[j]>a[j-1])
			count++;
			else
			break;
		}
	//	cout<<count;
		if(count>max)
		max=count;
	}
	cout<<max;;

}


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

int main()
{
	long long n,i,j,max=1,count,flag=0;
	cin>>n;
	long long a[n];
	for(i=0;i<n;i++)
	cin>>a[i];
	for(i=0;i<n-1;i++)
	{
		count=1;
		for(j=i+1;j<n;j++)
		{
			if(a[j]>a[j-1])
			count++;
			else
			break;
		}
	//	cout<<count;
		if(count>max)
		max=count;
	}
	cout<<max;

}


 #include <iostream>
using namespace std;

int main() {
  long long	int n,max=0;
	cin>>n;
	int a[n],count=1;
	cin>>a[0];
	int temp=a[0];
	for(int i=1;i<n;i++)
	{
	    cin>>a[i];
	    if(a[i]>temp)
	    {
		  temp=a[i];
	      count++;
	   }
	    else
	    {
	        count=1;
	       temp=a[i];
	    }

	    if(count>max)
	    max=count;
	}
	if(n>1)
	cout<<max;
	else
	cout<<count;
	return 0;
}


#include<iostream>
using namespace std;
int main()
{
    long long i,j,d,sum,n,a[100000];
    cin>>n;
    sum=1;
    d=1;
    for(i=0; i<n; i++){
        cin>>a[i];
    }
    for(j=0;j<n; j++){
        if (a[j]<a[j+1]){
              sum++;
              d=d<sum? sum:d;
        }
        else{
                sum=1;
        }
    }
    cout<<d<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long i,j,d,sum,n,a[100000];
    cin>>n;
    sum=1;
    d=0;
    for(i=0; i<n; i++){
        cin>>a[i];
    }
    for(j=0;j<n; j++){
        if (a[j]<a[j+1]){
              sum++;
              d=d<sum? sum:d;
        }
        else{
                sum=1;
        }
    }
    cout<<d<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long i,j,d,sum,n,a[100000];
    cin>>n;
    sum=1;
    d=0;
    for(i=0; i<n; i++){
        cin>>a[i];
    }
    for(j=0;j<n; j++){
        if (a[j]<a[j+1]){
              sum++;
        }
        else{
                if(d<sum){
                    d=sum;
                }
                sum=1;
        }
    }
    cout<<d<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long i,j,d,sum,n,a[100000];
    cin>>n;
    sum=1;
    d=0;
    for(i=0; i<n; i++){
        cin>>a[i];
    }
    for(j=0;j<n; j++){
        if (a[j]<a[j+1]){
              sum++;
              if(d<sum){
                    d=sum;
                }
        }
        else{
                sum=1;
        }
    }
    cout<<d<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long i,j,d,sum,n,a[100000];
    cin>>n;
    sum=1;
    d=0;
    for(i=0; i<n; i++){
        cin>>a[i];
    }
    for(j=0;j<n; j++){
        if (a[j]<a[j+1]){
              sum++;
        }
        else{
                if(d<sum){
                    d=sum;
                    sum=1;
               }
        }
    }
    cout<<d<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long int i,m,sum,n,a[100000];
    cin>>n;
    m=0;
    sum=1;
    for(i=0; i<n; i++){
        cin>>a[i];
        if(m>1 ){
           if (a[i-1-0]<a[i]){
              sum++;
           }
           else{
                sum=1;
           }
        }
        m=2;
    }
    cout<<sum<<endl;
    return 0;
}


#include<iostream>
using namespace std;
int main()
{
    long long i,j,d,sum,n,a[100000];
    cin>>n;
    sum=1;
    d=1;
    for(i=0; i<n; i++){
        cin>>a[i];
    }
    for(j=0; j<n-1;  j++){
        if (a[j]<a[j+1]){
              sum++;
              d=d<sum? sum : d;
        }
        else{
                sum=1;
        }
    }
    cout<<d<<endl;
    return 0;
}


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

int findLength(int arr[], int n){
    int max_len = 1;
    for (int i=0; i<n-1; i++){
        int mn = arr[i];
        int mx = arr[i];
        for (int j=i+1; j<n; j++){
            mn = min(mn, arr[j]);
            mx = max(mx, arr[j]);
            if ((mx - mn) == j-i)
                max_len = max(max_len, mx-mn+1);
        }
    }
    return max_len;
}

int main(){
    int arr[10010];
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>arr[i];
    cout<<findLength(arr, n)<<endl;
}


#include<iostream>
using namespace std;

int main(){
    long long a[1000010];
    long long n;
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>a[i];
    int flag = 0;
    int maxSubarray =0;
    int start=0,end=0;
    for(int i=1;i<n;i++){
        if(a[i-1]<a[i]){
            if(flag!=1){
                start = i-1;
                flag=1;
            }
            if(i == n-1){
                end = n-1;
            }
        }
        else{
            if(flag==1 ){
                end = i-1;
                flag =0;
            }
        }
        if(maxSubarray < end - start){
            maxSubarray = end - start;
        }
    }

    cout<<maxSubarray+1<<endl;
}


#include<iostream>
using namespace std;

int main(){
    int a[10010];
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>a[i];
    int flag = 0;
    int maxSubarray =0;
    int start=0,end=0;
    for(int i=1;i<n;i++){
        if(a[i-1]<a[i]){
            if(flag!=1){
                start = i-1;
                flag=1;
            }
            if(i == n-1){
                end = n-1;
            }
        }
        else{
            if(flag==1 ){
                end = i-1;
                flag =0;
            }
        }
        if(maxSubarray < end - start){
            maxSubarray = end - start;
        }
    }

    cout<<maxSubarray+1<<endl;
}


#include <bits/stdc++.h>
using namespace std;
int main(){
    int arr[10010];
    int n;
    cin>>n;
    int count=0,max=0;
    for(int i=0;i<n;i++){
        cin>>arr[i];
    }
    for(int i=1;i<n;i++){
        if(arr[i]>arr[i-1])
            count++;
        if(count>max)
            max=count;
    }

    if(max==0)
        max++;
    cout<<max<<endl;
}


#include<iostream>
using namespace std;

int main(){
    long long a[10010];
    long long n;
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>a[i];
    int flag = 0;
    int maxSubarray =0;
    int start=0,end=0;
    for(int i=1;i<n;i++){
        if(a[i-1]<a[i]){
            if(flag!=1){
                start = i-1;
                flag=1;
            }
            if(i == n-1){
                end = n-1;
            }
        }
        else{
            if(flag==1 ){
                end = i-1;
                flag =0;
            }
        }
        if(maxSubarray < end - start){
            maxSubarray = end - start;
        }
    }

    cout<<maxSubarray+1<<endl;
}


#include <iostream>
const int MaxN = 1e5 + 10;
long long int taar [MaxN];
int N, i, maxy;

using namespace std;
int main(int argc, char *argv[]) {
	cin>>N;
	for (i=0; i<N; i++) cin>>taar[i];

	int dp[N];
	dp[0]=1;
	for (i=1; i<N; i++){
		if (taar[i]>taar[i-1]) dp[i] = dp[i-1]+1;
		else dp[i]=1;
	}

	maxy = 0;
	for (i=0; i<N; i++) {
		maxy = max(maxy, dp[i]);
		//cout<<dp[i]<<"  ";
	}

	cout<<endl<<maxy;
}
/*
NOTE:

*/
// CSAA CF702A 03102017
// © SMP(SAM-MHD-PGM) @ AEH_COT96 / CF702A_MaximumIncreaseV2.03.Oct.2017


#include<bits/stdc++.h>
using namespace std;
int main()
{
	long long int n,m;
	cin>>n>>m;
//	cout<<1;
	vector<long long int> v[m+1];
	for(long long int i=0;i<n;i++)
	{
		long long int x,r;
		cin>>x>>r;
		v[x].push_back(r);
	}
	long long int a=INT_MIN,b=INT_MAX;
	for(long long int i=1;i<=m;i++)
	{
		long long int l;
		l=v[i].size();
		a=max(a,l);
		b=min(b,l);
		sort(v[i].rbegin(),v[i].rend());
//		for(int j=0;j<l;j++)
//		{
//			cout<<v[i][j];
//		}
	}
	long long int ans=INT_MIN;
	for(long long int i=1;i<=a;i++)
	{
		long long int sum=0;
		for(long long int j=1;j<=m;j++)
		{
			long long int t,s=0;
			t=i;
			if(v[j].size()>=t)
			{
//				cout<<j<<" ";
				long long int k=0;
				while(t--)
				{
					s=s+v[j][k];
					k++;
				}
				if(s>0)
				{
					sum=sum+s;
				}
//				cout<<s;
			}
			else
			{
				continue;
			}
		}
		ans=max(sum,ans);
	}
	cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	long long int n,m;
	cin>>n>>m;
//	cout<<1;
	vector<long long int> v[m+1];
	for(long long int i=0;i<n;i++)
	{
		long long int x,r;
		cin>>x>>r;
		v[x].push_back(r);
	}
	long long int a=INT_MIN,b=INT_MAX;
	for(long long int i=1;i<=m;i++)
	{
		long long int l;
		l=v[i].size();
		a=max(a,l);
		b=min(b,l);
		sort(v[i].rbegin(),v[i].rend());
//		for(int j=0;j<l;j++)
//		{
//			cout<<v[i][j];
//		}
	}
	long long int ans=INT_MIN,prev=0;
	for(long long int i=1;i<=a;i++)
	{
		long long int sum=0;
		for(long long int j=1;j<=m;j++)
		{
			long long int t,s=0;
			t=i;
			if(v[j].size()>=t)
			{
//				cout<<j<<" ";
				long long int k=0;
				while(t--)
				{
					s=s+v[j][k];
					k++;
				}
				if(s>0)
				{
					sum=sum+s;
				}
//				cout<<s;
			}
			else
			{
				continue;
			}
		}
		prev=sum;
		if(ans>prev)
		{
			cout<<ans;
			return 0;
		}
		ans=max(sum,ans);

	}
	cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
bool cmp(const vector<long long int> &a,const vector<long long int> &b)
{
	if(a.size()>b.size())
	{
		return true;
	}
	else
	{
		return false;
	}
}
int main()
{
	ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
	long long int n,m;
	cin>>n>>m;
//	cout<<1;
	vector<long long int> v[m];
	for(long long int i=0;i<n;i++)
	{
		long long int x,r;
		cin>>x>>r;
		x=x-1;
		v[x].push_back(r);
	}
	long long int a=INT_MIN,b=INT_MAX;
	for(long long int i=0;i<m;i++)
	{
		long long int l;
		l=v[i].size();
		a=max(a,l);
//		b=min(b,l);
		sort(v[i].rbegin(),v[i].rend());
//		for(int j=0;j<l;j++)
//		{
//			cout<<v[i][j];
//		}
	}
	sort(v,v+m,cmp);
	long long int f[m];
	for(long long int i=0;i<m;i++)
	{
		f[i]=0;
	}
	long long int ans=INT_MIN;
	for(long long int i=0;i<a;i++)
	{
		long long int sum=0;
		for(long long int j=0;j<m;j++)
		{
			if(v[j].size()<i+1)
			{
				break;
			}
			else
			{
				f[j]=f[j]+v[j][i];

				if(f[j]>0)
				sum=sum+f[j];

			}

		}
//		prev=sum;
//		if(ans>prev)
//		{
//			cout<<ans;
//			return 0;
//		}
		ans=max(sum,ans);

	}
	cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
	long long int n,m;
	cin>>n>>m;
//	cout<<1;
	vector<long long int> v[m+1];
	for(long long int i=0;i<n;i++)
	{
		long long int x,r;
		cin>>x>>r;
		v[x].push_back(r);
	}
	long long int a=INT_MIN,b=INT_MAX;
	for(long long int i=1;i<=m;i++)
	{
		long long int l;
		l=v[i].size();
		a=max(a,l);
		b=min(b,l);
		sort(v[i].rbegin(),v[i].rend());
//		for(int j=0;j<l;j++)
//		{
//			cout<<v[i][j];
//		}
	}
	long long int f[m+1];
	for(long long int i=1;i<=m;i++)
	{
		f[i]=0;
	}
	long long int ans=INT_MIN,prev=0;
	for(long long int i=0;i<a;i++)
	{
		long long int sum=0;
		for(long long int j=1;j<=m;j++)
		{
			if(v[j].size()>=i+1)
			{
				f[j]=f[j]+v[j][i];

				if(f[j]>0)
				sum=sum+f[j];

			}

		}
//		prev=sum;
//		if(ans>prev)
//		{
//			cout<<ans;
//			return 0;
//		}
		ans=max(sum,ans);

	}
	cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
//	ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
	long long int n,m;
	cin>>n>>m;
//	cout<<1;
	vector<long long int> v[m+1];
	for(long long int i=0;i<n;i++)
	{
		long long int x,r;
		cin>>x>>r;
		v[x].push_back(r);
	}
	long long int a=INT_MIN,b=INT_MAX;
	for(long long int i=1;i<=m;i++)
	{
		long long int l;
		l=v[i].size();
		a=max(a,l);
		b=min(b,l);
		sort(v[i].rbegin(),v[i].rend());
//		for(int j=0;j<l;j++)
//		{
//			cout<<v[i][j];
//		}
	}
	long long int f[m+1];
	for(long long int i=1;i<=m;i++)
	{
		f[i]=0;
	}
	long long int ans=INT_MIN,prev=0;
	for(long long int i=0;i<a;i++)
	{
		long long int sum=0;
		for(long long int j=1;j<=m;j++)
		{
			if(v[j].size()>=i+1)
			{
				f[j]=f[j]+v[j][i];

				if(f[j]>0)
				sum=sum+f[j];

			}

		}
//		prev=sum;
//		if(ans>prev)
//		{
//			cout<<ans;
//			return 0;
//		}
		ans=max(sum,ans);

	}
	cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
	long long int n,m;
	cin>>n>>m;
//	cout<<1;
	vector<long long int> v[m+1];
	for(long long int i=0;i<n;i++)
	{
		long long int x,r;
		cin>>x>>r;
		v[x].push_back(r);
	}
	long long int a=INT_MIN,b=INT_MAX;
	for(long long int i=1;i<=m;i++)
	{
		long long int l;
		l=v[i].size();
		a=max(a,l);
		b=min(b,l);
		sort(v[i].rbegin(),v[i].rend());
//		for(int j=0;j<l;j++)
//		{
//			cout<<v[i][j];
//		}
	}
	long long int f[m+1];
	for(long long int i=1;i<=m;i++)
	{
		f[i]=0;
	}
	long long int ans=INT_MIN,prev=0;
	for(long long int i=0;i<a;i++)
	{
		long long int sum=0;
		for(long long int j=1;j<=m;j++)
		{
			if(v[j].size()>=i+1)
			{
				f[j]=f[j]+v[j][i];

				if(f[j]>0)
				sum=sum+f[j];

			}
			else
			{
				break;
			}

		}
//		prev=sum;
//		if(ans>prev)
//		{
//			cout<<ans;
//			return 0;
//		}
		ans=max(sum,ans);

	}
	cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
#define fore(i, l, r) for(long long i = (l); i < (r); ++i)
#define forn(i, n) fore(i, 0, n)
#define fori(i, l, r) fore(i, l, (r) + 1)
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define fastio iostream::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define hell 1000000007
#define mset(x,val) memset(x, val, sizeof(x));
typedef long long li;
typedef long double ld;
typedef vector<li> vi;
typedef pair<li, li> pt;
template<class T1,class T2>
ostream& operator << (ostream& out,pair<T1,T2> &p)
{
    out<<p.first<<' '<<p.second;
}

template<class T>
istream& operator >> (istream& in,vector<T> &v)
{
    for(auto &x:v)
        in>>x;
    return in;
}

template<class T>
ostream& operator << (ostream& out,vector<T> &v)
{
    for(auto x:v)
        out<<x<<' ';
    return out;
}

vi v[100005];

int main()
{
    fastio;
	li n,m;
	cin>>n>>m;

	forn(i,n){
		li x,y;
		cin>>x>>y;
		x--;
		v[x].pb(y);
	}
	fore(i,0,m)sort(all(v[i]),greater<li>());
	vi ind(m,0);
	fore(i,0,m){
		fore(j,1,v[i].size()){
			v[i][j]+=v[i][j-1];
		}
	}
	li ans=0;
	li maxm=-1;
	bool f=1;
	while(f){
		f=0;
		ans=0;
		forn(i,m){
			if(ind[i]>=v[i].size()){
				ind[i]=-1;
				continue;
			}
			else if(ind[i]>0&&v[i][ind[i]]<0){
				ind[i]=-1;
				continue;
			}

			if(ind[i]>=0){
				f=1;
				ans+=v[i][ind[i]];
				ind[i]++;
				if(ind[i]>=v[i].size())ind[i]=-1;
			}
		}
		maxm=max(maxm,ans);
	}
	cout<<maxm;
}


#include<bits/stdc++.h>
using namespace std;
#define fore(i, l, r) for(long long i = (l); i < (r); ++i)
#define forn(i, n) fore(i, 0, n)
#define fori(i, l, r) fore(i, l, (r) + 1)
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define fastio iostream::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define hell 1000000007
#define mset(x,val) memset(x, val, sizeof(x));
typedef long long li;
typedef long double ld;
typedef vector<li> vi;
typedef pair<li, li> pt;
template<class T1,class T2>
ostream& operator << (ostream& out,pair<T1,T2> &p)
{
    out<<p.first<<' '<<p.second;
}

template<class T>
istream& operator >> (istream& in,vector<T> &v)
{
    for(auto &x:v)
        in>>x;
    return in;
}

template<class T>
ostream& operator << (ostream& out,vector<T> &v)
{
    for(auto x:v)
        out<<x<<' ';
    return out;
}

vi v[100005];

int main()
{
    fastio;
	li n,m;
	cin>>n>>m;

	forn(i,n){
		li x,y;
		cin>>x>>y;
		x--;
		v[x].pb(y);
	}
	fore(i,0,m)sort(all(v[i]),greater<li>());
	vi ind(m,0);
	fore(i,0,m){
		fore(j,1,v[i].size()){
			v[i][j]+=v[i][j-1];
		}
	}
	li ans=0;
	li maxm=-1;
	bool f=1;
	while(f){
		f=0;
		ans=0;
		forn(i,m){
			if(ind[i]>=v[i].size()){
				ind[i]=-1;
				continue;
			}
			if(ind[i]>=0){
				f=1;
				ans+=v[i][ind[i]];
				ind[i]++;
				if(ind[i]>=v[i].size())ind[i]=-1;
			}
		}
		maxm=max(maxm,ans);
	}
	cout<<maxm;
}


#include<bits/stdc++.h>
using namespace std;
#define fore(i, l, r) for(long long i = (l); i < (r); ++i)
#define forn(i, n) fore(i, 0, n)
#define fori(i, l, r) fore(i, l, (r) + 1)
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define fastio iostream::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define hell 1000000007
#define mset(x,val) memset(x, val, sizeof(x));
typedef long long li;
typedef long double ld;
typedef vector<li> vi;
typedef pair<li, li> pt;
template<class T1,class T2>
ostream& operator << (ostream& out,pair<T1,T2> &p)
{
    out<<p.first<<' '<<p.second;
}

template<class T>
istream& operator >> (istream& in,vector<T> &v)
{
    for(auto &x:v)
        in>>x;
    return in;
}

template<class T>
ostream& operator << (ostream& out,vector<T> &v)
{
    for(auto x:v)
        out<<x<<' ';
    return out;
}

vi v[100005];

int main()
{
    fastio;
	li n,m;
	cin>>n>>m;

	forn(i,n){
		li x,y;
		cin>>x>>y;
		x--;
		v[x].pb(y);
	}
	fore(i,0,m)sort(all(v[i]),greater<li>());
	vi ind(m,0);
	fore(i,0,m){
		fore(j,1,v[i].size()){
			v[i][j]+=v[i][j-1];
		}
	}
	li ans=0;
	li maxm=-1;
	bool f=1;
	while(f){
		f=0;
		ans=0;
		forn(i,m){
			if(ind[i]>=0){
				f=1;
				ans+=v[i][ind[i]];
				ind[i]++;
				if(ind[i]>=v[i].size())ind[i]=-1;
			}
		}
		maxm=max(maxm,ans);
	}
	cout<<maxm;
}


#include<bits/stdc++.h>
using namespace std;
#define fore(i, l, r) for(long long i = (l); i < (r); ++i)
#define forn(i, n) fore(i, 0, n)
#define fori(i, l, r) fore(i, l, (r) + 1)
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define fastio iostream::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define hell 1000000007
#define mset(x,val) memset(x, val, sizeof(x));
typedef long long li;
typedef long double ld;
typedef vector<li> vi;
typedef pair<li, li> pt;
template<class T1,class T2>
ostream& operator << (ostream& out,pair<T1,T2> &p)
{
    out<<p.first<<' '<<p.second;
}

template<class T>
istream& operator >> (istream& in,vector<T> &v)
{
    for(auto &x:v)
        in>>x;
    return in;
}

template<class T>
ostream& operator << (ostream& out,vector<T> &v)
{
    for(auto x:v)
        out<<x<<' ';
    return out;
}

vi v[100005];

int main()
{
    fastio;
	li n,m;
	cin>>n>>m;

	forn(i,n){
		li x,y;
		cin>>x>>y;
		x--;
		v[x].pb(y);
	}
	fore(i,0,m)sort(all(v[i]),greater<li>());
	vi ind(m,0);
	fore(i,0,m){
		fore(j,1,v[i].size()){
			v[i][j]+=v[i][j-1];
		}
	}
	li ans=0;
	li maxm=-1;
	bool f=1;
	while(f){
		f=0;
		ans=0;
		forn(i,m){
			if(ind[i]>=v[i].size()){
				ind[i]=-1;
				continue;
			}
			if(v[i][ind[i]]<0){
				ind[i]=-1;
				continue;
			}

			if(ind[i]>=0){
				f=1;
				ans+=v[i][ind[i]];
				ind[i]++;
				if(ind[i]>=v[i].size())ind[i]=-1;
			}
		}
		maxm=max(maxm,ans);
	}
	cout<<maxm;
}


#include<bits/stdc++.h>
using namespace std;
#define fore(i, l, r) for(long long i = (l); i < (r); ++i)
#define forn(i, n) fore(i, 0, n)
#define fori(i, l, r) fore(i, l, (r) + 1)
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define fastio iostream::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define hell 1000000007
#define mset(x,val) memset(x, val, sizeof(x));
typedef long long li;
typedef long double ld;
typedef vector<li> vi;
typedef pair<li, li> pt;
template<class T1,class T2>
ostream& operator << (ostream& out,pair<T1,T2> &p)
{
    out<<p.first<<' '<<p.second;
}

template<class T>
istream& operator >> (istream& in,vector<T> &v)
{
    for(auto &x:v)
        in>>x;
    return in;
}

template<class T>
ostream& operator << (ostream& out,vector<T> &v)
{
    for(auto x:v)
        out<<x<<' ';
    return out;
}

vi v[100005];

int main()
{
    fastio;
	li n,m;
	cin>>n>>m;

	forn(i,n){
		li x,y;
		cin>>x>>y;
		x--;
		v[x].pb(y);
	}
	fore(i,0,m)sort(all(v[i]),greater<li>());
	vi ind(m,0);
	fore(i,0,m){
		fore(j,1,v[i].size()){
			v[i][j]+=v[i][j-1];
		}
	}
	li ans=0;
	li maxm=-1;
	bool f=1;
	vi st;
	forn(i,m)st.pb(i);
	while(f){

		f=0;
		ans=0;
		vi st1;
		forn(j,st.size()){
			li i=st[j];
			// cout<<i<<" ";
			if(ind[i]>=v[i].size()){
				ind[i]=-1;
				continue;
			}
			if(ind[i]>=0&&v[i][ind[i]]<0){
				ind[i]=-1;
				continue;
			}
			st1.pb(i);
			if(ind[i]>=0){

				f=1;
				ans+=v[i][ind[i]];
				ind[i]++;
			}
		}
		st=st1;
		maxm=max(maxm,ans);
	}
	cout<<maxm;
}


#include<bits/stdc++.h>
using namespace std;
#define fore(i, l, r) for(long long i = (l); i < (r); ++i)
#define forn(i, n) fore(i, 0, n)
#define fori(i, l, r) fore(i, l, (r) + 1)
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define fastio iostream::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define hell 1000000007
#define mset(x,val) memset(x, val, sizeof(x));
typedef long long li;
typedef long double ld;
typedef vector<li> vi;
typedef pair<li, li> pt;
template<class T1,class T2>
ostream& operator << (ostream& out,pair<T1,T2> &p)
{
    out<<p.first<<' '<<p.second;
}

template<class T>
istream& operator >> (istream& in,vector<T> &v)
{
    for(auto &x:v)
        in>>x;
    return in;
}

template<class T>
ostream& operator << (ostream& out,vector<T> &v)
{
    for(auto x:v)
        out<<x<<' ';
    return out;
}

vi v[100005];

int main()
{
    fastio;
	li n,m;
	cin>>n>>m;

	forn(i,n){
		li x,y;
		cin>>x>>y;
		x--;
		v[x].pb(y);
	}
	fore(i,0,m)sort(all(v[i]),greater<li>());
	vi ind(m,0);
	fore(i,0,m){
		fore(j,1,v[i].size()){
			v[i][j]+=v[i][j-1];
		}
	}
	li ans=0;
	li maxm=-1;
	bool f=1;
	while(f){
		f=0;
		ans=0;
		forn(i,m){
			if(ind[i]>=v[i].size()||v[i][ind[i]]<0){
				ind[i]=-1;
				continue;
			}
			if(ind[i]>=0){
				f=1;
				ans+=v[i][ind[i]];
				ind[i]++;
				if(ind[i]>=v[i].size())ind[i]=-1;
			}
		}
		maxm=max(maxm,ans);
	}
	cout<<maxm;
}


#include<bits/stdc++.h>
using namespace std;
#define fore(i, l, r) for(long long i = (l); i < (r); ++i)
#define forn(i, n) fore(i, 0, n)
#define fori(i, l, r) fore(i, l, (r) + 1)
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define fastio iostream::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define hell 1000000007
#define mset(x,val) memset(x, val, sizeof(x));
typedef long long li;
typedef long double ld;
typedef vector<li> vi;
typedef pair<li, li> pt;
template<class T1,class T2>
ostream& operator << (ostream& out,pair<T1,T2> &p)
{
    out<<p.first<<' '<<p.second;
}

template<class T>
istream& operator >> (istream& in,vector<T> &v)
{
    for(auto &x:v)
        in>>x;
    return in;
}

template<class T>
ostream& operator << (ostream& out,vector<T> &v)
{
    for(auto x:v)
        out<<x<<' ';
    return out;
}

vi v[100005];

int main()
{
    fastio;
	li n,m;
	cin>>n>>m;

	forn(i,n){
		li x,y;
		cin>>x>>y;
		x--;
		v[x].pb(y);
	}
	fore(i,0,m)sort(all(v[i]),greater<li>());
	vi ind(m,0);
	fore(i,0,m){
		fore(j,1,v[i].size()){
			v[i][j]+=v[i][j-1];
		}
	}
	li ans=0;
	li maxm=-1;
	bool f=1;
	while(f){
		f=0;
		ans=0;
		forn(i,m){
			if(ind[i]>=v[i].size()){
				ind[i]=-1;
				continue;
			}
			if(ind[i]>=0&&v[i][ind[i]]<0){
				ind[i]=-1;
				continue;
			}

			if(ind[i]>=0){
				f=1;
				ans+=v[i][ind[i]];
				ind[i]++;
				if(ind[i]>=v[i].size())ind[i]=-1;
			}
		}
		maxm=max(maxm,ans);
	}
	cout<<maxm;
}


#include<bits/stdc++.h>
using namespace std;
#define fore(i, l, r) for(long long i = (l); i < (r); ++i)
#define forn(i, n) fore(i, 0, n)
#define fori(i, l, r) fore(i, l, (r) + 1)
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define fastio iostream::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define hell 1000000007
#define mset(x,val) memset(x, val, sizeof(x));
typedef long long li;
typedef long double ld;
typedef vector<li> vi;
typedef pair<li, li> pt;
template<class T1,class T2>
ostream& operator << (ostream& out,pair<T1,T2> &p)
{
    out<<p.first<<' '<<p.second;
}

template<class T>
istream& operator >> (istream& in,vector<T> &v)
{
    for(auto &x:v)
        in>>x;
    return in;
}

template<class T>
ostream& operator << (ostream& out,vector<T> &v)
{
    for(auto x:v)
        out<<x<<' ';
    return out;
}

vi v[100005];

int main()
{
    fastio;
	li n,m;
	cin>>n>>m;

	forn(i,n){
		li x,y;
		cin>>x>>y;
		x--;
		v[x].pb(y);
	}
	fore(i,0,m)sort(all(v[i]),greater<li>());
	vi ind(m,0);
	fore(i,0,m){
		fore(j,1,v[i].size()){
			v[i][j]+=v[i][j-1];
		}
	}
	li ans=0;
	li maxm=-1;
	bool f=1;
	unordered_set<li>st;
	forn(i,m)st.insert(i);
	while(f){
		f=0;
		ans=0;
		for(auto i:st){
			if(ind[i]>=v[i].size()){
				ind[i]=-1;
				st.erase(i);
				continue;
			}
			if(ind[i]>=0&&v[i][ind[i]]<0){
				ind[i]=-1;
				st.erase(i);
				continue;
			}

			if(ind[i]>=0){
				f=1;
				ans+=v[i][ind[i]];
				ind[i]++;
				if(ind[i]>=v[i].size())ind[i]=-1;
			}
		}
		maxm=max(maxm,ans);
	}
	cout<<maxm;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;       typedef long double ld;  typedef pair<int,int> pii; typedef pair<ll,ll> pll;  typedef vector<int> vi;   typedef vector<ll> vl;
typedef vector<string> vst; typedef vector<bool> vb; typedef vector<ld> vld;    typedef vector<pii> vpii; typedef vector<pll> vpll; typedef vector<vector<int> > vvi;
const int INF = (0x7FFFFFFFL); const ll INFF = (0x7FFFFFFFFFFFFFFFL); const string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const int MOD = 1e9 + 7;       const int MODD = 998244353;            const string alphabet = "abcdefghijklmnopqrstuvwxyz";
const double PI = acos(-1.0);  const double EPS = 1e-9;               const string Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
int dx[9] = { 1, 0, -1,  0,  1, -1, -1, 1, 0 };
int dy[9] = { 0, 1,  0, -1, -1, -1,  1, 1, 0 };
#define ln '\n'
#define scnaf scanf
#define sacnf scanf
#define sancf scanf
#define SS(type, ...)type __VA_ARGS__;MACRO_VAR_Scan(__VA_ARGS__);
template<typename T> void MACRO_VAR_Scan(T& t){cin >> t;}template<typename First, typename...Rest> void MACRO_VAR_Scan(First& first, Rest&...rest){cin >> first;MACRO_VAR_Scan(rest...);}
#define SV(type,c,n) vector<type> c(n);for(auto& i:c)cin >> i;
#define SVV(type,c,n,m) vector<vector<type>> c(n,vector<type>(m));for(auto& r:c)for(auto& i:r)cin >> i;
template<class T>ostream &operator<<(ostream &o,const vector<T>&j){o<<"{";for(int i=0;i<(int)j.size();++i)o<<(i>0?", ":"")<<j[i];o<<"}";return o;}
template<class T,class U>ostream &operator<<(ostream &o,const pair<T,U>&j){o<<"{"<<j.first<<", "<<j.second<<"}";return o;}
template<class T,class U>ostream &operator<<(ostream &o,const map<T,U>&j){o<<"{";for(auto t=j.begin();t!=j.end();++t)o<<(t!=j.begin()?", ":"")<<*t;o<<"}";return o;}
template<class T>ostream &operator<<(ostream &o,const set<T>&j){o<<"{";for(auto t=j.begin();t!=j.end();++t)o<<(t!=j.begin()?", ":"")<<*t;o<<"}";return o;}
inline void print(void){cout << endl;}
template<class Head> void print(Head&& head){cout << head;print();} template<class Head,class... Tail> void print(Head&& head,Tail&&... tail){cout<<head<<" ";print(forward<Tail>(tail)...);}
inline void debug(void){cerr << endl;}
template<class Head> void debug(Head&& head){cerr << head;debug();} template<class Head,class... Tail> void debug(Head&& head,Tail&&... tail){cerr<<head<<" ";debug(forward<Tail>(tail)...);}
template<typename T> void PA(T &a){int ASIZE=sizeof(a)/sizeof(a[0]);for(int ii=0;ii<ASIZE;++ii){cout<<a[ii]<<" \n"[ii==ASIZE-1];}}
template<typename T> void PV(T &v){int VSIZE=v.size();for(int ii=0;ii<VSIZE;++ii){cout<<v[ii]<<" \n"[ii==VSIZE-1];}}
#define ER(x)  cerr << #x << " = " << (x) << endl;
#define ERV(v) {cerr << #v << " : ";for(const auto& xxx : v){cerr << xxx << " ";}cerr << "\n";}
inline void YES(bool x){cout<<((x)?"YES":"NO")<<endl;} inline void Yes(bool x){cout<<((x)?"Yes":"No")<<endl;}  inline void yes(bool x){cout<<((x)?"yes":"no")<<endl;}
inline void yES(bool x){cout<<((x)?"yES":"nO")<<endl;} inline void Yay(bool x){cout<<((x)?"Yay!":":(")<<endl;}
template<typename A,typename B> void sankou(bool x,A a,B b){cout<<((x)?(a):(b))<<endl;}
#define _overload3(_1,_2,_3,name,...) name
#define _REP(i,n) REPI(i,0,n)
#define REPI(i,a,b) for(ll i=ll(a);i<ll(b);++i)
#define REP(...) _overload3(__VA_ARGS__,REPI,_REP,)(__VA_ARGS__)
#define _RREP(i,n) RREPI(i,n,0)
#define RREPI(i,a,b) for(ll i=ll(a);i>=ll(b);--i)
#define RREP(...) _overload3(__VA_ARGS__,RREPI,_RREP,)(__VA_ARGS__)
#define EACH(e,v) for(auto& e : v)
#define PERM(v) sort((v).begin(),(v).end());for(bool c##p=1;c##p;c##p=next_permutation((v).begin(),(v).end()))
#define ADD(a,b) a=(a+ll(b))%MOD
#define MUL(a,b) a=(a*ll(b))%MOD
inline ll MOP(ll x,ll n,ll m=MOD){ll r=1;while(n>0){if(n&1)(r*=x)%=m;(x*=x)%=m;n>>=1;}return r;}
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}inline ll lcm(ll a,ll b){return a*b/gcd(a,b);}inline ll POW(ll a,ll b){ll c=1ll;do{if(b&1)c*=1ll*a;a*=1ll*a;}while(b>>=1);return c;}
template<typename T> inline bool between(T x,T a,T b) {return ((a<=x)&&(x<b));}template<class T> inline T sqr(T x){return x*x;}
template<typename A,typename B> inline bool chmax(A &a,const B &b){if(a<b){a=b;return 1;}return 0;}
template<typename A,typename B> inline bool chmin(A &a,const B &b){if(a>b){a=b;return 1;}return 0;}
#define tmax(x,y,z) max((x),max((y),(z)))
#define tmin(x,y,z) min((x),min((y),(z)))
#define PB push_back
#define MP make_pair
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define SORT(v) sort((v).begin(),(v).end())
#define RSORT(v) sort((v).rbegin(),(v).rend())
#define EXIST(s,e) (find((s).begin(),(s).end(),(e))!=(s).end())
#define EXISTST(s,c) (((s).find(c))!=string::npos)
#define POSL(x,val) (lower_bound(x.begin(),x.end(),val)-x.begin())
#define POSU(x,val) (upper_bound(x.begin(),x.end(),val)-x.begin())
#define SZV(a) int((a).size())
#define SZA(a) sizeof(a)/sizeof(a[0])
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
#define MEMINF(a) memset(a,0x3f,sizeof(a))
#define FILL(a,b) memset(a,b,sizeof(a))
#define UNIQUE(v) sort((v).begin(),(v).end());(v).erase(unique((v).begin(),(v).end()),(v).end())
struct abracadabra{
    abracadabra(){
        cin.tie(0); ios::sync_with_stdio(0);
        cout << fixed << setprecision(20);
    };
} ABRACADABRA;
//#define int long long

//---------------8<---------------8<---------------8<---------------8<---------------//

ll r_acc[100100][100100];

signed main() {

  SS(int,N,M);
  vl r_l[N];

  REP(i,N) {
    SS(int,S,R);
    r_l[S-1].PB(R);
  }

  int lim = -INF;
  REP(i,M) {
    RSORT(r_l[i]);
    chmax(lim, (int)r_l[i].size());
  }
  ER(lim);

  REP(i,M) {
    int sz = r_l[i].size();
    ll sum = 0;
    REP(j,1,sz+1) {
      sum += r_l[i][j-1];
      r_acc[i][j] = max(0LL,sum);
      if (sum < 0) break;
    }
  }

  ll res[100100] = {0LL};
  REP(i,M) {
    int sz = r_l[i].size();
    REP(j,1,sz+1) {
      res[j] += r_acc[i][j];
    }
  }
  ll ans = 0;
  REP(i,lim+1) {
    chmax(ans, res[i]);
  }
  print(ans);

  //REP(i,lim+1) print(res[i]);

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;       typedef long double ld;  typedef pair<int,int> pii; typedef pair<ll,ll> pll;  typedef vector<int> vi;   typedef vector<ll> vl;
typedef vector<string> vst; typedef vector<bool> vb; typedef vector<ld> vld;    typedef vector<pii> vpii; typedef vector<pll> vpll; typedef vector<vector<int> > vvi;
const int INF = (0x7FFFFFFFL); const ll INFF = (0x7FFFFFFFFFFFFFFFL); const string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const int MOD = 1e9 + 7;       const int MODD = 998244353;            const string alphabet = "abcdefghijklmnopqrstuvwxyz";
const double PI = acos(-1.0);  const double EPS = 1e-9;               const string Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
int dx[9] = { 1, 0, -1,  0,  1, -1, -1, 1, 0 };
int dy[9] = { 0, 1,  0, -1, -1, -1,  1, 1, 0 };
#define ln '\n'
#define scnaf scanf
#define sacnf scanf
#define sancf scanf
#define SS(type, ...)type __VA_ARGS__;MACRO_VAR_Scan(__VA_ARGS__);
template<typename T> void MACRO_VAR_Scan(T& t){cin >> t;}template<typename First, typename...Rest> void MACRO_VAR_Scan(First& first, Rest&...rest){cin >> first;MACRO_VAR_Scan(rest...);}
#define SV(type,c,n) vector<type> c(n);for(auto& i:c)cin >> i;
#define SVV(type,c,n,m) vector<vector<type>> c(n,vector<type>(m));for(auto& r:c)for(auto& i:r)cin >> i;
template<class T>ostream &operator<<(ostream &o,const vector<T>&j){o<<"{";for(int i=0;i<(int)j.size();++i)o<<(i>0?", ":"")<<j[i];o<<"}";return o;}
template<class T,class U>ostream &operator<<(ostream &o,const pair<T,U>&j){o<<"{"<<j.first<<", "<<j.second<<"}";return o;}
template<class T,class U>ostream &operator<<(ostream &o,const map<T,U>&j){o<<"{";for(auto t=j.begin();t!=j.end();++t)o<<(t!=j.begin()?", ":"")<<*t;o<<"}";return o;}
template<class T>ostream &operator<<(ostream &o,const set<T>&j){o<<"{";for(auto t=j.begin();t!=j.end();++t)o<<(t!=j.begin()?", ":"")<<*t;o<<"}";return o;}
inline void print(void){cout << endl;}
template<class Head> void print(Head&& head){cout << head;print();} template<class Head,class... Tail> void print(Head&& head,Tail&&... tail){cout<<head<<" ";print(forward<Tail>(tail)...);}
inline void debug(void){cerr << endl;}
template<class Head> void debug(Head&& head){cerr << head;debug();} template<class Head,class... Tail> void debug(Head&& head,Tail&&... tail){cerr<<head<<" ";debug(forward<Tail>(tail)...);}
template<typename T> void PA(T &a){int ASIZE=sizeof(a)/sizeof(a[0]);for(int ii=0;ii<ASIZE;++ii){cout<<a[ii]<<" \n"[ii==ASIZE-1];}}
template<typename T> void PV(T &v){int VSIZE=v.size();for(int ii=0;ii<VSIZE;++ii){cout<<v[ii]<<" \n"[ii==VSIZE-1];}}
#define ER(x)  cerr << #x << " = " << (x) << endl;
#define ERV(v) {cerr << #v << " : ";for(const auto& xxx : v){cerr << xxx << " ";}cerr << "\n";}
inline void YES(bool x){cout<<((x)?"YES":"NO")<<endl;} inline void Yes(bool x){cout<<((x)?"Yes":"No")<<endl;}  inline void yes(bool x){cout<<((x)?"yes":"no")<<endl;}
inline void yES(bool x){cout<<((x)?"yES":"nO")<<endl;} inline void Yay(bool x){cout<<((x)?"Yay!":":(")<<endl;}
template<typename A,typename B> void sankou(bool x,A a,B b){cout<<((x)?(a):(b))<<endl;}
#define _overload3(_1,_2,_3,name,...) name
#define _REP(i,n) REPI(i,0,n)
#define REPI(i,a,b) for(ll i=ll(a);i<ll(b);++i)
#define REP(...) _overload3(__VA_ARGS__,REPI,_REP,)(__VA_ARGS__)
#define _RREP(i,n) RREPI(i,n,0)
#define RREPI(i,a,b) for(ll i=ll(a);i>=ll(b);--i)
#define RREP(...) _overload3(__VA_ARGS__,RREPI,_RREP,)(__VA_ARGS__)
#define EACH(e,v) for(auto& e : v)
#define PERM(v) sort((v).begin(),(v).end());for(bool c##p=1;c##p;c##p=next_permutation((v).begin(),(v).end()))
#define ADD(a,b) a=(a+ll(b))%MOD
#define MUL(a,b) a=(a*ll(b))%MOD
inline ll MOP(ll x,ll n,ll m=MOD){ll r=1;while(n>0){if(n&1)(r*=x)%=m;(x*=x)%=m;n>>=1;}return r;}
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}inline ll lcm(ll a,ll b){return a*b/gcd(a,b);}inline ll POW(ll a,ll b){ll c=1ll;do{if(b&1)c*=1ll*a;a*=1ll*a;}while(b>>=1);return c;}
template<typename T> inline bool between(T x,T a,T b) {return ((a<=x)&&(x<b));}template<class T> inline T sqr(T x){return x*x;}
template<typename A,typename B> inline bool chmax(A &a,const B &b){if(a<b){a=b;return 1;}return 0;}
template<typename A,typename B> inline bool chmin(A &a,const B &b){if(a>b){a=b;return 1;}return 0;}
#define tmax(x,y,z) max((x),max((y),(z)))
#define tmin(x,y,z) min((x),min((y),(z)))
#define PB push_back
#define MP make_pair
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define SORT(v) sort((v).begin(),(v).end())
#define RSORT(v) sort((v).rbegin(),(v).rend())
#define EXIST(s,e) (find((s).begin(),(s).end(),(e))!=(s).end())
#define EXISTST(s,c) (((s).find(c))!=string::npos)
#define POSL(x,val) (lower_bound(x.begin(),x.end(),val)-x.begin())
#define POSU(x,val) (upper_bound(x.begin(),x.end(),val)-x.begin())
#define SZV(a) int((a).size())
#define SZA(a) sizeof(a)/sizeof(a[0])
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
#define MEMINF(a) memset(a,0x3f,sizeof(a))
#define FILL(a,b) memset(a,b,sizeof(a))
#define UNIQUE(v) sort((v).begin(),(v).end());(v).erase(unique((v).begin(),(v).end()),(v).end())
struct abracadabra{
    abracadabra(){
        cin.tie(0); ios::sync_with_stdio(0);
        cout << fixed << setprecision(20);
    };
} ABRACADABRA;
//#define int long long

//---------------8<---------------8<---------------8<---------------8<---------------//

signed main() {

  SS(int,N,M);
  vl r_l[M];

  REP(i,N) {
    SS(int,S,R);
    r_l[S-1].PB(R);
  }

  int lim = -INF;
  REP(i,M) {
    RSORT(r_l[i]);
    chmax(lim, (int)r_l[i].size());
  }

  vector<vl> r_acc(M,vl(lim+1,0));

  REP(i,M) {
    int sz = r_l[i].size();
    ll sum = 0;
    REP(j,1,sz+1) {
      sum += r_l[i][j-1];
      r_acc[i][j] = max(0LL,sum);
      if (sum < 0) break;
    }
  }

  ll res[100100] = {0LL};
  REP(i,M) {
    int sz = r_l[i].size();
    REP(j,1,sz+1) {
      res[j] += r_acc[i][j];
    }
  }
  ll ans = 0;
  REP(i,lim+1) {
    chmax(ans, res[i]);
  }
  print(ans);

  //REP(i,lim+1) print(res[i]);

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;       typedef long double ld;  typedef pair<int,int> pii; typedef pair<ll,ll> pll;  typedef vector<int> vi;   typedef vector<ll> vl;
typedef vector<string> vst; typedef vector<bool> vb; typedef vector<ld> vld;    typedef vector<pii> vpii; typedef vector<pll> vpll; typedef vector<vector<int> > vvi;
const int INF = (0x7FFFFFFFL); const ll INFF = (0x7FFFFFFFFFFFFFFFL); const string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const int MOD = 1e9 + 7;       const int MODD = 998244353;            const string alphabet = "abcdefghijklmnopqrstuvwxyz";
const double PI = acos(-1.0);  const double EPS = 1e-9;               const string Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
int dx[9] = { 1, 0, -1,  0,  1, -1, -1, 1, 0 };
int dy[9] = { 0, 1,  0, -1, -1, -1,  1, 1, 0 };
#define ln '\n'
#define scnaf scanf
#define sacnf scanf
#define sancf scanf
#define SS(type, ...)type __VA_ARGS__;MACRO_VAR_Scan(__VA_ARGS__);
template<typename T> void MACRO_VAR_Scan(T& t){cin >> t;}template<typename First, typename...Rest> void MACRO_VAR_Scan(First& first, Rest&...rest){cin >> first;MACRO_VAR_Scan(rest...);}
#define SV(type,c,n) vector<type> c(n);for(auto& i:c)cin >> i;
#define SVV(type,c,n,m) vector<vector<type>> c(n,vector<type>(m));for(auto& r:c)for(auto& i:r)cin >> i;
template<class T>ostream &operator<<(ostream &o,const vector<T>&j){o<<"{";for(int i=0;i<(int)j.size();++i)o<<(i>0?", ":"")<<j[i];o<<"}";return o;}
template<class T,class U>ostream &operator<<(ostream &o,const pair<T,U>&j){o<<"{"<<j.first<<", "<<j.second<<"}";return o;}
template<class T,class U>ostream &operator<<(ostream &o,const map<T,U>&j){o<<"{";for(auto t=j.begin();t!=j.end();++t)o<<(t!=j.begin()?", ":"")<<*t;o<<"}";return o;}
template<class T>ostream &operator<<(ostream &o,const set<T>&j){o<<"{";for(auto t=j.begin();t!=j.end();++t)o<<(t!=j.begin()?", ":"")<<*t;o<<"}";return o;}
inline void print(void){cout << endl;}
template<class Head> void print(Head&& head){cout << head;print();} template<class Head,class... Tail> void print(Head&& head,Tail&&... tail){cout<<head<<" ";print(forward<Tail>(tail)...);}
inline void debug(void){cerr << endl;}
template<class Head> void debug(Head&& head){cerr << head;debug();} template<class Head,class... Tail> void debug(Head&& head,Tail&&... tail){cerr<<head<<" ";debug(forward<Tail>(tail)...);}
template<typename T> void PA(T &a){int ASIZE=sizeof(a)/sizeof(a[0]);for(int ii=0;ii<ASIZE;++ii){cout<<a[ii]<<" \n"[ii==ASIZE-1];}}
template<typename T> void PV(T &v){int VSIZE=v.size();for(int ii=0;ii<VSIZE;++ii){cout<<v[ii]<<" \n"[ii==VSIZE-1];}}
#define ER(x)  cerr << #x << " = " << (x) << endl;
#define ERV(v) {cerr << #v << " : ";for(const auto& xxx : v){cerr << xxx << " ";}cerr << "\n";}
inline void YES(bool x){cout<<((x)?"YES":"NO")<<endl;} inline void Yes(bool x){cout<<((x)?"Yes":"No")<<endl;}  inline void yes(bool x){cout<<((x)?"yes":"no")<<endl;}
inline void yES(bool x){cout<<((x)?"yES":"nO")<<endl;} inline void Yay(bool x){cout<<((x)?"Yay!":":(")<<endl;}
template<typename A,typename B> void sankou(bool x,A a,B b){cout<<((x)?(a):(b))<<endl;}
#define _overload3(_1,_2,_3,name,...) name
#define _REP(i,n) REPI(i,0,n)
#define REPI(i,a,b) for(ll i=ll(a);i<ll(b);++i)
#define REP(...) _overload3(__VA_ARGS__,REPI,_REP,)(__VA_ARGS__)
#define _RREP(i,n) RREPI(i,n,0)
#define RREPI(i,a,b) for(ll i=ll(a);i>=ll(b);--i)
#define RREP(...) _overload3(__VA_ARGS__,RREPI,_RREP,)(__VA_ARGS__)
#define EACH(e,v) for(auto& e : v)
#define PERM(v) sort((v).begin(),(v).end());for(bool c##p=1;c##p;c##p=next_permutation((v).begin(),(v).end()))
#define ADD(a,b) a=(a+ll(b))%MOD
#define MUL(a,b) a=(a*ll(b))%MOD
inline ll MOP(ll x,ll n,ll m=MOD){ll r=1;while(n>0){if(n&1)(r*=x)%=m;(x*=x)%=m;n>>=1;}return r;}
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}inline ll lcm(ll a,ll b){return a*b/gcd(a,b);}inline ll POW(ll a,ll b){ll c=1ll;do{if(b&1)c*=1ll*a;a*=1ll*a;}while(b>>=1);return c;}
template<typename T> inline bool between(T x,T a,T b) {return ((a<=x)&&(x<b));}template<class T> inline T sqr(T x){return x*x;}
template<typename A,typename B> inline bool chmax(A &a,const B &b){if(a<b){a=b;return 1;}return 0;}
template<typename A,typename B> inline bool chmin(A &a,const B &b){if(a>b){a=b;return 1;}return 0;}
#define tmax(x,y,z) max((x),max((y),(z)))
#define tmin(x,y,z) min((x),min((y),(z)))
#define PB push_back
#define MP make_pair
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define SORT(v) sort((v).begin(),(v).end())
#define RSORT(v) sort((v).rbegin(),(v).rend())
#define EXIST(s,e) (find((s).begin(),(s).end(),(e))!=(s).end())
#define EXISTST(s,c) (((s).find(c))!=string::npos)
#define POSL(x,val) (lower_bound(x.begin(),x.end(),val)-x.begin())
#define POSU(x,val) (upper_bound(x.begin(),x.end(),val)-x.begin())
#define SZV(a) int((a).size())
#define SZA(a) sizeof(a)/sizeof(a[0])
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
#define MEMINF(a) memset(a,0x3f,sizeof(a))
#define FILL(a,b) memset(a,b,sizeof(a))
#define UNIQUE(v) sort((v).begin(),(v).end());(v).erase(unique((v).begin(),(v).end()),(v).end())
struct abracadabra{
    abracadabra(){
        cin.tie(0); ios::sync_with_stdio(0);
        cout << fixed << setprecision(20);
    };
} ABRACADABRA;
//#define int long long

//---------------8<---------------8<---------------8<---------------8<---------------//

signed main() {

  SS(int,N,M);
  vl r_l[N];

  REP(i,N) {
    SS(int,S,R);
    r_l[S-1].PB(R);
  }

  int lim = -INF;
  REP(i,M) {
    RSORT(r_l[i]);
    chmax(lim, (int)r_l[i].size());
  }

  vector<vl> r_acc(M,vl(lim+1,0));

  REP(i,M) {
    int sz = r_l[i].size();
    ll sum = 0;
    REP(j,1,sz+1) {
      sum += r_l[i][j-1];
      r_acc[i][j] = max(0LL,sum);
      if (sum < 0) break;
    }
  }

  ll res[100100] = {0LL};
  REP(i,M) {
    int sz = r_l[i].size();
    REP(j,1,sz+1) {
      res[j] += r_acc[i][j];
    }
  }
  ll ans = 0;
  REP(i,lim+1) {
    chmax(ans, res[i]);
  }
  print(ans);

  //REP(i,lim+1) print(res[i]);

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;       typedef long double ld;  typedef pair<int,int> pii; typedef pair<ll,ll> pll;  typedef vector<int> vi;   typedef vector<ll> vl;
typedef vector<string> vst; typedef vector<bool> vb; typedef vector<ld> vld;    typedef vector<pii> vpii; typedef vector<pll> vpll; typedef vector<vector<int> > vvi;
const int INF = (0x7FFFFFFFL); const ll INFF = (0x7FFFFFFFFFFFFFFFL); const string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const int MOD = 1e9 + 7;       const int MODD = 998244353;            const string alphabet = "abcdefghijklmnopqrstuvwxyz";
const double PI = acos(-1.0);  const double EPS = 1e-9;               const string Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
int dx[9] = { 1, 0, -1,  0,  1, -1, -1, 1, 0 };
int dy[9] = { 0, 1,  0, -1, -1, -1,  1, 1, 0 };
#define ln '\n'
#define scnaf scanf
#define sacnf scanf
#define sancf scanf
#define SS(type, ...)type __VA_ARGS__;MACRO_VAR_Scan(__VA_ARGS__);
template<typename T> void MACRO_VAR_Scan(T& t){cin >> t;}template<typename First, typename...Rest> void MACRO_VAR_Scan(First& first, Rest&...rest){cin >> first;MACRO_VAR_Scan(rest...);}
#define SV(type,c,n) vector<type> c(n);for(auto& i:c)cin >> i;
#define SVV(type,c,n,m) vector<vector<type>> c(n,vector<type>(m));for(auto& r:c)for(auto& i:r)cin >> i;
template<class T>ostream &operator<<(ostream &o,const vector<T>&j){o<<"{";for(int i=0;i<(int)j.size();++i)o<<(i>0?", ":"")<<j[i];o<<"}";return o;}
template<class T,class U>ostream &operator<<(ostream &o,const pair<T,U>&j){o<<"{"<<j.first<<", "<<j.second<<"}";return o;}
template<class T,class U>ostream &operator<<(ostream &o,const map<T,U>&j){o<<"{";for(auto t=j.begin();t!=j.end();++t)o<<(t!=j.begin()?", ":"")<<*t;o<<"}";return o;}
template<class T>ostream &operator<<(ostream &o,const set<T>&j){o<<"{";for(auto t=j.begin();t!=j.end();++t)o<<(t!=j.begin()?", ":"")<<*t;o<<"}";return o;}
inline void print(void){cout << endl;}
template<class Head> void print(Head&& head){cout << head;print();} template<class Head,class... Tail> void print(Head&& head,Tail&&... tail){cout<<head<<" ";print(forward<Tail>(tail)...);}
inline void debug(void){cerr << endl;}
template<class Head> void debug(Head&& head){cerr << head;debug();} template<class Head,class... Tail> void debug(Head&& head,Tail&&... tail){cerr<<head<<" ";debug(forward<Tail>(tail)...);}
template<typename T> void PA(T &a){int ASIZE=sizeof(a)/sizeof(a[0]);for(int ii=0;ii<ASIZE;++ii){cout<<a[ii]<<" \n"[ii==ASIZE-1];}}
template<typename T> void PV(T &v){int VSIZE=v.size();for(int ii=0;ii<VSIZE;++ii){cout<<v[ii]<<" \n"[ii==VSIZE-1];}}
#define ER(x)  cerr << #x << " = " << (x) << endl;
#define ERV(v) {cerr << #v << " : ";for(const auto& xxx : v){cerr << xxx << " ";}cerr << "\n";}
inline void YES(bool x){cout<<((x)?"YES":"NO")<<endl;} inline void Yes(bool x){cout<<((x)?"Yes":"No")<<endl;}  inline void yes(bool x){cout<<((x)?"yes":"no")<<endl;}
inline void yES(bool x){cout<<((x)?"yES":"nO")<<endl;} inline void Yay(bool x){cout<<((x)?"Yay!":":(")<<endl;}
template<typename A,typename B> void sankou(bool x,A a,B b){cout<<((x)?(a):(b))<<endl;}
#define _overload3(_1,_2,_3,name,...) name
#define _REP(i,n) REPI(i,0,n)
#define REPI(i,a,b) for(ll i=ll(a);i<ll(b);++i)
#define REP(...) _overload3(__VA_ARGS__,REPI,_REP,)(__VA_ARGS__)
#define _RREP(i,n) RREPI(i,n,0)
#define RREPI(i,a,b) for(ll i=ll(a);i>=ll(b);--i)
#define RREP(...) _overload3(__VA_ARGS__,RREPI,_RREP,)(__VA_ARGS__)
#define EACH(e,v) for(auto& e : v)
#define PERM(v) sort((v).begin(),(v).end());for(bool c##p=1;c##p;c##p=next_permutation((v).begin(),(v).end()))
#define ADD(a,b) a=(a+ll(b))%MOD
#define MUL(a,b) a=(a*ll(b))%MOD
inline ll MOP(ll x,ll n,ll m=MOD){ll r=1;while(n>0){if(n&1)(r*=x)%=m;(x*=x)%=m;n>>=1;}return r;}
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}inline ll lcm(ll a,ll b){return a*b/gcd(a,b);}inline ll POW(ll a,ll b){ll c=1ll;do{if(b&1)c*=1ll*a;a*=1ll*a;}while(b>>=1);return c;}
template<typename T> inline bool between(T x,T a,T b) {return ((a<=x)&&(x<b));}template<class T> inline T sqr(T x){return x*x;}
template<typename A,typename B> inline bool chmax(A &a,const B &b){if(a<b){a=b;return 1;}return 0;}
template<typename A,typename B> inline bool chmin(A &a,const B &b){if(a>b){a=b;return 1;}return 0;}
#define tmax(x,y,z) max((x),max((y),(z)))
#define tmin(x,y,z) min((x),min((y),(z)))
#define PB push_back
#define MP make_pair
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define SORT(v) sort((v).begin(),(v).end())
#define RSORT(v) sort((v).rbegin(),(v).rend())
#define EXIST(s,e) (find((s).begin(),(s).end(),(e))!=(s).end())
#define EXISTST(s,c) (((s).find(c))!=string::npos)
#define POSL(x,val) (lower_bound(x.begin(),x.end(),val)-x.begin())
#define POSU(x,val) (upper_bound(x.begin(),x.end(),val)-x.begin())
#define SZV(a) int((a).size())
#define SZA(a) sizeof(a)/sizeof(a[0])
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
#define MEMINF(a) memset(a,0x3f,sizeof(a))
#define FILL(a,b) memset(a,b,sizeof(a))
#define UNIQUE(v) sort((v).begin(),(v).end());(v).erase(unique((v).begin(),(v).end()),(v).end())
struct abracadabra{
    abracadabra(){
        cin.tie(0); ios::sync_with_stdio(0);
        cout << fixed << setprecision(20);
    };
} ABRACADABRA;
//#define int long long

//---------------8<---------------8<---------------8<---------------8<---------------//

ll r_acc[100100][100100];

signed main() {

  SS(int,N,M);
  vl r_l[N];

  REP(i,N) {
    SS(int,S,R);
    r_l[S-1].PB(R);
  }

  int lim = -INF;
  REP(i,M) {
    RSORT(r_l[i]);
    chmax(lim, (int)r_l[i].size());
  }

  REP(i,M) {
    int sz = r_l[i].size();
    ll sum = 0;
    REP(j,1,sz+1) {
      sum += r_l[i][j-1];
      r_acc[i][j] = max(0LL,sum);
      if (sum < 0) break;
    }
  }

  ll res[100100] = {0LL};
  REP(i,M) {
    int sz = r_l[i].size();
    REP(j,1,sz+1) {
      res[j] += r_acc[i][j];
    }
  }
  ll ans = 0;
  REP(i,lim+1) {
    chmax(ans, res[i]);
  }
  print(ans);

  //REP(i,lim+1) print(res[i]);

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;       typedef long double ld;  typedef pair<int,int> pii; typedef pair<ll,ll> pll;  typedef vector<int> vi;   typedef vector<ll> vl;
typedef vector<string> vst; typedef vector<bool> vb; typedef vector<ld> vld;    typedef vector<pii> vpii; typedef vector<pll> vpll; typedef vector<vector<int> > vvi;
const int INF = (0x7FFFFFFFL); const ll INFF = (0x7FFFFFFFFFFFFFFFL); const string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const int MOD = 1e9 + 7;       const int MODD = 998244353;            const string alphabet = "abcdefghijklmnopqrstuvwxyz";
const double PI = acos(-1.0);  const double EPS = 1e-9;               const string Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
int dx[9] = { 1, 0, -1,  0,  1, -1, -1, 1, 0 };
int dy[9] = { 0, 1,  0, -1, -1, -1,  1, 1, 0 };
#define ln '\n'
#define scnaf scanf
#define sacnf scanf
#define sancf scanf
#define SS(type, ...)type __VA_ARGS__;MACRO_VAR_Scan(__VA_ARGS__);
template<typename T> void MACRO_VAR_Scan(T& t){cin >> t;}template<typename First, typename...Rest> void MACRO_VAR_Scan(First& first, Rest&...rest){cin >> first;MACRO_VAR_Scan(rest...);}
#define SV(type,c,n) vector<type> c(n);for(auto& i:c)cin >> i;
#define SVV(type,c,n,m) vector<vector<type>> c(n,vector<type>(m));for(auto& r:c)for(auto& i:r)cin >> i;
template<class T>ostream &operator<<(ostream &o,const vector<T>&j){o<<"{";for(int i=0;i<(int)j.size();++i)o<<(i>0?", ":"")<<j[i];o<<"}";return o;}
template<class T,class U>ostream &operator<<(ostream &o,const pair<T,U>&j){o<<"{"<<j.first<<", "<<j.second<<"}";return o;}
template<class T,class U>ostream &operator<<(ostream &o,const map<T,U>&j){o<<"{";for(auto t=j.begin();t!=j.end();++t)o<<(t!=j.begin()?", ":"")<<*t;o<<"}";return o;}
template<class T>ostream &operator<<(ostream &o,const set<T>&j){o<<"{";for(auto t=j.begin();t!=j.end();++t)o<<(t!=j.begin()?", ":"")<<*t;o<<"}";return o;}
inline void print(void){cout << endl;}
template<class Head> void print(Head&& head){cout << head;print();} template<class Head,class... Tail> void print(Head&& head,Tail&&... tail){cout<<head<<" ";print(forward<Tail>(tail)...);}
inline void debug(void){cerr << endl;}
template<class Head> void debug(Head&& head){cerr << head;debug();} template<class Head,class... Tail> void debug(Head&& head,Tail&&... tail){cerr<<head<<" ";debug(forward<Tail>(tail)...);}
template<typename T> void PA(T &a){int ASIZE=sizeof(a)/sizeof(a[0]);for(int ii=0;ii<ASIZE;++ii){cout<<a[ii]<<" \n"[ii==ASIZE-1];}}
template<typename T> void PV(T &v){int VSIZE=v.size();for(int ii=0;ii<VSIZE;++ii){cout<<v[ii]<<" \n"[ii==VSIZE-1];}}
#define ER(x)  cerr << #x << " = " << (x) << endl;
#define ERV(v) {cerr << #v << " : ";for(const auto& xxx : v){cerr << xxx << " ";}cerr << "\n";}
inline void YES(bool x){cout<<((x)?"YES":"NO")<<endl;} inline void Yes(bool x){cout<<((x)?"Yes":"No")<<endl;}  inline void yes(bool x){cout<<((x)?"yes":"no")<<endl;}
inline void yES(bool x){cout<<((x)?"yES":"nO")<<endl;} inline void Yay(bool x){cout<<((x)?"Yay!":":(")<<endl;}
template<typename A,typename B> void sankou(bool x,A a,B b){cout<<((x)?(a):(b))<<endl;}
#define _overload3(_1,_2,_3,name,...) name
#define _REP(i,n) REPI(i,0,n)
#define REPI(i,a,b) for(ll i=ll(a);i<ll(b);++i)
#define REP(...) _overload3(__VA_ARGS__,REPI,_REP,)(__VA_ARGS__)
#define _RREP(i,n) RREPI(i,n,0)
#define RREPI(i,a,b) for(ll i=ll(a);i>=ll(b);--i)
#define RREP(...) _overload3(__VA_ARGS__,RREPI,_RREP,)(__VA_ARGS__)
#define EACH(e,v) for(auto& e : v)
#define PERM(v) sort((v).begin(),(v).end());for(bool c##p=1;c##p;c##p=next_permutation((v).begin(),(v).end()))
#define ADD(a,b) a=(a+ll(b))%MOD
#define MUL(a,b) a=(a*ll(b))%MOD
inline ll MOP(ll x,ll n,ll m=MOD){ll r=1;while(n>0){if(n&1)(r*=x)%=m;(x*=x)%=m;n>>=1;}return r;}
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}inline ll lcm(ll a,ll b){return a*b/gcd(a,b);}inline ll POW(ll a,ll b){ll c=1ll;do{if(b&1)c*=1ll*a;a*=1ll*a;}while(b>>=1);return c;}
template<typename T> inline bool between(T x,T a,T b) {return ((a<=x)&&(x<b));}template<class T> inline T sqr(T x){return x*x;}
template<typename A,typename B> inline bool chmax(A &a,const B &b){if(a<b){a=b;return 1;}return 0;}
template<typename A,typename B> inline bool chmin(A &a,const B &b){if(a>b){a=b;return 1;}return 0;}
#define tmax(x,y,z) max((x),max((y),(z)))
#define tmin(x,y,z) min((x),min((y),(z)))
#define PB push_back
#define MP make_pair
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define SORT(v) sort((v).begin(),(v).end())
#define RSORT(v) sort((v).rbegin(),(v).rend())
#define EXIST(s,e) (find((s).begin(),(s).end(),(e))!=(s).end())
#define EXISTST(s,c) (((s).find(c))!=string::npos)
#define POSL(x,val) (lower_bound(x.begin(),x.end(),val)-x.begin())
#define POSU(x,val) (upper_bound(x.begin(),x.end(),val)-x.begin())
#define SZV(a) int((a).size())
#define SZA(a) sizeof(a)/sizeof(a[0])
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
#define MEMINF(a) memset(a,0x3f,sizeof(a))
#define FILL(a,b) memset(a,b,sizeof(a))
#define UNIQUE(v) sort((v).begin(),(v).end());(v).erase(unique((v).begin(),(v).end()),(v).end())
struct abracadabra{
    abracadabra(){
        cin.tie(0); ios::sync_with_stdio(0);
        cout << fixed << setprecision(20);
    };
} ABRACADABRA;
//#define int long long

//---------------8<---------------8<---------------8<---------------8<---------------//

ll res[100100];

signed main() {

  SS(int,N,M);
  vl r_l[N];

  REP(i,N) {
    SS(int,S,R);
    r_l[S-1].PB(R);
  }

  int lim = -INF;
  REP(i,M) {
    RSORT(r_l[i]);
    chmax(lim, (int)r_l[i].size());
  }
  vector<vl> r_acc;
  REP(i,M) {
    int sz = r_l[i].size();
    ll sum = 0;
    vl tmp;
    REP(j,1,sz+1) {
      sum += r_l[i][j-1];
      tmp.PB(max(0LL,sum));
      if (sum < 0) break;
    }
    r_acc.PB(tmp);
  }

  REP(i,M) {
    int sz = r_acc[i].size();
    REP(j,1,sz+1) {
      res[j] += r_acc[i][j-1];
      //print(i,j,res[i],r_acc[i][j-1]);
    }
  }
  ll ans = 0;
  REP(i,lim+1) {
    chmax(ans, res[i]);
  }
  print(ans);

  //REP(i,lim+1) print(res[i]);
  //REP(i,M) print(r_acc[i]);
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;       typedef long double ld;  typedef pair<int,int> pii; typedef pair<ll,ll> pll;  typedef vector<int> vi;   typedef vector<ll> vl;
typedef vector<string> vst; typedef vector<bool> vb; typedef vector<ld> vld;    typedef vector<pii> vpii; typedef vector<pll> vpll; typedef vector<vector<int> > vvi;
const int INF = (0x7FFFFFFFL); const ll INFF = (0x7FFFFFFFFFFFFFFFL); const string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const int MOD = 1e9 + 7;       const int MODD = 998244353;            const string alphabet = "abcdefghijklmnopqrstuvwxyz";
const double PI = acos(-1.0);  const double EPS = 1e-9;               const string Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
int dx[9] = { 1, 0, -1,  0,  1, -1, -1, 1, 0 };
int dy[9] = { 0, 1,  0, -1, -1, -1,  1, 1, 0 };
#define ln '\n'
#define scnaf scanf
#define sacnf scanf
#define sancf scanf
#define SS(type, ...)type __VA_ARGS__;MACRO_VAR_Scan(__VA_ARGS__);
template<typename T> void MACRO_VAR_Scan(T& t){cin >> t;}template<typename First, typename...Rest> void MACRO_VAR_Scan(First& first, Rest&...rest){cin >> first;MACRO_VAR_Scan(rest...);}
#define SV(type,c,n) vector<type> c(n);for(auto& i:c)cin >> i;
#define SVV(type,c,n,m) vector<vector<type>> c(n,vector<type>(m));for(auto& r:c)for(auto& i:r)cin >> i;
template<class T>ostream &operator<<(ostream &o,const vector<T>&j){o<<"{";for(int i=0;i<(int)j.size();++i)o<<(i>0?", ":"")<<j[i];o<<"}";return o;}
template<class T,class U>ostream &operator<<(ostream &o,const pair<T,U>&j){o<<"{"<<j.first<<", "<<j.second<<"}";return o;}
template<class T,class U>ostream &operator<<(ostream &o,const map<T,U>&j){o<<"{";for(auto t=j.begin();t!=j.end();++t)o<<(t!=j.begin()?", ":"")<<*t;o<<"}";return o;}
template<class T>ostream &operator<<(ostream &o,const set<T>&j){o<<"{";for(auto t=j.begin();t!=j.end();++t)o<<(t!=j.begin()?", ":"")<<*t;o<<"}";return o;}
inline void print(void){cout << endl;}
template<class Head> void print(Head&& head){cout << head;print();} template<class Head,class... Tail> void print(Head&& head,Tail&&... tail){cout<<head<<" ";print(forward<Tail>(tail)...);}
inline void debug(void){cerr << endl;}
template<class Head> void debug(Head&& head){cerr << head;debug();} template<class Head,class... Tail> void debug(Head&& head,Tail&&... tail){cerr<<head<<" ";debug(forward<Tail>(tail)...);}
template<typename T> void PA(T &a){int ASIZE=sizeof(a)/sizeof(a[0]);for(int ii=0;ii<ASIZE;++ii){cout<<a[ii]<<" \n"[ii==ASIZE-1];}}
template<typename T> void PV(T &v){int VSIZE=v.size();for(int ii=0;ii<VSIZE;++ii){cout<<v[ii]<<" \n"[ii==VSIZE-1];}}
#define ER(x)  cerr << #x << " = " << (x) << endl;
#define ERV(v) {cerr << #v << " : ";for(const auto& xxx : v){cerr << xxx << " ";}cerr << "\n";}
inline void YES(bool x){cout<<((x)?"YES":"NO")<<endl;} inline void Yes(bool x){cout<<((x)?"Yes":"No")<<endl;}  inline void yes(bool x){cout<<((x)?"yes":"no")<<endl;}
inline void yES(bool x){cout<<((x)?"yES":"nO")<<endl;} inline void Yay(bool x){cout<<((x)?"Yay!":":(")<<endl;}
template<typename A,typename B> void sankou(bool x,A a,B b){cout<<((x)?(a):(b))<<endl;}
#define _overload3(_1,_2,_3,name,...) name
#define _REP(i,n) REPI(i,0,n)
#define REPI(i,a,b) for(ll i=ll(a);i<ll(b);++i)
#define REP(...) _overload3(__VA_ARGS__,REPI,_REP,)(__VA_ARGS__)
#define _RREP(i,n) RREPI(i,n,0)
#define RREPI(i,a,b) for(ll i=ll(a);i>=ll(b);--i)
#define RREP(...) _overload3(__VA_ARGS__,RREPI,_RREP,)(__VA_ARGS__)
#define EACH(e,v) for(auto& e : v)
#define PERM(v) sort((v).begin(),(v).end());for(bool c##p=1;c##p;c##p=next_permutation((v).begin(),(v).end()))
#define ADD(a,b) a=(a+ll(b))%MOD
#define MUL(a,b) a=(a*ll(b))%MOD
inline ll MOP(ll x,ll n,ll m=MOD){ll r=1;while(n>0){if(n&1)(r*=x)%=m;(x*=x)%=m;n>>=1;}return r;}
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}inline ll lcm(ll a,ll b){return a*b/gcd(a,b);}inline ll POW(ll a,ll b){ll c=1ll;do{if(b&1)c*=1ll*a;a*=1ll*a;}while(b>>=1);return c;}
template<typename T> inline bool between(T x,T a,T b) {return ((a<=x)&&(x<b));}template<class T> inline T sqr(T x){return x*x;}
template<typename A,typename B> inline bool chmax(A &a,const B &b){if(a<b){a=b;return 1;}return 0;}
template<typename A,typename B> inline bool chmin(A &a,const B &b){if(a>b){a=b;return 1;}return 0;}
#define tmax(x,y,z) max((x),max((y),(z)))
#define tmin(x,y,z) min((x),min((y),(z)))
#define PB push_back
#define MP make_pair
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define SORT(v) sort((v).begin(),(v).end())
#define RSORT(v) sort((v).rbegin(),(v).rend())
#define EXIST(s,e) (find((s).begin(),(s).end(),(e))!=(s).end())
#define EXISTST(s,c) (((s).find(c))!=string::npos)
#define POSL(x,val) (lower_bound(x.begin(),x.end(),val)-x.begin())
#define POSU(x,val) (upper_bound(x.begin(),x.end(),val)-x.begin())
#define SZV(a) int((a).size())
#define SZA(a) sizeof(a)/sizeof(a[0])
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
#define MEMINF(a) memset(a,0x3f,sizeof(a))
#define FILL(a,b) memset(a,b,sizeof(a))
#define UNIQUE(v) sort((v).begin(),(v).end());(v).erase(unique((v).begin(),(v).end()),(v).end())
struct abracadabra{
    abracadabra(){
        cin.tie(0); ios::sync_with_stdio(0);
        cout << fixed << setprecision(20);
    };
} ABRACADABRA;
//#define int long long

//---------------8<---------------8<---------------8<---------------8<---------------//

/*
・1次元累積和
  >     build O(n)
  > add,query O(1)
[使用例]
CumulativeSum<int> acc(n);      // 要素数nの累積和用の配列
acc.add(k,x);     // 要素kに値xを加える
acc.build();      // 累積和を取る
cout << acc.query(k) << endl;   // 区間[0,k]の和
*/

template<class T> struct CumulativeSum {
  vector<T> data;
  CumulativeSum(int sz) : data(sz, 0) {};
  void add(int k, int x) {
    data[k] += x;
  }
  void build() {
    for(int i = 1; i < data.size(); i++) {
      data[i] += data[i - 1];
    }
  }
  T query(int k)
  {
    if(k < 0) return (0);
    return (data[min(k, (int) data.size() - 1)]);
  }
};

signed main() {

  SS(int,N,M);
  vl r_l[M];

  REP(i,N) {
    SS(int,S,R);
    r_l[S-1].PB(R);
  }

  int lim = -INF;
  REP(i,M) {
    RSORT(r_l[i]);
    chmax(lim, (int)r_l[i].size());
    //ER(r_l[i].size());
  }
  //ER(lim);

  vvi r_acc;
  REP(i,M) {
    int sz = r_l[i].size();
    ll sum = 0;
    vi tmp;
    REP(j,lim+1) {
      if (j <= sz) {
        if (sum > 0) {
          tmp.PB(sum);
        } else {
          tmp.PB(0);
        }
      } else {
        tmp.PB(0);
      }
      if (j < sz) {
        sum += r_l[i][j];
      }
    }
    r_acc.PB(tmp);
  }

  ll res = 0;
  REP(i,lim+1) {
    ll tmp = 0;
    REP(j,M) {
      tmp += r_acc[j][i];
    }
    chmax(res,tmp);
  }
  print(res);

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;       typedef long double ld;  typedef pair<int,int> pii; typedef pair<ll,ll> pll;  typedef vector<int> vi;   typedef vector<ll> vl;
typedef vector<string> vst; typedef vector<bool> vb; typedef vector<ld> vld;    typedef vector<pii> vpii; typedef vector<pll> vpll; typedef vector<vector<int> > vvi;
const int INF = (0x7FFFFFFFL); const ll INFF = (0x7FFFFFFFFFFFFFFFL); const string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const int MOD = 1e9 + 7;       const int MODD = 998244353;            const string alphabet = "abcdefghijklmnopqrstuvwxyz";
const double PI = acos(-1.0);  const double EPS = 1e-9;               const string Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
int dx[9] = { 1, 0, -1,  0,  1, -1, -1, 1, 0 };
int dy[9] = { 0, 1,  0, -1, -1, -1,  1, 1, 0 };
#define ln '\n'
#define scnaf scanf
#define sacnf scanf
#define sancf scanf
#define SS(type, ...)type __VA_ARGS__;MACRO_VAR_Scan(__VA_ARGS__);
template<typename T> void MACRO_VAR_Scan(T& t){cin >> t;}template<typename First, typename...Rest> void MACRO_VAR_Scan(First& first, Rest&...rest){cin >> first;MACRO_VAR_Scan(rest...);}
#define SV(type,c,n) vector<type> c(n);for(auto& i:c)cin >> i;
#define SVV(type,c,n,m) vector<vector<type>> c(n,vector<type>(m));for(auto& r:c)for(auto& i:r)cin >> i;
template<class T>ostream &operator<<(ostream &o,const vector<T>&j){o<<"{";for(int i=0;i<(int)j.size();++i)o<<(i>0?", ":"")<<j[i];o<<"}";return o;}
template<class T,class U>ostream &operator<<(ostream &o,const pair<T,U>&j){o<<"{"<<j.first<<", "<<j.second<<"}";return o;}
template<class T,class U>ostream &operator<<(ostream &o,const map<T,U>&j){o<<"{";for(auto t=j.begin();t!=j.end();++t)o<<(t!=j.begin()?", ":"")<<*t;o<<"}";return o;}
template<class T>ostream &operator<<(ostream &o,const set<T>&j){o<<"{";for(auto t=j.begin();t!=j.end();++t)o<<(t!=j.begin()?", ":"")<<*t;o<<"}";return o;}
inline void print(void){cout << endl;}
template<class Head> void print(Head&& head){cout << head;print();} template<class Head,class... Tail> void print(Head&& head,Tail&&... tail){cout<<head<<" ";print(forward<Tail>(tail)...);}
inline void debug(void){cerr << endl;}
template<class Head> void debug(Head&& head){cerr << head;debug();} template<class Head,class... Tail> void debug(Head&& head,Tail&&... tail){cerr<<head<<" ";debug(forward<Tail>(tail)...);}
template<typename T> void PA(T &a){int ASIZE=sizeof(a)/sizeof(a[0]);for(int ii=0;ii<ASIZE;++ii){cout<<a[ii]<<" \n"[ii==ASIZE-1];}}
template<typename T> void PV(T &v){int VSIZE=v.size();for(int ii=0;ii<VSIZE;++ii){cout<<v[ii]<<" \n"[ii==VSIZE-1];}}
#define ER(x)  cerr << #x << " = " << (x) << endl;
#define ERV(v) {cerr << #v << " : ";for(const auto& xxx : v){cerr << xxx << " ";}cerr << "\n";}
inline void YES(bool x){cout<<((x)?"YES":"NO")<<endl;} inline void Yes(bool x){cout<<((x)?"Yes":"No")<<endl;}  inline void yes(bool x){cout<<((x)?"yes":"no")<<endl;}
inline void yES(bool x){cout<<((x)?"yES":"nO")<<endl;} inline void Yay(bool x){cout<<((x)?"Yay!":":(")<<endl;}
template<typename A,typename B> void sankou(bool x,A a,B b){cout<<((x)?(a):(b))<<endl;}
#define _overload3(_1,_2,_3,name,...) name
#define _REP(i,n) REPI(i,0,n)
#define REPI(i,a,b) for(ll i=ll(a);i<ll(b);++i)
#define REP(...) _overload3(__VA_ARGS__,REPI,_REP,)(__VA_ARGS__)
#define _RREP(i,n) RREPI(i,n,0)
#define RREPI(i,a,b) for(ll i=ll(a);i>=ll(b);--i)
#define RREP(...) _overload3(__VA_ARGS__,RREPI,_RREP,)(__VA_ARGS__)
#define EACH(e,v) for(auto& e : v)
#define PERM(v) sort((v).begin(),(v).end());for(bool c##p=1;c##p;c##p=next_permutation((v).begin(),(v).end()))
#define ADD(a,b) a=(a+ll(b))%MOD
#define MUL(a,b) a=(a*ll(b))%MOD
inline ll MOP(ll x,ll n,ll m=MOD){ll r=1;while(n>0){if(n&1)(r*=x)%=m;(x*=x)%=m;n>>=1;}return r;}
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}inline ll lcm(ll a,ll b){return a*b/gcd(a,b);}inline ll POW(ll a,ll b){ll c=1ll;do{if(b&1)c*=1ll*a;a*=1ll*a;}while(b>>=1);return c;}
template<typename T> inline bool between(T x,T a,T b) {return ((a<=x)&&(x<b));}template<class T> inline T sqr(T x){return x*x;}
template<typename A,typename B> inline bool chmax(A &a,const B &b){if(a<b){a=b;return 1;}return 0;}
template<typename A,typename B> inline bool chmin(A &a,const B &b){if(a>b){a=b;return 1;}return 0;}
#define tmax(x,y,z) max((x),max((y),(z)))
#define tmin(x,y,z) min((x),min((y),(z)))
#define PB push_back
#define MP make_pair
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define SORT(v) sort((v).begin(),(v).end())
#define RSORT(v) sort((v).rbegin(),(v).rend())
#define EXIST(s,e) (find((s).begin(),(s).end(),(e))!=(s).end())
#define EXISTST(s,c) (((s).find(c))!=string::npos)
#define POSL(x,val) (lower_bound(x.begin(),x.end(),val)-x.begin())
#define POSU(x,val) (upper_bound(x.begin(),x.end(),val)-x.begin())
#define SZV(a) int((a).size())
#define SZA(a) sizeof(a)/sizeof(a[0])
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
#define MEMINF(a) memset(a,0x3f,sizeof(a))
#define FILL(a,b) memset(a,b,sizeof(a))
#define UNIQUE(v) sort((v).begin(),(v).end());(v).erase(unique((v).begin(),(v).end()),(v).end())
struct abracadabra{
    abracadabra(){
        cin.tie(0); ios::sync_with_stdio(0);
        cout << fixed << setprecision(20);
    };
} ABRACADABRA;
//#define int long long

//---------------8<---------------8<---------------8<---------------8<---------------//

signed main() {

  SS(int,N,M);
  vl r_l[N];

  REP(i,N) {
    SS(int,S,R);
    r_l[S-1].PB(R);
  }

  int lim = -INF;
  REP(i,M) {
    RSORT(r_l[i]);
    chmax(lim, (int)r_l[i].size());
  }
  vector<vl> r_acc;
  REP(i,M) {
    int sz = r_l[i].size();
    ll sum = 0;
    vl tmp;
    REP(j,1,sz+1) {
      sum += r_l[i][j-1];
      tmp.PB(max(0LL,sum));
      if (sum < 0) break;
    }
    r_acc.PB(tmp);
  }

  ll res[100100] = {0LL};
  REP(i,M) {
    int sz = r_l[i].size();
    REP(j,1,sz+1) {
      res[j] += r_acc[i][j];
    }
  }
  ll ans = 0;
  REP(i,lim+1) {
    chmax(ans, res[i]);
  }
  print(ans);

  //REP(i,lim+1) print(res[i]);

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;       typedef long double ld;  typedef pair<int,int> pii; typedef pair<ll,ll> pll;  typedef vector<int> vi;   typedef vector<ll> vl;
typedef vector<string> vst; typedef vector<bool> vb; typedef vector<ld> vld;    typedef vector<pii> vpii; typedef vector<pll> vpll; typedef vector<vector<int> > vvi;
const int INF = (0x7FFFFFFFL); const ll INFF = (0x7FFFFFFFFFFFFFFFL); const string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const int MOD = 1e9 + 7;       const int MODD = 998244353;            const string alphabet = "abcdefghijklmnopqrstuvwxyz";
const double PI = acos(-1.0);  const double EPS = 1e-9;               const string Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
int dx[9] = { 1, 0, -1,  0,  1, -1, -1, 1, 0 };
int dy[9] = { 0, 1,  0, -1, -1, -1,  1, 1, 0 };
#define ln '\n'
#define scnaf scanf
#define sacnf scanf
#define sancf scanf
#define SS(type, ...)type __VA_ARGS__;MACRO_VAR_Scan(__VA_ARGS__);
template<typename T> void MACRO_VAR_Scan(T& t){cin >> t;}template<typename First, typename...Rest> void MACRO_VAR_Scan(First& first, Rest&...rest){cin >> first;MACRO_VAR_Scan(rest...);}
#define SV(type,c,n) vector<type> c(n);for(auto& i:c)cin >> i;
#define SVV(type,c,n,m) vector<vector<type>> c(n,vector<type>(m));for(auto& r:c)for(auto& i:r)cin >> i;
template<class T>ostream &operator<<(ostream &o,const vector<T>&j){o<<"{";for(int i=0;i<(int)j.size();++i)o<<(i>0?", ":"")<<j[i];o<<"}";return o;}
template<class T,class U>ostream &operator<<(ostream &o,const pair<T,U>&j){o<<"{"<<j.first<<", "<<j.second<<"}";return o;}
template<class T,class U>ostream &operator<<(ostream &o,const map<T,U>&j){o<<"{";for(auto t=j.begin();t!=j.end();++t)o<<(t!=j.begin()?", ":"")<<*t;o<<"}";return o;}
template<class T>ostream &operator<<(ostream &o,const set<T>&j){o<<"{";for(auto t=j.begin();t!=j.end();++t)o<<(t!=j.begin()?", ":"")<<*t;o<<"}";return o;}
inline void print(void){cout << endl;}
template<class Head> void print(Head&& head){cout << head;print();} template<class Head,class... Tail> void print(Head&& head,Tail&&... tail){cout<<head<<" ";print(forward<Tail>(tail)...);}
inline void debug(void){cerr << endl;}
template<class Head> void debug(Head&& head){cerr << head;debug();} template<class Head,class... Tail> void debug(Head&& head,Tail&&... tail){cerr<<head<<" ";debug(forward<Tail>(tail)...);}
template<typename T> void PA(T &a){int ASIZE=sizeof(a)/sizeof(a[0]);for(int ii=0;ii<ASIZE;++ii){cout<<a[ii]<<" \n"[ii==ASIZE-1];}}
template<typename T> void PV(T &v){int VSIZE=v.size();for(int ii=0;ii<VSIZE;++ii){cout<<v[ii]<<" \n"[ii==VSIZE-1];}}
#define ER(x)  cerr << #x << " = " << (x) << endl;
#define ERV(v) {cerr << #v << " : ";for(const auto& xxx : v){cerr << xxx << " ";}cerr << "\n";}
inline void YES(bool x){cout<<((x)?"YES":"NO")<<endl;} inline void Yes(bool x){cout<<((x)?"Yes":"No")<<endl;}  inline void yes(bool x){cout<<((x)?"yes":"no")<<endl;}
inline void yES(bool x){cout<<((x)?"yES":"nO")<<endl;} inline void Yay(bool x){cout<<((x)?"Yay!":":(")<<endl;}
template<typename A,typename B> void sankou(bool x,A a,B b){cout<<((x)?(a):(b))<<endl;}
#define _overload3(_1,_2,_3,name,...) name
#define _REP(i,n) REPI(i,0,n)
#define REPI(i,a,b) for(ll i=ll(a);i<ll(b);++i)
#define REP(...) _overload3(__VA_ARGS__,REPI,_REP,)(__VA_ARGS__)
#define _RREP(i,n) RREPI(i,n,0)
#define RREPI(i,a,b) for(ll i=ll(a);i>=ll(b);--i)
#define RREP(...) _overload3(__VA_ARGS__,RREPI,_RREP,)(__VA_ARGS__)
#define EACH(e,v) for(auto& e : v)
#define PERM(v) sort((v).begin(),(v).end());for(bool c##p=1;c##p;c##p=next_permutation((v).begin(),(v).end()))
#define ADD(a,b) a=(a+ll(b))%MOD
#define MUL(a,b) a=(a*ll(b))%MOD
inline ll MOP(ll x,ll n,ll m=MOD){ll r=1;while(n>0){if(n&1)(r*=x)%=m;(x*=x)%=m;n>>=1;}return r;}
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}inline ll lcm(ll a,ll b){return a*b/gcd(a,b);}inline ll POW(ll a,ll b){ll c=1ll;do{if(b&1)c*=1ll*a;a*=1ll*a;}while(b>>=1);return c;}
template<typename T> inline bool between(T x,T a,T b) {return ((a<=x)&&(x<b));}template<class T> inline T sqr(T x){return x*x;}
template<typename A,typename B> inline bool chmax(A &a,const B &b){if(a<b){a=b;return 1;}return 0;}
template<typename A,typename B> inline bool chmin(A &a,const B &b){if(a>b){a=b;return 1;}return 0;}
#define tmax(x,y,z) max((x),max((y),(z)))
#define tmin(x,y,z) min((x),min((y),(z)))
#define PB push_back
#define MP make_pair
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define SORT(v) sort((v).begin(),(v).end())
#define RSORT(v) sort((v).rbegin(),(v).rend())
#define EXIST(s,e) (find((s).begin(),(s).end(),(e))!=(s).end())
#define EXISTST(s,c) (((s).find(c))!=string::npos)
#define POSL(x,val) (lower_bound(x.begin(),x.end(),val)-x.begin())
#define POSU(x,val) (upper_bound(x.begin(),x.end(),val)-x.begin())
#define SZV(a) int((a).size())
#define SZA(a) sizeof(a)/sizeof(a[0])
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
#define MEMINF(a) memset(a,0x3f,sizeof(a))
#define FILL(a,b) memset(a,b,sizeof(a))
#define UNIQUE(v) sort((v).begin(),(v).end());(v).erase(unique((v).begin(),(v).end()),(v).end())
struct abracadabra{
    abracadabra(){
        cin.tie(0); ios::sync_with_stdio(0);
        cout << fixed << setprecision(20);
    };
} ABRACADABRA;
//#define int long long

//---------------8<---------------8<---------------8<---------------8<---------------//

/*
・1次元累積和
  >     build O(n)
  > add,query O(1)
[使用例]
CumulativeSum<int> acc(n);      // 要素数nの累積和用の配列
acc.add(k,x);     // 要素kに値xを加える
acc.build();      // 累積和を取る
cout << acc.query(k) << endl;   // 区間[0,k]の和
*/

template<class T> struct CumulativeSum {
  vector<T> data;
  CumulativeSum(int sz) : data(sz, 0) {};
  void add(int k, int x) {
    data[k] += x;
  }
  void build() {
    for(int i = 1; i < data.size(); i++) {
      data[i] += data[i - 1];
    }
  }
  T query(int k)
  {
    if(k < 0) return (0);
    return (data[min(k, (int) data.size() - 1)]);
  }
};

signed main() {

  SS(int,N,M);
  vl r_l[N];

  REP(i,N) {
    SS(int,S,R);
    r_l[S-1].PB(R);
  }

  int lim = -INF;
  REP(i,M) {
    RSORT(r_l[i]);
    chmax(lim, (int)r_l[i].size());
    //ER(r_l[i].size());
  }
  //ER(lim);

  vvi r_acc;
  REP(i,M) {
    int sz = r_l[i].size();
    ll sum = 0;
    vi tmp;
    REP(j,lim+1) {
      if (j <= sz) {
        if (sum > 0) {
          tmp.PB(sum);
        } else {
          tmp.PB(0);
        }
      } else {
        tmp.PB(0);
      }
      if (j < sz) {
        sum += r_l[i][j];
      }
    }
    r_acc.PB(tmp);
  }

  ll res = 0;
  REP(i,lim+1) {
    ll tmp = 0;
    REP(j,M) {
      tmp += r_acc[j][i];
    }
    chmax(res,tmp);
  }
  print(res);

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;       typedef long double ld;  typedef pair<int,int> pii; typedef pair<ll,ll> pll;  typedef vector<int> vi;   typedef vector<ll> vl;
typedef vector<string> vst; typedef vector<bool> vb; typedef vector<ld> vld;    typedef vector<pii> vpii; typedef vector<pll> vpll; typedef vector<vector<int> > vvi;
const int INF = (0x7FFFFFFFL); const ll INFF = (0x7FFFFFFFFFFFFFFFL); const string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const int MOD = 1e9 + 7;       const int MODD = 998244353;            const string alphabet = "abcdefghijklmnopqrstuvwxyz";
const double PI = acos(-1.0);  const double EPS = 1e-9;               const string Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
int dx[9] = { 1, 0, -1,  0,  1, -1, -1, 1, 0 };
int dy[9] = { 0, 1,  0, -1, -1, -1,  1, 1, 0 };
#define ln '\n'
#define scnaf scanf
#define sacnf scanf
#define sancf scanf
#define SS(type, ...)type __VA_ARGS__;MACRO_VAR_Scan(__VA_ARGS__);
template<typename T> void MACRO_VAR_Scan(T& t){cin >> t;}template<typename First, typename...Rest> void MACRO_VAR_Scan(First& first, Rest&...rest){cin >> first;MACRO_VAR_Scan(rest...);}
#define SV(type,c,n) vector<type> c(n);for(auto& i:c)cin >> i;
#define SVV(type,c,n,m) vector<vector<type>> c(n,vector<type>(m));for(auto& r:c)for(auto& i:r)cin >> i;
template<class T>ostream &operator<<(ostream &o,const vector<T>&j){o<<"{";for(int i=0;i<(int)j.size();++i)o<<(i>0?", ":"")<<j[i];o<<"}";return o;}
template<class T,class U>ostream &operator<<(ostream &o,const pair<T,U>&j){o<<"{"<<j.first<<", "<<j.second<<"}";return o;}
template<class T,class U>ostream &operator<<(ostream &o,const map<T,U>&j){o<<"{";for(auto t=j.begin();t!=j.end();++t)o<<(t!=j.begin()?", ":"")<<*t;o<<"}";return o;}
template<class T>ostream &operator<<(ostream &o,const set<T>&j){o<<"{";for(auto t=j.begin();t!=j.end();++t)o<<(t!=j.begin()?", ":"")<<*t;o<<"}";return o;}
inline void print(void){cout << endl;}
template<class Head> void print(Head&& head){cout << head;print();} template<class Head,class... Tail> void print(Head&& head,Tail&&... tail){cout<<head<<" ";print(forward<Tail>(tail)...);}
inline void debug(void){cerr << endl;}
template<class Head> void debug(Head&& head){cerr << head;debug();} template<class Head,class... Tail> void debug(Head&& head,Tail&&... tail){cerr<<head<<" ";debug(forward<Tail>(tail)...);}
template<typename T> void PA(T &a){int ASIZE=sizeof(a)/sizeof(a[0]);for(int ii=0;ii<ASIZE;++ii){cout<<a[ii]<<" \n"[ii==ASIZE-1];}}
template<typename T> void PV(T &v){int VSIZE=v.size();for(int ii=0;ii<VSIZE;++ii){cout<<v[ii]<<" \n"[ii==VSIZE-1];}}
#define ER(x)  cerr << #x << " = " << (x) << endl;
#define ERV(v) {cerr << #v << " : ";for(const auto& xxx : v){cerr << xxx << " ";}cerr << "\n";}
inline void YES(bool x){cout<<((x)?"YES":"NO")<<endl;} inline void Yes(bool x){cout<<((x)?"Yes":"No")<<endl;}  inline void yes(bool x){cout<<((x)?"yes":"no")<<endl;}
inline void yES(bool x){cout<<((x)?"yES":"nO")<<endl;} inline void Yay(bool x){cout<<((x)?"Yay!":":(")<<endl;}
template<typename A,typename B> void sankou(bool x,A a,B b){cout<<((x)?(a):(b))<<endl;}
#define _overload3(_1,_2,_3,name,...) name
#define _REP(i,n) REPI(i,0,n)
#define REPI(i,a,b) for(ll i=ll(a);i<ll(b);++i)
#define REP(...) _overload3(__VA_ARGS__,REPI,_REP,)(__VA_ARGS__)
#define _RREP(i,n) RREPI(i,n,0)
#define RREPI(i,a,b) for(ll i=ll(a);i>=ll(b);--i)
#define RREP(...) _overload3(__VA_ARGS__,RREPI,_RREP,)(__VA_ARGS__)
#define EACH(e,v) for(auto& e : v)
#define PERM(v) sort((v).begin(),(v).end());for(bool c##p=1;c##p;c##p=next_permutation((v).begin(),(v).end()))
#define ADD(a,b) a=(a+ll(b))%MOD
#define MUL(a,b) a=(a*ll(b))%MOD
inline ll MOP(ll x,ll n,ll m=MOD){ll r=1;while(n>0){if(n&1)(r*=x)%=m;(x*=x)%=m;n>>=1;}return r;}
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}inline ll lcm(ll a,ll b){return a*b/gcd(a,b);}inline ll POW(ll a,ll b){ll c=1ll;do{if(b&1)c*=1ll*a;a*=1ll*a;}while(b>>=1);return c;}
template<typename T> inline bool between(T x,T a,T b) {return ((a<=x)&&(x<b));}template<class T> inline T sqr(T x){return x*x;}
template<typename A,typename B> inline bool chmax(A &a,const B &b){if(a<b){a=b;return 1;}return 0;}
template<typename A,typename B> inline bool chmin(A &a,const B &b){if(a>b){a=b;return 1;}return 0;}
#define tmax(x,y,z) max((x),max((y),(z)))
#define tmin(x,y,z) min((x),min((y),(z)))
#define PB push_back
#define MP make_pair
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define SORT(v) sort((v).begin(),(v).end())
#define RSORT(v) sort((v).rbegin(),(v).rend())
#define EXIST(s,e) (find((s).begin(),(s).end(),(e))!=(s).end())
#define EXISTST(s,c) (((s).find(c))!=string::npos)
#define POSL(x,val) (lower_bound(x.begin(),x.end(),val)-x.begin())
#define POSU(x,val) (upper_bound(x.begin(),x.end(),val)-x.begin())
#define SZV(a) int((a).size())
#define SZA(a) sizeof(a)/sizeof(a[0])
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
#define MEMINF(a) memset(a,0x3f,sizeof(a))
#define FILL(a,b) memset(a,b,sizeof(a))
#define UNIQUE(v) sort((v).begin(),(v).end());(v).erase(unique((v).begin(),(v).end()),(v).end())
struct abracadabra{
    abracadabra(){
        cin.tie(0); ios::sync_with_stdio(0);
        cout << fixed << setprecision(20);
    };
} ABRACADABRA;
//#define int long long

//---------------8<---------------8<---------------8<---------------8<---------------//

ll res[100100];

signed main() {

  SS(int,N,M);
  vl r_l[M];

  REP(i,N) {
    SS(int,S,R);
    r_l[S-1].PB(R);
  }

  int lim = -INF;
  REP(i,M) {
    RSORT(r_l[i]);
    int sz = r_l[i].size();
    if (lim < sz) {
      lim = sz;
    }
  }

  vector< vector<ll> > r_acc;
  REP(i,M) {
    int sz = r_l[i].size();
    ll sum = 0;
    vl tmp;
    REP(j,1,sz+1) {
      sum += r_l[i][j-1];
      if (sum >= 0) {
        tmp.PB(sum);
      } else {
        break;
      }
    }
    r_acc.PB(tmp);
  }

  REP(i,M) {
    int sz = r_acc[i].size();
    REP(j,1,sz+1) {
      res[j] += r_acc[i][j-1];
    }
  }

  ll ans = 0;
  REP(i,lim+1) {
    if (ans < res[i]) ans = res[i];
  }
  cout << (ans) << ln;

  //REP(i,lim+1) print(res[i]);
  //REP(i,M) print(r_acc[i]);

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;       typedef long double ld;  typedef pair<int,int> pii; typedef pair<ll,ll> pll;  typedef vector<int> vi;   typedef vector<ll> vl;
typedef vector<string> vst; typedef vector<bool> vb; typedef vector<ld> vld;    typedef vector<pii> vpii; typedef vector<pll> vpll; typedef vector<vector<int> > vvi;
const int INF = (0x7FFFFFFFL); const ll INFF = (0x7FFFFFFFFFFFFFFFL); const string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const int MOD = 1e9 + 7;       const int MODD = 998244353;            const string alphabet = "abcdefghijklmnopqrstuvwxyz";
const double PI = acos(-1.0);  const double EPS = 1e-9;               const string Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
int dx[9] = { 1, 0, -1,  0,  1, -1, -1, 1, 0 };
int dy[9] = { 0, 1,  0, -1, -1, -1,  1, 1, 0 };
#define ln '\n'
#define scnaf scanf
#define sacnf scanf
#define sancf scanf
#define SS(type, ...)type __VA_ARGS__;MACRO_VAR_Scan(__VA_ARGS__);
template<typename T> void MACRO_VAR_Scan(T& t){cin >> t;}template<typename First, typename...Rest> void MACRO_VAR_Scan(First& first, Rest&...rest){cin >> first;MACRO_VAR_Scan(rest...);}
#define SV(type,c,n) vector<type> c(n);for(auto& i:c)cin >> i;
#define SVV(type,c,n,m) vector<vector<type>> c(n,vector<type>(m));for(auto& r:c)for(auto& i:r)cin >> i;
template<class T>ostream &operator<<(ostream &o,const vector<T>&j){o<<"{";for(int i=0;i<(int)j.size();++i)o<<(i>0?", ":"")<<j[i];o<<"}";return o;}
template<class T,class U>ostream &operator<<(ostream &o,const pair<T,U>&j){o<<"{"<<j.first<<", "<<j.second<<"}";return o;}
template<class T,class U>ostream &operator<<(ostream &o,const map<T,U>&j){o<<"{";for(auto t=j.begin();t!=j.end();++t)o<<(t!=j.begin()?", ":"")<<*t;o<<"}";return o;}
template<class T>ostream &operator<<(ostream &o,const set<T>&j){o<<"{";for(auto t=j.begin();t!=j.end();++t)o<<(t!=j.begin()?", ":"")<<*t;o<<"}";return o;}
inline void print(void){cout << endl;}
template<class Head> void print(Head&& head){cout << head;print();} template<class Head,class... Tail> void print(Head&& head,Tail&&... tail){cout<<head<<" ";print(forward<Tail>(tail)...);}
inline void debug(void){cerr << endl;}
template<class Head> void debug(Head&& head){cerr << head;debug();} template<class Head,class... Tail> void debug(Head&& head,Tail&&... tail){cerr<<head<<" ";debug(forward<Tail>(tail)...);}
template<typename T> void PA(T &a){int ASIZE=sizeof(a)/sizeof(a[0]);for(int ii=0;ii<ASIZE;++ii){cout<<a[ii]<<" \n"[ii==ASIZE-1];}}
template<typename T> void PV(T &v){int VSIZE=v.size();for(int ii=0;ii<VSIZE;++ii){cout<<v[ii]<<" \n"[ii==VSIZE-1];}}
#define ER(x)  cerr << #x << " = " << (x) << endl;
#define ERV(v) {cerr << #v << " : ";for(const auto& xxx : v){cerr << xxx << " ";}cerr << "\n";}
inline void YES(bool x){cout<<((x)?"YES":"NO")<<endl;} inline void Yes(bool x){cout<<((x)?"Yes":"No")<<endl;}  inline void yes(bool x){cout<<((x)?"yes":"no")<<endl;}
inline void yES(bool x){cout<<((x)?"yES":"nO")<<endl;} inline void Yay(bool x){cout<<((x)?"Yay!":":(")<<endl;}
template<typename A,typename B> void sankou(bool x,A a,B b){cout<<((x)?(a):(b))<<endl;}
#define _overload3(_1,_2,_3,name,...) name
#define _REP(i,n) REPI(i,0,n)
#define REPI(i,a,b) for(ll i=ll(a);i<ll(b);++i)
#define REP(...) _overload3(__VA_ARGS__,REPI,_REP,)(__VA_ARGS__)
#define _RREP(i,n) RREPI(i,n,0)
#define RREPI(i,a,b) for(ll i=ll(a);i>=ll(b);--i)
#define RREP(...) _overload3(__VA_ARGS__,RREPI,_RREP,)(__VA_ARGS__)
#define EACH(e,v) for(auto& e : v)
#define PERM(v) sort((v).begin(),(v).end());for(bool c##p=1;c##p;c##p=next_permutation((v).begin(),(v).end()))
#define ADD(a,b) a=(a+ll(b))%MOD
#define MUL(a,b) a=(a*ll(b))%MOD
inline ll MOP(ll x,ll n,ll m=MOD){ll r=1;while(n>0){if(n&1)(r*=x)%=m;(x*=x)%=m;n>>=1;}return r;}
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}inline ll lcm(ll a,ll b){return a*b/gcd(a,b);}inline ll POW(ll a,ll b){ll c=1ll;do{if(b&1)c*=1ll*a;a*=1ll*a;}while(b>>=1);return c;}
template<typename T> inline bool between(T x,T a,T b) {return ((a<=x)&&(x<b));}template<class T> inline T sqr(T x){return x*x;}
template<typename A,typename B> inline bool chmax(A &a,const B &b){if(a<b){a=b;return 1;}return 0;}
template<typename A,typename B> inline bool chmin(A &a,const B &b){if(a>b){a=b;return 1;}return 0;}
#define tmax(x,y,z) max((x),max((y),(z)))
#define tmin(x,y,z) min((x),min((y),(z)))
#define PB push_back
#define MP make_pair
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define SORT(v) sort((v).begin(),(v).end())
#define RSORT(v) sort((v).rbegin(),(v).rend())
#define EXIST(s,e) (find((s).begin(),(s).end(),(e))!=(s).end())
#define EXISTST(s,c) (((s).find(c))!=string::npos)
#define POSL(x,val) (lower_bound(x.begin(),x.end(),val)-x.begin())
#define POSU(x,val) (upper_bound(x.begin(),x.end(),val)-x.begin())
#define SZV(a) int((a).size())
#define SZA(a) sizeof(a)/sizeof(a[0])
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
#define MEMINF(a) memset(a,0x3f,sizeof(a))
#define FILL(a,b) memset(a,b,sizeof(a))
#define UNIQUE(v) sort((v).begin(),(v).end());(v).erase(unique((v).begin(),(v).end()),(v).end())
struct abracadabra{
    abracadabra(){
        cin.tie(0); ios::sync_with_stdio(0);
        cout << fixed << setprecision(20);
    };
} ABRACADABRA;
//#define int long long

//---------------8<---------------8<---------------8<---------------8<---------------//

ll res[100100];

signed main() {

  SS(int,N,M);
  vl r_l[N];

  REP(i,N) {
    SS(int,S,R);
    r_l[S-1].PB(R);
  }

  int lim = -INF;
  REP(i,M) {
    RSORT(r_l[i]);
    int sz = r_l[i].size();
    if (lim < sz) {
      lim = sz;
    }
  }

  vector< vector<ll> > r_acc;
  REP(i,M) {
    int sz = r_l[i].size();
    ll sum = 0;
    vl tmp;
    REP(j,1,sz+1) {
      sum += r_l[i][j-1];
      if (sum >= 0) {
        tmp.PB(sum);
      } else {
        break;
      }
    }
    r_acc.PB(tmp);
  }

  REP(i,M) {
    int sz = r_acc[i].size();
    REP(j,1,sz+1) {
      res[j] += r_acc[i][j-1];
    }
  }

  ll ans = 0;
  REP(i,lim+1) {
    if (ans < res[i]) ans = res[i];
  }
  cout << (ans) << ln;

  //REP(i,lim+1) print(res[i]);
  //REP(i,M) print(r_acc[i]);

}


#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];
            }

        }
        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;

    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;
        cout<<"Size "<<siz<<" : "<<sum<<"\n";
        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<=m; 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;
    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(m+1, 0);

    for(int i = 1; i<=m; 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;
}