n,m=gets.chomp.split.map(&:to_i)
a = []
a = gets.chomp.split.map(&:to_i)
a = a.sort
x = y = n - 1
sum = a[x] * 2
m = m - 1
temp = []
while m > 0
  y = y - 1
  while temp.count > 0 && m > 0 && temp[temp.count - 1] > a[x] + a[y] && a[x] + a[y] >= a[x - 1] + a[x - 1]
  	if m > 1
	  sum = sum + temp.pop() * 2
	  m = m - 2
	else
	  sum = sum + temp.pop()
	  m = m - 1
	end
  end
  break if m == 0
  if x > 0 && a[x] + a[y] < a[x - 1] + a[x - 1]
  	while y > 0
  	  temp << a[x] + a[y]
  	  y = y - 1
  	end
  	y = x - 1
  	x = x - 1
  	temp = temp.sort
  end
  if x == y || m == 1
	sum = sum + a[x] + a[y]
	m = m - 1
  else
	sum = sum + (a[x] + a[y]) * 2
	m = m - 2
  end
end
print("#{sum}\n")