Problem:
Score : 400 points

Problem Statement

You are given N integers; the i-th of them is A_i.
Find the maximum possible sum of the absolute differences between the adjacent elements after arranging these integers in a row in any order you like.

Constraints

2 \leq N \leq 10^5

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

Print the maximum possible sum of the absolute differences between the adjacent elements after arranging the given integers in a row in any order you like.

Sample Input 1

5
6
8
1
2
3

Sample Output 1

21

When the integers are arranged as 3,8,1,6,2, the sum of the absolute differences between the adjacent elements is |3 - 8| + |8 - 1| + |1 - 6| + |6 - 2| = 21. This is the maximum possible sum.

Sample Input 2

6
3
1
4
1
5
9

Sample Output 2

25

Sample Input 3

3
5
5
1

Sample Output 3

8

Solution in Kotlin (1.0.0) (judge verdict: Runtime Error)


fun main(args : Array<String>) {
    val N = readLine()!!.toInt()
    var A = listOf<Long>().toMutableList()

    if (N == 2) {
        println(A.first() - A.last())
        return
    }

    (1..N).forEach {
        val num = readLine()!!.toLong()
        A.add(num)
    }
    A.sort()
    var AL = LinkedList<Long>()
    A.forEach {
        AL.append(it)
    }


    var sum = 0L

    // Init
    val center = AL.first()!!.value
    AL.removeAtIndex(0)
    var left = AL.last()!!.value
    AL.removeLast()
    var right = AL.last()!!.value
    AL.removeLast()

    sum += Math.abs(left - center)
    sum += Math.abs(right - center)

    var head = true
    var count = N - 3
    while (count > 0) {
        if (count == 1) {
            val a = AL.first()!!.value
            sum += Math.max(Math.abs(left - a), Math.abs(right - a))
            break
        }

        var a : Long
        var b : Long

        if (head) {
            a = AL.first()!!.value
            AL.removeAtIndex(0)
            b = AL.first()!!.value
            AL.removeAtIndex(0)
        } else {
            a = AL.last()!!.value
            AL.removeLast()
            b = AL.last()!!.value
            AL.removeLast()
        }
        val pattern1 = Math.abs(left - a) + Math.abs(right - b)
        val pattern2 = Math.abs(left - b) + Math.abs(right - a)
        if (pattern1 >= pattern2) {
            left = a
            right = b
            sum += pattern1
        } else {
            right = a
            left = b
            sum += pattern2
        }

        count = count - 2
        head = !head
    }

    println(sum)
}

class Node<T>(value: T){
    var value:T = value
    var next: Node<T>? = null
    var previous:Node<T>? = null
}

class LinkedList<T> {

    private var head:Node<T>? = null

    var isEmpty:Boolean = head == null

    fun first():Node<T>? = head

    fun last(): Node<T>? {
        var node = head
        if (node != null){
            while (node?.next != null) {
                node = node?.next
            }
            return node
        } else {
            return null
        }
    }

    fun count():Int {
        var node = head
        if (node != null){
            var counter = 1
            while (node?.next != null){
                node = node?.next
                counter += 1
            }
            return counter
        } else {
            return 0
        }
    }

    fun nodeAtIndex(index: Int) : Node<T>? {
        if (index >= 0) {
            var node = head
            var i = index
            while (node != null) {
                if (i == 0) return node
                i -= 1
                node = node.next
            }
        }
        return null
    }

    fun append(value: T) {
        var newNode = Node(value)

        var lastNode = this.last()
        if (lastNode != null) {
            newNode.previous = lastNode
            lastNode.next = newNode
        } else {
            head = newNode
        }
    }

    fun removeAll() {
        head = null
    }

    fun removeNode(node: Node<T>):T {
        val prev = node.previous
        val next = node.next

        if (prev != null) {
            prev.next = next
        } else {
            head = next
        }
        next?.previous = prev

        node.previous = null
        node.next = null

        return node.value
    }

    fun removeLast() : T? {
        val last = this.last()
        if (last != null) {
            return removeNode(last)
        } else {
            return null
        }
    }

    fun removeAtIndex(index: Int):T? {
        val node = nodeAtIndex(index)
        if (node != null) {
            return removeNode(node)
        } else {
            return null
        }
    }

    override fun toString(): String {
        var s = "["
        var node = head
        while (node != null) {
            s += "${node.value}"
            node = node.next
            if (node != null) { s += ", " }
        }
        return s + "]"
    }
}