Problem:
Score : 600 points

Problem Statement

Snuke has an integer sequence A of length N.

He will make three cuts in A and divide it into four (non-empty) contiguous subsequences B, C, D and E.
The positions of the cuts can be freely chosen.

Let P,Q,R,S be the sums of the elements in B,C,D,E, respectively.
Snuke is happier when the absolute difference of the maximum and the minimum among P,Q,R,S is smaller.
Find the minimum possible absolute difference of the maximum and the minimum among P,Q,R,S.

Constraints

4 \leq N \leq 2 \times 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_2 ... A_N

Output

Find the minimum possible absolute difference of the maximum and the minimum among P,Q,R,S.

Sample Input 1

5
3 2 4 1 2

Sample Output 1

2

If we divide A as B,C,D,E=(3),(2),(4),(1,2), then P=3,Q=2,R=4,S=1+2=3.
Here, the maximum and the minimum among P,Q,R,S are 4 and 2, with the absolute difference of 2.
We cannot make the absolute difference of the maximum and the minimum less than 2, so the answer is 2.

Sample Input 2

10
10 71 84 33 6 47 23 25 52 64

Sample Output 2

36

Sample Input 3

7
1 2 3 1000000000 4 5 6

Sample Output 3

999999994

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

package atcoder.abc102

fun main(args : Array<String>) {
    val N = readLine()!!.toInt()
    val A = readLine()!!.split(" ").map { it.toLong() }.toMutableList()
    prefixSum(A)

    var min = Long.MAX_VALUE

    fun findPositionCandidates(a: List<Long>) : List<Int> {
        val totalSum = a.last()
        val candidate = lowerBound(a, totalSum / 2)

        return listOf<Int>(candidate - 1, candidate, candidate + 1).filter { 0 <= it && it <= a.size - 2 }
    }

    // O(NlogN)
    for (center in 2..N-2) {
        val L = A.subList(0, center)
        val R = A.subList(center, N).map{ it - L.last() }

        val partitionPositionForL = findPositionCandidates(L)
        val partitionPositionForR = findPositionCandidates(R)

        val (diffPQ, sumP, sumQ) = partitionPositionForL.map {
            val sumP = L.get(it)
            val sumQ = L.last() - L.get(it)
             Triple(Math.abs(sumP - sumQ), sumP, sumQ)
        }.minBy { it.first }!!

        val (diffRS, sumR, sumS) = partitionPositionForR.map {
            val sumR = R.get(it)
            val sumS = R.last() - R.get(it)
            Triple(Math.abs(sumR - sumS), sumR, sumS)
        }.minBy { it.first }!!


        val list = listOf<Long>(sumP, sumQ, sumR, sumS).sorted()
        val result = list.last() - list.first()
        min = Math.min(min, result)
    }

    println(min)
}

fun prefixSum(A: MutableList<Long>) {
    for(i in 1..(A.size - 1)) {
        A.set(i, A.get(i) + A.get(i - 1))
    }
}


fun lowerBound(sortedList: List<Long>, key: Long) : Int {
    var ok = sortedList.size
    var ng = -1

    fun isOk(mid: Int) : Boolean {
        return sortedList.get(mid) >= key
    }

    while (Math.abs(ok-ng) > 1) {
        val mid = (ok + ng) / 2

        if (isOk(mid)) {
            ok = mid
        } else {
            ng = mid
        }
    }

    return ok
}