#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
  int N, K;
  cin >> N >> K;
  vector<pair<ll, ll> > vec;
  for (int i = 1; i <= N; i++) {
    int a, b;
    cin >> a >> b;
    vec.push_back(make_pair(b, a));
  }
  sort(vec.begin(), vec.end(), greater<pair<ll, ll> >());

  priority_queue<ll> pq;
  set<ll> s;
  ll sum = 0;
  for (int i = 0; i < K; i++) {
    sum += vec[i].first;
    if (s.count(vec[i].second)) {
      pq.push(-vec[i].first);
    } else {
      s.insert(vec[i].second);
    }
  }

  ll ans = sum + s.size() * s.size();
  for (int i = K; i < N && !pq.empty(); i++) {
    if (s.count(vec[i].second)) {
      continue;
    }
    sum += pq.top();
    pq.pop();
    sum += vec[i].first;
    s.insert(vec[i].second);
    ll tmp = sum + s.size() * s.size();
    ans = max(ans, tmp);
  }
  cout << ans << endl;
  return 0;
}