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

typedef long long ll;
const int maxg = 10;
const int maxn = 2e3 + 3;
vector<ll> G[maxg];
int n, m;
ll dp[maxn][maxn];

int main()
{
	scanf("%d %d", &n, &m);
	for (int i = 0; i < maxg; ++i)// 为以后sum做准备
		G[i].push_back(0);
	for (int i = 0; i < n; ++i)
	{
		ll c, g;
		scanf("%lld %lld", &c, &g);
		--g;
		G[g].push_back(c);
	}
	for (int i = 0; i < maxg; ++i)
	{
		sort(G[i].begin(), G[i].end()); // 先顺排
		auto ite = G[i].begin(); ite++;
		reverse(ite, G[i].end());// 后逆排
		for (int j = 1; j < G[i].size(); ++j)
			G[i][j] += G[i][j - 1] + 2 * (j - 1);// 累加并加附加值
	}
	for (int i = 1; i < G[0].size(); ++i)// dp边界
		dp[0][i] = G[0][i];
	for (int i = 1; i < maxg; ++i)
	{
		int len = G[i].size();
		if (len == 1)	// 该类没书直接继承上次有书的
		{
			for (int j = m; j >= 1; --j)
				dp[i][j] = dp[i - 1][j];
			continue;
		}
		for (int j = 1; j <= m; ++j)
			for (int k = 1; k <= j && k < len; ++k)
				dp[i][j] = max(dp[i][j], max(dp[i - 1][j], dp[i - 1][j - k] + G[i][k]));
	}
	printf("%lld\n", dp[9][m]);
	return 0;
}
