#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
int main(){
  int N, K;
  cin >> N >> K;
  vector< vector<int> > S(N);
  for(int i = 0; i < N; ++i){
    int t, d;
    cin >> t >> d;
    --t;
    S[t].push_back(d);
  }
  for(int i = 0; i < N; ++i) sort(S[i].rbegin(), S[i].rend());
  sort(S.rbegin(), S.rend());
  long long ans = 0, s = 0, t = 0;
  priority_queue<int> pq;
  for(int i = 0; i < min(N,K); ++i){
    if(S[i].empty()) continue;
    s += S[i][0];
    while(i+1+pq.size() > K){
      s += pq.top();
      pq.pop();
    }
    ans = max(ans, s+t+(long long)(i+1)*(i+1));
    for(int j = 1; j < (int)S[i].size(); ++j){
      t += S[i][j];
      pq.push(-S[i][j]);
      if((int)pq.size() > K-i-1) t += pq.top(), pq.pop();
      ans = max(ans, s+t+(i+1)*(i+1));
    }
    //cerr << ans << endl;
  }
  cout << ans << endl;
}
