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