#include<cstdio>
#include <iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<queue>
#include<vector>
#include<functional>
#include<cmath>
#include<map>
#include<stack>
#include<cstdlib>
#include<list>
#include<set>
#include<numeric>

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int dp[2000];

int main()
{
	int N, K, C, G;
	cin >> N >> K;
	vector<int> b[11], v[11];

	for (int i = 0; i < N; i++) {
		cin >> C >> G;
		b[G].push_back(C);
	}

	for (int i = 1; i <= 10; i++) {
		sort(b[i].begin(),b[i].end(),greater<int>());
		v[i].push_back(0);
	}

	for (int i = 1; i <= 10; i++) {
		for (int j = 0; j < b[i].size(); j++) {
			v[i].push_back(v[i][j]+b[i][j]+j*2);
		}
	}

	for (int i = 1; i <= 10; i++) {
		for (int j = K; j >= 0; j--) {
			for (int h = max(0, j - (int)v[i].size() + 1); h < j; h++) {
				dp[j] = max(dp[j],dp[h]+v[i][j-h]);
			}
		}
	}
	cout << dp[K] << endl;
	return 0;
}