Problem:
Score : 500 points

Problem Statement

Takahashi has come to a party as a special guest.
There are N ordinary guests at the party. The i-th ordinary guest has a power of A_i.

Takahashi has decided to perform M handshakes to increase the happiness of the party (let the current happiness be 0).
A handshake will be performed as follows:

Takahashi chooses one (ordinary) guest x for his left hand and another guest y for his right hand (x and y can be the same).

Then, he shakes the left hand of Guest x and the right hand of Guest y simultaneously to increase the happiness by A_x+A_y.

However, Takahashi should not perform the same handshake more than once. Formally, the following condition must hold:

Assume that, in the k-th handshake, Takahashi shakes the left hand of Guest x_k and the right hand of Guest y_k. Then, there is no pair p, q (1 \leq p < q \leq M) such that (x_p,y_p)=(x_q,y_q).

What is the maximum possible happiness after M handshakes?

Constraints

1 \leq N \leq 10^5

1 \leq M \leq N^2

1 \leq A_i \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 ... A_N

Output

Print the maximum possible happiness after M handshakes.

Sample Input 1

5 3
10 14 19 34 33

Sample Output 1

202

Let us say that Takahashi performs the following handshakes:

In the first handshake, Takahashi shakes the left hand of Guest 4 and the right hand of Guest 4.

In the second handshake, Takahashi shakes the left hand of Guest 4 and the right hand of Guest 5.

In the third handshake, Takahashi shakes the left hand of Guest 5 and the right hand of Guest 4.

Then, we will have the happiness of (34+34)+(34+33)+(33+34)=202.

We cannot achieve the happiness of 203 or greater, so the answer is 202.

Sample Input 2

9 14
1 3 5 110 24 21 34 5 3

Sample Output 2

1837

Sample Input 3

9 73
67597 52981 5828 66249 75177 64141 40773 79105 16076

Sample Output 3

8128170

Solution in C# (.NET Core 3.1.201) (judge verdict: Accepted)

using System;
using CompLib.Util;
using System.Diagnostics;
using CompLib.Algorithm;
using CompLib.Mathematics;

public class Program
{
    private int N;
    private long M;
    private int[] A;

    public void Solve()
    {
        var sc = new Scanner();
        N = sc.NextInt();
        M = sc.NextLong();
        A = sc.IntArray();

        ModInt2[] x = new ModInt2[100001];
        foreach (int i in A)
        {
            x[i] += 1;
        }

        FastFourierTransform2.Convolution2(ref x);

        long ans = 0;
        long cnt = 0;
        for (int i = 200000; i >= 0 && cnt < M; i--)
        {
            long n = x[i].Num;
            long c = Math.Min(M - cnt, n);
            ans += i * c;
            cnt += c;
        }

        Console.WriteLine(ans);
    }

    public static void Main(string[] args) => new Program().Solve();
}


namespace CompLib.Algorithm
{
    public class FastFourierTransform2
    {
        private const int E = 26;
        public static readonly ModInt2 O = new ModInt2(18769, 136);

        public static readonly ModInt2[] Z;
        public static readonly ModInt2[] InvZ;
        private static readonly ModInt2[] InvLen;

        static FastFourierTransform2()
        {
            Z = new ModInt2[E + 1];
            InvZ = new ModInt2[E + 1];
            Z[E] = O;
            InvZ[E] = ModInt2.Inverse(O);

            for (int i = E - 1; i >= 0; i--)
            {
                Z[i] = Z[i + 1] * Z[i + 1];
                InvZ[i] = InvZ[i + 1] * InvZ[i + 1];
            }

            InvLen = new ModInt2[E + 1];
            for (int lg = 0; lg <= E; lg++)
            {
                InvLen[lg] = ModInt2.Inverse(1 << lg);
            }
        }

        private static int BitReverse(int v, int lg)
        {
            int result = 0;
            for (int i = 0; i < lg; i++)
            {
                if (((1 << i) & v) > 0) result |= 1 << (lg - i - 1);
            }

            return result;
        }

        private static void Transform(ModInt2[] a, int lg, bool inverse)
        {
            Debug.Assert(a.Length == (1 << lg));
            for (int i = 0; i < a.Length; i++)
            {
                int rev = BitReverse(i, lg);
                if (i < rev)
                {
                    var t = a[i];
                    a[i] = a[rev];
                    a[rev] = t;
                }
            }

            for (int i = 1; i <= lg; i++)
            {
                int b = 1 << (i - 1);
                ModInt2 z = 1;
                for (int j = 0; j < b; j++)
                {
                    for (int k = j; k < a.Length; k += (b << 1))
                    {
                        var t = z * a[k + b];
                        var u = a[k];
                        a[k] = u + t;
                        a[k + b] = u - t;
                    }

                    z *= inverse ? InvZ[i] : Z[i];
                }
            }
        }

        public static void Convolution(ref ModInt2[] a, ModInt2[] b)
        {
            int sz = a.Length + b.Length - 1;
            int lg = 0;
            int len = 1;
            for (; len < sz; lg++, len <<= 1) ;

            Array.Resize(ref a, len);
            Array.Resize(ref b, len);
            Transform(a, lg, false);
            Transform(b, lg, false);
            for (int i = 0; i < len; i++)
            {
                a[i] *= b[i];
            }

            Transform(a, lg, true);
            for (int i = 0; i < len; i++)
            {
                a[i] *= InvLen[lg];
            }
        }

