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)

#include <iostream>
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cstring>
#include <complex>
#include <deque>
#include <functional>
#include <initializer_list>
#include <math.h>
#include <map>
#include <random>
#include <set>
#include <stack>
#include <tuple>
#include <type_traits>
#include <queue>
#include <vector>

using lli = long long int;
using pii = std::pair<int, int>;

#define REP(i, n) for(int (i) = 0; (i) < (n); ++(i))

// グリッド上の縦横移動
constexpr std::array<std::pair<int, int>, 4> dxdy = {
    { {1, 0}, {-1, 0}, {0, 1}, {0, -1} }
};

template <typename T> inline bool chmin(T& a, const T b){ if(b < a){ a = b; return true; } return false; }
template <typename T> inline bool chmax(T& a, const T b){ if(a < b){ a = b; return true; } return false; }
template <typename T> inline bool bitUP(const T state, const unsigned int k) { return (state >> k) & 0b01; }

inline bool isIn(int x, int y, int H, int W){return 0 <= x and x < H and 0 <= y and y < W;}
inline auto isInTheGrid(const int H, const int W){
    return std::bind(isIn, std::placeholders::_1, std::placeholders::_2, H, W);
}

template <class element_t> 
std::vector<element_t> make_vector(const unsigned int n, const element_t& init_value){ return std::vector<element_t>(n, init_value); }
template <class element_t, class ...Tp>
auto make_vector(const unsigned int n, const unsigned int m, Tp... args){
    return std::vector<decltype(make_vector<element_t>(m, args...))>(n, make_vector<element_t>(m, args...));
}

