#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;
const double PI = 3.141592653589793238;
const double EPS = 1e-10;
typedef pair<ll, int> P;
int main() {
	int N, K;
	cin >> N >> K;
	vector<P> sushi;
	for (int i = 0; i < N; i++) {
		int t, d;
		cin >> t >> d;
		sushi.emplace_back(d, t);
	}
	sort(sushi.begin(), sushi.end(), greater<P>());
	set<int> S;
	priority_queue<ll, vector<ll>, greater<ll>> Q;
	ll ans = 0;
	ll sum = 0;
	ll t = 0;
	for (int i = 0; i < K; i++) {
		sum += sushi[i].first;
		if (S.find(sushi[i].second) == S.end()) {
			S.insert(sushi[i].second);
			t++;
		}
		else {
			Q.push(sushi[i].first);
		}
	}
	ans = max(ans, sum + t * t);
	for (int i = K; i < N; i++) {
		if (Q.empty()) break;
		if (S.find(sushi[i].second) == S.end()) {
			t++;
			sum = sum - Q.top() + sushi[i].first;
			ans = max(ans, sum + t * t);
			Q.pop();
			S.insert(sushi[i].second);
		}
	}
	cout << ans << endl;
}