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


int main(){
	int n, k;
	cin >> n >> k;
	pair<long long, int> p[100010];
	for(int i = 0;i < n;i++){
		cin >> p[i].second >> p[i].first;
	}

	sort(p, p+n, greater<pair<long long, int> >());

	int t[100010];
	long long d[100010];
	for(int i = 0;i < n;i++){
		t[i] = p[i].second;
		d[i] = p[i].first;
	}

	priority_queue<long long, vector<long long>, greater<long long> > que1;
	priority_queue<long long> que2;
	int memo[100010] = {};
	long long ans = 0;
	long long num = 0;
	for(int i = 0;i < n;i++){
		if(i < k){
			if(memo[t[i]] == 0){ 
				memo[t[i]] = 1;
				ans += d[i];
				num++;
			}else{
				ans += d[i];
				que1.push(d[i]);
			}
		}else{
			if(memo[t[i]] == 0){
				memo[t[i]] = 1;
				que2.push(d[i]);
			}
		}
	}

	long long aa = ans + num * num;
	while(!que2.empty() && !que1.empty()){
		ans = ans + que2.top() - que1.top();
		num++;
		que2.pop(); que1.pop();
		aa = max(aa, ans + num*num);
	}

	cout << aa << endl;

	return 0;
}