namespace MyInputAndOutput{
    // 入力関係 (cin)
    class user_input{
    private:
        static constexpr unsigned int sizeOfAscii = 128;
        bool isBlankChar[sizeOfAscii];
            /*  < definition of getchar >
                reference
                MacOS   : https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/getchar.3.html
                Windows : https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/getchar-nolock-getwchar-nolock?view=vs-2019
                Linux   : https://linux.die.net/man/3/unlocked_stdio 
                Ubuntu  : http://manpages.ubuntu.com/manpages/trusty/man3/getchar_unlocked.3posix.html
            */
            #if defined(__APPLE__)
                #define DAGGER_GETCHAR_UNLOCKED_DAGGER getchar_unlocked
            #elif defined(_WIN32) || defined(_WIN64)
                #define DAGGER_GETCHAR_UNLOCKED_DAGGER _getchar_nolock
            #elif defined(__linux) 
                #if _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _POSIX_SOURCE || _BSD_SOURCE || _SVID_SOURCE
                    #define DAGGER_GETCHAR_UNLOCKED_DAGGER getchar_unlocked
                #else
                    #define DAGGER_GETCHAR_UNLOCKED_DAGGER getchar
                #endif
            #else
                #define DAGGER_GETCHAR_UNLOCKED_DAGGER getchar
            #endif
        // 符号あり整数を getchar で読み込んで構成する (もし, 0 ~ 9 以外の文字が含まれると困る)
        template <typename Tp>
        inline void charToInteger(Tp& val) const {
            val = 0; int c; while(true){
                c = DAGGER_GETCHAR_UNLOCKED_DAGGER();
                if(c == EOF) return;
                if('-' or (not isBlankChar[c])) break;
            }
            if(c == '-'){
                while(true){
                    c = DAGGER_GETCHAR_UNLOCKED_DAGGER();
                    if(c < '0' or c > '9') break;
                    val = 10 * val + c - '0';
                } val = -val;
            }
            else{
                if(c < '0' or c > '9') return;
                do{
                    val = 10 * val + c - '0';
                    c = DAGGER_GETCHAR_UNLOCKED_DAGGER();
                } while('0' <= c and c <= '9');
            }
        }
        // 符号なし整数を getchar で読み込んで構成する (もし, 符号付きだとバグる)
        template <typename Tp>
        inline void charToUnsignedInteger(Tp& val) const {
            val = 0; int c; while(true){
                c = DAGGER_GETCHAR_UNLOCKED_DAGGER();
                if(c == EOF) return;
                if(not isBlankChar[c]) break;
            }
            if(c < '0' or c > '9') return;
            do{
                val = 10 * val + c - '0';
                c = DAGGER_GETCHAR_UNLOCKED_DAGGER();
            } while(not (c == EOF or isBlankChar[c]));
        }
    public:
        constexpr user_input(void) : isBlankChar(){
            for(unsigned int i = 0; i < sizeOfAscii; ++i) isBlankChar[i] = false;
            isBlankChar[int('\n')] = true; isBlankChar[int('\t')] = true; 
            isBlankChar[int(' ')]  = true; isBlankChar[int('\v')] = true;
        }
        inline const user_input& operator >> (int& int_arg) const {
            //scanf("%d", &int_arg);
            charToInteger<int>(int_arg); return *this;
        }
        inline const user_input& operator >> (long long int& llint_arg) const {
            //scanf("%lld", &llint_arg); return *this;
            charToInteger<long long int>(llint_arg); return *this;
        }
        inline const user_input& operator >> (unsigned int& uint_arg) const {
            //scanf("%u", &uint_arg); 
            charToUnsignedInteger<unsigned int>(uint_arg); return *this;
        }
        inline const user_input& operator >> (unsigned long long int& ullint_arg) const {
            //scanf("%llu", &ullint_arg);
            charToUnsignedInteger<unsigned long long int>(ullint_arg); return *this;
        }
        inline const user_input& operator >> (unsigned long& ulint_arg) const {
            charToUnsignedInteger<unsigned long>(ulint_arg); return *this;
        }
        inline const user_input& operator >> (float& float_arg) const {
            scanf("%f", &float_arg); return *this;
        }
        inline const user_input& operator >> (double& double_arg) const {
            scanf("%lf", &double_arg); return *this;
        }
        inline const user_input& operator >> (long double& ldouble_arg) const {
            scanf("%Lf", &ldouble_arg); return *this;
        }
        inline const user_input& operator >> (char* str_arg) const {
            scanf("%s", str_arg); 
            return *this;
        }
        inline const user_input& operator >> (char& char_arg) const {
            do{
                if((char_arg = DAGGER_GETCHAR_UNLOCKED_DAGGER()) == EOF) return *this;
            } while(isBlankChar[int(char_arg)]);
            return *this;
        }
        inline const user_input& operator >> (std::string& str_arg) const {
            str_arg.erase();
            int c; while(true){ 
                c = DAGGER_GETCHAR_UNLOCKED_DAGGER();
                if(c == EOF) return *this;
                if(not isBlankChar[c]) break;
            }
            constexpr unsigned int buffer_size = 128; 
            char buffer_input[buffer_size]; unsigned int buffer_length = 0;
            do{
                buffer_input[buffer_length++] = c;
                if(buffer_length == buffer_size){
                    buffer_length = 0; str_arg.append(buffer_input, buffer_size);
                }
                c = DAGGER_GETCHAR_UNLOCKED_DAGGER();
            } while(c != EOF and (not isBlankChar[c]) );
            str_arg.append(buffer_input, buffer_length);
            return *this;
        }
        template <typename S, typename T>
        inline const user_input& operator >>(std::pair<S, T>& pair_arg) const{
            (*this) >> pair_arg.first >> pair_arg.second; return *this;
        }
        template <typename T>
        inline const user_input& operator >>(std::vector<T>& vec) const {
            for(T& ele : vec) (*this) >> ele; 
            return *this;
        }
        // getchar の define の解除
        #undef DAGGER_GETCHAR_UNLOCKED_DAGGER
    }; constexpr user_input cin;

    void ends(void) {putchar('\0'); }
    void endl(void) {putchar('\n'); fflush(stdout);}
    void flush(void) {fflush(stdout);}
    constexpr char eol = '\n';

