import Control.Applicative import Control.Monad import Data.Array import Data.Array.ST import Data.List import Data.Ord (comparing) big = 2^31 solve n c arr = let r = fmap snd go in if r == [] then -1 else minimum r where go = filter (\(k,v) -> fst k == n && v /= big) $ assocs arr' arr' = solve' n c arr solve' n c arr = last go where acc0 = (listArray ((1,0),(n,c)) $ repeat big) // [((1,c),0)] go = unfoldr f (1,acc0) f (i,acc) | i > n = Nothing | otherwise = Just (acc', (i+1,acc')) where current = filter (\(j,_) -> fst j == i) $ assocs acc acc' = accum min acc $ [((t,c-y),m+m0)|((i,c),m0) <- current, (t,y,m) <- arr!i, c-y >= 0] main = do n <- (read :: String -> Int) <$> getLine c <- (read :: String -> Int) <$> getLine _ <- getLine ss <- fmap (read :: String -> Int) . words <$> getLine ts <- fmap (read :: String -> Int) . words <$> getLine ys <- fmap (read :: String -> Int) . words <$> getLine ms <- fmap (read :: String -> Int) . words <$> getLine let arr = accumArray (flip (:)) [] (1,n) $ zip ss $ zip3 ts ys ms print $ solve n c arr