#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
#define a first
#define b second
#define sz(x) (ll)((x).size())
#define pb push_back 
#define bg begin()
#define ed end()
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep1(i,n) for(ll i=1;i<=n;i++)
#define mp(x,y) make_pair(x,y)

int main(){
	ll N,K; cin>>N>>K;
	vector<P> sushi(N);//おいしさ、種類
	rep(i,N){
		ll A,B; cin>>A>>B;
		sushi[i]=mp(B,A);
	}
	sort(sushi.bg,sushi.ed,greater<P>());

	set<ll> se;//種類
	priority_queue<ll,vector<ll>,greater<ll>> que;
	ll point=0;

	rep(i,K){
		point+=sushi[i].a;
		ll S=sz(se);
		se.insert(sushi[i].b);
		if(sz(se)==S) que.push(sushi[i].a);
	}
	ll ans=point+sz(se)*sz(se);
	ll i=K;
	while(i<N){
		while(i<N && se.find(sushi[i].b)!=se.ed){
			i++;
		}
		if(que.empty() || i==N) break;
		point-=que.top(); que.pop();
		point+=sushi[i].a;
		se.insert(sushi[i].b);
		ans=max(ans,point+sz(se)*sz(se));
	}
	cout<<ans;
}
