import           Control.Applicative
import           Control.Monad
import qualified Data.ByteString.Char8 as B

import           Data.Array.IArray

readi :: B.ByteString -> Int
readi b | Just (n, _) <- B.readInt b = n

solve :: Int -> Array Int Int -> Int
solve n p = go (1, n)
  where
    m :: Array (Int, Int) Int
    m = listArray ((1, 1), (n, n)) [go (i, j) | i <- [1 .. n], j <- [1 .. n]]

    go :: (Int, Int) -> Int
    go (i, j)
        | i > j = error "something wrong"
        | i == j = 0
        | i < j =
            minimum
                [ m ! (i, k) + m ! (k + 1, j) + p ! (i - 1) * p ! k * p ! j
                | k <- [i .. j - 1]
                ]

main :: IO ()
main = do
    n <- readLn :: IO Int
    xss <- fmap (fmap readi . B.words) . B.lines <$> B.getContents
    let ps = listArray (0, n) $ fmap head xss ++ [last xss !! 1]
    print $ solve n ps

