#include <bits/stdc++.h>
#define MOD 100000
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
 
int N, K;
vector<vector<int>> book(11);
vector<int> dp(2001, -1);

int main(void){
	
	scanf("%d %d", &N, &K);
	for(int i = 0; i < N; ++i){
	    int a, b; scanf("%d %d", &a, &b);
	    book[b].push_back(a);
	}
	
	for(int i = 1; i <= 10; ++i) if(book[i].size()) sort(book[i].begin(), book[i].end(), greater<int>());
	
    for(int i = 1; i <= 10; ++i){
        int sum = 0;
        for(int j = 0; j < book[i].size(); ++j){
            int t = j + 1;
            sum += book[i][j];
            book[i][j] = sum + (t - 1) * t;
        }
    }
    
    dp[0] = 0;
	for(int g = 1; g <= 10; ++g){
	    for(int k = K; k >= 0; --k){
	        if(dp[k] == -1) continue;
	        for(int b = 0; b < book[g].size(); ++b){
	            dp[k + b + 1] = max(dp[k + b + 1], dp[k] + book[g][b]);
	        }
	    }
	}
    printf("%d\n", dp[K]);
    
    return 0;
}

