
n = gets.to_i
a = []
for i in 1..n
  a << gets.to_i
end

a = a.sort
ls = [a[0], a[-1]]
lmin = true
rmin = false
a = a[1..(a.size-2)]

while a.size > 0
  if lmin
    ldif = a[-1] - ls[0]
  else
    ldif = ls[-1] - a[0]
  end
  if rmin
    rdif = a[-1] - ls[0]
  else
    rdif = ls[-1] - a[0]
  end

  if rdif > ldif
    if rmin
      ls << a[-1]
      a = a[0,a.size-1]
      rmin = false
    else
      ls << a[0]
      a = a[1,a.size-1]
      rmin = true
    end
  else
    if lmin
      ls = [ a[-1] ].push(ls).flatten
      a = a[0,a.size-1]
      lmin = false
    else
      ls = [ a[0] ].push(ls).flatten
      a = a[1,a.size-1]
      lmin = true
    end
  end
end

sum = 0
for i in 0..(ls.size-2)
  sum += (ls[i+1]-ls[i]).abs
end
print sum

