import Control.Monad
import Data.Maybe
import qualified Data.ByteString.Char8 as BS
import Data.List
import qualified Data.Array as A
import qualified Data.Vector.Unboxed as VU

readInt = fst . fromJust . BS.readInt
readIntList = map readInt . BS.words
getInt = readInt <$> BS.getLine
getIntList = readIntList <$> BS.getLine

main = do
    n <- getInt
    as <- VU.fromList <$> getIntList
    let ss = VU.scanl (+) 0 as
    let arr = A.array ((1,1),(n,n)) [((l,r), f l r) | l <- [1..n], r <- [1..n]]
                where f l r | l >= r = 0
                            | otherwise = minimum $ map g [l..r-1]
                            where g m = (arr A.! (l,m)) + (arr A.! (m+1,r)) + (ss VU.! r) - (ss VU.! (l-1))
    print $ arr A.! (1,n)