nu = gets.to_i
ar = Array.new(nu, 0)
for i in 0..nu - 1
  ar[i] = gets.to_i
end
nu1 = ar.sort
if nu1.length == 2
  puts nu1[1] - nu1[0]
  exit
end

ans = 0
inst1 = nu1[0]
inst2 = nu1[0]
nu1.delete_at(0)
nu -= 1
count = 0
while nu >= 1
  count += 1
  if count % 4 == 1
    ans += nu1[-1] - inst1
    inst1 = nu1[-1]
    nu1.delete_at(-1)
  elsif count % 4 == 2
    ans += nu1[-1] - inst2
    inst2 = nu1[-1]
    nu1.delete_at(-1)
  elsif count % 4 == 3
    ans += inst1 - nu1[0]
    inst1 = nu1[0]
    nu1.delete_at(0)
  else
    ans += inst2 - nu1[0]
    inst2 = nu1[0]
    nu1.delete_at(0)
  end
  nu -= 1
end

puts ans
