import java.io._
import java.util.StringTokenizer
import scala.util.control.Breaks.{breakable,break}

object Main extends App {
  val in = new InputReader(System.in)
  val pw = new PrintWriter(System.out)

  val N = in.next().toInt
  val A = new Array[Int](N+1)

  for(i <- 1 to N) {
    A(i) = in.next().toInt
  }




  //
  val imos = new Array[Long](N+1)
  for (i <- 1 to N)
    imos(i) = A(i) + imos(i-1)


  def check(mid: Int, thre: Int): Long = {    
    if(thre > mid) {
      Math.abs(imos(mid) - (imos(thre) - imos(mid)))
    }
    else {
      Math.abs((imos(mid) - imos(thre-1)) - (imos(N) - imos(mid)))
    }
  }

  // 下に凸のグラフの最小値
  def ternarySearchInt(small: Int, big: Int, thre: Int): Int = {
    if(big - small <= 2) {
      val mid = (big + small) / 2
      if(check(big, thre) <= check(mid, thre)) big
      else if(check(small, thre) <= check(mid, thre)) small
      else mid
    }
    else {
      val midl = (big + small) / 2
      val midr = midl + 1
      if (check(midl, thre) >= check(midr, thre))
        ternarySearchInt(midl, big, thre)
      else
        ternarySearchInt(small, midr, thre)
    }
  }
 
  var ans: Long = Long.MaxValue
  for (m <- 2 to N-2) {
    val l = ternarySearchInt(1, m, m)
    val r = ternarySearchInt(m+1, N, m+1)
    val lst = List(imos(l), (imos(m)-imos(l)),
      (imos(r)-imos(m)), (imos(N)-imos(r)))
    // println(lst)
    ans = Math.min(ans, lst.max - lst.min)
  }
  println(ans)
}


class InputReader(stream: InputStream) {
  private val reader = new BufferedReader(new InputStreamReader(stream))
  private var tokenizer: StringTokenizer = new StringTokenizer(reader.readLine())
  def next(): String = {
    while (!tokenizer.hasMoreTokens()) {
      tokenizer = new StringTokenizer(reader.readLine())
    }
    tokenizer.nextToken()
  }
}
