#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fr(i,n) for(int i=0;i<n;i++)

struct td{
  ll t,d;
  bool operator<(const td& right ) const {
    return d==right.d?t<right.t:d>right.d;
  }
};

int main(){
  ll k,n,t,d,tm=0,ans=0,v=0;
  bool c[100010]={};
  td p[100010];
  cin>>n>>k;
  fr(i,n){
    cin>>t>>d;
    t--;
    p[i]=(td){t,d};
  }
  sort(p,p+n);
  stack<ll> q;
  fr(i,n){
    if(i<k){
      tm+=p[i].d;
      if(c[p[i].t]){
        q.push(p[i].d);
      }
      else{
        c[p[i].t]=1;
        v++;
      }
      ans=tm+v*v;
    }
    else{
      if(q.empty()) break;
      if(!c[p[i].t]){
        c[p[i].t]=1;
        tm-=q.top();
        q.pop();
        tm+=p[i].d;
        v++;
        ans=max(ans,tm+v*v);
      }
    }
  }
  cout<<ans<<endl;
}