let n = stdin.ReadLine() |> int
let a = stdin.ReadLine().Split(' ') |> Array.map int
let flattenSums start count =
    let sum = Array.sub a start count |> Array.sum
    let half = (float sum) / 2.0
    let mutable current = a.[start]
    if half <= (float current) then
        (current, sum - current)
    else
        let mutable result = (0, 0)
        let mutable ended = false
        for i in (start+1) .. (start+count-2) do
            if (not ended) then
                let next = current + a.[i]
                if half <= (float next) then
                    result <- if (half - (float current) < (float next) - half) then (sum - current, current) else (next, sum - next)
                    ended <- true
                current <- next
        if ended then result else (sum - current, current)
let getMin center =
    let M1, m1 = flattenSums 0 center
    let M2, m2 = flattenSums center (n-center)
    (max M1 M2) - (min m1 m2)
[2 .. n-2] |> Seq.map getMin |> Seq.min |> stdout.WriteLine
