Problem:
Knapsack Problem

You have N kinds of items that you want to put them into a knapsack. Item i has value vi and weight wi.

You want to find a subset of items to put such that:

The total value of the items is as large as possible.

The items have combined weight at most W, that is capacity of the knapsack.

You can select as many items as possible into a knapsack for each kind.

Find the maximum total value of items in the knapsack.

Input

N W
v1 w1
v2 w2
:
vN wN

The first line consists of the integers N and W. In the following lines, the value and weight of the i-th item are given.

Output

Print the maximum total values of the items in a line.

Constraints

1 ≤ N ≤ 100

1 ≤ vi ≤ 1000

1 ≤ wi ≤ 1000

1 ≤ W ≤ 10000

Sample Input 1

4 8
4 2
5 2
2 1
8 3

Sample Output 1

21

Sample Input 2

2 20
5 9
4 10

Sample Output 2

10

Sample Input 3

3 9
2 1
3 1
5 2

Sample Output 3

27

Solution in Haskell (judge verdict: Time Limit Exceeded)

import Data.Array.Unboxed

naive_rec::Array Int (Int,Int)->Int->Int->Int
naive_rec _    0 _     = 0
naive_rec item i limit = let (v_i,w_i) = item!i in
                          maximum [ (naive_rec item (i-1) (limit - w_i*k)) + (v_i*k) | k <- [0..div limit w_i] ]


memo_ls::Array Int (Int,Int)->Int->Int->[UArray Int Int]
memo_ls item m limit = (listArray (0,limit) (replicate (limit+1) 0)):
                       [ listArray (0,limit) [ let (v_i,w_i) = item!i in
                                               maximum [ (prev!(l - j*w_i)) + (j*v_i)
                                                       | j <- [0..div l w_i]]
                                             | l <- [0..limit] ]
                       | (prev,i) <- zip (memo_ls item m limit) [1..m]]


--vwlist,item_idx_bound,limit,Ans
solver::[(Int,Int)]->Int->Int->Int
solver item_ls m limit = let item = listArray (1,m) item_ls in
                         (last (memo_ls item m limit))!limit

n_tp_get::Int->IO [(Int,Int)]
n_tp_get 0 = return []
n_tp_get n = do
  vw<-getLine
  let v:w:[]= map read (words vw)
  rest<-n_tp_get (n-1)
  return ((v,w):rest)

main::IO()
main=do
  ml<-getLine
  let m:l:[]=map read (words ml)
  dat<-n_tp_get m
  print (solver dat m l)