    // 出力関係 (cout)
    class user_output{
    public:
        constexpr user_output(void){}
        inline const user_output& operator << (const int int_arg) const{
            printf("%d", int_arg); return *this;
        }
        inline const user_output& operator << (const unsigned int uint_arg) const{
            printf("%u", uint_arg); return *this;
        }
        inline const user_output& operator << (const long long int llint_arg) const {
            printf("%lld", llint_arg); return *this;
        }
        inline const user_output& operator << (const unsigned long long int ullint_arg) const {
            printf("%llu", ullint_arg); return *this;
        }
        inline const user_output& operator << (const unsigned long ulint_arg) const {
            printf("%lu", ulint_arg); return *this;
        }
        inline const user_output& operator << (const float flt_arg) const {
            printf("%.16f", flt_arg); return *this;
        }
        inline const user_output& operator << (const double ld_arg) const {
            printf("%.16lf", ld_arg); return *this;
        }
        inline const user_output& operator << (const long double ld_arg) const {
            printf("%.16Lf", ld_arg); return *this;
        }
        inline const user_output& operator << (const char char_arg) const {
            putchar(char_arg); return *this;
        }
        inline const user_output& operator << (const unsigned char uchar_arg) const {
            putchar(uchar_arg); return *this;
        }
        inline const user_output& operator << (const char* str_arg) const {
            fputs(str_arg, stdout); return *this;
        }
        inline const user_output& operator << (const std::string& str_arg) const {
            fputs(str_arg.c_str(), stdout); return *this;
        }
        inline const user_output& operator << (void(* const func_arg)(void)) const {
            func_arg(); return *this;
        }
        template <typename S, typename T>
        inline const user_output& operator <<(const std::pair<S, T>& pair_arg) const{
            (*this) << pair_arg.first << ' ' << pair_arg.second; return *this;
        }
        template <typename Tp_name>
        inline const user_output& operator << (const std::vector<Tp_name>& vec) const {
            const size_t size_of_vec = vec.size();
            if(size_of_vec <= 0) return *this;
            (*this) << vec[0]; 
            for(size_t index = 1; index < size_of_vec; ++index) (*this) << ' ' << vec[index];
            return *this;
        }
    }; constexpr user_output cout;

    // その他出力関数
    void binary_output(int64_t value, size_t length = 64){
        char out[length + 1];
        for(size_t i = 0; i < length; ++i) out[length - 1 - i] = ((value >> i) & 1) ? '1' : '0';
        out[length] = '\0'; puts(out);
    }
    template <typename InputType>
    void print(InputType first, InputType last, const char separate_c = ' ', const char end_c = '\n'){
        InputType it = first;
        while(true){
            MyInputAndOutput::cout << *it;
            if(++it == last){MyInputAndOutput::cout << end_c; return;}
            MyInputAndOutput::cout << separate_c;  
        }
    }
}; namespace MIO = MyInputAndOutput;

// z-algorithm
template <class T> std::vector<unsigned int> z_algorithm(const T &str) {
    const size_t n = str.size();
    std::vector<unsigned int> resOfCP(n); resOfCP[0] = n;
    int i = 1, j = 0;
    while (i < n) {
        while (i + j < n and str[j] == str[i + j]) ++j;
        resOfCP[i] = j;
        if (j == 0) { ++i; continue;}
        int k = 1;
        while (i + k < n and k + resOfCP[k] < j) resOfCP[i + k] = resOfCP[k], ++k;
        i += k; j -= k;
    }
    return resOfCP;
} 

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

using MIO::cin; using MIO::cout; using MIO::eol;


template <class precision = double>
class FFT{
private:
    using number_type = std::complex<precision>;

    static constexpr precision PI_VALUE = std::acos(-1);

    static unsigned int bitreverse(unsigned int n){
        #ifdef __has_builtin
            #if __has_builtin(__builtin_bitreverse32)
                return __builtin_bitreverse32(n);
            #else
                n = ((n & 0x55555555) << 1) | ((n & 0xAAAAAAAA) >> 1); // 1ビットの交換
                n = ((n & 0x33333333) << 2) | ((n & 0xCCCCCCCC) >> 2);
                n = ((n & 0x0F0F0F0F) << 4) | ((n & 0xF0F0F0F0) >> 4);
                n = ((n & 0x00FF00FF) << 8) | ((n & 0xFF00FF00) >> 8);
                n = (n << 16) | (n >> 16);
                return n;
            #endif
        #else
            n = ((n & 0x55555555) << 1) | ((n & 0xAAAAAAAA) >> 1); // 1ビットの交換
            n = ((n & 0x33333333) << 2) | ((n & 0xCCCCCCCC) >> 2);
            n = ((n & 0x0F0F0F0F) << 4) | ((n & 0xF0F0F0F0) >> 4);
            n = ((n & 0x00FF00FF) << 8) | ((n & 0xFF00FF00) >> 8);
            n = (n << 16) | (n >> 16);
            return n;
        #endif
    }
public:

