import Control.Applicative
import Data.Char

type PQRS = ([Int],Int,Int,Int,Int,Int,Int,Int)

main :: IO ()
main = do
    n <- readLn
    xs <- map read . words <$> getLine
    print . minimum . take (n - 3) $ solve (xs,0,1,2,xs!!0,xs!!1,xs!!2,sum $ drop 3 xs)


solve :: PQRS -> [Int]
solve pqrs = (dist pqrs') : (solve $ helper3 pqrs')
    where pqrs' = helper2 $ helper1 pqrs

helper1 :: PQRS -> PQRS
helper1 (xs,i1,i2,i3,a1,a2,a3,a4)
    | a1 - a2 + x2 + x2 < 0 = helper1 (xs,i1+1,i2,i3,a1+x2,a2-x2,a3,a4)
    | abs(a1 - a2) >= abs(a1 - a2 + x2 + x2) = (xs,i1+1,i2,i3,a1+x2,a2-x2,a3,a4)
    | otherwise = (xs,i1,i2,i3,a1,a2,a3,a4)
        where x2 = xs !! (i1+1)

helper2 :: PQRS -> PQRS
helper2 (xs,i1,i2,i3,a1,a2,a3,a4)
    | a3 - a4 + x4 + x4 < 0 = helper2 (xs,i1,i2,i3+1,a1,a2,a3+x4,a4-x4)
    | abs(a3 - a4) >= abs(a3 - a4 + x4 + x4) = (xs,i1,i2,i3+1,a1,a2,a3+x4,a4-x4)
    | otherwise = (xs,i1,i2,i3,a1,a2,a3,a4)
        where x4 = xs !! (i3+1)

helper3 :: PQRS -> PQRS
helper3 (xs,i1,i2,i3,a1,a2,a3,a4) = (xs,i1,i2+1,i3,a1,a2+x3,a3-x3,a4)
        where x3 = xs !! (i2+1)


dist :: PQRS -> Int
dist (_,_,_,_,a1,a2,a3,a4) = (maximum as) - (minimum as)
    where as = [a1,a2,a3,a4]
