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.15.1) (judge verdict: Time Limit Exceeded)

#[doc = " https://github.com/hatoo/competitive-rust-snippets"]
#[allow(unused_imports)]
use std::cmp::{max, min, Ordering};
#[allow(unused_imports)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};
#[allow(unused_imports)]
use std::io::{stdin, stdout, BufWriter, Write};
#[allow(unused_imports)]
use std::iter::FromIterator;
#[macro_export]
macro_rules ! chmax { ( $ x : expr , $ ( $ v : expr ) ,+ ) => { $ ( $ x = std :: cmp :: max ( $ x ,$ v ) ; ) + } ; }
#[macro_export]
macro_rules ! chmin { ( $ x : expr , $ ( $ v : expr ) ,+ ) => { $ ( $ x = std :: cmp :: min ( $ x ,$ v ) ; ) + } ; }
#[macro_export]
macro_rules ! max { ( $ x : expr ) => ( $ x ) ; ( $ x : expr , $ ( $ xs : expr ) ,+ ) => { std :: cmp :: max ( $ x , max ! ( $ ( $ xs ) ,+ ) ) } ; }
#[macro_export]
macro_rules ! min { ( $ x : expr ) => ( $ x ) ; ( $ x : expr , $ ( $ xs : expr ) ,+ ) => { std :: cmp :: min ( $ x , min ! ( $ ( $ xs ) ,+ ) ) } ; }
#[macro_export]
macro_rules ! dvec { ( $ t : expr ; $ len : expr ) => { vec ! [ $ t ; $ len ] } ; ( $ t : expr ; $ len : expr , $ ( $ rest : expr ) ,* ) => { vec ! [ dvec ! ( $ t ; $ ( $ rest ) ,* ) ; $ len ] } ; }
#[allow(unused_macros)]
macro_rules ! debug { ( $ ( $ a : expr ) ,* ) => { eprintln ! ( concat ! ( $ ( stringify ! ( $ a ) , " = {:?}, " ) ,* ) , $ ( $ a ) ,* ) ; } }
#[macro_export]
macro_rules ! input { ( source = $ s : expr , $ ( $ r : tt ) * ) => { let mut parser = Parser :: from_str ( $ s ) ; input_inner ! { parser , $ ( $ r ) * } } ; ( parser = $ parser : ident , $ ( $ r : tt ) * ) => { input_inner ! { $ parser , $ ( $ r ) * } } ; ( new_stdin_parser = $ parser : ident , $ ( $ r : tt ) * ) => { let stdin = std :: io :: stdin ( ) ; let reader = std :: io :: BufReader :: new ( stdin . lock ( ) ) ; let mut $ parser = Parser :: new ( reader ) ; input_inner ! { $ parser , $ ( $ r ) * } } ; ( $ ( $ r : tt ) * ) => { input ! { new_stdin_parser = parser , $ ( $ r ) * } } ; }
#[macro_export]
macro_rules ! input_inner { ( $ parser : ident ) => { } ; ( $ parser : ident , ) => { } ; ( $ parser : ident , $ var : ident : $ t : tt $ ( $ r : tt ) * ) => { let $ var = read_value ! ( $ parser , $ t ) ; input_inner ! { $ parser $ ( $ r ) * } } ; }
#[macro_export]
macro_rules ! read_value { ( $ parser : ident , ( $ ( $ t : tt ) ,* ) ) => { ( $ ( read_value ! ( $ parser , $ t ) ) ,* ) } ; ( $ parser : ident , [ $ t : tt ; $ len : expr ] ) => { ( 0 ..$ len ) . map ( | _ | read_value ! ( $ parser , $ t ) ) . collect ::< Vec < _ >> ( ) } ; ( $ parser : ident , chars ) => { read_value ! ( $ parser , String ) . chars ( ) . collect ::< Vec < char >> ( ) } ; ( $ parser : ident , usize1 ) => { read_value ! ( $ parser , usize ) - 1 } ; ( $ parser : ident , $ t : ty ) => { $ parser . next ::<$ t > ( ) . expect ( "Parse error" ) } ; }
use std::io;
use std::io::BufRead;
use std::str;
pub struct Parser<R> {
    reader: R,
    buf: Vec<u8>,
    pos: usize,
}
impl Parser<io::Empty> {
    pub fn from_str(s: &str) -> Parser<io::Empty> {
        Parser {
            reader: io::empty(),
            buf: s.as_bytes().to_vec(),
            pos: 0,
        }
    }
}
impl<R: BufRead> Parser<R> {
    pub fn new(reader: R) -> Parser<R> {
        Parser {
            reader: reader,
            buf: vec![],
            pos: 0,
        }
    }
    pub fn update_buf(&mut self) {
        self.buf.clear();
        self.pos = 0;
        loop {
            let (len, complete) = {
                let buf2 = self.reader.fill_buf().unwrap();
                self.buf.extend_from_slice(buf2);
                let len = buf2.len();
                if len == 0 {
                    break;
                }
                (len, buf2[len - 1] <= 0x20)
            };
            self.reader.consume(len);
            if complete {
                break;
            }
        }
    }
    pub fn next<T: str::FromStr>(&mut self) -> Result<T, T::Err> {
        loop {
            let mut begin = self.pos;
            while begin < self.buf.len() && (self.buf[begin] <= 0x20) {
                begin += 1;
            }
            let mut end = begin;
            while end < self.buf.len() && (self.buf[end] > 0x20) {
                end += 1;
            }
            if begin != self.buf.len() {
                self.pos = end;
                return str::from_utf8(&self.buf[begin..end]).unwrap().parse::<T>();
            } else {
                self.update_buf();
            }
        }
    }
}
#[allow(unused_macros)]
macro_rules ! debug { ( $ ( $ a : expr ) ,* ) => { eprintln ! ( concat ! ( $ ( stringify ! ( $ a ) , " = {:?}, " ) ,* ) , $ ( $ a ) ,* ) ; } }
#[doc = " https://github.com/hatoo/competitive-rust-snippets"]
const BIG_STACK_SIZE: bool = true;
#[allow(dead_code)]
fn main() {
    use std::thread;
    if BIG_STACK_SIZE {
        thread::Builder::new()
            .stack_size(32 * 1024 * 1024)
            .name("solve".into())
            .spawn(solve)
            .unwrap()
            .join()
            .unwrap();
    } else {
        solve();
    }
}
fn solve() {
    let out = stdout();
    let mut out = BufWriter::new(out.lock());
    input!{
        n:usize,m:usize,
        a:[usize;n],
    }

    let mut amax = 0;
    for i in 0..n {
        chmax!(amax,a[i]);
    }

    let mut x = vec![0;amax+1];
    for i in 0..n {
        x[a[i]] += 1;
    }

    let mo = std::i64::MAX;
    let p1 = Polynomial::new(x, mo);
    let z = p1.pow(2).coeff;

    let mut tot = 0;
    let mut remaining = m as i64;
    for i in (0..z.len()).rev() {
        let v = z[i];
        let take = min(v, remaining);
        tot += take * i as i64;
        remaining -= take;
        if remaining == 0 {
            break;
        }
    }
    println!("{}", tot);
}
#[derive(Debug)]
struct Polynomial {
    coeff: Vec<i64>,
    mo: i64,
}
impl Polynomial {
    pub fn new(coeff: Vec<i64>, mo: i64) -> Polynomial {
        Polynomial {
            coeff: coeff,
            mo: mo,
        }
    }
    pub fn multiply(&self, other: &Self) -> Self {
        assert!(self.mo == other.mo);
        Polynomial {
            coeff: ntt_multiply(&self.coeff, &other.coeff, self.mo),
            mo: self.mo,
        }
    }
    pub fn pow(&self, k: i64) -> Self {
        let mut res = Polynomial {
            coeff: vec![1],
            mo: self.mo,
        };
        let mut x = Polynomial {
            coeff: self.coeff.clone(),
            mo: self.mo,
        };
        let mut k = k;
        while k > 0 {
            if k & 1 == 1 {
                res = res.multiply(&x);
            }
            x = x.multiply(&x);
            k >>= 1;
        }
        res
    }
}
struct NTT {
    pub mo: i64,
}
impl NTT {
    pub fn new(mo: i64) -> NTT {
        NTT { mo: mo }
    }
    fn _ntt(&self, a: &mut [i64], n: usize, inverse: bool) {
        let g = 3;
        let mut h = modpow(g, (self.mo - 1) / n as i64, self.mo);
        if inverse {
            h = modinv(h, self.mo);
        }
        let mut i = 0;
        for j in 1..n - 1 {
            let mut k = n >> 1;
            loop {
                i ^= k;
                if k > i {
                    k >>= 1;
                } else {
                    break;
                }
            }
            if j < i {
                let tmp = a[i];
                a[i] = a[j];
                a[j] = tmp;
            }
        }
        let mut m = 1;
        while m < n {
            let m2 = m * 2;
            let base = modpow(h, (n / m2) as i64, self.mo);
            let mut w = 1;
            for x in 0..m {
                let mut s = x;
                while s < n {
                    let u = a[s];
                    let d = (a[s + m] * w) % self.mo;
                    a[s] = u + d;
                    if a[s] >= self.mo {
                        a[s] -= self.mo;
                    }
                    a[s + m] = u - d;
                    if a[s + m] < 0 {
                        a[s + m] += self.mo;
                    }
                    s += m2;
                }
                w = (w * base) % self.mo;
            }
            m *= 2;
        }
        for i in 0..n {
            if a[i] < 0 {
                a[i] += self.mo;
            }
        }
    }
    fn ntt(&self, a: &mut [i64], n: usize) {
        self._ntt(a, n, false);
    }
    fn intt(&self, a: &mut [i64], n: usize) {
        self._ntt(a, n, true);
        let n_inv = modinv(a.len() as i64, self.mo);
        for i in 0..n {
            a[i] = (a[i] * n_inv) % self.mo;
        }
    }
    pub fn convolve(&self, a: &[i64], b: &[i64]) -> Vec<i64> {
        let mut a = a.to_vec();
        let mut b = b.to_vec();
        let mut n = 1;
        while n < a.len() + b.len() {
            n <<= 1;
        }
        a.resize(n, 0);
        b.resize(n, 0);
        self.ntt(&mut a, n);
        self.ntt(&mut b, n);
        let mut c = vec![0; n];
        for i in 0..n {
            c[i] = (a[i] * b[i]) % self.mo;
        }
        self.intt(&mut c, n);
        c
    }
}
pub fn ntt_multiply(a: &[i64], b: &[i64], mo: i64) -> Vec<i64> {
    let mut a = a.to_vec();
    let mut b = b.to_vec();
    let n = a.len();
    let m = b.len();
    for i in 0..n {
        a[i] %= mo;
    }
    for i in 0..m {
        b[i] %= mo;
    }
    let ntt1 = NTT::new(167772161);
    let ntt2 = NTT::new(469762049);
    let ntt3 = NTT::new(1224736769);
    let x = ntt1.convolve(&a, &b);
    let y = ntt2.convolve(&a, &b);
    let z = ntt3.convolve(&a, &b);
    let m1 = ntt1.mo;
    let m2 = ntt2.mo;
    let m3 = ntt3.mo;
    let m1_inv_m2 = modinv(m1, m2);
    let m12_inv_m3 = modinv(m1 * m2, m3);
    let m12_mod = (m1 * m2) % mo;
    let L = x.len();
    let mut res = vec![0; L];
    for i in 0..L {
        let mut v1 = (y[i] - x[i]) * m1_inv_m2;
        v1 %= m2;
        if v1 < 0 {
            v1 += m2;
        }
        let mut v2 = (z[i] - (x[i] + m1 * v1) % m3) * m12_inv_m3;
        v2 %= m3;
        if v2 < 0 {
            v2 += m3;
        }
        let mut const3 = (x[i] + m1 * v1 + m12_mod * v2) % mo;
        if const3 < 0 {
            const3 += mo;
        }
        res[i] = const3;
    }
    res.truncate(n + m - 1);
    res
}
#[allow(dead_code)]
#[doc = " x ^ n % m"]
pub fn modpow(x: i64, n: i64, m: i64) -> i64 {
    let mut res = 1;
    let mut x = x % m;
    let mut n = n;
    while n > 0 {
        if n & 1 == 1 {
            res = (res * x) % m;
        }
        x = (x * x) % m;
        n >>= 1;
    }
    res
}
#[doc = "ay=1 (mod m) -> y=a^{-1}"]
pub fn modinv(a: i64, m: i64) -> i64 {
    let (_, x, _) = extgcd(a, m);
    (m + x % m) % m
}
#[doc = "ax+by=gcd(a,b) returns (gcd, x, y)"]
#[allow(dead_code)]
pub fn extgcd(a: i64, b: i64) -> (i64, i64, i64) {
    if b == 0 {
        (a, 1, 0)
    } else {
        let (gcd, x, y) = extgcd(b, a % b);
        (gcd, y, x - (a / b) * y)
    }
}