import Control.Arrow
import Data.List
import qualified Data.ByteString.Char8 as B
import Data.Ord
import Data.Function
import Debug.Trace

main :: IO ()
main = getLine >> breads >>= print . solve 

solve :: [Int] -> Int
solve as = loop (s, []) $ zip as ss 
    where
        s = sum as
        ss = scanl1 (+) as

        loop (acc, ps) (p@(a, t) : qs) = loop (min acc val, p : ps) qs
            where
                (b, c) = split t (p : ps)
                (d, e) = split (s - t) qs
                vs = sort [b, c, d, e]
                val = last vs - head vs
        loop (acc, _) _ = acc

        split t = check 0
            where
                check acc ((a, _) : xs)
                    | ((>=) `on` absf) acc next = check next xs
                    | otherwise = (acc, t - acc)
                    where
                        next = acc + a
                check acc _ = (acc, 0)
                absf = abs . (t -) . (2 *)


breads :: IO [Int]
breads = unfoldr (\s -> second (maybe B.empty snd . B.uncons) <$> B.readInt s) <$> B.getLine