Scanf.scanf "%d" (fun n -> let module M = struct type t = int * int array let max (la, a) (lb, b) = if la > lb then la, a else if la < lb then lb, b else (la, max a b) let inc digit (l, m) = l + 1, (m.(9 - digit) <- m.(9 - digit) + 1; m) let copy (l, m) = l, Array.copy m let zero = 0, Array.make 10 0 let none = -1, [||] let isnone (l, _) = l = -1 let show (_, m) = for i = 9 downto 0 do for j = 1 to m.(9 - i) do print_char "0123456789".[i] done done; print_newline () end in let dp = Array.make (n + 1) M.none in let hh = [| 6; 2; 5; 5; 4; 5; 6; 3; 7; 6 |] in dp.(0) <- M.zero; for i = 9 downto 0 do let h = hh.(i) in for j = 0 to n - h do if not (M.isnone dp.(j)) then ( let nn = M.copy dp.(j) in let nn = M.inc i nn in dp.(j + h) <- M.max dp.(j + h) nn ) done done; Array.fold_left M.max M.zero dp |> M.show )