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[Long](N+1)
  for(i <- 1 to N)
    A(i) = in.next().toLong


  //
  def check(mid: Int, base: Int): Long = {
    if(mid < base) {
      val l = imos(mid-1)
      val r = imos(base-1) - l
      (r-l).abs
    }
    else {
      val l = imos(mid) - imos(base-1)
      val r = imos(N) - imos(mid)
      (r-l).abs
    }
  }



  // 下に凸のグラフの最小値
  def ternarySearchInt(small: Int, big: Int, i: Int): Int = {
    if(big - small <= 2) {
      val mid = (big + small) / 2
      if(check(big, i) <= check(mid, i)) big
      else if(check(small, i) <= check(mid, i)) small
      else mid
    }
    else{
      val midl = (big + small) / 2
      val midr = midl + 1
      if (check(midl, i) >= check(midr, i))
        ternarySearchInt(midl, big, i)
      else
        ternarySearchInt(small, midr, i)
    }
  }


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

  var ans = Long.MaxValue
  for(i <- 3 to N-1) { // iが右ブロックの最初
    val l = ternarySearchInt(1, i-1, i)
    val r = ternarySearchInt(i, N, i)
    val P = imos(l-1)
    val Q = imos(i-1) - P
    val R = imos(r) - imos(i-1)
    val S = imos(N) - imos(r)
    val hoge = List(P,Q,R,S)

    ans = Math.min(ans, hoge.max - hoge.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()
  }
}

