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

typedef long long ll;

ll squared(ll x) {
  return (ll)x * x;
}

int main(void) {

  ll N, K;
  vector<pair<ll, ll>> sushi;
  set<ll> type;
  stack<ll> st;

  cin >> N >> K;

  sushi.reserve(N);
  for (int i = 0; i < N; i++) {
    ll t, d;
    cin >> t >> d;
    sushi.push_back(make_pair(d, t));
  }

  sort(sushi.begin(), sushi.end());
  reverse(sushi.begin(), sushi.end());

  ll ans = 0;

  for (int i = 0; i < K; i++) {
    ans += sushi[i].first;
    if (type.count(sushi[i].second)) st.push(sushi[i].first);
    type.insert(sushi[i].second);
  }

  ans += squared(type.size());
  ll tmp = ans;
  for (int i = K; i < N; i++) {

    if (st.empty()) break;
    if (!type.count(sushi[i].second)) {
      tmp = tmp - st.top() + sushi[i].first - squared(type.size()) + squared(type.size() + 1);
      st.pop();
      type.insert(sushi[i].second);
      ans = max(ans, tmp);
    }
  }

  cout << ans << endl;

  return 0;
}