#include <bits/stdc++.h>
#ifdef __DEBUG__
    #define DBG(X) cout << #X << " = " << (X) << endl;
    #define SAY(X) cout << (X) << endl;
#else
    #define DBG(X)
    #define SAY(X)
#endif

using namespace std;

using ll = long long int;
using ull = unsigned long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

int dx[]={1, 0, -1, 0, 1,-1, 1,-1};
int dy[]={0, 1, 0, -1, 1, 1,-1,-1};
const int INT_INF = (int)(2e9);
const ll  LL_INF = (ll)(2e18);

inline void fast_io() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); };
template<typename T, typename S> inline ostream& operator<<(ostream& os, const pair<T, S> p) { cout << "[" << p.first << ";" << p.second << "]"; return os; }
template<typename T, typename S> inline ostream& operator<<(ostream& os, const map<T, S> p) { for (auto el : p) cout << "[" << el.first << ";" << el.second << "]"; return os; }
template<typename T> inline ostream& operator<<(ostream& os, const vector<T>& v) { for (auto el : v) cout << el << " "; return os; }
template<typename T> inline ostream& operator<<(ostream& os, const deque<T>& v) { for (auto el : v) cout << el << " "; return os; }
template<typename T> inline ostream& operator<<(ostream& os, const set<T>& v) { for (auto el : v) cout << el << " "; return os; }
template<typename T> inline vector<T> fetch_vec(int sz) { vector<T> ret(sz); for (auto& elem : ret) cin >> elem; return ret; }

int N,K;
vector<pll> sushi;
void input(){
    fast_io();
    cin >> N >> K;
    for (int i = 0; i < N; i++)
    {
        ll t,d; cin >> t >> d;
        t--;
        sushi.emplace_back(t,d);
    }   
}

// int WAsolve(){
//     sort(sushi.begin(),sushi.end(),[](pll a, pll b){return a.second > b.second;});
//     set<int> cnt;
//     for (int i = 0; i < K; i++)
//     {
//         cnt.insert(sushi[i].first);
//     }
//     int C = cnt.size();
    
//     vector<ll> max_val(N,0);
//     vector<ll> sec;
//     for (int i = 0; i < N; i++)
//     {
//         if(max_val[sushi[i].first] >= sushi[i].second){
//             sec.push_back(sushi[i].second);
//         }
//         else
//         {
//             if(max_val[sushi[i].first]) sec.push_back(max_val[sushi[i].first]);
//             max_val[sushi[i].first] = sushi[i].second;
//         }
//     }
//     sort(max_val.begin(),max_val.end(),greater<ll>());
//     sort(sec.begin(),sec.end(),greater<ll>());
//     vector<ll> max_val_sum(N+1,0);
//     vector<ll> sec_sum(N+1,0);
//     for (int i = 0; i < N; i++)
//     {
//         max_val_sum[i+1] = max_val_sum[i] + max_val[i];
//         sec_sum[i+1] = sec_sum[i] + sec[i];
//     }
//     ll ans = 0;
//     for (int k = C; k <= K; k++)
//     {
//         if(max_val_sum[k-1]==0) continue;
//         ll tmp = k*k;
//         tmp += max_val_sum[k];
//         tmp += sec_sum[K-k];
//         ans = max(ans,tmp);
//     }
//     cout << ans << endl;    
//     return 0;
// }

int solve(){
    sort(sushi.begin(),sushi.end(),[](pll a, pll b){return a.second > b.second;});
    ll ans = 0;
    ll sum = 0;
    set<int> cal_cnt;
    vector<int> cnt(N,0);
    priority_queue<pii,vector<pii>,greater<pii>> pq;
    for (int i = 0; i < K; i++)
    {
        sum += sushi[i].second;
        pq.emplace(sushi[i].second, sushi[i].first);
        cal_cnt.insert(sushi[i].first);
        cnt[sushi[i].first]++;
    }
    ans = sum + cal_cnt.size()*cal_cnt.size();
    for (int i = K; i < N; i++)
    {
        if(cal_cnt.find(sushi[i].first)!=cal_cnt.end()) continue;
        while(cnt[pq.top().second]<=1 && !pq.empty()){
            pq.pop();
        }
        if(pq.empty()) break;
        sum -= pq.top().first;
        cnt[pq.top().second]--;
        pq.pop();
        sum += sushi[i].second;
        cal_cnt.insert(sushi[i].first);
        pq.emplace(sushi[i].second, sushi[i].first);
        ans = max(ans, sum+(ll)cal_cnt.size()*(ll)cal_cnt.size());
    }
    cout << ans << endl;
    return 0;
}

int main()
{
    input();
    solve();
    return 0;
}

Measured memory_bytes: 5872