#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;

const int INF = -1000000000;

int main(){
  int n,k;
  vector<int> C[10];
  scanf("%d%d",&n,&k);
  for(int i=0;i<n;i++){
    int c,g;
    scanf("%d%d",&c,&g);
    C[g-1].push_back(c);
  }
  for(int i=0;i<10;i++){
    sort(C[i].begin(),C[i].end());
    reverse(C[i].begin(),C[i].end());
    for(int j=0;j<C[i].size();j++){
      if(j>0) C[i][j] += C[i][j-1] + 2 * j;
    }
  }
  int dp[11][k+1];
  fill(dp[0],dp[0]+k+1,INF);
  dp[0][0] = 0;
  for(int i=1;i<=10;i++){
    for(int j=0;j<=k;j++){
      for(int l=0;l<=C[i-1].size();l++){
        if(l==0) dp[i][j] = dp[i-1][j];
        else if(j-l>=0) dp[i][j] = max(dp[i][j],dp[i-1][j-l] + C[i-1][l-1]);
      }
    }
  }
  printf("%d\n",dp[10][k]);
  return 0;
}