import Data.List

splits :: [Int] -> [[[Int]]]
splits [] = [[]]
splits xs =
  concat [let (bef, aft) = splitAt n xs in map (bef:) $ splits aft | n <- [1..(length xs)]]

energy :: Int -> [[Int]] -> Int
energy _  []     = 0
energy x' (x:xs) = cost + energy x' xs
 where cost = x'*(num+1) + last x + sum (zipWith (\k d -> (k+1)^2*d) [num..1] (zipWith (-) x (0:x)))
       num = length x

main = do
  x' <- read.last.words <$> getLine
  xs <- map read.words <$> getLine
  let ans = minimum $ map (energy x') $ [xs] : splits xs
  print ans