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.Linq;
using System.Collections.Generic;
using System.Diagnostics;
using Debug = System.Diagnostics.Trace;
using SB = System.Text.StringBuilder;
using static System.Math;
using Number = System.Int32;
using System.Numerics;

namespace Program {
	public class Solver {
		Random rnd = new Random();
		public void Solve() {
			var n = ri; var m = rl;
			var a = Enumerate(n, x => ri);
			Array.Sort(a);
			var l = -1; var r = 200050;
			while (r - l > 1) {
				var x = (l + r) / 2;
				if (f(a, x) >= m) l = x;
				else r = x;
			}
			var b = new long[n + 1];
			for (int i = 0; i < n; i++) b[i + 1] = b[i] + a[i];
			var sum = 0L;
			Debug.WriteLine(a.AsJoinedString());
			foreach (var v in a) {
				var lb = a.LowerBound(r - v);
				m -= n - lb;
				sum += 1L * (n - lb) * v;
				sum += b[n] - b[lb];
				Debug.WriteLine($"{v} {lb}");
			}
			Debug.WriteLine(r);
			Debug.WriteLine(l);
			sum += l * m;
			Console.WriteLine(sum);
		}
		long f(int[] a, int x) {
			var ret = 0L;
			var n = a.Length;
			foreach (var v in a) {
				var lb = a.LowerBound(x - v);
				ret += n - lb;
			}
			return ret;
		}

		const long INF = 1L << 60;
		static int[] dx = { -1, 0, 1, 0 };
		static int[] dy = { 0, 1, 0, -1 };
		int ri { get { return sc.Integer(); } }
		long rl { get { return sc.Long(); } }
		double rd { get { return sc.Double(); } }
		string rs { get { return sc.Scan(); } }
		public IO.StreamScanner sc = new IO.StreamScanner(Console.OpenStandardInput());

		static T[] Enumerate<T>(int n, Func<int, T> f) {
			var a = new T[n];
			for (int i = 0; i < a.Length; ++i) a[i] = f(i);
			return a;
		}
		static public void Swap<T>(ref T a, ref T b) { var tmp = a; a = b; b = tmp; }
	}
}

#region main
static class Ex {
	static public string AsString(this IEnumerable<char> ie) { return new string(ie.ToArray()); }
	static public string AsJoinedString<T>(this IEnumerable<T> ie, string st = " ") {
		return string.Join(st, ie);
	}
	static public void Main() {
		Console.SetOut(new Program.IO.Printer(Console.OpenStandardOutput()) { AutoFlush = true });
		var solver = new Program.Solver();
		//* 
		var t = new System.Threading.Thread(solver.Solve, 50000000);
		t.Start();
		t.Join();
		//*/
		// solver.Solve();
		Console.Out.Flush();
	}
}
#endregion
#region Ex
namespace Program.IO {
	using System.IO;
	using System.Text;
	using System.Globalization;

	public class Printer : StreamWriter {
		public override IFormatProvider FormatProvider { get { return CultureInfo.InvariantCulture; } }
		public Printer(Stream stream) : base(stream, new UTF8Encoding(false, true)) { }
	}

	public class StreamScanner {
		public StreamScanner(Stream stream) { str = stream; }

		public readonly Stream str;
		private readonly byte[] buf = new byte[1024];
		private int len, ptr;
		public bool isEof = false;
		public bool IsEndOfStream { get { return isEof; } }

		private byte read() {
			if (isEof) return 0;
			if (ptr >= len) {
				ptr = 0;
				if ((len = str.Read(buf, 0, 1024)) <= 0) {
					isEof = true;
					return 0;
				}
			}
			return buf[ptr++];
		}

		public char Char() {
			byte b = 0;
			do b = read(); while ((b < 33 || 126 < b) && !isEof);
			return (char)b;
		}
		public string Scan() {
			var sb = new StringBuilder();
			for (var b = Char(); b >= 33 && b <= 126; b = (char)read()) sb.Append(b);
			return sb.ToString();
		}
		public string ScanLine() {
			var sb = new StringBuilder();
			for (var b = Char(); b != '\n' && b != 0; b = (char)read()) if (b != '\r') sb.Append(b);
			return sb.ToString();
		}
		public long Long() { return isEof ? long.MinValue : long.Parse(Scan()); }
		public int Integer() { return isEof ? int.MinValue : int.Parse(Scan()); }
		public double Double() { return isEof ? double.NaN : double.Parse(Scan(), CultureInfo.InvariantCulture); }
	}
}

