from itertools import accumulate

from bisect import bisect_left

def main():

    n,m=list(map(int,input().split()))

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

    A.sort()

    

    def count(k):

        cnt=0

        for a in A:

            cnt+=bisect_left(A,k-a)

        return cnt

    

    ok=0

    ng=2*10**5+1

    while ng-ok>1:

        mid=(ok+ng)//2

        if count(mid)<n**2-m:

            ok=mid

        else:

            ng=mid

    border=ok

    

    k=n**2-count(border)

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

    s=Acc[-1]

    ans=0

    for a in A:

        idx=bisect_left(A,border-a)

        ans+=a*(n-idx)+(s-Acc[idx])

    ans-=border*(k-m)

    print(ans)

    

if __name__=='__main__':

    main()
import numpy as np

from numpy.fft import rfft,irfft



def main():

    n,m=list(map(int,input().split()))

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

    F=np.bincount(A)

    fft_len=2*10**5+1

    Ff=rfft(F,fft_len)

    G=np.rint(irfft(Ff*Ff,fft_len)).astype(np.int64)

    G_acc=G.cumsum()

    remove_cnt=n**2-m

    border=np.searchsorted(G_acc,n**2-m)

    x=n**2-m-G_acc[border-1]

    remove_sum=(G[:border]*np.arange(border)).sum()+border*x

    ans=A.sum()*2*n-remove_sum

    print(ans)

    

if __name__=='__main__':

    main()
from itertools import accumulate
from bisect import bisect_left


def main():
    n, m = list(map(int, input().split()))
    A = list(map(int, input().split()))
    A.sort()

    def count(k):
        cnt = 0
        for a in A:
            cnt += bisect_left(A, k - a)
        return cnt

    ok = 0
    ng = 2 * 10**5 + 1
    while ng - ok > 1:
        mid = (ok + ng) // 2
        if count(mid) < n**2 - m:
            ok = mid
        else:
            ng = mid
    border = ok
    k = n**2 - count(border)
    Acc = [0] + list(accumulate(A))
    s = Acc[-1]
    ans = 0
    for a in A:
        idx = bisect_left(A, border - a)
        ans += a * (n - idx) + (s - Acc[idx])
    ans -= border * (k - m)
    print(ans)


if __name__ == "__main__":
    main()

import numpy as np
from numpy.fft import rfft, irfft


def main():
    n, m = list(map(int, input().split()))
    A = np.array(list(map(int, input().split())))
    F = np.bincount(A)
    fft_len = 2 * 10**5 + 1
    Ff = rfft(F, fft_len)
    G = np.rint(irfft(Ff * Ff, fft_len)).astype(np.int64)
    G_acc = G.cumsum()
    remove_cnt = n**2 - m
    border = np.searchsorted(G_acc, n**2 - m)
    x = n**2 - m - G_acc[border - 1]
    remove_sum = (G[:border] * np.arange(border)).sum() + border * x
    ans = A.sum() * 2 * n - remove_sum
    print(ans)


if __name__ == "__main__":
    main()

-from itertools import accumulate -from bisect import bisect_left +import numpy as np +from numpy.fft import rfft, irfft -    A = list(map(int, input().split())) -    A.sort() - -    def count(k): -        cnt = 0 -        for a in A: -            cnt += bisect_left(A, k - a) -        return cnt - -    ok = 0 -    ng = 2 * 10**5 + 1 -    while ng - ok > 1: -        mid = (ok + ng) // 2 -        if count(mid) < n**2 - m: -            ok = mid -        else: -            ng = mid -    border = ok -    k = n**2 - count(border) -    Acc = [0] + list(accumulate(A)) -    s = Acc[-1] -    ans = 0 -    for a in A: -        idx = bisect_left(A, border - a) -        ans += a * (n - idx) + (s - Acc[idx]) -    ans -= border * (k - m) +    A = np.array(list(map(int, input().split()))) +    F = np.bincount(A) +    fft_len = 2 * 10**5 + 1 +    Ff = rfft(F, fft_len) +    G = np.rint(irfft(Ff * Ff, fft_len)).astype(np.int64) +    G_acc = G.cumsum() +    remove_cnt = n**2 - m +    border = np.searchsorted(G_acc, n**2 - m) +    x = n**2 - m - G_acc[border - 1] +    remove_sum = (G[:border] * np.arange(border)).sum() + border * x +    ans = A.sum() * 2 * n - remove_sum