Problem:
Score : 400 points

Problem Statement

There are N pieces of sushi. Each piece has two parameters: "kind of topping" t_i and "deliciousness" d_i.
You are choosing K among these N pieces to eat.
Your "satisfaction" here will be calculated as follows:

The satisfaction is the sum of the "base total deliciousness" and the "variety bonus".

The base total deliciousness is the sum of the deliciousness of the pieces you eat.

The variety bonus is x*x, where x is the number of different kinds of toppings of the pieces you eat.

You want to have as much satisfaction as possible.
Find this maximum satisfaction.

Constraints

1 \leq K \leq N \leq 10^5

1 \leq t_i \leq N

1 \leq d_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
t_1 d_1
t_2 d_2
.
.
.
t_N d_N

Output

Print the maximum satisfaction that you can obtain.

Sample Input 1

5 3
1 9
1 7
2 6
2 5
3 1

Sample Output 1

26

If you eat Sushi 1,2 and 3:

The base total deliciousness is 9+7+6=22.

The variety bonus is 2*2=4.

Thus, your satisfaction will be 26, which is optimal.

Sample Input 2

7 4
1 1
2 1
3 1
4 6
4 5
4 5
4 5

Sample Output 2

25

It is optimal to eat Sushi 1,2,3 and 4.

Sample Input 3

6 5
5 1000000000
2 990000000
3 980000000
6 970000000
6 960000000
4 950000000

Sample Output 3

4900000016

Note that the output may not fit into a 32-bit integer type.

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

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

//#define int long long
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define rep(i,n) for(int i=0;i<n;++i)
#define rep1(i,n) for(int i=1;i<n;++i)
#define exrep(i, a, b) for(ll i = a; i < b; i++)
#define out(x) cout << x << endl
#define EPS (1e-7)
#define gearup ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long double ld;
typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<pair<int,int> > vpii;
typedef vector<vector<int> > vvi;
typedef vector<vector<char> > vvc;
typedef vector<vector<bool> > vvb;
typedef vector<vector<double> > vvd;
typedef vector<vector<string> > vvs;
typedef vector<ll> vl;
typedef vector<vector<ll> > vvl;
typedef vector<vector<vector<ll> > > vvvl;
ll MOD = 1000000007;
const long long L_INF = 1LL << 60;
const int INF = 2147483647; // 2^31-1
const double PI = acos(-1);
//cout<<fixed<<setprecision(10);

template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b;return true;}return false;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b;return true;}return false;}
template<class T> void debug(T v){rep(i,v.size()) cout<<v[i]<<" ";cout<<endl;}
const ll dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};

signed main()
{
  gearup;
  ll n,k; cin >> n >> k;
  vl t(n),d(n); //tがネタ種類, dが美味しさ
  vpii p;
  rep(i,n){
    cin>>t[i]>>d[i];
    p.push_back({d[i],t[i]});
  }
  sort(rall(p)); //昇順ソート
  ll res = 0;
  map<int,int> mp;
  vector<int> b(n+1); //最大値をいれる
  priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > que;//降順ソート
  rep(i,k){
    res += p[i].first;
    mp[p[i].second]++;
    b[p[i].second] = max(p[i].first,b[p[i].second]);
  }
  rep(i,k){ //同じ種類が2つ以上のもので最大ではない値をqueにいれる
    if(mp[p[i].second] >= 2) if(b[p[i].second] != p[i].first)que.push({p[i].first,p[i].second});
  }
  ll type = mp.size();
  res += type*type;
  ll ans = res;
  //out(res);
  int idx = k;
  while(1){ //終了条件queが空か,n種類になったか,nまで選んだか
    if(que.empty() || idx >= n)break;
    int num = que.top().first;
    que.pop();
    bool is_res = false;
    while(idx < n){
      int num1 = p[idx].first;
      int type1 = p[idx++].second;
      if(mp[type1] == 0){
        mp[type1] = 1;
        is_res = true;
        type++;
        break;
      }
    }
    if(is_res){
      res -= num + (type-1)*(type-1);
      res += type*type;
      ans = max(res,ans);
    }
    else break;
  }
  out(ans);
}