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

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

// using System.Numerics;
using System.Runtime.CompilerServices;
using ll=System.Int64;
using static MyIO;

public class Contest
{
	static void Swap1<T>(ref T a, ref T b) { T t = a; a = b; b = t; }
	static void Swap2<T>(ref T a, ref T b) where T : IComparable { if (a.CompareTo(b)==1) Swap1(ref a, ref b); }  // b の方が小さければ交換
	static bool Chmin<T>(ref T a, T b) where T : IComparable { if (a.CompareTo(b)== 1) { a = b; return true; } return false; }  // b の方が小さければ a を更新
	static bool Chmax<T>(ref T a, T b) where T : IComparable { if (a.CompareTo(b)==-1) { a = b; return true; } return false; }  // b の方が大きければ a を更新
	static T[][] InitMat<T>(long len1, long len2, T val) where T : struct { T[][] m = new T[len1][]; for (int i = 0; i < m.Length; i++) m[i] = Enumerable.Repeat(val, (int)len2).ToArray(); return m; }
	static T[][][] InitMat<T>(long len1, long len2, long len3, T val) where T : struct { T[][][] m = new T[len1][][]; for (int i = 0; i < m.Length; i++) m[i] = InitMat(len2,len3,val); return m; }
	public const long MOD1 = 1000000007; // 10^9+7
	public const double EPS1 = 1e-8;

	public static void Main() {
		Solve_E();
 	}

	public static void Solve_A() {
		string  n, m;
		MyIO.ReadMulti(out n, out m);





		MyIO.WriteMulti(m+n);
	}
	public static void Solve_B() {
		long  a, b, k;
		MyIO.ReadMulti(out a, out b, out k);

		long[] ans = new ll[]{a, b};

		if(k<a) {
			ans[0] -= k;
		}else{
			k -= a;
			ans[0] = 0;
			if(k<b){
				ans[1] -= k;
			}else{
				ans[1] = 0;
			}
		} 

		MyIO.WriteArray(ans);
	}
	public static void Solve_C() {
		long  x;
		MyIO.ReadMulti(out x);

		bool[] pt = PrimeTable(2*x);

		long ans = x;
		while(!pt[ans]) ans++;




		MyIO.WriteMulti(ans);
	}
	public static void Solve_D() {
		int  n, k,r,s,p;
		string t;
		MyIO.ReadMulti(out n, out k);
		MyIO.ReadMulti(out r, out s,out p);
		MyIO.ReadMulti(out t);

		StringBuilder[] sa = new StringBuilder[k];
		for (int i = 0; i < n; i++)
		{
			if(i<k) sa[i] = new StringBuilder();
			sa[i%k].Append(t[i]);
		}

		long ans = 0;
		ll[][] dp;
		for (int i = 0; i < k; i++)
		{
			dp = InitMat(n/k+2,3,0L);
			string ss = sa[i].ToString();
			dp[0][0] = ss[0]=='s'?r:0;
			dp[0][1] = ss[0]=='p'?s:0;
			dp[0][2] = ss[0]=='r'?p:0;
			for (int j = 1; j < ss.Length; j++)
			{
				dp[j][0] = Math.Max(dp[j-1][1],dp[j-1][2]) + (ss[j]=='s'?r:0);
				dp[j][1] = Math.Max(dp[j-1][2],dp[j-1][0]) + (ss[j]=='p'?s:0);
				dp[j][2] = Math.Max(dp[j-1][0],dp[j-1][1]) + (ss[j]=='r'?p:0);
	
			}
			ans += Math.Max(dp[ss.Length-1][0],Math.Max(dp[ss.Length-1][1],dp[ss.Length-1][2]));
		}

		MyIO.WriteMulti(ans);
	}
	public static void Solve_E() {
		long  n, m;
		MyIO.ReadMulti(out n, out m);
		ll[] a;
		ReadArray(out a);

		ll[] p = new ll[100001];
		foreach (var e in a)
		{
			p[e]++;
		}
		double[] q = new double[100001];
		for (int i = 0; i < q.Length; i++)
		{
			q[i] = p[i];
		}

		FastFourierTransform fft = new FastFourierTransform();
		FastFourierTransform.Complex[] b = fft.Convolute(q,q);
		ll ans = 0;
		for (ll i = b.Length-1; 0<=i; i--)
		{
			if(b[i].Re<m){
				ans += i*(ll)Math.Round(b[i].Re);
				m -= (ll)Math.Round(b[i].Re);
			}else{
				ans += i*m;
				m = 0;
				break;
			}
		}


		MyIO.WriteMulti(ans);
	}
	public static void Solve_F() {
		long  n, m;
		MyIO.ReadMulti(out n, out m);



		long ans = 0;


		MyIO.WriteMulti(ans);
	}




