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++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

    /**************************************
    Author : Susanka Majumder ( bingobong )
    ***************************************/
     
    #ifndef _GLIBCXX_NO_ASSERT
    #include <cassert>
    #endif
    #include <cctype>
    #include <cerrno>
    #include <cfloat>
    #include <ciso646>
    #include <climits>
    #include <clocale>
    #include <cmath>
    #include <csetjmp>
    #include <csignal>
    #include <cstdarg>
    #include <cstddef>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <ctime>
     
    #if __cplusplus >= 201103L
    #include <ccomplex>
    #include <cfenv>
    #include <cinttypes>
    #include <cstdbool>
    #include <cstdint>
    #include <ctgmath>
    #include <cwchar>
    #include <cwctype>
    #endif
     
    // C++
    #include <algorithm>
    #include <bitset>
    #include <complex>
    #include <deque>
    #include <exception>
    #include <fstream>
    #include <functional>
    #include <iomanip>
    #include <ios>
    #include <iosfwd>
    #include <iostream>
    #include <istream>
    #include <iterator>
    #include <limits>
    #include <list>
    #include <locale>
    #include <map>
    #include <memory>
    #include <new>
    #include <numeric>
    #include <ostream>
    #include <queue>
    #include <set>
    #include <sstream>
    #include <stack>
    #include <stdexcept>
    #include <streambuf>
    #include <string>
    #include <typeinfo>
    #include <utility>
    #include <valarray>
    #include <vector>
     
    #if __cplusplus >= 201103L
    #include <array>
    #include <atomic>
    #include <chrono>
    #include <condition_variable>
    #include <forward_list>
    #include <future>
    #include <initializer_list>
    #include <mutex>
    #include <random>
    #include <ratio>
    #include <regex>
    #include <scoped_allocator>
    #include <system_error>
    #include <thread>
    #include <tuple>
    #include <typeindex>
    #include <type_traits>
    #include <unordered_map>
    #include <unordered_set>
    #endif
     
    #include<bits/stdc++.h>
    using namespace std;
     
    /* Template file for Online Algorithmic Competitions */
    /* Typedefs */
        /* Basic types */
        typedef long long           ll;
        typedef long double         ld;
        typedef unsigned long long ull;
        /* STL containers */
        typedef vector <int>    vi;
        typedef vector <ll>     vll;
        typedef pair <int, int> pii;
        typedef pair <ll, ll>   pll;
        typedef vector < pii >  vpii;
        typedef vector < pll >  vpll;
        typedef vector <string> vs;
        typedef vector < vi >   vvi;
        typedef vector < vll >  vvll;
        typedef vector < vpii > vvpii;
        typedef set <int>       si;
    /* Macros */
        /* Loops */
        #define fl(i, a, b)     for(int i(a); i <= (b); i ++)
        #define rep(i, n)       fl(i, 1, (n))
        #define loop(i, n)      fl(i, 0, (n) - 1)
        #define rfl(i, a, b)    for(int i(a); i >= (b); i --)
        #define rrep(i, n)      rfl(i, (n), 1)
        /* Algorithmic functions */
        #define pow2(x) ((x)*(x))
        #define mod(x, m) ((((x) % (m)) + (m)) % (m))
        #define max3(a, b, c) max(a, max(b, c))
        #define min3(a, b, c) min(a, min(b, c))
        #define srt(v)          sort((v).begin(), (v).end())
        /* STL container methods */
        #define pb              push_back
        #define mp              make_pair
        #define eb              emplace_back
        /* String methods */
        #define dig(i)          (s[i] - '0')
        #define slen(s)         s.length()
        /* Shorthand notations */
        #define fr              first
        #define sc              second
        #define re              return 
        #define sz(x)           ((int) (x).size())
        #define all(x)          (x).begin(), (x).end()
        #define sqr(x)          ((x) * (x))
        #define fill(x, y)      memset(x, y, sizeof(x))
        #define clr(a)          fill(a, 0)
        #define endl            '\n'
        /* Mathematical */
        #define IINF            0x3f3f3f3f
        #define LLINF           1000111000111000111LL
        #define PI              3.14159265358979323
        #define NIL             -1
        /* Debugging purpose */
        #define trace1(x)                cerr << #x << ": " << x << endl
        #define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl
        #define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl
        /* Fast Input Output */
        #define FAST_IO                  ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
    /* Constants */
        const ll MOD = 1000000007LL;
        const ll MAX = 100010LL;
    /* Templates */
    template<class T> T abs(T x) { re x > 0 ? x : -x; }
    template<typename T> T gcd(T a, T b){ if(b == 0) return a; return gcd(b, a % b); }
    template<typename T> T power(T x, T y, ll m = MOD){T ans = 1; x %= m; while(y > 0){ if(y & 1LL) ans = (ans * x)%m; y >>= 1LL; x = (x*x)%m; } return ans%m; }
    /* additional*/
    #define tr(c,i) for(typeof((c).begin() i = (c).begin(); i != (c).end(); i++)
    #define present(c,x) ((c).find(x) != (c).end())
    #define cpresent(c,x) (find(all(c),x) != (c).end())
    //double inf = 1.0/0.0;
   int count(vector<int> a,int n,ll x)
   {
   	    int cnt = 0 ; 
   		//int mid = (l+r)/2;
   		for(int i=0 ;i<n;i++)
   		{
   			int may = lower_bound(all(a),x-a[i]) - a.begin();
   			//trace2(i,may);	
   			cnt += (n - may);
   		}
   		return cnt;
   }
   ll binmid(vector<int> a,int n,int m)
   {
   		ll l = 0 , r = 1e10 , mid ;
   		while(r-l>1)
   		{
   			 mid = (l+r)/2;

   			if(count(a,n,mid)==m)
   			 	break;
   			 if(count(a,n,mid)<m)
   				r = mid ;
   			else
   				l = mid ;
   			//trace2(mid,count(a,n,mid));
   		}
   		return mid ;
   }
   ll res_func(vector<int> a,int n,ll sum,int& vnt)
   {
   		vector<ll> acum(n+1) ; 
   		acum[0] = 0 ; 
   		for(int i=1;i<=n;++i)
   			acum[i] = acum[i-1] + a[i-1] ; 
   	    ll  res = 0 ;
   		for(int i=0;i<n;i++)
   		{
   			int k = lower_bound(all(a),sum-a[i]) - a.begin();
   			res += ( acum[n] - acum[k] + a[i]*(n-k) ) ;
   			vnt += (n-k) ;
   			//trace3(i,k,( acum[n] - acum[k] + a[i]*(n-k) ));
   		}
   		return res ; 
   }
    int main(){
     
       /* #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
        freopen("error.txt","w",stderr);
        #endif
        */
        FAST_IO;   
        ll n,m;
        cin>>n>>m;
        vector<int> a(n);
        for(int i=0;i<n;++i)
        	cin>>a[i];
        sort(all(a));
     	//cout<<count(a,n,0)<<" ";
     	//cout<<
     	//cout<<
     	int vnt = 0 ;
     	cout<<res_func(a,n,binmid(a,n,m)+1,vnt) + (m-vnt)*(binmid(a,n,m));
     	//cout<<vnt;
      /*  #ifndef ONLINE_JUDGE
        cout<<"\nTime Elapsed: " << 1.0*clock() / CLOCKS_PER_SEC << " sec\n";
        #endif*/
        return 0;
    }