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

ai = sorted(ai, reverse=True)

def score(ai, k):
    nankai = min( len(ai) * 2 -1  ,k)
    inx = int(nankai //2)

    s = (ai[0] * (inx  + 1)  + sum(ai[0:inx + 1]) ) * 2 - 2 * ai[0]  
    s -=  (ai[inx] + ai[0]  if  nankai %2 == 0 and nankai !=0 else 0)
    k-=nankai
    if k > 0:
        return score(ai[1:],k) + s
    return s

print(score(ai,m))
