CodeEff/ECCO huggingface dataset:
# Original solution
from itertools import permutations,combinations

N, K = list(map(int, input().split()))
L = list(map(int, input().split()))
P = list(combinations(L, K))
maximum = 0
for p in P:
    maximum = max(maximum, sum(p))
print(maximum)


# Optimized solution
N, K = list(map(int, input().split()))
L = sorted(list(map(int, input().split())), reverse=True)

print((sum(L[:K])))
# Original solution
N, K = [int(i) for i in input().split()]
L = [int(i) for i in input().split()]
LL = sorted(L, reverse=True)
LL_max = sum([LL[i] for i in range(0, K)])
print(LL_max)

# Optimized solution
N, K = [int(i) for i in input().split()]

L = sorted([int(i) for i in input().split()], reverse=True)

print((sum([L[i] for i in range(K)])))
# Original solution
from itertools import combinations

N, K = list(map(int, input().split()))
L = list(map(int, input().split()))

ans = max([sum(combi) for combi in combinations(L, K)])
print(ans)


# Optimized solution
from itertools import combinations

N, K = list(map(int, input().split()))
L = sorted(list(map(int, input().split())), reverse=True)
ans = sum(L[:K])
print(ans)
# Original solution
def Q2(x):
    N, K = x[0][0], x[0][1]
    lst = x[1][:]
    lst = sorted(lst)[::-1]
    return sum(lst[:K])

l = [int(i) for i in input().split()]
m = [int(i) for i in input().split()]
n = [l,m]
print((Q2(n)))

# Optimized solution
N, K = [int(i) for i in input().split()]
L = [int(i) for i in input().split()]
print((sum(sorted(L)[N - K:])))
# Original solution
n, k = list(map(int, input().split()))
L = sorted([int(i) for i in input().split()])
print((sum(L[-k:])))

# Optimized solution
n, k = list(map(int, input().split()))
L = sorted([int(i) for i in input().split()], reverse = True)
print((sum(L[: k])))
# Original solution
# -*- coding: utf-8 -*-
n, k = list(map(int,input().split()))
l = [int(i) for i in input().split()]

l.sort()
tmp = 0
for i in range(n - k):
    tmp += l[i]
ans = sum(l) - tmp
print(ans)


# Optimized solution
# -*- coding: utf-8 -*-
n, k = list(map(int,input().split()))
l = [int(i) for i in input().split()]

l.sort(reverse = True)
ans = 0
for i in range(k):
    ans += l[i]
print(ans)
# Original solution
N, K = list(map(int, input().split()))
l = list(map(int, input().split()))
l.sort(reverse=True)
print((sum(l[:K])))

# Optimized solution
N, K = list(map(int, input().split()))
L = list(map(int, input().split()))
L.sort(reverse=True)
print((sum(L[:K])))
# Original solution
# import bisect
# import copy
# import fractions
# import math
# import numpy as np
# from collections import Counter, deque
# from itertools import accumulate,permutations, combinations,combinations_with_replacement,product

def resolve():

    N,K=list(map(int,input().split()))
    L=sorted(list(map(int,input().split())),reverse=True)

    print((sum([L[i] for i in range(K)])))


resolve()

# Optimized solution
def resolve():

    N,K=list(map(int,input().split()))
    L=sorted(list(map(int,input().split())),reverse=True)

    print((sum(L[:K])))

resolve()
# Original solution
import itertools

N,K=list(map(int, input().split()))
l=[int(x) for x in input().split()]

max=-3

for i in list(itertools.combinations(l,K)):
    if sum(i)>=max:
        max=sum(i)

print(max)

# Optimized solution
N,K=list(map(int, input().split()))
l=[int(x) for x in input().split()]

buf=sorted(l,reverse=True)
result=0

for i in range(K):
    result+=buf[i]

print(result)
# Original solution
import bisect

N,M = list(map(int,input().split()))
A = sorted(list(map(int,input().split())))
#print(A)

# Aの累積和を求めておく。
csum = [A[0] for _ in range(N+1)]
for i in range(1,N):
  csum[i] = csum[i-1]+A[i]

