N = gets.to_i
A = $<.map &:to_i
A.sort!

def f(_a)
  a = _a.dup
  res = [a.pop]
  while !a.empty?
    res.unshift a.shift
    res << a.shift

    res.unshift a.pop
    res << a.pop
  end
  res.compact
end

def f2(_a)
  a = _a.dup
  res = [a.pop]
  while !a.empty?
    res << a.shift
    res.unshift a.shift

    res << a.pop
    res.unshift a.pop
  end
  res.compact!
end

def g(a)
  a.each_cons(2).inject(0){|r, (a, b)|
    r + (a - b).abs
  }
end

p [g(f(A)), g(f(A.reverse))].max
