#include<bits/stdc++.h>
#define d first
#define t second
using namespace std;typedef long l;l N,K,i,n,p,s,u[1<<17];vector<pair<l,l>>v;priority_queue<l,vector<l>,greater<l>>q;
int main(){cin>>N>>K;v.resize(N);for(i=0;i<N;i++)cin>>v[i].t>>v[i].d;sort(v.rbegin(),v.rend());for(i=0;i<K;i++){s+=v[i].d;if(u[v[i].t])q.push(v[i].d);else u[v[i].t]=1,n++;}p=s+n*n;for(i=K;i<N;i++){if(!u[v[i].t]){if(!q.size())break;u[v[i].t]=1;s+=v[i].d;s-=q.top();q.pop();p=max(p,s+(++n)*n);}}cout<<p<<endl;}
