fun main(args: Array<String>) {
  val N = readInt()
  val A = readLongs().toMutableList()
  val cumSumA = Array(N) { 0L }
  cumSumA[0] = A[0]
  for (i in 1 until N) {
    cumSumA[i] = cumSumA[i - 1] + A[i]
  }

  var min = Long.MAX_VALUE

  var iL = 0
  var iR = 2
  for (iM in 1 until N - 2) {
    val sumL = cumSumA[iM]
    val sumR = cumSumA[N - 1] - sumL

    while (true) {
      val l = cumSumA[iL]
      val r = sumL - l
      val ll = cumSumA[iL + 1]
      val rr = sumL - ll
      if (Math.abs(l - r) <= Math.abs(ll - rr)) break
      iL++
    }
    while (true) {
      val l = cumSumA[iR] - sumL
      val r = sumR - l
      val ll = cumSumA[iR + 1] - sumL
      val rr = sumR - ll
      if (Math.abs(l - r) <= Math.abs(ll - rr)) break
      iR++
    }

    val d0 = cumSumA[iL]
    val d1 = sumL - d0
    val d2 = cumSumA[iR] - sumL
    val d3 = sumR - d2
    val dList = listOf(d0, d1, d2, d3)

    min = Math.min(min, dList.max()!! - dList.min()!!)
  }
  println(min)
}

fun gcd(a: Int, b: Int): Int = if (a < b) gcd(b, a) else if (b == 0) a else gcd(b, a % b)
fun lcm(a: Int, b: Int): Int = (a * b) / gcd(a, b)

fun readString() = readLine()!!
fun readInt() = readLine()!!.toInt()
fun readInts() = readLine()!!.split(" ").map { it.toInt() }
fun readLongs() = readLine()!!.split(" ").map { it.toLong() }