        public static void Convolution2(ref ModInt2[] a)
        {
            int sz = a.Length + a.Length - 1;
            int lg = 0;
            int len = 1;
            for (; len < sz; lg++, len <<= 1) ;

            Array.Resize(ref a, len);
            Transform(a, lg, false);
            for (int i = 0; i < len; i++)
            {
                a[i] *= a[i];
            }

            Transform(a, lg, true);
            for (int i = 0; i < len; i++)
            {
                a[i] *= InvLen[lg];
            }
        }
    }

    public struct ModInt2
    {
        private const long Mod1 = 15 * (1 << 27) + 1;
        private const long Mod2 = 27 * (1 << 26) + 1;

        public long Num1 { private set; get; }
        public long Num2 { private set; get; }

        public long Num
        {
            get
            {
                Num1 %= Mod1;
                Num2 %= Mod2;
                long o;
                return EMath.ChaineseRemainderTheorem(Num1, Mod1, Num2, Mod2, out o);
            }
        }

        public ModInt2(long n, long m)
        {
            Num1 = n;
            Num2 = m;
        }

        public static implicit operator ModInt2(long n)
        {
            return new ModInt2(n % Mod1, n % Mod2);
        }

        public static ModInt2 operator +(ModInt2 l, ModInt2 r)
        {
            return new ModInt2((l.Num1 + r.Num1) % Mod1, (l.Num2 + r.Num2) % Mod2);
        }

        public static ModInt2 operator -(ModInt2 l, ModInt2 r)
        {
            long n = (l.Num1 - r.Num1) % Mod1;
            if (n < 0) n += Mod1;
            long m = (l.Num2 - r.Num2) % Mod2;
            if (m < 0) m += Mod2;
            return new ModInt2(n, m);
        }

        public static ModInt2 operator *(ModInt2 l, ModInt2 r)
        {
            return new ModInt2((l.Num1 * r.Num1) % Mod1, (l.Num2 * r.Num2) % Mod2);
        }

        public static ModInt2 Inverse(ModInt2 v)
        {
            return new ModInt2(Pow(v.Num1, Mod1 - 2, Mod1), Pow(v.Num2, Mod2 - 2, Mod2));
        }

        private static long Pow(long v, long k, long mod)
        {
            long result = 1;
            while (k > 0)
            {
                if ((k & 1) > 0)
                {
                    result *= v;
                    result %= mod;
                }

                v *= v;
                v %= mod;
                k >>= 1;
            }

            return result;
        }
    }
}

namespace CompLib.Mathematics
{
    public static partial class EMath
    {
        private static long Mod(long a, long m)
        {
            return (a % m + m) % m;
        }

        /// <summary>
        /// n%p=a, n%q=b, n%lcm(p*q)を求める ただしa=b (mod gcd(p,q))
        /// </summary>
        /// <param name="a"></param>
        /// <param name="p"></param>
        /// <param name="b"></param>
        /// <param name="q"></param>
        /// <returns></returns>
        public static long ChaineseRemainderTheorem(long a, long p, long b, long q, out long pq)
        {
            long c, d;
            long e = EMath.ExtGCD(p, q, out c, out d);
            pq = p / e * q;
            if (a % e != b % e)
            {
                return -1;
            }

            // pc + qd = e 

            // n = a + p(b-a)c/e

            // p(b-a)c/e (mod lcm(pq))

            long tmp = (b - a) / e * c % (q / e);
            return (a + Mod(p * tmp, pq)) % pq;
        }
    }
}

namespace CompLib.Mathematics
{
    public partial class EMath
    {
        /// <summary>
        /// ap+bq=gcd(a,b)を求め,gcd(a,b)を返す
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <param name="p"></param>
        /// <param name="q"></param>
        /// <returns></returns>
        public static long ExtGCD(long a, long b, out long p, out long q)
        {
            if (b == 0)
            {
                p = 1;
                q = 0;
                return a;
            }

            long d = ExtGCD(b, a % b, out q, out p);
            q -= a / b * p;
            return d;
        }
    }
}

namespace CompLib.Util
{
    using System;
    using System.Linq;

    class Scanner
    {
        private string[] _line;
        private int _index;
        private const char Separator = ' ';

        public Scanner()
        {
            _line = new string[0];
            _index = 0;
        }

        public string Next()
        {
            if (_index >= _line.Length)
            {
                string s;
                do
                {
                    s = Console.ReadLine();
                } while (s.Length == 0);

                _line = s.Split(Separator);
                _index = 0;
            }

            return _line[_index++];
        }

        public string ReadLine()
        {
            _index = _line.Length;
            return Console.ReadLine();
        }

        public int NextInt() => int.Parse(Next());
        public long NextLong() => long.Parse(Next());
        public double NextDouble() => double.Parse(Next());
        public decimal NextDecimal() => decimal.Parse(Next());
        public char NextChar() => Next()[0];
        public char[] NextCharArray() => Next().ToCharArray();

        public string[] Array()
        {
            string s = Console.ReadLine();
            _line = s.Length == 0 ? new string[0] : s.Split(Separator);
            _index = _line.Length;
            return _line;
        }

        public int[] IntArray() => Array().Select(int.Parse).ToArray();
        public long[] LongArray() => Array().Select(long.Parse).ToArray();
        public double[] DoubleArray() => Array().Select(double.Parse).ToArray();
        public decimal[] DecimalArray() => Array().Select(decimal.Parse).ToArray();
    }
}