    public static bool[] PrimeTable(long n) {
		bool[] e = Enumerable.Repeat(true, (int)n+1).ToArray();
		if(n >= 0) e[0] = false;
		if(n >= 1) e[1] = false;
		for(int j = 4; j < e.Length; j += 2) e[j] = false;
		for(int j = 9; j < e.Length; j += 6) e[j] = false;
        long sqi = (long)Math.Sqrt(n);
		while(sqi*sqi<=n) sqi++;
		for(int i = 5, g = 2; i < sqi; i += g, g ^= 6) {
			if(!e[i]) continue;
			for(int j = i * i; j < e.Length; j += i * 2) e[j] = false;
		}
		
		return e;
    }


public class FastFourierTransform
{
	public struct Complex{
		public double Re; public double Im;
		public Complex(double r, double i) { Re = r; Im = i; }
		static public Complex Add(Complex a, Complex b) { return new Complex(a.Re+b.Re, a.Im+b.Im); }
		static public Complex Sub(Complex a, Complex b) { return new Complex(a.Re-b.Re, a.Im-b.Im); }
		static public Complex Mul(Complex a, Complex b) { return new Complex(a.Re*b.Re-a.Im*b.Im, a.Re*b.Im+a.Im*b.Re); }
	}

    public FastFourierTransform() { }
 
    public Complex[] Convolute(double[] A, double[] B)
    {
        int sz = A.Length + B.Length + 1;
        int N = 1; while (N < sz) N <<= 1;
 
        Complex[] G = new Complex[N];
        Complex[] H = new Complex[N];
        for (int i = 0; i < A.Length; i++) G[i] = new Complex(A[i],0);
        for (int i = 0; i < B.Length; i++) H[i] = new Complex(B[i],0);
 
        FFT(G); FFT(H);
        Complex[] F = new Complex[N];
        for (int i = 0; i < N; i++)F[i] = Complex.Mul(G[i], H[i]);
 
        FFT(F, -1);
        return F;
    }
    private void FFT(Complex[] F, int rev = 1)
    {
        for (int i = 0, j = 1; j < F.Length - 1; j++) {
            for (int k = F.Length >> 1; k > (i ^= k); k >>= 1) ;
            if (j < i) swap(ref F[i], ref F[j]);
        }
 
        for (int i = 2; i <= F.Length; i <<= 1) {
            int m = i >> 1;
			Complex[] zp = new Complex[F.Length];
			for (int j = 0; j < F.Length; j++) zp[j] = new Complex(Math.Cos(Math.PI / m * j), Math.Sin(Math.PI / m * j) * rev);
 
            for (int j = 0; j < F.Length; j += i) {
                for (int u = j, v = j + m; v < j + i; u++, v++) {
                    Complex vl = F[u], vr = Complex.Mul(zp[u-j], F[v]);
                    F[u] = Complex.Add(vl, vr);
                    F[v] = Complex.Sub(vl, vr);
                }
            }
        }
 
        if (rev == -1) {
            for (int i = 0; i < F.Length; i++) F[i] = new Complex(F[i].Re/F.Length, F[i].Im/F.Length);
        }
    }
     private void swap<T>(ref T a, ref T b) { var t = a; a = b; b = t; }
}








}




















#region MyIO

public struct VT<T, U>
{
	public T Item1; public U Item2;
    public VT(T a, U b) { Item1 = a; Item2 = b; }
}
public struct VT<T, U, V>
{
	public T Item1; public U Item2; public V Item3;
    public VT(T a, U b, V c) { Item1 = a; Item2 = b; Item3 = c; }
}
public struct VT<T, U, V, W>
{
	public T Item1; public U Item2; public V Item3; public W Item4;
    public VT(T a, U b, V c, W d) { Item1 = a; Item2 = b; Item3 = c; Item4 = d; }
}

public class Printer : System.IO.StreamWriter
{
	public override IFormatProvider FormatProvider { get { return System.Globalization.CultureInfo.InvariantCulture; } }
	public Printer(System.IO.Stream stream) : base(stream, new UTF8Encoding(false, true)) { }
	public Printer(System.IO.Stream stream, Encoding encoding) : base(stream, encoding) { }
}
public static class MyIO
{
	private static char[] splitter = new char[] {' '};
	private static bool eq<T, U>() => typeof(T).Equals(typeof(U));
	private static T ct<T, U>(U a) => (T)Convert.ChangeType(a, typeof(T));
	private static T cv<T>(string s) =>
		eq<T, int>()    ? ct<T, int>(int.Parse(s))
	  : eq<T, long>()   ? ct<T, long>(long.Parse(s))
	  : eq<T, double>() ? ct<T, double>(double.Parse(s, System.Globalization.CultureInfo.InvariantCulture))
	  : eq<T, char>()   ? ct<T, char>(s[0])
						: ct<T, string>(s);
			
