// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

//#include <boost/multiprecision/cpp_int.hpp>

//using multiInt = boost::multiprecision::cpp_int;



using ll = long long int;

using ld = long double;

using pii = pair<int, int>;

using pll = pair<ll, ll>;

template<typename Q_temp>

using smaller_queue = priority_queue <Q_temp, vector<Q_temp>, greater<Q_temp> >;



const int INF = (int) 1e9;

const ll LINF = (ll) 4e18;

const ll MOD = (ll) (1e9 + 7);

const double PI = acos(-1.0);

const int limit = 100010;



#define REP(i,m,n) for(ll i = m; i < (ll)(n); ++i)

#define rep(i,n) REP(i, 0, n)

#define MP make_pair

#define YES(n) cout << ((n) ? "YES" : "NO") << endl

#define Yes(n) cout << ((n) ? "Yes" : "No") << endl

#define Possible(n) cout << ((n) ? "Possible" : "Impossible") << endl

#define all(v) v.begin(), v.end()

#define NP(v) next_permutation(all(v))

#define dbg(x_) cerr << #x_ << ":" << x_ << endl;

#define dbg2(x_) for(auto a_ : x_) cerr << a_ << " "; cerr << endl;

#define dbg3(x_ , sx_) rep(i, sx_) cerr << x_[i] << " "; cerr << endl;

vector<int> Dx = {0, 0, -1, 1, -1, 1, -1, 1, 0};

vector<int> Dy = {1, -1, 0, 0, -1, -1, 1, 1, 0};



//------------------------------------------------------



int main() {

    cin.tie(0);

    ios::sync_with_stdio(false);

    ll n, k;

    cin >> n >> k;

    ll t[n], d[n];

    rep(i, n) cin >> t[i] >> d[i];



    vector<pll> all_v;

    rep(i, n) all_v.push_back(MP(d[i], t[i]));

    sort(all(all_v));

    reverse(all(all_v));

    bool top_used[limit] = {};

    priority_queue<ll> each_top, others;

    for (auto p : all_v) {

        if (!top_used[p.second]) {

            top_used[p.second] = true;

            each_top.push(p.first);

        } else {

            others.push(p.first);

        }

    }



    ll each_top_sum[limit], others_sum[limit]; //1-indexed

    fill(each_top_sum, each_top_sum + limit, -LINF);

    fill(others_sum, others_sum + limit, -LINF);

    each_top_sum[0] = others_sum[0] = 0;

    int index = 1;

    while (!each_top.empty()) {

        each_top_sum[index] = each_top_sum[index - 1] + each_top.top();

        each_top.pop();

        index++;

    }

    index = 1;

    while (!others.empty()) {

        others_sum[index] = others_sum[index - 1] + others.top();

        others.pop();

        index++;

    }



    ll Max = 0;

    for (ll kind = 1; kind <= k; ++kind) {

        ll point = kind * kind;

        int remainder = k - kind;

        point += each_top_sum[kind];

        point += others_sum[remainder];

        Max = max(Max, point);

    }

    cout << Max << endl;

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 2.4570508712917056x):
#pragma region Macros

#include <bits/stdc++.h>

using namespace std;

//#include <boost/multiprecision/cpp_int.hpp>

//using multiInt = boost::multiprecision::cpp_int;



using ll = long long int;

using ld = long double;

using pii = pair<int, int>;

using pll = pair<ll, ll>;

using pld = pair<ld, ld>;

template <typename Q_type>

using smaller_queue = priority_queue<Q_type, vector<Q_type>, greater<Q_type>>;



constexpr int MOD_TYPE = 2;

constexpr ll MOD = (MOD_TYPE == 1 ? (ll)(1e9 + 7) : 998244353);

constexpr int INF = (int)1e9;

constexpr ll LINF = (ll)4e18;

constexpr ld PI = acos(-1.0);

constexpr ld EPS = 1e-11;

constexpr int Dx[] = {0, 0, -1, 1, -1, 1, -1, 1, 0};

constexpr int Dy[] = {1, -1, 0, 0, -1, -1, 1, 1, 0};



#define REP(i, m, n) for (ll i = m; i < (ll)(n); ++i)

#define rep(i, n) REP(i, 0, n)

#define MP make_pair

#define MT make_tuple

#define YES(n) cout << ((n) ? "YES" : "NO") << "\n"

#define Yes(n) cout << ((n) ? "Yes" : "No") << "\n"

#define possible(n) cout << ((n) ? "possible" : "impossible") << "\n"

#define Possible(n) cout << ((n) ? "Possible" : "Impossible") << "\n"

#define Yay(n) cout << ((n) ? "Yay!" : ":(") << "\n"

#define all(v) v.begin(), v.end()

#define NP(v) next_permutation(all(v))

#define dbg(x) cerr << #x << ":" << x << "\n";



#pragma endregion



void solve()

{

  int n, k;

  cin >> n >> k;

  vector<pll> v(n);



  rep(i, n)

  {

    cin >> v[i].second >> v[i].first;

  }

  sort(all(v), greater<pll>());

  ll sum = 0;

  bool used[100010] = {};

  smaller_queue<ll> que;

  ll kind = 0;

  rep(i, k)

  {

    sum += v[i].first;

    if (used[v[i].second])

    {

      que.push(v[i].first);

    }

    else

    {

      used[v[i].second] = true;

      kind++;

    }

  }



  ll Max = sum + kind * kind;

  REP(i, k, n)

  {

    if (que.empty())

      break;

    if (used[v[i].second])

      continue;

    sum += v[i].first;

    used[v[i].second] = true;

    sum -= que.top();

    que.pop();

    kind++;

    Max = max(Max, sum + kind * kind);

  }

  cout << Max << endl;

}



int main()

{

  cin.tie(0);

  ios::sync_with_stdio(false);

  cout << setprecision(30) << setiosflags(ios::fixed);

  solve();

  return 0;

}