from bisect import bisect_left

def main():

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

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

    a.sort()

    b = [0]*(n+1)

    for i in range(n):

        b[i+1] = a[i] + b[i]

    ok = 0; ng = 2 * 10**5 + 5

    while ok+1 < ng:

        mid = (ok + 1 + ng) // 2

        buf = 0

        for i in range(n):

            p = mid - a[i]

            buf += n - bisect_left(a, p)

        if buf >= m:

            ok = mid

        else:

            ng = mid

    ans = 0

    num = 0

    tot = 0

    for i in range(n):

        p = ok - a[i]

        index = bisect_left(a, p)

        num += n - index

        tot += b[n] - b[index]

        tot += a[i] * (n-index)

    ans = tot - (num - m) * ok

    print(ans)



if __name__ == "__main__":

    main()
from bisect import bisect_left



def main():

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

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

    a.sort()

    L, R = 0, 2 * 10**5 + 1

    while L+1 < R:

        P = (L+R)//2

        cnt = 0

        for v in a:

            x = P - v

            cnt += n - bisect_left(a, x)

        if cnt >= m:

            L = P

        else:

            R = P

    csum = [0]

    for v in a:

        csum.append(v)

    for i in range(n):

        csum[i+1] += csum[i]

    ans = 0

    cnt = 0

    for v in a:

        x = L - v

        idx = bisect_left(a, x)

        cnt += n-idx

        ans += csum[-1] - csum[idx]

        ans += v * (n - idx)

    ans -= (cnt - m) * L

    print(ans)



if __name__ == "__main__":

    main()
from bisect import bisect_left


def main():
    n, m = list(map(int, input().split()))
    a = list(map(int, input().split()))
    a.sort()
    b = [0] * (n + 1)
    for i in range(n):
        b[i + 1] = a[i] + b[i]
    ok = 0
    ng = 2 * 10**5 + 5
    while ok + 1 < ng:
        mid = (ok + 1 + ng) // 2
        buf = 0
        for i in range(n):
            p = mid - a[i]
            buf += n - bisect_left(a, p)
        if buf >= m:
            ok = mid
        else:
            ng = mid
    ans = 0
    num = 0
    tot = 0
    for i in range(n):
        p = ok - a[i]
        index = bisect_left(a, p)
        num += n - index
        tot += b[n] - b[index]
        tot += a[i] * (n - index)
    ans = tot - (num - m) * ok
    print(ans)


if __name__ == "__main__":
    main()

from bisect import bisect_left


def main():
    n, m = list(map(int, input().split()))
    a = list(map(int, input().split()))
    a.sort()
    L, R = 0, 2 * 10**5 + 1
    while L + 1 < R:
        P = (L + R) // 2
        cnt = 0
        for v in a:
            x = P - v
            cnt += n - bisect_left(a, x)
        if cnt >= m:
            L = P
        else:
            R = P
    csum = [0]
    for v in a:
        csum.append(v)
    for i in range(n):
        csum[i + 1] += csum[i]
    ans = 0
    cnt = 0
    for v in a:
        x = L - v
        idx = bisect_left(a, x)
        cnt += n - idx
        ans += csum[-1] - csum[idx]
        ans += v * (n - idx)
    ans -= (cnt - m) * L
    print(ans)


if __name__ == "__main__":
    main()

-    b = [0] * (n + 1) +    L, R = 0, 2 * 10**5 + 1 +    while L + 1 < R: +        P = (L + R) // 2 +        cnt = 0 +        for v in a: +            x = P - v +            cnt += n - bisect_left(a, x) +        if cnt >= m: +            L = P +        else: +            R = P +    csum = [0] +    for v in a: +        csum.append(v) -        b[i + 1] = a[i] + b[i] -    ok = 0 -    ng = 2 * 10**5 + 5 -    while ok + 1 < ng: -        mid = (ok + 1 + ng) // 2 -        buf = 0 -        for i in range(n): -            p = mid - a[i] -            buf += n - bisect_left(a, p) -        if buf >= m: -            ok = mid -        else: -            ng = mid +        csum[i + 1] += csum[i] -    num = 0 -    tot = 0 -    for i in range(n): -        p = ok - a[i] -        index = bisect_left(a, p) -        num += n - index -        tot += b[n] - b[index] -        tot += a[i] * (n - index) -    ans = tot - (num - m) * ok +    cnt = 0 +    for v in a: +        x = L - v +        idx = bisect_left(a, x) +        cnt += n - idx +        ans += csum[-1] - csum[idx] +        ans += v * (n - idx) +    ans -= (cnt - m) * L