# M回以上握手できるとなる最小の幸福度を求める。
# r:M回以上握手ができる最小の幸福度。
# l:M回以上握手が出来ない最大の幸福度。
l = 0
r = 10**6
while (l+1 < r):
  m = (l+r)//2
  p = 0 # 幸福度がm以上になる握手の回数
  for a in A:
    q = bisect.bisect_left(A,m-a)
    #print(q,m-a,A)
    p += N-q
  #print(l,m,r,p)
  if p >= M:
    l = m
  else:
    r = m
#print(l,m,r)

# 幸福度がlのときに握手ができる回数を求め、
# それとMとの差

ans = 0
cnt = 0
for a in A:
  q = bisect.bisect_right(A,l-a)
  if q == 0:
    ans += csum[N-1]+a*N
    cnt += N
  elif q < N:
    ans += csum[N-1]-csum[q-1]+a*(N-q)
    cnt += N-q
  #print(a,q,ans,cnt)
ans +=l*(M-cnt)
print(ans)


# Optimized solution
import bisect

N,M = list(map(int,input().split()))
A = sorted(list(map(int,input().split())))
#print(A)

# Aの累積和を求めておく。
csum = [A[0] for _ in range(N+1)]
for i in range(1,N):
  csum[i] = csum[i-1]+A[i]

# M回以上握手できるとなる最小の幸福度を求める。
# l:M回以上握手ができる最小の幸福度。
l = 0
r = 10**6
while (l+1 < r):
  m = (l+r)//2
  p = 0 # 幸福度がm以上になる握手の回数
  for a in A:
    q = bisect.bisect_left(A,m-a)
    #print(q,m-a,A)
    p += N-q
  #print(l,m,r,p)
  if p >= M:
    l = m
  else:
    r = m
#print(l,m,r)

# 幸福度がl-1のときに握手ができる回数と幸福度を求める。
# その回数とMとの差の回数だけ、幸福度にlを追加する。
# (幸福度がlになる握手のパターンは複数ある可能性がある)
ans = 0
cnt = 0
for a in A:
  q = bisect.bisect_right(A,l-1-a)
  if q == 0:
    ans += csum[N-1]+a*N
    cnt += N
  elif q < N:
    ans += csum[N-1]-csum[q-1]+a*(N-q)
    cnt += N-q
  #print(a,q,ans,cnt)
ans +=l*(M-cnt)
print(ans)
# Original solution
import bisect

N,M = list(map(int,input().split()))
A = sorted(list(map(int,input().split())))
#print(A)

# Aの累積和を求めておく。
csum = [A[0] for _ in range(N+1)]
for i in range(1,N):
  csum[i] = csum[i-1]+A[i]

# M回以上握手できるとなる最小の幸福度を求める。
# l:M回以上握手ができる最小の幸福度。
l = 0
r = 10**6
while (l+1 < r):
  m = (l+r)//2
  p = 0 # 幸福度がm以上になる握手の回数
  for a in A:
    q = bisect.bisect_left(A,m-a)
    #print(q,m-a,A)
    p += N-q
  #print(l,m,r,p)
  if p >= M:
    l = m
  else:
    r = m
#print(l,m,r)

# 幸福度がl-1のときに握手ができる回数と幸福度を求める。
# その回数とMとの差の回数だけ、幸福度にlを追加する。
# (幸福度がlになる握手のパターンは複数ある可能性がある)
ans = 0
cnt = 0
for a in A:
  q = bisect.bisect_right(A,l-1-a)
  if q == 0:
    ans += csum[N-1]+a*N
    cnt += N
  elif q < N:
    ans += csum[N-1]-csum[q-1]+a*(N-q)
    cnt += N-q
  #print(a,q,ans,cnt)
ans +=l*(M-cnt)
print(ans)


# Optimized solution
import bisect

N,M = list(map(int,input().split()))
A = sorted(list(map(int,input().split())))
#print(A)

# Aの累積和を求めておく。
csum = [A[0] for _ in range(N+1)]
for i in range(1,N):
  csum[i] = csum[i-1]+A[i]

# M回以上握手できるとなる最小の幸福度を求める。
# l:幸福度l以上となる握手がMパターン以上存在する。
# r:幸福度r以上となる握手がMパターン以上存在しない。
l = 0
r = 10**6
while (l+1 < r):
  m = (l+r)//2
  p = 0 # 幸福度がm以上になる握手の回数
  for a in A:
    q = bisect.bisect_left(A,m-a)
    #print(q,m-a,A)
    p += N-q
  #print(l,m,r,p)
  if p >= M:
    l = m
  else:
    r = m