    // 2^nの長さの配列に対し高速フーリエ変換を実行する関数 : 2^n ≤ A.size() である必要がある
    std::vector<number_type> fft(const std::vector<number_type>& A, const unsigned int n) const {
        const unsigned int N = 1 << n; 
        assert(A.size() <= N);

        std::vector<number_type> res(N, 0);{
            const unsigned int len = A.size(); 
            for(unsigned int i = 0; i < len; ++i) res[i] = A[i]; // 0 埋め
        }
        for(unsigned int i = 0; i < N; ++i){
            unsigned int j = bitreverse(i) >> (32 - n);
            if(i < j) std::swap(res[i], res[j]);
        }

        for(unsigned int len1 = 1, len2 = 2; len1 < N; len1 <<= 1, len2 <<= 1){
            const number_type w = std::exp<precision>(number_type(0, -PI_VALUE / len1));
            for(unsigned int begin_idx1 = 0, begin_idx2 = len1; begin_idx1 < N; begin_idx1 += len2, begin_idx2 += len2){
                number_type w_v = 1;
                for(unsigned int idx = 0; idx < len1; ++idx, w_v *= w){
                    res[begin_idx2 + idx] =                         res[begin_idx1 + idx] - w_v * res[begin_idx2 + idx];
                    res[begin_idx1 + idx] = res[begin_idx1 + idx] + res[begin_idx1 + idx] -       res[begin_idx2 + idx];
                }
            }
        }
        return res;
    }

    // {Ai}_{i=0}^{n-1}, {Bi}_{i=0}^{m-1} に対して畳み込み
    // Ck = ∑_{i + j = k} Ai * Bj を計算する
    std::vector<number_type> convolve(const std::vector<number_type>& A, const std::vector<number_type>& B) const {
        const unsigned int n = A.size(); const unsigned int m = B.size();
        // (n - 1) + (m - 1) ≤ 2^k - 1 : n + m - 1 ≤ 2^k
        unsigned int k = 0; { const unsigned int degree = n + m - 1; while((1U << k) < degree) ++k; }
        const unsigned int K = 1 << k; 
        std::vector<number_type> A2 = fft(A, k), B2 = fft(B, k);
        for(unsigned int i = 0; i < K; ++i) A2[i] = std::conj(A2[i] * B2[i]);
        A2 = fft(A2, k);
        const precision waru = 1.0 / K;
        for(unsigned int i = 0; i < K; ++i) A2[i] = std::conj(A2[i]) * waru;
        return A2;
    }
    std::vector<precision> convolve(const std::vector<precision>& A, const std::vector<precision>& B) const {
        const unsigned int n = A.size(); const unsigned int m = B.size();
        unsigned int k = 0; { const unsigned int degree = n + m - 1; while((1U << k) < degree) ++k; }
        const unsigned int K = 1 << k; 
        std::vector<number_type> C(K, 0); 
        for(unsigned int i = 0; i < n; ++i) C[i].real(A[i]);
        for(unsigned int i = 0; i < m; ++i) C[i].imag(B[i]);
        C = fft(C, k); std::vector<number_type> D(K); 
        D[0] = C[0].real() * C[0].imag();
        constexpr precision divide_four = 1.0 / 4;
        for(unsigned int i = 1, j = K - 1; i < K; ++i, --j){
            const number_type&& tmp = ( std::conj(C[i] * C[i]) - (C[j] * C[j]) ) * divide_four;
            D[i].real(-tmp.imag()); D[i].imag( tmp.real());
        }
        D = fft(D, k);
        const precision waru = 1.0 / K;
        std::vector<precision> res(K);
        for(unsigned int i = 0; i < K; ++i) res[i] = D[i].real() * waru;
        return res;
    }
    constexpr FFT(void){}
};

constexpr FFT<long double> F;

constexpr unsigned int MAX_A = 100'000;


int main(void){
    unsigned long long int n, m; cin >> n >> m;
    std::vector<long double> A(MAX_A + 1, 0);
    for(unsigned int i = 0; i < n; ++i){ unsigned int a; cin >> a; A[a]++; }
    const auto v = F.convolve(A, A);
    unsigned long long int res = 0;
    for(unsigned long long int i = 2 * MAX_A; i > 0; --i){
        unsigned int val = std::round(v[i]);
        if(val >= m){ res += m * i; break; }
        res += val * i; m -= val;
    }
    cout << res << eol;
    return 0;
}