Problem:
Score : 100 points

Problem Statement

There are N items, numbered 1, 2, \ldots, N.
For each i (1 \leq i \leq N), Item i has a weight of w_i and a value of v_i.

Taro has decided to choose some of the N items and carry them home in a knapsack.
The capacity of the knapsack is W, which means that the sum of the weights of items taken must be at most W.

Find the maximum possible sum of the values of items that Taro takes home.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq W \leq 10^9

1 \leq w_i \leq W

1 \leq v_i \leq 10^3

Input

Input is given from Standard Input in the following format:

N W
w_1 v_1
w_2 v_2
:
w_N v_N

Output

Print the maximum possible sum of the values of items that Taro takes home.

Sample Input 1

3 8
3 30
4 50
5 60

Sample Output 1

90

Items 1 and 3 should be taken.
Then, the sum of the weights is 3 + 5 = 8, and the sum of the values is 30 + 60 = 90.

Sample Input 2

1 1000000000
1000000000 10

Sample Output 2

10

Sample Input 3

6 15
6 5
5 6
6 4
6 6
3 5
7 2

Sample Output 3

17

Items 2, 4 and 5 should be taken.
Then, the sum of the weights is 5 + 6 + 3 = 14, and the sum of the values is 6 + 6 + 5 = 17.

Solution in Haskell (GHC 8.8.3) (judge verdict: Runtime Error)

{-# OPTIONS_GHC -O2 #-}
import qualified Control.Arrow         as Arrow
import qualified Control.Monad         as Monad
import qualified Data.Bits             as Bits
import qualified Data.ByteString.Char8 as BSC8
import qualified Data.Char             as Char
import qualified Data.List             as List
import qualified Data.Vector.Unboxed   as VU
import qualified Data.Bool             as Bool

main :: IO ()
main = do
  (n, w) <- getAB
  xv <- VU.replicateM n getAB -- VU.Vector (重量, 価値)
  print $ solver xv (n, w)

data TreeF a
  = TreeF (Int, Int) (Maybe a)
instance Functor TreeF where
  fmap f (TreeF x Nothing)  = TreeF x Nothing
  fmap f (TreeF x (Just y)) = TreeF x (Just (f y))
solver :: VU.Vector (Int, Int) -> (Int, Int) -> Int
solver xv (n, w) = VU.foldr max (-1) $ dyna phi psi (n - 1) -- (n番目まで見たときの)価値表を作り、その要素の最大値を取り出す
  where
    psi 0 = TreeF (xv VU.! 0) Nothing -- 入力 VU.Vector (重量, 価値) に対して最初から順番に調べる
    psi i = TreeF (xv VU.! i) (Just (i - 1)) -- 一つ前までの結果を保持

    phi :: TreeF (CoFree TreeF (VU.Vector Int)) -> VU.Vector Int
    phi (TreeF (weight, value) Nothing)  = VU.generate (w + 1) (Bool.bool 0 value . (weight ==))
    phi (TreeF (weight, value) (Just t)) = VU.zipWith max prev vec
      where
        prev = extract t
        vec  = VU.replicate weight 0 VU.++ VU.map (+value) prev

-- * 入出力
getI :: BSC8.ByteString -> Maybe (Int, BSC8.ByteString)
getI = fmap (Arrow.second BSC8.tail) . BSC8.readInt
getAB :: IO (Int, Int)
getAB = (\vec -> (vec VU.! 0, vec VU.! 1)) . VU.unfoldrN 2 getI <$> BSC8.getLine
getABC :: IO (Int, Int, Int)
getABC = (\vec -> (vec VU.! 0, vec VU.! 1, vec VU.! 2)) . VU.unfoldrN 3 getI <$> BSC8.getLine
getAN :: Int -> IO (VU.Vector Int)
getAN n = VU.unfoldrN n getI <$> BSC8.getLine


-- ! 射
newtype Fix f
  = InF { outF :: f (Fix f) }

pair :: (a -> b, a -> c) -> a -> (b, c)
pair (f, g) x = (f x, g x)

-- ! 下方射
cata :: Functor f => (f a -> a) -> (Fix f -> a)
cata phi = phi . fmap (cata phi) . outF
-- ! 上方射
ana  :: Functor f => (b -> f b) -> (b -> Fix f)
ana psi = InF . fmap (ana psi) . psi
-- ! 双方射
hylo :: Functor f => (f b -> b) -> (a -> f a) -> (a -> b)
hylo phi psi = cata phi . ana psi

-- * 未来用データ(直和)
data Fx f a x
  = Fx { unFx :: Either a (f x) }
-- * 過去用データ（直積）
data Hx f a x
  = Hx { unHx :: (a, f x) }

-- * Fx f a は関手
instance Functor f => Functor (Fx f a) where
  fmap f (Fx (Left x))  = Fx (Left x)
  fmap f (Fx (Right x)) = Fx (Right (fmap f x))
-- * Hx f a は関手
instance Functor f => Functor (Hx f a) where
  fmap f (Hx (x, y)) = Hx (x, fmap f y)

-- * Fx f a の不動点
newtype Free f a
  = Free { unFree :: Fix (Fx f a) }
-- * Hx f a の不動点
newtype CoFree f a
  = CoFree { unCoFree :: Fix (Hx f a) }

-- * Free f は関手
instance Functor f => Functor (Free f) where
  fmap f = Free . cata (InF . phi) . unFree
    where
      phi (Fx (Left a))  = Fx (Left (f a))
      phi (Fx (Right b)) = Fx (Right b)
-- * CoFree f は関手
instance Functor f => Functor (CoFree f) where
  fmap f = CoFree . ana (psi . outF) . unCoFree
    where
      psi (Hx (a, x)) = Hx (f a, x)

-- * Free と CoFree の情報を出し入れする関数
extract :: Functor f => CoFree f t -> t
extract cf = case outF (unCoFree cf) of
  Hx (a, _) -> a

sub :: Functor f => CoFree f a -> f (CoFree f a)
sub cf = case outF (unCoFree cf) of
  Hx (_, b) -> fmap CoFree b

inject :: Functor f => a -> Free f a
inject = Free
    . InF
    . Fx
    . Left

-- ! 時空射
chrono :: Functor f => (f (CoFree f b) -> b) -> (a -> f (Free f a)) -> (a -> b)
chrono phi psi = extract . hylo phi' psi' . inject
  where
    phi' = CoFree
      . InF
      . fmap unCoFree
      . Hx
      . pair (phi, id)
    psi' = uncurry either (psi, id)
      . unFx
      . fmap Free
      . outF
      . unFree

-- ! dp射
dyna :: Functor f => (f (CoFree f b) -> b) -> (a -> f a) -> (a -> b)
dyna phi psi = chrono phi (fmap inject . psi)