#print(l,m,r)

# 幸福度がl-1のときに握手ができる回数と幸福度を求める。
# その回数とMとの差の回数だけ、幸福度にlを追加する。
# (幸福度がlになる握手のパターンは複数ある可能性がある)
ans = 0
cnt = 0
for a in A:
  q = bisect.bisect_right(A,l-1-a)
  if q == 0:
    ans += csum[N-1]+a*N
  elif q < N:
    ans += csum[N-1]-csum[q-1]+a*(N-q)
  cnt += N-q
  #print(a,q,ans,cnt)
ans += l*(M-cnt)
print(ans)
# Original solution
import bisect
from itertools import accumulate
N, M = list(map(int, input().split()))
A = sorted(list(map(int, input().split())))
ruiseki = list(accumulate(A[::-1]))

# 自分以上の数の個数がM個以上になる値のうち最大のもの
ok = 0
ng = 10 ** 15
while ok + 1 < ng:
    mid = (ok + ng) // 2
    cnt = 0
    ans = 0
    for a in A:
        x = bisect.bisect_left(A, mid - a)
        cnt += N - x
        if N - x >= 1:
            ans += ruiseki[N - x - 1] + (N - x) * a
    if cnt >= M:
        ok = mid
    else:
        ng = mid
# okで終わってる場合はcntがM個以上の場合があるから過分を引く
# ngで終わってる場合は残りM-cnt個のokを足す
print((ans + ok * (M - cnt))) 

# Optimized solution
from itertools import accumulate
N, M = list(map(int, input().split()))
A = sorted(list(map(int, input().split())))
ruiseki = list(accumulate(A[::-1]))

def syakutori(mid):
    x = 0
    cnt = 0
    ans = 0
    for i in range(N)[::-1]: # 大きい方から
        while x < N:
            if A[i] + A[x] >= mid:
                cnt += N - x
                if N - x >= 1:
                    ans += A[i] * (N - x) + ruiseki[N - x - 1]
                break
            else:
                x += 1

    return cnt, ans

# 自分以上の数の個数がM個以上になる値のうち最大のもの
ok = 0
ng = 10 ** 15
while ok + 1 < ng:
    mid = (ok + ng) // 2
    cnt, ans = syakutori(mid)
    if cnt >= M:
        ok = mid
    else:
        ng = mid
# okで終わってる場合はcntがM個以上の場合があるから過分を引く
# ngで終わってる場合は残りM-cnt個のokを足す
print((ans + ok * (M - cnt)))
# Original solution
import itertools
N,M = list(map(int,input().split()))
A = sorted(map(int,input().split()))[::-1]
I = M
for i in range(N):
    if i*i > M:
        I = i
A = A[:I]
print((
    sum(
        sorted(
            map(sum,itertools.product(A, repeat=2))
            )[::-1][:M]
        )
    ))


# Optimized solution
import itertools
N,M = list(map(int,input().split()))
A = sorted(map(int,input().split()))[::-1]

print((sum(sorted([sum(i) for i in itertools.product(A, repeat=2)])[::-1][:M])))
# Original solution
import sys
input = sys.stdin.readline
from bisect import bisect_left
n,m = list(map(int,input().split()))
a = list(map(int,input().split()))

b = [0]*(n+1)
a.sort()
for i in range(n):
  b[i+1] = b[i]+a[i]

def is_ok(arg):
    c = 0
    f = False
    for i in a:
      if c >= m:
        break
      c += (n-bisect_left(a,arg-i))
    if c >= m:
      f = True
    return f

def bisect_ok(ng, ok):
    while (abs(ok - ng) > 1):
        mid = (ok + ng) // 2
        if is_ok(mid):
            ok = mid
        else:
            ng = mid
    return ok

x = bisect_ok(2*a[-1]*m+1,1)
ans = 0
cnt = 0
for i in a:
  ind = n-bisect_left(a,x-i)
  cnt += ind
  ans += i*ind+(b[-1]-b[n-ind])
if cnt > m:
  ans -= x*(cnt-m)
print(ans)

# Optimized solution
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from bisect import bisect_left

n,m = list(map(int,readline().split()))
a = list(map(int,readline().split()))

b = [0]*(n+1)
a.sort()
for i in range(n):
  b[i+1] = b[i]+a[i]

