import kotlin.math.abs

fun main() {
    val n = readLine()!!.toInt()
    val a = readLine()!!.split(" ").map { it.toLong() }

    val sums = Array(n + 1) { 0L }
    for (i in 1..n) sums[i] = sums[i - 1] + a[i - 1]

    var ans = 1e18.toLong()

    var x = 1
    var z = 3
    for (y in 2..n - 2) {
        while (true) {
            val cur = abs(sums[x] - (sums[y] - sums[x]))
            val next = abs(sums[x + 1] - (sums[y] - sums[x + 1]))
            if (cur > next) x++
            else break
        }

        while (true) {
            val cur = abs((sums[z] - sums[y]) - (sums[n] - sums[z]))
            val next = abs((sums[z + 1] - sums[y]) - (sums[n] - sums[z + 1]))
            if (cur > next) z++
            else break
        }

        val cuts = listOf(sums[x], sums[y] - sums[x], sums[z] - sums[y], sums[n] - sums[z])
        ans = minOf(ans, cuts.max()!! - cuts.min()!!)
    }

    println(ans)
}