#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
int n, k;
int g[12];
int memo[2048][12];
vector<int> c[12];
int solve(int i, int j){
  int res = 0, m = 0;
  if(memo[i][j]) return memo[i][j];
  if(i >= k || j > 10) return 0;
  for(int l = 0;l < g[j]; l++){
    m += c[j][l] + 2 * l;
    if(i + l + 1 > k) break;
    res = max(res, solve(i + l + 1, j + 1) + m);
  }
  res = max(res, solve(i, j + 1));
  return memo[i][j] = res;
}
int main(void){
  int C, G;
  scanf("%d %d", &n, &k);
  for(int i = 0;i < n; i++){
    scanf("%d %d", &C, &G);
    c[G].push_back(C);
    g[G]++;
  }
  for(int i = 1;i <= 10; i++){
    sort(c[i].begin(), c[i].end());
    reverse(c[i].begin(), c[i].end());
  }
  
  printf("%d\n", solve(0, 1));
}
