#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mod 1000000007

int n, k, cnt[100005], sz, cur, ans;
vector<pair<int,int> > vp;
priority_queue<pair<int,int> > pq;
signed main(){
	cin >> n >> k;
	for(int i = 1 ; i <= n ; i ++){
		int t, d;
		cin >> t >> d;
		pq.push({d, t - 1});
	}
	for(int i = 1 ; i <= k ; i ++){
		pair<int,int> now = pq.top(); pq.pop();
		cnt[now.second] ++;
		cur += now.first;
		vp.push_back(now);
		sz += (cnt[now.second] == 1);
	}
	ans = sz*sz + cur;
	while(k --){
		if(cnt[vp[k].second] > 1){
			while(pq.size()){
				pair<int,int> now = pq.top(); pq.pop();
				if(!cnt[now.second]){
					cur = cur - vp[k].first + now.first;
					sz ++; cnt[now.second] ++; cnt[vp[k].second] --;
					ans = max(ans, sz*sz + cur);
					break;
				}
			}
		}
	}
	cout << ans;
}
