#include<bits/stdc++.h>
#include<iostream>
#include<cmath>
#include<vector>
#include<string>
#include<queue>
#include<set>
#include<map>
#include<algorithm>
#include<functional>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pullull;
typedef pair<ll, int> plli;
typedef pair<int, pii> pipii;
typedef vector<vector<int> > mati;
typedef vector<vector<double> > matd;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;

#define rep(i,x,y) for(int i=(x); i<(y); ++i)

template <typename T>
void vec_print(vector<T> VEC){
  rep(i, 0, VEC.size()){
    cout << VEC[i] << " ";
  }
  cout << "\n";
};

template <typename T>
void mat_print(vector<vector<T> > MAT){
  rep(i, 0, MAT.size()){
    rep(j, 0, MAT[i].size()){
      cout << MAT[i][j] << " ";
    }
    cout << "\n";
  }
};

/*
template <typename CLASS1, typename CLASS2>
class HOGE{
  public:
    CLASS1 key;
    CLASS2 value;
    HOGE(ll key, ll value){
      this->key = index;
      this->value = value;
    };
    ~HOGE(void){
      return;
    };

    void print(void){
      cout << "key : " << key << ", value : " << value << "\n";
      return;
    };
    
    bool operator==(const HOGE &obj){
      return (this->value == obj.value);
    };
    bool operator<(const HOGE &obj){
      return (this->value < obj.value);
    };
    bool operator>(const HOGE &obj){
      return (this->value > obj.value);
    };
};
*/

template <typename CLASS1, typename CLASS2>
class HOGE{
  public:
    CLASS1 key;
    CLASS2 value;
    HOGE(void){
      this->key = 0;
      this->value = 0;
    };
    HOGE(ll key, ll value){
      this->key = index;
      this->value = value;
    };
    ~HOGE(void){
      return;
    };

    void print(void){
      cout << "key : " << key << ", value : " << value << "\n";
      return;
    };

    void operator=(const HOGE &obj){
      this->key = obj.key;
      this->value = obj.value;
      return;
    }
    
    bool operator==(const HOGE &obj){
      return (this->key == obj.key && this->value == obj.value);
    };
    bool operator<(const HOGE &obj){
      if(this->value == obj.value) return (this->key > obj.key);
      return (this->value > obj.value);
    };
    bool operator>(const HOGE &obj){
      if(this->value == obj.value) return (this->key < obj.key);
      return (this->value < obj.value);
    };
};

constexpr int INF = (1<<30);
constexpr ll INFLL = 1LL<<62;
constexpr long double EPS = 1e-12;

int main(){
  cin.tie(0); // cut the cin and cout (default, std::flush is performed after std::cin)
  ios::sync_with_stdio(false); // cut the iostream and stdio (DON'T endl; BUT "\n";)

  ll N, K;
  cin >> N >> K;
  vector<HOGE<ll, ll>> hoge(N);
  rep(i, 0, N){
    ll t, d;
    cin >> t >> d;
    hoge[i].key = t;
    hoge[i].value = d;
  }

  ////get larget values with greedy algorithm start////

  sort(hoge.begin(), hoge.end());

  vector<HOGE<ll, ll>> cand(K);
  ll kind = 0;
  unordered_map<ll, int> mp;
  vector<ll> start(K, -1);

  ll cand_sum = 0;
  
  for(int i=0; i<K; ++i){
    cand[i].key = hoge[i].key;
    cand[i].value = hoge[i].value;
    cand_sum += cand[i].value;
    if(mp.find(hoge[i].key)==mp.end()){
      mp.emplace(hoge[i].key, kind);
      start[kind] = i;
      kind++;
    }
  }

  ////get larget values with greedy algorithm end////
  ////change values from lower with untaken kind start////

  ll ans = cand_sum + kind*kind;

  int cand_end = K-1;
  while(start[mp[cand[cand_end].key]] == cand_end) cand_end--;
  for(int i=K; i<N && cand_end>=0 && kind<=K; ++i){
    if(mp.find(hoge[i].key)!=mp.end()) continue;
    cand_sum += hoge[i].value - cand[cand_end].value;
    cand_end--;
    while(start[mp[cand[cand_end].key]] == cand_end) cand_end--;
    mp.emplace(hoge[i].key, kind);
    kind++;
    ans = max(ans, cand_sum + kind*kind);
  }

  ////change values from lower with untaken kind end////

  cout << ans << "\n";

  return 0;
}


Measured memory_bytes: 7436