n = gets.chop.to_i
as = []
n.times do
  as << gets.chop.to_i
end

as.sort!
ans = 0

tmp = [as.pop]
small_flag = true
time = 1
(n-1).times do |i|
  if small_flag
    num = as.shift
    first = (num - tmp.first).abs
    last = (num - tmp.last).abs
    if first > last
      tmp.unshift(num)
      ans += first
    else
      tmp.push(num)
      ans += last
    end
    small_flag = !(small_flag) if i.odd?
  else
    num = as.pop
    first = (num - tmp.first).abs
    last = (num - tmp.last).abs
    if first > last
      tmp.unshift(num)
      ans += first
    else
      tmp.push(num)
      ans += last
    end
    small_flag = !(small_flag) if i.odd?
  end
end

puts ans