def is_ok(arg):
    c = 0
    f = False
    for i in a:
      if c >= m:
        break
      c += (n-bisect_left(a,arg-i))
    if c >= m:
      f = True
    return f

def bisect_ok(ng, ok):
    while (abs(ok - ng) > 1):
        mid = (ok + ng) // 2
        if is_ok(mid):
            ok = mid
        else:
            ng = mid
    return ok

x = bisect_ok(2*a[-1]+1,1)
ans = 0
cnt = 0
for i in a:
  ind = n-bisect_left(a,x-i)
  cnt += ind
  ans += i*ind+(b[-1]-b[n-ind])
if cnt > m:
  ans -= x*(cnt-m)
print(ans)
# Original solution
N,M=list(map(int,input().split()))
A=list(map(int,input().split()))
A.sort(reverse=True)

def binary_search(y):
   left_index=-1
   right_index=len(A)

   while(left_index<right_index-1):
      i=(left_index+right_index)//2
      if A[i] >= y:
         left_index=i
      else:
         right_index=i
   return right_index

X=[c for c in range(2*max(A)+1)]
left_index_2=0
right_index_2=len(X)

while left_index_2<right_index_2-1:
   count=0
   j=(left_index_2+right_index_2)//2
   #print("X[j]:",X[j])

   for a in A:
      y=X[j]-a
      #Aの中でy以上の数を数える
      count+=binary_search(y)
   if count > M:
      left_index_2=j
   elif count <= M:
      right_index_2=j

#print(X[left_index_2])
happiness=0
count_2=0
for a in A:
   y=X[left_index_2]-a
   z=binary_search(y)
   #print(z)
   happiness+=sum(A[0:z])+a*z
   count_2+=z

if count_2>M:
   happiness-=X[left_index_2]*(count_2-M)
print(happiness)

# Optimized solution
N, M = list(map(int, input().split()))
a = [10**18] + sorted(map(int, input().split()), reverse=True)


def solve(x):
   count = 0
   j = N
   for i in range(1, N+1):
      while a[i] + a[j] < x:
         j -= 1
      count += j
   
   return count


left, right = 0, 10**18
while left < right - 1:
   mid = (left + right) // 2
   if solve(mid) >= M:
      left = mid
   else:
      right = mid

acc = a[:]
acc[0] = 0
for i in range(N):
   acc[i+1] += acc[i]

ans = 0
j = N
for i in range(1, N+1):
   while a[i] + a[j] < left+1:
      j -= 1
   ans += a[i]*j + acc[j]
   M -= j

if M:
   ans += left * M

print(ans)
# Original solution
N, M = list(map(int, input().split()))
a = [10**18] + sorted(map(int, input().split()), reverse=True)


def solve(x):
   count = 0
   j = N
   for i in range(1, N+1):
      while a[i] + a[j] < x:
         j -= 1
      count += j
   
   return count


left, right = 0, 10**18
while left < right - 1:
   mid = (left + right) // 2
   if solve(mid) >= M:
      left = mid
   else:
      right = mid

acc = a[:]
acc[0] = 0
for i in range(N):
   acc[i+1] += acc[i]

ans = 0
j = N
for i in range(1, N+1):
   while a[i] + a[j] < left+1:
      j -= 1
   ans += a[i]*j + acc[j]
   M -= j

if M:
   ans += left * M

print(ans)

# Optimized solution
N, M = list(map(int, input().split()))
a = [2*10**5+1] + sorted(map(int, input().split()), reverse=True)


def solve(x):
   count = 0
   j = N
   for i in range(1, N+1):
      while a[i] + a[j] < x:
         j -= 1
      count += j
   
   return count

left, right = 0, 2*10**5+1
while left < right - 1:
   mid = (left + right) // 2
   if solve(mid) >= M:
      left = mid
   else:
      right = mid

acc = a[:]
acc[0] = 0
for i in range(N):
   acc[i+1] += acc[i]

ans = 0
j = N
for i in range(1, N+1):
   while a[i] + a[j] < left+1:
      j -= 1
   ans += a[i]*j + acc[j]
   M -= j

if M:
   ans += left * M

print(ans)
# Original solution
import sys
N, M = list(map(int, input().split()))
A = sorted(list(map(int, input().split())), reverse=True)[:M]