	public static string[] ReadMulti<T>(out T a) {
		var ar = Console.ReadLine().Split(splitter, StringSplitOptions.RemoveEmptyEntries); 
		a = cv<T>(ar[0]);
		return ar;
	}
	public static string[] ReadMulti<T, U>(out T a, out U b) {
		var ar = ReadMulti<T>(out a); 
        b = cv<U>(ar[1]);
		return ar;
	}
	public static string[] ReadMulti<T, U, V>(out T a, out U b, out V c) {
		var ar = ReadMulti<T, U>(out a, out b); 
        c = cv<V>(ar[2]);
		return ar;
	}
	public static string[] ReadMulti<T, U, V, W>(out T a, out U b, out V c, out W d) {
		var ar = ReadMulti<T, U, V>(out a, out b, out c); 
        d = cv<W>(ar[3]);
		return ar;
	}
	public static string[] ReadMulti<T, U, V, W, X>(out T a, out U b, out V c, out W d, out X e) {
		var ar = ReadMulti<T, U, V, W>(out a, out b, out c, out d); 
        e = cv<X>(ar[4]);
		return ar;
	}
	public static string[] ReadMulti<T, U, V, W, X, Y>(out T a, out U b, out V c, out W d, out X e, out Y f) {
		var ar = ReadMulti<T, U, V, W, X>(out a, out b, out c, out d, out e); 
        f = cv<Y>(ar[5]);
		return ar;
	}
	public static string[] ReadArray<T>(out T[] a) {		
		var ar = Console.ReadLine().Split(splitter, StringSplitOptions.RemoveEmptyEntries);
		a = ar.Select(cv<T>).ToArray();
		return ar;
	}		
	public static string[][] ReadLines<T>(long n, out T[] a) {
		a = new T[n];
		string[][] arr = new string[n][];
		for(int i=0; i<n; i++) arr[i] = ReadMulti(out a[i]);
		return arr;
	}
	public static string[][] ReadLines<T, U>(long n, out VT<T, U>[] a) {
		a = new VT<T, U>[n];
		string[][] arr = new string[n][];
		for(int i=0; i<n; i++) {
			T o; U p;
			arr[i] = ReadMulti(out o, out p);
			a[i] = new VT<T, U>(o, p);
		}
		return arr;
	}
	public static string[][] ReadLines<T, U, V>(long n, out VT<T, U, V>[] a) {
		a = new VT<T, U, V>[n];
		string[][] arr = new string[n][];
		for(int i=0; i<n; i++) {
			T o; U p; V q;
			arr[i] = ReadMulti(out o, out p, out q);
			a[i] = new VT<T, U, V>(o, p, q);
		}
		return arr;
	}
	public static string[][] ReadLines<T, U, V, W>(long n, out VT<T, U, V, W>[] a) {
		a = new VT<T, U, V, W>[n];
		string[][] arr = new string[n][];
		for(int i=0; i<n; i++) {
			T o; U p; V q; W r;
			arr[i] = ReadMulti(out o, out p, out q, out r);
			a[i] = new VT<T, U, V, W>(o, p, q, r);
		}
		return arr;
	}
	public static string[][] ReadMatrix<T>(long n, out T[][] a) {
		a = new T[n][];
		string[][] arr = new string[n][];
		for(int i=0; i<n; i++) arr[i] = ReadArray(out a[i]);
		return arr;
	}

	private static void wwp(Action act){
		var sw = new Printer(Console.OpenStandardOutput()){AutoFlush = false};
		Console.SetOut(sw);
		act();
		Console.Out.Flush();
		sw.AutoFlush = true;
		Console.SetOut(sw);
	}
	private static string wfm(Type t) =>t.Equals(typeof(double)) ? "{0:F10}" : "{0}";
	public static void WriteMulti(params object[] a) {
		wwp(()=>{
			for(int i=0;i<a.Length;i++) {
				string fs = wfm(a[i].GetType()) + ((i<a.Length-1) ? " " : Environment.NewLine);
				Console.Write(fs, a[i]);
			}
		});
	}
	public static void WriteArray<T>(T[] a) {
		wwp(()=>{
			string fs = wfm(typeof(T));
			string fs1 = fs + " ";
			string fs2 = fs + Environment.NewLine;
			for(int i=0;i<a.Length;i++) Console.Write( ((i<a.Length-1) ? fs1 : fs2), a[i]);
		});
	}
	public static void WriteLines<T>(T[] a) {
		wwp(()=>{
			string fs = wfm(typeof(T)) + Environment.NewLine;
			foreach(T x in a) Console.Write(fs, x);
		});
	}


}

#endregion