#include <bits/stdc++.h>
#define ll long long
#define int long long
#define debug(x) cerr << "\n" \
                      << #x << ": " << x << "\n"
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define all(x) (x).begin(), (x).end()
#define INF 1e9
#define pii pair<int, int>
#define pt(xx) cout << "\n" \
                    << xx << "\n"
#define debugv(x, n)               \
    cout << "\n"                   \
         << #x << ": ";            \
    rep(i, n) cout << x[i] << " "; \
    cout << endl
#define debug2d(x, m, n)            \
    cout << "\n"                    \
         << #x << " is: \n";        \
    for (int i = 0; i < m; ++i)     \
    {                               \
        for (int j = 0; j < n; ++j) \
        {                           \
            cout << x[i][j] << " "; \
        }                           \
        cout << endl;               \
    }
#define nl cout << endl

#define MOD 1000000007
using namespace std;

int N, M, K;
int ans = 0;

signed main()
{
    cin >> N >> K;

    int a, b;
    map<int, int> state;
    vector<pii> sushi(N);

    rep(i, N)
    {
        cin >> a >> b;
        sushi[i].second = a;
        sushi[i].first = b;
    }

    sort(sushi.rbegin(), sushi.rend());
    priority_queue<int, vector<int>, greater<int>> q;

    int tmp = 0;

    int variety = 0;
    int sum = 0;
    for (int i = 0; i < N; ++i)
    { // おいしい順

        if (state[sushi[i].second])
        { // すでに食べた種類のとき
            if ((i + 1) > K)
                continue;
            q.push(sushi[i].first);
        }
        else
        {
            if ((i + 1) > K)
            {
                if (q.empty())
                {
                    break;
                }
                sum -= q.top();
                q.pop();
            }
            state[sushi[i].second] = true;
            variety++;
        }
        sum += sushi[i].first;
        ans = max(ans, sum + variety * variety);
    }

    cout << ans << endl;
    return 0;
}
/*     


























    vector<pii> dt(N);
    vector<vector<int>> tt(N);
    set<int> a;
    vector<priority_queue<int>> ttt(N);
    rep(i, N)
    {
        cin >> dt[i].first >> dt[i].second;
        tt[dt[i].first].push_back(dt[i].second);
        a.insert(dt[i].first);
        ttt[dt[i].first].push(dt[i].second);
    }
    int xmax = a.size();

    sort(all(dt));

    for (auto x : tt)
    {
        sort(x.rbegin(), x.rend());
    }

    vector<pii> stt; // <美味しさ，種類>, 大きい順


    //vector<priority_queue<int>> stt_q(N);
    priority_queue<int> stt_q;
    vector<int> kouhos;
    priority_queue<int> allg;

    priority_queue<int> ansq;
//    for (int i = 0; i < K; i++)
//    {
        for (auto xx : a)
        {
            while (!ttt[xx].empty())
            {
                stt.push_back(make_pair(ttt[xx].top(), xx));
                stt_q.push(ttt[xx].top());
                ttt[xx].pop();
            }
        }
        sort(stt.rbegin(), stt.rend());

        // 種類数分試す
        int tmp = 0;
        debug(xmax);
        for (int i = 1; i <= xmax; ++i)
        {
            for (int j = 0; j < i; ++j)
            {
                // i種類大きいものを選ぶ
            }
            for (auto x : kouhos){
                tmp += x;
            }
            tmp += i * i;
            debug(tmp);
            for (int kk = i; kk <= K; kk++)
            {
                tmp += stt_q.top();
                stt_q.pop();
            }
        }
        ansq.push(tmp);
    }
    
    while(!ansq.empty()){
        cout<<ansq.top()<<endl;
        ansq.pop();
    }
    return 0;
}

/*
  vector<pii> dt(N);
    rep(i, N)
    {
        cin >> dt[i].second >> dt[i].first;
    }

    sort(dt.rbegin(), dt.rend());
    set<int> tset;
    for (auto x : dt)
    {
        cout << x.first << " " << x.second << endl;
    }

    int x = 0;
    vector<pii> alist;
    int tmpsum = 0;

    vector<pii> maxcands; //(m,x)
    for (int i = 0; i < K; i++)
    {
        if (i == 0)
        {
            tset.insert(dt[0].second);
            alist.push_back(dt[0]);
            tmpsum += dt[0].first;
            dt.erase(dt.begin());

        }
        else
        { // i > 0

            // 新しいネタを探さない場合
            int tmp_1 = tmpsum + dt[0].first + tset.size() * tset.size();

            int tmp_2;
            // 新しいネタを探す
            // + K-tset.size()種類
            
            for (int hoge = tset.size(); hoge <= K; hoge++)
            {
                for (int i = 0; i < dt.size(); i++)
                {
                    pii tmax = dt[i];
                    if (tset.find(tmax.second) == tset.end())
                    { // みつけた
                        if (tmp_1 > tmpsum + tmax.first + (tset.size() + 1) * (tset.size() + 1))
                        {
                            alist.push_back(dt[0]);
                            tmpsum += dt[0].first;
                            dt.erase(dt.begin());
                        }
                        else
                        {
                            tset.insert(tmax.second);
                            alist.push_back(tmax);
                            tmpsum += tmax.first;
                            dt.erase(dt.begin() + i);
                        }
                        break;
                    }
                }
            }
        }
    }
    debug(tset.size());
    ans = tmpsum + (tset.size() * tset.size());
    cout << ans << endl;
    return 0;
}
*/

Measured memory_bytes: 6140