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

int main(){
  int n,k;
  cin >> n >> k;
  vector<int> book[12];
  for(int i=0;i<n;i++){
    int a,b; cin >> a >> b;
    book[b-1].push_back(a);
  }
  int dp[2010];
  memset(dp,-1,sizeof(dp));
  dp[0] = 0;
  for(int i=0;i<10;i++){
    sort(book[i].begin(),book[i].end(),greater<int>() );
    for(int j=k-1;j>=0;j--){
      if(dp[j] == -1) continue;
      int sum = 0;
      for(int l=0;l<book[i].size() && j + l + 1 <= k;l++){
	sum += book[i][l];
	dp[j+l+1] = max(dp[j+l+1],dp[j] + sum + (l + 1) * l);
      }
    }
  }
  cout << dp[k] << endl;
}