item = []
for i in A:
    for j in A:
        item.append(i+j)
item.sort(reverse = True)
print((sum(item[:M])))


# Optimized solution
import sys
N, M = list(map(int, input().split()))
A = sorted(list(map(int, input().split())), reverse=True)[:M]

item = []
for i in A:
    for j in A:
        k = i + j
        if len(item) >= M:
            if min(item) < k:
                item.append(k)
                item.remove(min(item))
            else:
                break
        else:
            item.append(k)

item.sort(reverse = True)
print((sum(item[:M])))
# Original solution
from bisect import bisect_left as BL 
N,M=list(map(int,input().split()))
A=list(map(int,input().split()))
A.sort()

#border以上の幸福度を返す握手の組の数が、M以上であるか
def isok(border):
  res=0
  for i in range(N):
    res+=N-BL(A,border-A[i])
  return res>=M
    

ok=0
ng=A[-1]*2+1
mid=(ok+ng)//2

#幸福度h以上の握手がM組以上存在するような最大のhを探す
while abs(ok-ng)>1:
  mid=(ok+ng)//2
  if isok(mid):
    ok=mid
  else:
    ng=mid
  
ans=0
shake_c=0
for i in range(N):
  goodshake=N-BL(A,ok-A[i])
  shake_c+=goodshake
  for j in range(goodshake):
    ans+=A[i]+A[-1-j]
    
ans-=(shake_c-M)*ok
print(ans)
  

# Optimized solution
from bisect import bisect_left as BL 
N,M=list(map(int,input().split()))
A=list(map(int,input().split()))
A.sort()

#border以上の幸福度を返す握手の組の数が、M以上であるか
def isok(border):
  res=0
  for i in range(N):
    res+=N-BL(A,border-A[i])
  return res>=M
    

ok=0
ng=A[-1]*2+1
mid=(ok+ng)//2

#幸福度h以上の握手がM組以上存在するような最大のhを探す
while abs(ok-ng)>1:
  mid=(ok+ng)//2
  if isok(mid):
    ok=mid
  else:
    ng=mid
  
B=reversed(A)
SB=[0]
for b in B:
  SB.append(b+SB[-1])
  
ans=0
shake_c=0
for i in range(N):
  goodshake=N-BL(A,ok-A[i])
  shake_c+=goodshake
  ans+=A[i]*goodshake
  ans+=SB[goodshake]  
ans-=(shake_c-M)*ok
print(ans)
# Original solution
def resolve():
    import sys

    def LI():
        return list(map(int, sys.stdin.readline().rstrip().split()))  # 空白あり

    def S():
        return sys.stdin.readline().rstrip()

    N, M = list(map(int, S().split()))
    A = LI()
    A.sort()

    from bisect import bisect_left

    low = 2
    high = 2 * A[-1] + 1

    while low + 1 < high:
        mid = (low + high) // 2
        if sum(N - bisect_left([A[i] + A[j] for j in range(N)], mid) for i in range(N)) >= M:
            low = mid
        else:
            high = mid

    a = low  # a以上のA[i]+A[j]がM個以上となる最大のa

    from itertools import accumulate

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

    r = 0
    ans = 0
    for i in range(N):  # a+1以上のA[i]+A[j]を足し合わせていく
        b = bisect_left([A[i] + A[j] for j in range(N)], a + 1)
        r += N - b
        ans += B[N] - B[b]
        ans += A[i] * (N - b)

    ans += (M - r) * a  # A[i]+A[j] == a となるものを足し合わせる

    print(ans)

if __name__ == '__main__':
    resolve()

# Optimized solution
def resolve():
    import sys

    def LI():
        return list(map(int, sys.stdin.readline().rstrip().split()))  # 空白あり

    def S():
        return sys.stdin.readline().rstrip()

    N, M = list(map(int, S().split()))
    A = LI()
    A.sort()

    def f(n,x):  # A[n]+A[i]>=x となる i の最小値
        left = -1
        right = N
        while left + 1 < right:
            mid = (left + right) // 2
            if A[n] + A[mid] < x:
                left = mid
            else:
                right = mid
        return right

    low = 2 * A[0]
    high = 2 * A[-1] + 1

    while low + 1 < high:
        mid = (low + high) // 2
        if sum(N - f(i,mid) for i in range(N)) >= M:
            low = mid
        else:
            high = mid

    a = low  # a以上のA[i]+A[j]がM個以上となる最大のa

    from itertools import accumulate

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

    r = 0
    ans = 0
    for i in range(N):  # a+1以上のA[i]+A[j]を足し合わせていく
        b = f(i,a+1)
        r += N - b
        ans += B[N] - B[b]
        ans += A[i] * (N - b)

    ans += (M - r) * a  # A[i]+A[j] == a となるものを足し合わせる

    print(ans)

