num = []
ans = 0

n = gets.to_i
n.times do
  num << gets.to_i
end

num.sort!
smaller = num[0..(n/2.to_f).ceil-1]
larger = num[(n/2.to_f).ceil..n].reverse

num.clear
num[0] = larger.shift

while smaller.size != 0 || larger.size != 0

  if smaller.size == 1 && larger.size == 1

    larger<<smaller[0]
    smaller.clear

  end

  a = larger.shift
  b = smaller.shift
  c = larger.shift
  d = smaller.shift

  b != nil && num << b
  d != nil && num.unshift(d)
  a != nil && num << a
  c != nil && num.unshift(c)

end

for i in 0..n-2
  ans += (num[i+1]-num[i]).abs
end

p ans
