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

if ((n % 4 == 3) && (num[n/2] - num[n/2-1] < num[n/2+1] - num[n/2])) || ((n % 4 == 1) && (num[n/2] - num[n/2-1] > num[n/2+1] - num[n/2]))

  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

    b = smaller.shift
    d = smaller.shift

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

      larger<<smaller[0]
      smaller.clear

    end

    a = larger.shift
    c = larger.shift


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

  end

else

  num.clear
  num[0] = smaller.shift

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

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

      larger<<smaller[0]
      smaller.clear

    end

    a = larger.shift
    c = larger.shift

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

      larger<<smaller[0]
      smaller.clear

    end

    b = smaller.shift
    d = smaller.shift

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

  end

end

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

p ans
