
equalCut ls@(l:lls) rs@(r:lrs) lsum rsum lacc racc n len
  | n == len = (reverse lacc, racc)
  | lsum < rsum =  equalCut lls rs (l+lsum) rsum (l:lacc) racc n (len+1)
  | lsum >= rsum = equalCut ls lrs lsum (r+rsum) lacc (r:racc) n (len+1)
  | otherwise = undefined

getEqCuts0 as = equalCut las lrs (a0+a1) (r0+r0) [a1,a0] [r1,r0] (length as-4) 0
  where rs = reverse as
        (a0:a1:las) = as
        (r0:r1:lrs) = rs

getEqCuts1 as = equalCut las lrs a r [a] [r] (length as-2) 0
  where rs = reverse as
        (a:las) = as
        (r:lrs) = rs

solve0 as = [as0, as1, as2, as3]
  where (ls, rs) = getEqCuts0 as
        (as0, as1) = getEqCuts1 ls
        (as2, as3) = getEqCuts1 rs

solveA [as0, as1, as2, as3] = [as0_, as1__, as2__, as3_]
  where (as1_, as2_) = getEqCuts1 (as1++as2)
        (as0_, as1__) = getEqCuts1 (as0++as1_)
        (as2__, as3_) = getEqCuts1 (as2_++as3)

solve ass
  | ass == nss = ass
  | otherwise = solve nss
  where nss = solveA ass

main :: IO ()
main = do
  _ <- read <$> getLine :: IO Int
  as <- map read.words <$> getLine :: IO [Int]
  let ss = map sum $ solve (solve0 as)
  print $ maximum ss - minimum ss
