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# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Linq;

namespace CSharpSample01
{

    class PriorityQue_Sushi
    {
        public Sushi[] prque_sushi;
        public int last_index;

        public PriorityQue_Sushi(int num_index)
        {
            prque_sushi = new Sushi[num_index + 1];
            last_index = 0;
        }

        void MaxHeap(int start_index)
        {
            int largest_index = start_index;
            int right_index = 2 * start_index;
            int left_index = 2 * start_index + 1;

            //左確認
            if (left_index <= last_index && prque_sushi[left_index].delicious < prque_sushi[largest_index].delicious)
            {
                largest_index = left_index;
            }

            //右確認
            if (right_index <= last_index && prque_sushi[right_index].delicious < prque_sushi[largest_index].delicious)
            {
                largest_index = right_index;
            }

            if (largest_index != start_index)
            {
                Sushi tmp = prque_sushi[start_index];
                prque_sushi[start_index] = prque_sushi[largest_index];
                prque_sushi[largest_index] = tmp;
                MaxHeap(largest_index);
            }

            return;
        }


        public void Insert(Sushi new_sushi)
        {
            last_index++;
            prque_sushi[last_index] = new_sushi;

            int current_index = last_index;

            while (current_index > 1 && prque_sushi[current_index / 2].delicious < prque_sushi[current_index].delicious)
            {
                Sushi tmp = prque_sushi[current_index / 2];
                prque_sushi[current_index / 2] = prque_sushi[current_index];
                prque_sushi[current_index] = tmp;
                current_index /= 2;
            }

            return;
        }


        public Sushi Pop()
        {
            Sushi max_sushi;

            if (last_index < 1)
            {
                return null;
            }

            max_sushi = prque_sushi[1];
            prque_sushi[1] = prque_sushi[last_index];
            last_index--;

            MaxHeap(1);

            return max_sushi;
        }

    }

    public class Sushi
    {
        internal int neta;
        internal int delicious;

        public Sushi() { }
        public Sushi(int neta, int delicious)
        {
            this.neta      = neta;
            this.delicious = delicious;
        }
    }    
    
    class Program
    {

        //O(numoftype * numofeat)
        static long GetMaxSatisfactionPoint(Sushi[] sushi_data, int numofsushi, int numofeat, int numoftype)
        {

            long max_satis_point = 0;
            int num_ate_netatype = 0;
            long delicious_point = 0;

            var dic_eatneat = new Dictionary<int, Sushi>();
            PriorityQue_Sushi prique_emit_sushi = new PriorityQue_Sushi(numofsushi);
            PriorityQue_Sushi prique_insert_sushi = new PriorityQue_Sushi(numofsushi);

            Array.Sort(sushi_data, (a, b) => b.delicious - a.delicious);
            
            for(int i = 0; i < numofeat; i++)
            {

                Sushi current_sushi = sushi_data[i];

                    //吐き出す予定の寿司を登録
                if (!dic_eatneat.ContainsKey(current_sushi.neta))
                {
                    dic_eatneat[current_sushi.neta] = current_sushi;
                    num_ate_netatype++;                                     
                }
                else
                {
                    if(dic_eatneat[current_sushi.neta].delicious < current_sushi.delicious)
                    {
                        prique_emit_sushi.Insert(dic_eatneat[current_sushi.neta]);
                        dic_eatneat[current_sushi.neta] = current_sushi;                          
                    }
                    else
                    {
                        prique_emit_sushi.Insert(current_sushi);
                    }

                }

                delicious_point += current_sushi.delicious;
            }

            //食べる予定の寿司を登録
            for(int i = numofeat; i < numofsushi; i++)
            {
                    Sushi current_sushi = sushi_data[i];
                    if (!dic_eatneat.ContainsKey(current_sushi.neta))
                    {
                        prique_insert_sushi.Insert(current_sushi);
                        dic_eatneat[current_sushi.neta] = current_sushi;
                    }
            }

            max_satis_point = delicious_point + num_ate_netatype * num_ate_netatype;

            while (true)
            {
                Sushi emit_sushi = prique_emit_sushi.Pop();
                if (emit_sushi == null)
                {
                    break;
                }
                delicious_point -= emit_sushi.delicious;

                Sushi insert_sushi = prique_insert_sushi.Pop();
                if (insert_sushi == null)
                {
                    break;
                }
                delicious_point += insert_sushi.delicious;

                num_ate_netatype++;

                long now_satis_point = delicious_point + num_ate_netatype * num_ate_netatype;
                if (max_satis_point < now_satis_point)
                {
                    max_satis_point = now_satis_point;
                }
            }


            return max_satis_point;
        }


        
        static void Main(string[] args)
        {
            string[] input = Console.ReadLine().Split(' ');
            int numofsushi = int.Parse(input[0]);
            int numofeat   = int.Parse(input[1]);

            Sushi[] sushi_data = new Sushi[numofsushi];
            Dictionary<int, int> dic_neta = new Dictionary<int, int>();

            long max_satis_point  = 0;
            int numoftype         = 0;
            
            for(int i = 0; i < numofsushi; i++)
            {
                string[] sushi_info = Console.ReadLine().Split(' ');
                int neta      = int.Parse(sushi_info[0]);
                int delicious = int.Parse(sushi_info[1]);

                sushi_data[i] = new Sushi(neta, delicious);

                if (!dic_neta.ContainsKey(neta))
                {
                    dic_neta[neta] = 1;
                    numoftype++;
                }

            }
           
            max_satis_point = GetMaxSatisfactionPoint(sushi_data, numofsushi, numofeat, numoftype);

            Console.WriteLine(max_satis_point);

        }
    }
}