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

#define REP(i,n) for(int i=0;i<n;i++)
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<int,int> P;
int n,k;
int c,g;
vector<P> v;
int G[12];
int dp[11][2002];
vector<int> val[11];//?????£??????G???N-1????????£??????????????§
priority_queue<int> que[12];

int main(){
  cin>>n>>k;
  REP(i,n){
    cin>>c>>g;
    val[g].push_back(c);
  }
  FOR(i,1,11){
    sort(all(val[i]),greater<int>());
    REP(j,val[i].size()){
      if(j==0)continue;
      val[i][j]+=val[i][j-1]+j*2;
    }
  }
  FOR(i,1,11){
    REP(j,k+1){
      FOR(l,j,k+1){
        if(j==l)dp[i][l]=max(dp[i][l],dp[i-1][j]);
        else if(l-j-1<val[i].size()){
          dp[i][l]=max(dp[i][l],dp[i-1][j]+val[i][l-j-1]);
        }
      }
    }
  }
  cout<<dp[10][k]<<endl;
  return 0;
}