module M = Map.Make (struct type t = int let compare = compare end) ;;

Scanf.scanf "%d %d" (fun n w ->
    let vw = Array.init n (fun _ -> Scanf.scanf " %d %d" (fun w v -> w, v)) in
    let map = Array.fold_left (fun map (w0, v0) ->
        M.fold (fun ww vv newmap ->
            let key = ww + w0 in
            if key <= w then (
                try
                    let v1 = M.find key newmap in
                    if v1 < vv + v0 then M.add key (vv + v0) newmap else newmap
                with _ ->
                    M.add key (vv + v0) newmap
            ) else newmap
        ) map map
    ) (M.add 0 0 M.empty) vw
    in
    M.fold (fun _ vv acc -> max vv acc) map 0 |> Printf.printf "%d\n"
)