import Control.Applicative ((<$>)) import Control.Monad (replicateM) import Data.List main :: IO () main = do n <- readLn solve <$> replicateM (n - 1) readLn <*> replicateM n (getl $ map read . words) >>= print solve :: [Int] -> [[Int]] -> Int solve as bcs = loop 0 0 (0:as) bcs where loop fr pn [] [] = fr loop fr pn (a:xs) ([b, c]:ys) = loop (fr + pn * a) (pn - b + c) xs ys getl :: (String -> a) -> IO a getl f = f <$> getLine