#include <bits/stdc++.h>
typedef long long ll;

using namespace std;
using P = pair<ll, ll>;

int main() {
	const ll INF = 1e18;
	ll N, K;
	cin >> N >> K;
	vector<P> sushi(N);
	for (int i = 0; i < N; ++i) {
		int t;
		ll d;
		cin >> t >> d;
		sushi[i] = P(d, t - 1);
	}
	sort(sushi.begin(), sushi.end(), greater<P>());

	stack<ll> rmv;
	set<ll> st;
	ll sum = 0;
	for (int i = 0; i < K; ++i) {
		sum += sushi[i].first;
		if (st.count(sushi[i].second)) {
			rmv.push(sushi[i].first);
		}
		st.insert(sushi[i].second);
	}

	ll res = sum + (ll)(st.size()) * (st.size());
	for (int i = K; i < N; ++i) {
		if (rmv.empty()) { break; }
		if (st.count(sushi[i].second)) { continue; }

		sum = sum - rmv.top() + sushi[i].first;
		rmv.pop();
		st.insert(sushi[i].second);
		ll X = st.size();
		ll tmp = sum + X * X;
		res = max(res, tmp);
	}

	cout << res << endl;

	return 0;
}
