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 Common Lisp (SBCL 1.1.14) (judge verdict: Accepted)

;; E - Handshake

(defun main ()
  (let* ((N (read)) ; 人数
         (M (read)) ; 握手回数
         (A (loop repeat N collect (read)))) ; ゲストのパワー
    (princ (solve N M A))
    (fresh-line)))

(defparameter *max-A* (expt 10 5))

(defun solve (N M A-list)
  (setf A-list (sort A-list #'<)) ; 昇順
  (let ((A (make-array N :initial-contents A-list)) ; 配列化
        (suffix-sum (make-array (1+ N) :initial-element 0)))
    ; 以降の要素の総和
    (loop for i from (1- N) downto 0
      do (setf (aref suffix-sum i)
               (+ (aref suffix-sum (1+ i)) (aref A i))))
    (labels
      ((find-guest (h)
         "幸福度 h 以上の最小のゲスト"
         (bin-search 0 N #'(lambda (x) (< (aref A x) h))))
       (count-handshake (h)
         "幸福度 h 以上の握手の個数"
         (loop for x across A
               ; xと同時に握手すると幸福度 h 以上になる最小のゲスト
               for y = (find-guest (- h x))
               sum (- N y))) ; y以降の人数
       (sum-handshake (h)
         "幸福度 h 以上の握手の総幸福度"
         (loop for x across A
               for y = (find-guest (- h x))
               sum (+ (* (- N y) x) (aref suffix-sum y)))))
      ; 幸福度 h 以上の握手の個数が M 以下になる最小の h
      (let ((h (bin-search 2 (1+ (* 2 *max-A*))
                 #'(lambda (h) (> (count-handshake h) M)))))
        (+ (sum-handshake h) ; 幸福度 h 以上の握手の総幸福度
           ; 残りの握手は幸福度 h - 1
           (* (- M (count-handshake h)) (1- h)))))))

(defun bin-search (low high <-than-target)
  "low: 探索範囲の左端, high: 探索範囲の右端+1,
   (<-than-target i)が偽になる最小のiを返す."
  (if (>= low high)
      low
      (let* ((mid (+ low (ash (- high low) -1))))
        (if (funcall <-than-target mid)
            (bin-search (1+ mid) high <-than-target)
            (bin-search low mid <-than-target)))))

(main)