#include <bits/stdc++.h>
using namespace std;
#define int long long

const int MAXN = 1E5+3;

vector<pair<int,int> > a(MAXN);
vector<bool> appear(MAXN, 0);
vector<int> zero;

int sum = 0, cnt = 0;

int n, k;
int ans = 0;

signed main(){
  cin >> n >> k;
  for(int i=1; i<= n; ++i){
    cin >> a[i].second >> a[i].first;
  }
  sort(a.begin()+1, a.begin()+n+1, greater<pair<int,int> >());
  a[n+1] = {0, n+1};
  for(int i=1; i<= n+1; ++i){
    if(cnt > k) continue;
    if(!appear[a[i].second]){
      int cur = 0;
      if(zero.empty()) cur = 0;
      else if(k-cnt-1 >= (int)zero.size()) cur = zero.back();
      else if(k-cnt-1 >= 0) cur = zero[k-cnt-1]; 
      ans = max(ans, sum + cnt*cnt + cur);
      cerr << i << " " << sum << " " << cnt << " " << cur << "\n";
      appear[a[i].second] = 1;
      ++cnt;
      sum += a[i].first;
    }
    else{
      if(zero.empty()) zero.push_back(a[i].first);
      else zero.push_back(zero.back() + a[i].first);
    }
  }
  cout << ans << "\n";
}