if __name__ == '__main__':
    resolve()
# Original solution
import heapq
n, m = list(map(int, input().split()))
a = list([-int(x) for x in input().split()])
a.sort()
hq = [(a[0] + a[0], 0, 0)]
s = set()
ans = 0
for _ in range(m):
    x, i, j = heapq.heappop(hq)
    ans += x
    if i < n - 1 and (i + 1) * n + j not in s:
        heapq.heappush(hq, (a[i + 1] + a[j], i + 1, j))
        s.add((i + 1) * n + j)
    if j < n - 1 and i * n + j + 1 not in s:
        heapq.heappush(hq, (a[i] + a[j + 1], i, j + 1))
        s.add(i * n + j + 1)
print((-ans))


# Optimized solution
from bisect import bisect_left
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
a.sort()
l, r = 0, 10000000000
while r - l > 1:
    m = (l + r) // 2
    res = 0
    for x in a:
        res += n - bisect_left(a, m - x)
    if res >= k:
        l = m
    else:
        r = m
b = [0] * (n + 1)
for i in range(1, n + 1):
    b[i] = b[i - 1] + a[n - i]
cnt = 0
ans = 0
for x in a:
    t = n - bisect_left(a, l - x)
    ans += b[t] + x * t
    cnt += t
print((ans - (cnt - k) * l))
# Original solution
from heapq import heappush, heappop

N, M = list(map(int, input().split()))
A = sorted(list(map(int, input().split())), reverse=True)
ans = 0
heap = []
heappush(heap, (-A[0]*2, 0, 0))
checked = set()
checked.add((0, 0))
for i in range(M):
    power, x, y = heappop(heap)
    ans -= power
    if x+1 < N and (x+1, y) not in checked:
        heappush(heap, (-(A[x+1]+A[y]), x+1, y))
        checked.add((x+1, y))
    if y+1 < N and (x, y+1) not in checked:
        heappush(heap, (-(A[x]+A[y+1]), x, y+1))
        checked.add((x, y+1))
print(ans)


# Optimized solution
from itertools import accumulate
import sys
input = sys.stdin.readline

N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
A.sort(reverse=True)
lb = 0
ub = 2*10**5+1
while ub - lb > 1:
    m = (lb + ub) // 2
    cnt = 0
    s, t = 0, N-1
    while t >= 0:
        while s < N and A[s] + A[t] >= m:
            s += 1
        cnt += s
        t -= 1

    if cnt >= M:
        lb = m
    else:
        ub = m

accum = list(accumulate([0] + A))
ans = 0
cnt = 0
s, t = 0, N-1
while t >= 0:
    while s < N and A[s] + A[t] >= lb:
        s += 1
    ans += accum[s] + s * A[t]
    cnt += s
    t -= 1
print((ans - (cnt-M) * lb))
# Original solution
import sys

def f(x):
    p=0
    cnt=0
    for i in range(n-1,-1,-1):
        while p<n and a[i]+a[p]<x:
            p+=1
        cnt+=n-p
    return cnt

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

l,r=a[0]*2,a[n-1]*2

while r-l>0:
    if r-l<=1:
        if f(r)==m: md=r;break 
        else: md=l;break
    md=(r+l)//2
    k=f(md)
    if k==m: break
    elif k>m: l=md
    else: r=md

p=0
cnt=0
ans=0

for i in range(n-1,-1,-1):
    try:
        while True:
            if p<n:
                if a[i]+a[p]<=md: p+=1
                else: break
            else: break
    except:
        b=1
        while True:
            b+=1

    if p==n: break
    cnt+=n-p
    ans+=a[i]*(n-p)*2


ans+=(m-cnt)*md
print(ans)


# Optimized solution
import sys

def f(x):
    p=0
    cnt=0
    for i in range(n-1,-1,-1):
        while p<n and a[i]+a[p]<x:
            p+=1
        cnt+=n-p
    return cnt

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

