n=gets.to_i
a=[]
b=[]
n.times{a<<gets.to_i}
a.sort!
if n%2==0
  count=0
  while a.size!=0
    if count%2==0
      b.insert 0,a[0]
      a.delete_at 0
      b.insert(-1,a[-1])
      a.delete_at(-1)
    else
      b.insert(-1,a[0])
      a.delete_at 0
      b.insert(0,a[-1])
      a.delete_at(-1)
    end
    count+=1
  end
elsif n%2==1
  count=0
  b.insert 0,a[0]
  a.delete_at 0
  (n/2).times do
    if count%2==0
      b.insert(-1,a[-1])
      a.delete_at(-1)
      b.insert(0,a[-1])
      a.delete_at(-1)
    elsif count%2==1
      b.insert(-1,a[0])
      a.delete_at(0)
      b.insert(0,a[0])
      a.delete_at(0)
    end
  end
end
ans=0
(n-1).times{|i|
  ans+=(b[i+1]-b[i]).abs
}
puts ans