Scanf.scanf "%d" (fun n ->
    let hp = Array.init n (fun _ -> Scanf.scanf " %d %d" (fun h p -> h, p)) in
    Array.sort (fun (h1, p1) (h2, p2) -> compare (h1 + p1, h1) (h2 + p2, h2)) hp;

    let rec loop i dp =
        if i = n then dp else (
            let h, p = hp.(i) in
            let dp2 = Array.copy dp in
            for j = 1 to n do
                if dp.(j - 1) <= h then dp2.(j) <- min dp.(j) (dp.(j - 1) + p)
            done;
            loop (i + 1) dp2
        )
    in
    let init = Array.make (n + 1) max_int in
    init.(0) <- 0;
    let dp = loop 0 init in

    let rec loop i =
        if i = n + 1 || dp.(i) = max_int then i - 1 else loop (i + 1)
    in
    loop 0 |> Printf.printf "%d\n"
)