-- import qualified  Data.Set as S
-- import Data.Bits
-- import Data.List
-- import Control.Monad
import Control.Monad.State
import Control.Applicative
import Data.Char

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

main :: IO ()
main = do
    n <- readLn
    (x1:x2:x3:xs) <- map read . words <$> getLine
    print . minimum . take (n - 3) $ solve ([x1],[x2],[x3],xs,x1,x2,x3,sum xs)


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

helper1 :: PQRS -> PQRS
helper1 (xs1,x2:xs2,xs3,xs4,a1,a2,a3,a4)
    | a1 - a2 + x2 + x2 < 0 = helper1 (xs1++[x2],xs2,xs3,xs4,a1+x2,a2-x2,a3,a4)
    | abs(a1 - a2) >= abs(a1 - a2 + x2 + x2) = (xs1++[x2],xs2,xs3,xs4,a1+x2,a2-x2,a3,a4)
    | otherwise = (xs1,x2:xs2,xs3,xs4,a1,a2,a3,a4)

helper2 :: PQRS -> PQRS
helper2 (xs1,xs2,xs3,x4:xs4,a1,a2,a3,a4)
    | a3 - a4 + x4 + x4 < 0 = helper2 (xs1,xs2,xs3++[x4],xs4,a1,a2,a3+x4,a4-x4)
    | abs(a3 - a4) >= abs(a3 - a4 + x4 + x4) = (xs1,xs2,xs3++[x4],xs4,a1,a2,a3+x4,a4-x4)
    | otherwise = (xs1,xs2,xs3,x4:xs4,a1,a2,a3,a4)

helper3 :: PQRS -> PQRS
helper3 (xs1,xs2,x3:xs3,xs4,a1,a2,a3,a4) = (xs1,xs2++[x3],xs3,xs4,a1,a2+x3,a3-x3,a4)

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