#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <utility>
#define maxn 100005
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
struct sushi{
	int t,d;
	bool operator < (const sushi &b)const
	{
		return d > b.d;
	}
}a[maxn];
int n,k;
bool app[maxn];
ll ans,res,cnt;
int main(){
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++) scanf("%d%d",&a[i].t,&a[i].d);
	sort(a + 1,a + n + 1);
	vector <int> lis; int i;
	for(i=1;i<=k;i++){
		ans += a[i].d;
		if(!app[a[i].t]){
			app[a[i].t] = true;
			ans -= cnt * cnt;
			cnt++;
			ans += cnt * cnt;
		}else{
			lis.push_back(i);
		}
	}
	res = ans;
	for(int j=lis.size() - 1;j>=0;j--){
		while(i <= n && app[a[i].t]) i++;
		if(i > n) break;
		ans -= a[lis[j]].d;
		ans += a[i].d;
		app[a[i].t] = true;
		ans -= cnt * cnt;
		cnt++;
		ans += cnt * cnt;
		res = max(res,ans);
	}
	printf("%lld\n",res);
	return 0;
}