#endregion
#region BinarySearch
public static partial class Algorithm {
	static int binarySearch<T>(IList<T> a, T v, IComparer<T> cmp, bool islb) {
		var l = 0;
		var r = a.Count - 1;
		while (l <= r) {
			var m = (l + r) / 2;
			var res = cmp.Compare(a[m], v);
			if (res < 0 || (res == 0 && !islb)) l = m + 1;
			else r = m - 1;
		}
		return l;
	}

	/// <summary>
	/// 与えられた比較関数に従って，<paramref name="a"/> の要素のうち，<paramref name="v"/> 以上の要素であるような最小のインデックスを取得します．
	/// </summary>
	/// <typeparam name="T"><see cref="IList{T}"/> の要素の型を指定します．</typeparam>
	/// <param name="a">対象となるコレクション</param>
	/// <param name="v">対象となる要素</param>
	/// <param name="f"></param>
	/// <returns><paramref name="v"/> 以上の要素であるような最小の o-indexed でのインデックス．</returns>
	/// <remarks> <paramref name="a"/> は比較関数に対して昇順であることを仮定しています．この関数は O(log N) で実行されます．</remarks>
	public static int LowerBound<T>(this IList<T> a, T v, Comparison<T> f) { return binarySearch(a, v, Comparer<T>.Create(f), true); }

	/// <summary>
	///　デフォルトの比較関数に従って，<paramref name="a"/> の要素のうち，<paramref name="v"/> 以上の要素であるような最小のインデックスを取得します．
	/// </summary>
	/// <typeparam name="T"><see cref="IList{T}"/> の要素の型を指定します．</typeparam>
	/// <param name="a">対象となるコレクション</param>
	/// <param name="v">対象となる要素</param>
	/// <param name="f"></param>
	/// <returns><paramref name="v"/> 以上の要素であるような最小の o-indexed でのインデックス．</returns>
	/// <remarks> <paramref name="a"/> は比較関数に対して昇順であることを仮定しています．この関数は O(log N) で実行されます．</remarks>
	public static int LowerBound<T>(this IList<T> a, T v) { return binarySearch(a, v, Comparer<T>.Default, true); }

	/// <summary>
	/// 与えられた比較関数に従って，<paramref name="a"/> の要素のうち，<paramref name="v"/> 以上の要素であるような最小のインデックスを取得します．
	/// </summary>
	/// <typeparam name="T"><see cref="IList{T}"/> の要素の型を指定します．</typeparam>
	/// <param name="a">対象となるコレクション</param>
	/// <param name="v">対象となる要素</param>
	/// <param name="f"></param>
	/// <returns><paramref name="v"/> 以上の要素であるような最小の o-indexed でのインデックス．</returns>
	/// <remarks> <paramref name="a"/> は比較関数に対して昇順であることを仮定しています．この関数は O(log N) で実行されます．</remarks>
	public static int UpperBound<T>(this IList<T> a, T v, Comparison<T> cmp) { return binarySearch(a, v, Comparer<T>.Create(cmp), false); }

	/// <summary>
	///　デフォルトの比較関数に従って，<paramref name="a"/> の要素のうち，<paramref name="v"/> より真に大きい要素が現れる最小のインデックスを取得します．
	/// </summary>
	/// <typeparam name="T"><see cref="IList{T}"/> の要素の型を指定します．</typeparam>
	/// <param name="a">対象となるコレクション</param>
	/// <param name="v">対象となる要素</param>
	/// <param name="f"></param>
	/// <returns><paramref name="v"/> 以上の要素であるような最小の o-indexed でのインデックス．</returns>
	/// <remarks> <paramref name="a"/> は比較関数に対して昇順であることを仮定しています．この関数は O(log N) で実行されます．</remarks>
	public static int UpperBound<T>(this IList<T> a, T v) { return binarySearch(a, v, Comparer<T>.Default, false); }

}
#endregion