import kotlin.math.max import kotlin.math.min val MOD = 1_000_000_007 fun main() { val (N) = readInts() val A = readLongs() if (N == 1 || N == 2) { println(0) return } A.sort() val cum = LongArray(N + 1) for (i in 0 until N) { cum[i + 1] = cum[i] + A[i] } var ans = 0L for (i in 0 until N) { var l = 0 var h = min(i, N - 1 - i) + 1 while(h - l > 1) { val m = (h + l) / 2 if (A[N - m] + A[i - m] - A[i] * 2 >= 0) l = m else h = m } val v = cum[N] - cum[N - l] + cum[i] - cum[i - l] - A[i] * 2 * l debug{"i:$i l:$l v:$v"} ans = max(ans, v) } println(ans) } private val isDebug = try { // なんか本番でエラーでる System.getenv("MY_DEBUG") != null } catch (t: Throwable) { false } private fun readLn() = readLine()!! private fun readStrings() = readLn().split(" ") private fun readInts() = readStrings().map { it.toInt() }.toIntArray() private fun readLongs() = readStrings().map { it.toLong() }.toLongArray() private inline fun debug(msg: () -> String) { if (isDebug) System.err.println(msg()) } private fun debug(a: LongArray) { debug{a.joinToString(" ")} } private fun debug(a: IntArray) { debug{a.joinToString(" ")} } private fun debug(a: BooleanArray) { debug{a.map{if(it) 1 else 0}.joinToString("")} } private fun debugDim(A: Array) { if (isDebug) { for (a in A) { debug(a) } } }