require 'pp'

n = gets.chomp!.to_i
a_n = Array.new(n)
n.times do |i|
    a_n[i] = gets.to_i
end
a_n.sort!

if n % 2 == 1
    before = a_n[n / 2] 
    a_n[n / 2] = 0
    if (before - a_n[0]).abs >= (before - a_n[-1]).abs
        i = 1
    else
        i = 0
    end
else
    t1 = a_n[n / 2 - 1] - a_n[-1]
    t2 = a_n[n / 2] - a_n[0]
    if t1.abs >= t2.abs
        before = a_n[n / 2 - 1]
        a_n[n / 2 - 1] = 0
        i = 0
    else
        before = a_n[n / 2]
        a_n[n / 2] = 0
        i = 1
    end
end

ans = 0
while a_n.empty?.!
    x = if i % 2 == 0
            a_n.pop
        else
            a_n.shift
        end

    if x == 0
        break
    end

    #pp x
    #pp before
    #pp a_n

    ans += (x - before).abs
    before = x
    i += 1
end

puts ans
