PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000193. Bắt tay tối ưu (Handshake)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Bắt tay tối ưu (Handshake)

Handshake

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

There are NN guests; guest ii has power AiA_i. Takahashi performs exactly MM handshakes. In each handshake, he selects guest xx for his left hand and guest yy for his right hand, gaining Ax+AyA_x+A_y happiness.

Choosing x=yx=y is allowed. No ordered pair (x,y)(x,y) may be used more than once. Therefore (x,y)(x,y) and (y,x)(y,x) count as distinct handshakes when x≠yx\ne y.

Find the maximum total happiness obtainable with exactly MM handshakes.

Input

  • First line: integers NN, MM.
  • Second line: NN integers A1,A2,…,ANA_1,A_2,\ldots,A_N.

Output

  • Print one integer: the maximum total happiness.

Subtasks

  • Subtask 1 (20 points): 1≤N≤151\le N\le 15; 1≤M≤N21\le M\le N^2; 1≤Ai≤1001\le A_i\le 100.
  • Subtask 2 (30 points): 1≤N≤20001\le N\le 2000; 1≤M≤N21\le M\le N^2; 1≤Ai≤1000001\le A_i\le 100000.
  • Subtask 3 (50 points): 1≤N≤1000001\le N\le 100000; 1≤M≤N21\le M\le N^2; 1≤Ai≤1000001\le A_i\le 100000.

Examples

Input

5 3
10 14 19 34 33

Output

202

Explanation

For powers [10,14,19,34,33][10,14,19,34,33] and M=3M=3, the three largest ordered-pair scores are:

Handshake Ordered pair Score
1 (4,4)(4,4) 6868
2 (4,5)(4,5) 6767
3 (5,4)(5,4) 6767

The maximum is 68+67+67=20268+67+67=202.