{-# LANGUAGE BangPatterns #-}

import Data.Map.Strict (Map, (!), fromList, insert, union)
import Data.List (foldl', sortBy, groupBy, minimum)
import Control.Applicative ((<$>))
import Debug.Trace (trace)

type RowCol = (Int, Int)
type Memo   = Map (Int, Int) Int
type IdxRC  = Map Int (Int, Int)

main :: IO ()
main = do
    n  <- readLn :: IO Int
    ms <- map toPair . lines <$> getContents :: IO [RowCol]
    let idxrc'   = fromList idxrc  :: IdxRC
        idxrc    = zip [1..] ms    :: [(Int, RowCol)]
        neighbor = fromList $ zipWith (\(a, (x, y)) (b, (x', y')) -> ((a, b), x * y * y')) idxrc (tail idxrc) :: Memo
        single   = fromList $ zipWith (\(a, (x, y)) (b, (x', y')) -> ((a, b), 0)) idxrc idxrc :: Memo
        memo     = union neighbor single
        rest     = sort' [ (x, y) | x <- [1..(n-2)], y <- [(x+2)..n] ]
    print $ (fst (foldl' solve (memo, idxrc') rest)) ! (1, n)
  where
    toPair = (\[x, y] -> (x, y)) . map read . words
    sort'  = concatMap (sortBy (flip compare)) . groupBy (\(_, a) (_, b) -> a == b) . sortOn
    sortOn = sortBy (\(_, a) (_, b) -> if a >= b then GT else LT)


solve :: (Memo, IdxRC) -> (Int, Int) -> (Memo, IdxRC)
solve (!memo, !idxrc) (!a, !b) = (insert (a, b) value memo, idxrc)
  where
    value = minimum [ ar * bc * snd (idxrc ! (a + (i - 1)))
                    + memo ! (a, a + (i - 1))
                    + memo ! (a + i, b)
                    | i <- [1..(b - a)]
                    ]
      where
        ar    = fst $ idxrc ! a
        bc    = snd $ idxrc ! b

