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

fun main() {
    val n = readLine()?.toInt() ?: return
    val al = mutableListOf<Long>()
    repeat(n) {
        val a = readLine()?.toLong() ?: return
        al.add(a)
    }
    al.sort()
    var l = 1
    var r = n - 1
    var lv = al[0]
    var rv = al[0]
    var f = 2
    var t = 0L
    while (l <= r) {
        if (l == r) {
            val a = abs(al[l] - lv)
            val b = abs(al[l] - rv)
            t += max(a, b)
            l++
        } else if (f < 0) {
            val all = al[l]
            if (f == -2) {
                t += abs(all - lv)
                lv = al[l]
            } else {
                t += abs(all - rv)
                rv = al[l]
            }
            l++
            f++
            if (f == 0) {
                f = 2
            }
        } else {
            val alr = al[r]
            if (f == 2) {
                t += abs(alr - lv)
                lv = alr
            } else {
                t += abs(alr - rv)
                rv = alr
            }
            r--
            f--
            if (f == 0) {
                f = -2
            }
        }
    }
    println(t.toString())
}