import kotlin.math.abs
import kotlin.math.max
import kotlin.math.min

//

fun main() {
    tenka1_2018c()
}

fun tenka1_2018c() {
    val n = readLine()!!.toInt()
    val a = LongArray(n)
    for (i in 0 until n) {
        a[i] = readLine()!!.toLong()
    }
    a.sort()
    var sum = 0L
    var left = a[0]
    var right = a[0]
    var i = 1
    var j = n - 1
    while (i <= j) {
        val leftMin = left < right
        val edgeMin = if (leftMin) left else right
        val edgeMax = if (leftMin) right else left
        val minSub = abs(edgeMin - a[j])
        val maxSub = abs(edgeMax - a[i])
        if (minSub > maxSub) {
            sum += minSub
            // edgeMin側をjで更新
            if (leftMin) left = a[j] else right = a[j]
            j--
        } else {
            sum += maxSub
            // edgeMax側をiで更新
            if (leftMin) right = a[i] else left = a[i]
            i++
        }
        
    }
    println(sum)
}