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;
using System.Text;

namespace Practice
{
    class Program
    {
        static void Main(string[] args)
        {
            Solve();
        }
        private const int MOD = 1000000007;
        private struct Data
        {
            public long t;
            public long d;

        }
#if true
        static void Solve()
        {
            var nk = ReadAndParseLongArr();
            long n = nk[0];
            long k = nk[1];
            var datas = new List<Data>();
            for (int i = 0; i < n; ++i)
            {
                var td = ReadAndParseLongArr();
                datas.Add(new Data() { t = td[0], d = td[1] });
            }
            long ans = 0;
            datas.Sort((a, b) => b.d.CompareTo(a.d));
            long ans0 = Sum(datas, k);
            long ans1 = SumPriorKind(datas, k);
            ans = Math.Max(ans0, ans1);
            Console.WriteLine(ans);
        }
        private static long Sum(List<Data> datas, long k)
        {
            long ans = 0;
            var hash = new HashSet<long>();
            for(int i = 0; i < k; ++i)
            {
                var data = datas[i];
                ans += data.d;
                if(! hash.Contains(data.t))
                {
                    hash.Add(data.t);
                }
            }
            ans += hash.Count * hash.Count;
            return ans;
        }
        private static long SumPriorKind(List<Data> datas, long k)
        {
            long ans = 0;
            long eat = 0;
            var hash = new HashSet<long>();
            var eatids = new HashSet<long>();
            for (int i = 0; i < datas.Count; ++i)
            {
                var data = datas[i];
                if (hash.Contains(data.t)) { continue; }

                ans += data.d;
                hash.Add(data.t);
                eatids.Add(i);
                eat++;
                if (eat >= k) { break; }
            }
            for (int i = 0; i < datas.Count; ++i)
            {
                if (eat >= k) { break; }
                if (eatids.Contains(i)) { continue; }

                var data = datas[i];
                ans += data.d;
                eat++;
            }

            ans += hash.Count * hash.Count;

            return ans;
        }

#else
        static void Solve()
        {
            var nk = ReadAndParseLongArr();
            long n = nk[0];
            long k = nk[1];
            var datas = new List<Data>();
            for(int i=0;i<n;++i)
            {
                var td = ReadAndParseLongArr();
                datas.Add(new Data() { t = td[0], d = td[1] });
            }
            long[,] dp = new long[n + 1, n + 1];
            HashSet<long> eatT = new HashSet<long>();
            long eatTotal = 0;
            for(int i=0;i<n;++i)
            {
                for(int j=0;j<n;++j)
                {
                    var data = datas[i];
                    if( 1<= j && j <= k && eatTotal < k)
                    {
                        eatTotal++;
                        if( eatT.Contains(data.t))
                        {
                            dp[i + 1, j] =dp[i, j] + data.d;
                        }
                        else
                        {
                            eatT.Add(data.t);
                            dp[i + 1, j] = Math.Max(dp[i, j - 1] + data.d + j * j - (j - 1) * (j - 1), dp[i, j]);
                        }
                    }
                    else
                    {
                        dp[i + 1, j] = dp[i, j];
                    }
                }
            }
            long ans = dp[n, k];
            

            Console.WriteLine(ans);
        }
#endif
        #region よく使う便利関数
        private static bool isPrime(long x)
        {
            if (x == 2) { return true; }
            if (x < 2 || x % 2 == 0) { return false; }
            long i = 3;
            while (i * i <= x)
            {
                if (x % i == 0) { return false; }
                i = i + 2;
            }
            return true;
        }
        private static long lcm(long x, long y)
        {
            return x / gcd(x, y) * y;
        }
        private static long gcd(long x, long y)
        {
            return y > 0 ? gcd(y, x % y) : x;
        }
        private static long pow(long x, long n)
        {
            if (n == 0) { return 1; }
            long res = pow(x * x % MOD, n / 2);
            if (n % 2 == 1)
            {
                res = res * x % MOD;
            }
            return res;
        }
        private static int ReadAndParseInt()
        {
            return int.Parse(Console.ReadLine());
        }
        private static int[] ReadAndParseIntArr()
        {
            return Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse);
        }
        private static long ReadAndParseLong()
        {
            return long.Parse(Console.ReadLine());
        }
        private static long[] ReadAndParseLongArr()
        {
            return Array.ConvertAll(Console.ReadLine().Split(' '), long.Parse);
        }

        /// <summary>
        /// 指定した値以上の先頭のインデクスを返す
        /// </summary>
        /// <typeparam name="T">比較する値の型</typeparam>
        /// <param name="arr">対象の配列（※ソート済みであること）</param>
        /// <param name="start">開始インデクス [inclusive]</param>
        /// <param name="end">終了インデクス [exclusive]</param>
        /// <param name="value">検索する値</param>
        /// <param name="comparer">比較関数(インターフェイス)</param>
        /// <returns>指定した値以上の先頭のインデクス</returns>
        public static int LowerBound<T>(IReadOnlyList<T> arr, int start, int end, T value, IComparer<T> comparer)
        {
            int low = start;
            int high = end;
            int mid;
            while (low < high)
            {
                mid = ((high - low) >> 1) + low;
                if (comparer.Compare(arr[mid], value) < 0)
                    low = mid + 1;
                else
                    high = mid;
            }
            return low;
        }

        //引数省略のオーバーロード
        public static int LowerBound<T>(IReadOnlyList<T>  arr, T value) where T : IComparable
        {
            return LowerBound(arr, 0, arr.Count, value, Comparer<T>.Default);
        }

        /// <summary>
        /// 指定した値より大きい先頭のインデクスを返す
        /// </summary>
        /// <typeparam name="T">比較する値の型</typeparam>
        /// <param name="arr">対象の配列（※ソート済みであること）</param>
        /// <param name="start">開始インデクス [inclusive]</param>
        /// <param name="end">終了インデクス [exclusive]</param>
        /// <param name="value">検索する値</param>
        /// <param name="comparer">比較関数(インターフェイス)</param>
        /// <returns>指定した値より大きい先頭のインデクス</returns>
        public static int UpperBound<T>(IReadOnlyList<T> arr, int start, int end, T value, IComparer<T> comparer)
        {
            int low = start;
            int high = end;
            int mid;
            while (low < high)
            {
                mid = ((high - low) >> 1) + low;
                if (comparer.Compare(arr[mid], value) <= 0)
                    low = mid + 1;
                else
                    high = mid;
            }
            return low;
        }

        //引数省略のオーバーロード
        public static int UpperBound<T>(IReadOnlyList<T> arr, T value)
        {
            return UpperBound(arr, 0, arr.Count, value, Comparer<T>.Default);
        }
#endregion
    }
}