from bisect import bisect_left

from itertools import accumulate

N,M,*A = list(map(int, open(0).read().split()))

A.sort()

B = list(accumulate(A[::-1]))

l = -1

r = 10**6

while l+1<r:

  s = (l+r)//2

  cnt = 0

  for c in A:

    i = bisect_left(A,s-c)

    cnt += N-i

  if cnt>=M:

    l = s

  else:

    r = s

ans = 0

for c in A:

  i = bisect_left(A,r-c)

  M -= N-i

  if i<N:

    ans += B[N-1-i]+c*(N-i)

ans += M*l

print(ans)

import numpy as np

N,M,*A = list(map(int, open(0).read().split()))

A = np.sort(A)

B = np.cumsum(np.concatenate([[0],A[::-1]]))

l = -1

r = 10**6

while l+1<r:

  s = (l+r)//2

  cnt = np.sum(N-np.searchsorted(A,s-A))

  if cnt>=M:

    l = s

  else:

    r = s

ind = np.searchsorted(A,r-A)

M -= np.sum(N-ind)

ans = np.sum(B[N-ind]+A*(N-ind))+M*l

print(ans)

from bisect import bisect_left
from itertools import accumulate

N, M, *A = list(map(int, open(0).read().split()))
A.sort()
B = list(accumulate(A[::-1]))
l = -1
r = 10**6
while l + 1 < r:
    s = (l + r) // 2
    cnt = 0
    for c in A:
        i = bisect_left(A, s - c)
        cnt += N - i
    if cnt >= M:
        l = s
    else:
        r = s
ans = 0
for c in A:
    i = bisect_left(A, r - c)
    M -= N - i
    if i < N:
        ans += B[N - 1 - i] + c * (N - i)
ans += M * l
print(ans)

import numpy as np

N, M, *A = list(map(int, open(0).read().split()))
A = np.sort(A)
B = np.cumsum(np.concatenate([[0], A[::-1]]))
l = -1
r = 10**6
while l + 1 < r:
    s = (l + r) // 2
    cnt = np.sum(N - np.searchsorted(A, s - A))
    if cnt >= M:
        l = s
    else:
        r = s
ind = np.searchsorted(A, r - A)
M -= np.sum(N - ind)
ans = np.sum(B[N - ind] + A * (N - ind)) + M * l
print(ans)

-from bisect import bisect_left -from itertools import accumulate +import numpy as np -A.sort() -B = list(accumulate(A[::-1])) +A = np.sort(A) +B = np.cumsum(np.concatenate([[0], A[::-1]])) -    cnt = 0 -    for c in A: -        i = bisect_left(A, s - c) -        cnt += N - i +    cnt = np.sum(N - np.searchsorted(A, s - A)) -ans = 0 -for c in A: -    i = bisect_left(A, r - c) -    M -= N - i -    if i < N: -        ans += B[N - 1 - i] + c * (N - i) -ans += M * l +ind = np.searchsorted(A, r - A) +M -= np.sum(N - ind) +ans = np.sum(B[N - ind] + A * (N - ind)) + M * l