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: Runtime Error)

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 Sushi(int t, long d)
        {
            neta = t;
            oishisa = d;
        }

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

    public class GMax: IComparable<GMax>
    {
        public long val;
        private int neta;
        private long oishisa;
        public GMax(int t, long d)
        {
            neta = t;
            oishisa = d;
            val = oishisa + neta * neta;
        }

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

    public class SushiTable
    {
        public MyLib.PriorityQueue<Sushi> slist;
        private int[] netalist = new int[100001];
        private long oishisa = 0;
        public int Neta
        {
            get
            {
                int temp = 0;
                for (int i = 0; i < 100001; i++)
                {
                    if (netalist[i] > 0)
                        temp++;
                }
                return temp;
            }
        }
        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)
        {
            netalist[param.neta]++;
            oishisa += param.oishisa;
            slist.Push(param);
        }

        public bool IncreaseNeta(MyLib.PriorityQueueDescending<Sushi> param)
        {
            bool flag = RemoveMinNeta(param);
            if (flag == false) return false;
            flag = AddMaxNeta(param);
            return flag;
        }

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

            return flag;
        }

        private bool AddMaxNeta(MyLib.PriorityQueueDescending<Sushi> param)
        {
            MyLib.PriorityQueueDescending<Sushi> temp = new MyLib.PriorityQueueDescending<Sushi>();
            bool flag = false;
            int size = param.Size;
            for (int i = 0; i < size; i++)
            {
                Sushi s = param.Pop();
                if (netalist[s.neta] == 0)
                {
                    netalist[s.neta]++;
                    oishisa += s.oishisa;
                    slist.Push(s);
                    flag = true;
                    break;
                }
                temp.Push(s);
            }
            size = temp.Size;
            for (int i = 0; i < size; i++)
            {
                param.Push(temp.Pop());
            }

            return flag;
        }
    }

    class Program
    {
        public static void Main(string[] args)
        {
            MyLib.PriorityQueueDescending<GMax> res = new MyLib.PriorityQueueDescending<GMax>();
            MyLib.PriorityQueueDescending<Sushi> remainedSushi = new MyLib.PriorityQueueDescending<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.Push(new Sushi(t, d));
            }

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

            int neta = st.Neta;
            res.Push(new GMax(neta, st.Oishisa));
            for (int i = neta; i < N; i++)
            {
                if (remainedSushi.Size == 0) break;
                bool flag = st.IncreaseNeta(remainedSushi);
                if (!flag)
                {
                    break;
                }
                neta++;
                res.Push(new GMax(neta, st.Oishisa));
            }

            GMax g = res.Pop();
            Console.WriteLine(g.val);
        }
    }
}

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
}