from bisect import bisect_left

from itertools import accumulate



N, M = list(map(int, input().split()))

A = sorted(list(map(int, input().split())))



Acum = [0] + list(accumulate(A))



left = 0

right = max(A) * 2 + 1



while left + 1 < right:

  mid = (left + right) // 2

  tmp = 0

  for i in range(N):

    bi = bisect_left(A, mid - A[i])

    tmp += N - bi

  if tmp <= M:

    right = mid

  else:

    left = mid



cnt = 0

answer = 0

for i in range(N):

  bi = bisect_left(A, right - A[i])

  cnt += N - bi

  answer += Acum[N] - Acum[bi] + A[i] * (N - bi)



print((answer + (M - cnt) * left))
from itertools import accumulate



N, M = list(map(int, input().split()))

A = list(map(int, input().split()))



A.sort(reverse = True)

#print(A)



ok = -1

ng = 10 ** 11

while abs(ok - ng) > 1:

    mid = (ok + ng) // 2

    left, right = 0, N - 1

    cnt = 0

    while left <= N - 1 and right >= 0:

        if A[left] + A[right] >= mid:

            cnt += right + 1

            left += 1

        else:

            if right:

                right -= 1

            else:

                left += 1

    if cnt >= M:

        ok = mid

    else:

        ng = mid

#print(ok)



Acum = [0] + list(accumulate(A))

#print(Acum)



left, right = 0, N - 1

cnt = 0

answer = 0

while left <= N - 1 and right >= 0:

    if A[left] + A[right] >= ok:

        cnt += right + 1

        answer += (right + 1) * A[left] + Acum[right + 1]

        left += 1

    else:

        if right:

            right -= 1

        else:

            left += 1

        

answer -= (cnt - M) * ok

print(answer)
from bisect import bisect_left
from itertools import accumulate

N, M = list(map(int, input().split()))
A = sorted(list(map(int, input().split())))
Acum = [0] + list(accumulate(A))
left = 0
right = max(A) * 2 + 1
while left + 1 < right:
    mid = (left + right) // 2
    tmp = 0
    for i in range(N):
        bi = bisect_left(A, mid - A[i])
        tmp += N - bi
    if tmp <= M:
        right = mid
    else:
        left = mid
cnt = 0
answer = 0
for i in range(N):
    bi = bisect_left(A, right - A[i])
    cnt += N - bi
    answer += Acum[N] - Acum[bi] + A[i] * (N - bi)
print((answer + (M - cnt) * left))

from itertools import accumulate

N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
A.sort(reverse=True)
# print(A)
ok = -1
ng = 10**11
while abs(ok - ng) > 1:
    mid = (ok + ng) // 2
    left, right = 0, N - 1
    cnt = 0
    while left <= N - 1 and right >= 0:
        if A[left] + A[right] >= mid:
            cnt += right + 1
            left += 1
        else:
            if right:
                right -= 1
            else:
                left += 1
    if cnt >= M:
        ok = mid
    else:
        ng = mid
# print(ok)
Acum = [0] + list(accumulate(A))
# print(Acum)
left, right = 0, N - 1
cnt = 0
answer = 0
while left <= N - 1 and right >= 0:
    if A[left] + A[right] >= ok:
        cnt += right + 1
        answer += (right + 1) * A[left] + Acum[right + 1]
        left += 1
    else:
        if right:
            right -= 1
        else:
            left += 1
answer -= (cnt - M) * ok
print(answer)

-from bisect import bisect_left -A = sorted(list(map(int, input().split()))) +A = list(map(int, input().split())) +A.sort(reverse=True) +# print(A) +ok = -1 +ng = 10**11 +while abs(ok - ng) > 1: +    mid = (ok + ng) // 2 +    left, right = 0, N - 1 +    cnt = 0 +    while left <= N - 1 and right >= 0: +        if A[left] + A[right] >= mid: +            cnt += right + 1 +            left += 1 +        else: +            if right: +                right -= 1 +            else: +                left += 1 +    if cnt >= M: +        ok = mid +    else: +        ng = mid +# print(ok) -left = 0 -right = max(A) * 2 + 1 -while left + 1 < right: -    mid = (left + right) // 2 -    tmp = 0 -    for i in range(N): -        bi = bisect_left(A, mid - A[i]) -        tmp += N - bi -    if tmp <= M: -        right = mid -    else: -        left = mid +# print(Acum) +left, right = 0, N - 1 -for i in range(N): -    bi = bisect_left(A, right - A[i]) -    cnt += N - bi -    answer += Acum[N] - Acum[bi] + A[i] * (N - bi) -print((answer + (M - cnt) * left)) +while left <= N - 1 and right >= 0: +    if A[left] + A[right] >= ok: +        cnt += right + 1 +        answer += (right + 1) * A[left] + Acum[right + 1] +        left += 1 +    else: +        if right: +            right -= 1 +        else: +            left += 1 +answer -= (cnt - M) * ok +print(answer)