N,M = gets.split.map(&:to_i)
pows = gets.split.map(&:to_i).sort!.reverse!

cnt = 0
total = 0
idx = 0
target = idx
while cnt < M
  total += (pows[idx] * 2)
  cnt += 1
  puts "total:#{total}"
  break if cnt == M

  target = idx + 1
  while target < N
    total += (pows[idx]) + (pows[target])
    cnt += 1
    break if cnt == M
    total += (pows[idx]) + (pows[target])
    cnt += 1
    break if cnt == M
    target += 1
  end
  idx += 1
end

puts total
