#include <bits/stdc++.h>
using namespace std;
int N,K;

int main() {
	cin >> N >> K;
	long long ans = 0;
	vector<pair<int, int>> sushi(N);
	for (int i = 0; i < N; i++) {
		int t, d;
		cin >> t >> d;
		sushi[i] = make_pair(d, t);
	}
	sort(sushi.rbegin(), sushi.rend());
	priority_queue<int, vector<int>,greater<int>> pq;
	set<int> set;
	for (int i = 0; i < K; i++) {
		if (set.count(sushi[i].second))pq.push(sushi[i].first);
		else set.insert(sushi[i].second);
		ans += sushi[i].first;
	}
	long long sum = ans;
	ans += set.size()*set.size();
	for (int i = K; i < N; i++) {
		if (pq.empty())break;
		if (set.count(sushi[i].second))continue;
		set.insert(sushi[i].second);
		sum -= pq.top();pq.pop();
		sum += sushi[i].first;
		ans = max(ans, sum + (long long)(set.size()*set.size()));
	}
	cout << ans << endl;
}