l,r=a[0]*2,a[n-1]*2
md=(r+l)//2
while r-l>0:
    if r-l<=1:
        if f(r)==m: md=r;break 
        else: md=l;break
    md=(r+l)//2
    k=f(md)
    if k==m: break
    elif k>m: l=md
    else: r=md

p=0
cnt=0
ans=0

for q in range(n-1,-1,-1):
    try:
        while p<n and a[q]+a[p]<=md:
            p+=1
    except:
        b=1
        while True:
            b+=1

    if p==n: break
    cnt+=n-p
    ans+=a[q]*(n-p)*2


ans+=(m-cnt)*md
print(ans)
# Original solution
import sys

def f(x):
    p=0
    cnt=0
    for i in range(n-1,-1,-1):
        while p<n and a[i]+a[p]<x:
            p+=1
        cnt+=n-p
    return cnt

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

l,r=a[0]*2,a[n-1]*2
md=(r+l)//2
while r-l>0:
    if r-l<=1:
        if f(r)==m: md=r;break 
        else: md=l;break
    md=(r+l)//2
    k=f(md)
    if k==m: break
    elif k>m: l=md
    else: r=md

p=0
cnt=0
ans=0

for q in range(n-1,-1,-1):
    try:
        while p<n and a[q]+a[p]<=md:
            p+=1
    except:
        b=1
        while True:
            b+=1

    if p==n: break
    cnt+=n-p
    ans+=a[q]*(n-p)*2


ans+=(m-cnt)*md
print(ans)


# Optimized solution
import sys

def f(x):
    p=0
    cnt=0
    for i in range(n-1,-1,-1):
        while p<n and a[i]+a[p]<x:
            p+=1
        cnt+=n-p
    return cnt

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

l,r=a[0]*2,a[n-1]*2
while True:
    if r-l<=1:
        if f(r)==m: md=r;break 
        else: md=l;break
        
    md=(r+l)//2
    k=f(md)
    if k==m: break
    elif k>m: l=md
    else: r=md

p=0
cnt=0
ans=0

for q in range(n-1,-1,-1):
    while p<n and a[q]+a[p]<=md:
        p+=1

    if p==n: break
    cnt+=n-p
    ans+=a[q]*(n-p)*2


ans+=(m-cnt)*md
print(ans)
# Original solution
n,m=list(map(int,input().split()))
a=list(map(int,input().split()))
for i in range(n):a[i]*=-1
a.sort()
from bisect import bisect_left,bisect_right
def check(mid):
  mm=0
  for i in range(n):
    if -(a[i]+a[0])<mid:break
    mm+=bisect_right(a,-(mid+a[i]))
  return mm
ok=0
ng=10**10+7
while ng!=ok+1:
  mid=(ok+ng)//2
  if check(mid)>=m:ok=mid
  else:ng=mid
b=[0]
for i in a:b.append(b[-1]+i)
ans=0
for i in range(n):
  if -(a[i]+a[0])<ok:break
  ind=bisect_right(a,-(ok+a[i]))
  ans+=a[i]*ind
  ans+=b[ind]
print((-(ans+(check(ok)-m)*ok)))

# Optimized solution
from bisect import bisect_right as br
n,m=list(map(int,input().split()))
a=list(map(int,input().split()))
for i in range(n):a[i]*=-1
a.sort()
b=[0]
for i in a:b.append(b[-1]+i)
ng=2*10**5+7
ok=-1
while ok+1!=ng:
  mid=(ng+ok)//2
  co=0
  for i in a:co+=br(a,-(mid+i))
  if co<m:ng=mid
  else:ok=mid
ans=0
co=0
for i in a:
  ind=br(a,-(ok+i)-1)
  co+=ind
  ans+=-b[ind]-i*ind
ans+=(m-co)*ok
print(ans)
# Original solution


N,M=[int(c) for c in input().split()]
A=[int(c) for c in input().split()]
B=sorted([x+y for x in A for y in A],reverse=True)
ans=0
for i in range(M):
  ans+=B[i]

print(ans)





# Optimized solution

N,M=[int(c) for c in input().split()]
A=[int(c) for c in input().split()]
B=sorted([x+y for x in A for y in A],reverse=True)

print((sum(B[0:M])))