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.Linq;
using System.Collections;
using System.Collections.Generic;

namespace AtCoder
{
    public class Sushi: IComparable<Sushi>
    {
        public int neta;
        public long oishisa;
        public bool isIncluded;

        public Sushi(int t, long d)
        {
            neta = t;
            oishisa = d;
            isIncluded = false;
        }

        public int CompareTo(Sushi param)
        {
            return (oishisa > param.oishisa)? 1 :
                   (oishisa < param.oishisa)? -1 : 0;
        }
    }

    public class SushiTable
    {
        public MyLib.PriorityQueue<Sushi> slist;
        private int[] netalist = new int[100001];
        private int neta = 0;
        private long oishisa = 0;
        public int Neta => neta;
        public long Oishisa => oishisa;

        public SushiTable()
        {
            slist = new MyLib.PriorityQueue<Sushi>();
            for (int i = 0; i < 100001; i++) { netalist[i] = 0; }
        }

        public void Push(Sushi param)
        {
            if (netalist[param.neta] == 0) neta++;
            netalist[param.neta]++;
            oishisa += param.oishisa;
            param.isIncluded = true;
            slist.Push(param);
        }

        public bool IncreaseNeta(List<Sushi> param)
        {
            bool flag = RemoveMinNeta(param);
            if (flag == false) return false;
            flag = AddMaxNeta(param);
            if (flag) neta++;
            return flag;
        }

        private bool RemoveMinNeta(List<Sushi> param)
        {
            List<Sushi> temp = new List<Sushi>();
            bool flag = false;
            int size = slist.Size;
            for (int i = 0; i < size; i++)
            {
                Sushi s = slist.Pop();
                if (netalist[s.neta] > 1)
                {
                    netalist[s.neta]--;
                    oishisa -= s.oishisa;
                    s.isIncluded = false;
                    flag = true;
                    break;
                }
                temp.Add(s);
            }
            size = temp.Count;
            for (int i = 0; i < size; i++)
            {
                slist.Push(temp[i]);
            }

            return flag;
        }

        private bool AddMaxNeta(List<Sushi> param)
        {
            bool flag = false;
            int size = param.Count;
            for (int i = 0; i < size; i++)
            {
                if (netalist[param[i].neta] == 0)
                {
                    netalist[param[i].neta]++;
                    oishisa += param[i].oishisa;
                    param[i].isIncluded = true;
                    slist.Push(param[i]);
                    flag = true;
                    break;
                }
            }

            return flag;
        }
    }

    class Program
    {
        public static long CalcScore(int t, long d)
        {
            return d + t * t;
        }

        public static void Main(string[] args)
        {
            long res = 0;
            List<Sushi> remainedSushi = new List<Sushi>();
            SushiTable st = new SushiTable();

            int N;
            long K;
            string[] str = Console.ReadLine().Split(' ');
            N = int.Parse(str[0]);
            K = long.Parse(str[1]);

            for (int i = 0; i < N; i++)
            {
                str = Console.ReadLine().Split(' ');
                int t = int.Parse(str[0]);
                long d = long.Parse(str[1]);
                remainedSushi.Add(new Sushi(t, d));
            }

            remainedSushi = remainedSushi.OrderByDescending(x => x.oishisa).ToList();

            for (int i = 0; i < K; i++)
            {
                st.Push(remainedSushi[i]);
            }

            int neta = st.Neta;
            res = CalcScore(neta, st.Oishisa);
            for (int i = neta; i < N; i++)
            {
                bool flag = st.IncreaseNeta(remainedSushi);
                if (!flag)
                {
                    break;
                }
                neta++;
                res = (res < CalcScore(neta, st.Oishisa)) ? CalcScore(neta, st.Oishisa) : res;
            }

            Console.WriteLine(res);
        }
    }
}

namespace MyLib
{
    #region Heap
    public class Heap<T> where T: IComparable<T>
    {
        private List<T> _heap;
        private int _size;
        public int Size => _size;

        public Heap()
        {
            _size = 0;
            _heap = new List<T>();
        }

        public void Push(T elem)
        {
            int _ref = _size;
            _heap.Add(elem);

            while (_ref != 0)
            {
                int i = (_ref - 1) / 2;
                if (_heap[_ref].CompareTo(_heap[i]) < 0) Swap(_ref, i);
                _ref = i;
            }
            _size++;
        }

        public T Pop()
        {
            T ret = _heap[0];
            int _ref = _size - 1;
            _heap[0] = _heap[_ref];
            _heap.RemoveAt(_ref);

            for (int i = 0, j; (j = 2 * i + 1) < _ref;)
            {
                if ((j != _ref - 1) && (_heap[j].CompareTo(_heap[j + 1]) > 0)) j++;
                if (_heap[i].CompareTo(_heap[j]) > 0) Swap(i, j);
                i = j;
            }
            _size--;

            return ret;
        }

        private void Swap(int m, int n)
        {
            T temp = _heap[m];
            _heap[m] = _heap[n];
            _heap[n] = temp;
        }
    }

    public class HeapDescending<T> where T: IComparable<T>
    {
        private List<T> _heap;
        private int _size;
        public int Size => _size;

        public HeapDescending()
        {
            _size = 0;
            _heap = new List<T>();
        }

        public void Push(T elem)
        {
            int _ref = _size;
            _heap.Add(elem);

            while (_ref != 0)
            {
                int i = (_ref - 1) / 2;
                if (_heap[_ref].CompareTo(_heap[i]) > 0) Swap(_ref, i);
                _ref = i;
            }
            _size++;
        }

        public T Pop()
        {
            T ret = _heap[0];
            int _ref = _size - 1;
            _heap[0] = _heap[_ref];
            _heap.RemoveAt(_ref);

            for (int i = 0, j; (j = 2 * i + 1) < _ref;)
            {
                if ((j != _ref - 1) && (_heap[j].CompareTo(_heap[j + 1]) < 0)) j++;
                if (_heap[i].CompareTo(_heap[j]) < 0) Swap(i, j);
                i = j;
            }
            _size--;

            return ret;
        }

        private void Swap(int m, int n)
        {
            T temp = _heap[m];
            _heap[m] = _heap[n];
            _heap[n] = temp;
        }
    }
    #endregion

    #region PriorityQueue
    public class PriorityQueue<T> where T: IComparable<T>
    {
        Heap<T> _priority_queue;
        public int Size => _priority_queue.Size;

        public PriorityQueue()
        {
            _priority_queue = new Heap<T>();
        }

        public void Push(T elem) { _priority_queue.Push(elem); }

        public T Pop() { return _priority_queue.Pop(); }
    }
    public class PriorityQueueDescending<T> where T: IComparable<T>
    {
        HeapDescending<T> _priority_queue;
        public int Size => _priority_queue.Size;

        public PriorityQueueDescending()
        {
            _priority_queue = new HeapDescending<T>();
        }

        public void Push(T elem) { _priority_queue.Push(elem); }

        public T Pop() { return _priority_queue.Pop(); }
    }
    #endregion
}