#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll read(){
	ll a=0,b=getchar(),c=1;
	while(!isdigit(b))c=b=='-'?-1:1,b=getchar();
	while(isdigit(b))a=a*10+b-'0',b=getchar();
	return a*c;
}
stack<int> s;
struct node{
	int t,x;
	bool operator <(const node& a)const{
		return x>a.x;
	}
}a[100005];
ll n,k,tp,sum,ans,b[100005];
int main(){
	n=read(),k=read();
	for(int i=1;i<=n;i++)
		a[i].t=read(),a[i].x=read();
	sort(a+1,a+1+n);
	for(int i=1;i<=n;i++)
		if(i<=k){
			if(b[a[i].t])s.push(a[i].x);
			else b[a[i].t]=1,tp++;
			sum+=a[i].x,ans=max(ans,sum+tp*tp);
		}
		else{
			if(s.empty())break;
			if(b[a[i].t])continue;
			int v=s.top();
			s.pop(),b[a[i].t]=1,tp++;
			sum=sum-v+a[i].x,ans=max(ans,sum+tp*tp);
		}
	printf("%lld",ans);
	return 0;
}