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: Wrong Answer)

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]++;
		}

		NumberTheoreticTransform ntt = new NumberTheoreticTransform();
		ll[] b = ntt.Multiply(p,p);
		ll ans = 0;
		for (ll i = b.Length-1; 0<=i; i--)
		{
			if(b[i]<m){
				ans += i*b[i];
				m -= b[i];
			}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;
    }


// mod 上でのFFTによる畳込みを行う。長さ n のFFTを行うとき mod の原始 n 乗根が必要になる。
// よく使いそうな素数と原始根のペアとして 周期 221 の (1012924417,5),(924844033,5)などがある。
// Multiply(A, B):= 配列 A と配列 B を畳み込みした結果を返す。
// 参考  https://lumakernel.github.io/ecasdqina/math/FFT/NTT
public class NumberTheoreticTransform {
	private long[][] _rts, _rrts;
	private long _mod, _primitiveroot;

	public NumberTheoreticTransform() : this(1012924417, 5) {}
	public NumberTheoreticTransform(long mod, long primitiveroot) {
		_rts = new long[0][];
		_rrts = new long[0][];
		_mod = mod;
		_primitiveroot = primitiveroot;
	}

	private void ensure_base(int N) {
		if(_rts.Length >= N) return;
		Array.Resize(ref _rts, N);
		Array.Resize(ref _rrts, N);
		for(int i = 1; i < N; i <<= 1) {
			if((_rts[i]!=null) && (0<_rts[i].Length)) continue;
			long w = mod_pow(_primitiveroot, (_mod - 1) / (i * 2));
			long rw = inverse(w);
			_rts[i] = new long[i];
			_rrts[i] = new long[i];
			_rts[i][0] = 1;
			_rrts[i][0] = 1;
			for(int k = 1; k < i; k++) {
				_rts[i][k] = mul(_rts[i][k - 1], w);
				_rrts[i][k] = mul(_rrts[i][k - 1], rw);
			}
		}
	}
	[MethodImpl(MethodImplOptions.AggressiveInlining)]
	private long mod_pow(long x, long n) {
		long ret = 1;
		while(n > 0) {
			if(0<(n & 1)) ret = mul(ret, x);
			x = mul(x, x);
			n >>= 1;
		}
		return ret;
	}
	[MethodImpl(MethodImplOptions.AggressiveInlining)]
	private long inverse(long x) {
		return mod_pow(x, _mod - 2);
	}
	[MethodImpl(MethodImplOptions.AggressiveInlining)]
	private long add(long x, long y) {
		x += y;
		if(x >= _mod) x -= _mod;
		return x;
	}
	[MethodImpl(MethodImplOptions.AggressiveInlining)]
	private long mul(long a, long b) {
		return 1L * a * b % _mod;
	}

	public void DiscreteFourierTransform(long[] F, bool rev) {
		int N = F.Length;
		ensure_base(N);
		for(int i = 0, j = 1; j + 1 < N; j++) {
			for(int k = N >> 1; k > (i ^= k); k >>= 1);
			if(i > j) { long t = F[j]; F[j] = F[i]; F[i] = t; }
		}
		for(int i = 1; i < N; i <<= 1) {
			for(int j = 0; j < N; j += i * 2) {
				for(int k = 0; k < i; k++) {
					long s = F[j + k];
					long t = mul(F[j + k + i], rev ? _rrts[i][k] : _rts[i][k]);
					F[j + k] = add(s, t);
					F[j + k + i] = add(s, _mod - t);
				}
			}
		}
		if(rev) {
			long temp = inverse(N);
			for(int i = 0; i < N; i++) F[i] = mul(F[i], temp);
		}
	}

	public long[] Multiply(long[] A, long[] B) {
		int sz = 1;
		while(sz < A.Length + B.Length - 1) sz <<= 1;
		long[] F = new long[sz];
		long[] G = new long[sz];
		Array.Copy(A, 0, F, 0, A.Length);
		Array.Copy(B, 0, G, 0, B.Length);
		DiscreteFourierTransform(F, false);
		DiscreteFourierTransform(G, false);
		for(int i = 0; i < sz; i++) F[i] = mul(F[i], G[i]);
		DiscreteFourierTransform(F, true);
		Array.Resize(ref F, A.Length + B.Length - 1);
		return F;
	}

}






}




















#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