import Control.Applicative import Data.List main = do n <- read <$> getLine c <- read <$> getLine getLine s <- map read . words <$> getLine t <- map read . words <$> getLine y <- map read . words <$> getLine m <- map read . words <$> getLine print $ solve n c $ createRouteList s t y m inf :: Int inf = 2^20 solve :: Int -> Int -> [Route] -> Int solve n cmax routes = if ans < inf then ans else -1 where ans = f 1 cmax f m c | m == n = 0 | otherwise = foldr min inf [(memo !! ((to r) - 1) !! (c - (cost r))) + (rtime r) | r <- es] where es = [r | r <- routes, (from r) == m, c - (cost r) >= 0] memo = [[f m c | c <- [0..cmax]] | m <- [1..n]] data Route = Route { from :: Int , to :: Int , cost :: Int , rtime :: Int } deriving (Show) createRouteList :: [Int] -> [Int] -> [Int] -> [Int] -> [Route] createRouteList = zipWith4 Route