n = gets.chomp.to_i
a_arr = n.times.map { gets.chomp.to_i }.sort_by{|a| -a}

a = a_arr.shift
result = [a]
while a_arr.length > 0 do
  b = a_arr.pop
  if (result[0] - b).abs > (result[-1] - b).abs
    result.unshift(b)
  else
    result.push(b)
  end
  break if a_arr.length == 0
  a = a_arr.shift
  if (result[0] - a).abs > (result[-1] - a).abs
    result.unshift(a)
  else
    result.push(a)
  end
end
ans = 0
result.each_cons(2) do |a, b|
  ans += (a - b).abs
end
puts ans
