Scanf.scanf "%d %d" (fun n k -> let a = Array.init n (fun _ -> Scanf.scanf " %d" (fun a -> a)) in let module S = Set.Make (struct type t = int let compare = compare end) in let rec loop i set = if i = n then S.max_elt set else let set = S.fold (fun v set -> if v + a.(i) <= k then S.add (v + a.(i)) set else set) set set in loop (i + 1) set in loop 0 (S.singleton 0) |> Printf.printf "%d\n" )