let rec solve (ws, vs) weight =
    match (ws, vs) with
    | (w :: ws', v :: vs') ->
        if weight >= w
        then max (solve (ws', vs') weight) (v + solve (ws', vs') (weight - w))
        else solve (ws', vs') weight
    | _ -> 0

let () =
    Scanf.scanf "%d %d\n" @@ fun n w ->
        let rec loop n (ws, vs) =
            if n = 0 then (ws, vs)
            else Scanf.scanf "%d %d\n" @@ fun w v ->
                loop (n - 1) (w :: ws, v :: vs)
        in
        let ws, vs = loop n ([], []) in
        Printf.printf "%d\n" @@ solve (ws, vs) w
