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 Rust (1.42.0) (judge verdict: Accepted)

//  ____            _                  _                       _       _
// |  _ \ _   _ ___| |_    ___ _ __   | |_ ___ _ __ ___  _ __ | | __ _| |_ ___
// | |_) | | | / __| __|  / __| '_ \  | __/ _ \ '_ ` _ \| '_ \| |/ _` | __/ _ \
// |  _ <| |_| \__ \ |_  | (__| |_) | | ||  __/ | | | | | |_) | | (_| | ||  __/
// |_| \_\\__,_|___/\__|  \___| .__/___\__\___|_| |_| |_| .__/|_|\__,_|\__\___|
//                            |_| |_____|               |_|

//https://github.com/manta1130/Competitive_Programming_Template_Rust

mod fft{
///FFTを計算する
///
/// ar:実部
///
/// ai:虚部
///
/// inv:逆変換時はtrue
///
/// arとaiの要素数は同一である必要がある。
pub fn fft(ar: &mut Vec<f64>, ai: &mut Vec<f64>, inv: bool) {
    if ar.len() != ai.len() {
        panic!("ar.len()!=ai.len()");
    }

    let mut n = ar.len();
    //配列の要素数が2のべき乗でない場合は調整する
    while ar.len().count_ones() != 1 {
        n += 1;
        ar.push(0.0);
        ai.push(0.0);
    }

    let (mut wr, mut wi, mut xr, mut xi): (f64, f64, f64, f64);
    let w = if inv { -1.0 } else { 1.0 } * 2.0 * std::f64::consts::PI / (n as f64);

    let mut i = 0;
    for j in 1..n - 1 {
        let mut k = n >> 1;
        loop {
            i ^= k;
            if k <= i {
                break;
            }
            k >>= 1
        }

        if j < i {
            xr = ar[j];
            xi = ai[j];
            ar[j] = ar[i];
            ai[j] = ai[i];
            ar[i] = xr;
            ai[i] = xi;
        }
    }

    let mut mh = 1;
    loop {
        let m = mh << 1;
        if m > n {
            break;
        }
        let mut irev = 0;

        i = 0;
        while i < n {
            wr = (w * irev as f64).cos();
            wi = (w * irev as f64).sin();
            let mut k = n >> 2;
            loop {
                irev ^= k;
                if k <= irev {
                    break;
                }
                k >>= 1;
            }

            for j in i..mh + i {
                k = j + mh;
                xr = ar[j] - ar[k];
                xi = ai[j] - ai[k];
                ar[j] += ar[k];
                ai[j] += ai[k];
                ar[k] = wr * xr - wi * xi;
                ai[k] = wr * xi + wi * xr;
            }

            i += m;
        }
        mh = m;
    }
    if inv {
        for i in ar.iter_mut() {
            *i /= n as f64;
        }
        for i in ai.iter_mut() {
            *i /= n as f64;
        }
    }
}

}

#[allow(unused_imports)]
use proconio::{
    fastout, input,
    marker::{Bytes, Chars, Isize1, Usize1},
};

use fft::*;

fn main() {
    input! {
        n:usize,
        mut m:usize,
        a:[usize;n],
    }
    let mut va = vec![0.0; 200001];
    let mut vb = vec![0.0; 200001];
    let mut vai = vec![0.0; 200001];
    let mut vbi = vec![0.0; 200001];
    for &i in &a {
        va[i] += 1.0;
        vb[i] += 1.0;
    }

    fft(&mut va, &mut vai, false);
    fft(&mut vb, &mut vbi, false);

    for i in 0..va.len() {
        let r = va[i] * vb[i] - vai[i] * vbi[i];
        let j = va[i] * vbi[i] + vb[i] * vai[i];
        va[i] = r;
        vai[i] = j;
    }
    fft(&mut va, &mut vai, true);
    let mut ans = 0_usize;
    for (i, x) in va.iter().enumerate().rev() {
        let x = (x + 0.1) as usize;
        if x <= m {
            ans += x * i;
            m -= x;
        } else {
            ans += m * i;
            m = 0;
        }
        if m == 0 {
            break;
        }
    }
    println!("{}", ans);
}