let cost = Array.get [| 2; 5; 5; 4; 5; 6; 3; 7; 6 |]

let () = Scanf.scanf "%d %d\n" @@ fun n m ->
  let as_ = Array.init m @@ fun m -> Scanf.scanf "%d " @@ fun a -> a in
  let dp = Array.make (n + 1) (0, []) in
  Array.iter (fun a ->
    dp.(cost (a - 1)) <- max dp.(cost (a - 1)) (1, [a])) as_;
  for i = 0 to n do
    Array.iter (fun a ->
      if 0 < fst dp.(i) && i + cost (a - 1) <= n then
        dp.(i + cost (a - 1)) <-
          max dp.(i + cost (a - 1)) @@ (fst dp.(i) + 1, a :: snd dp.(i))) as_;
  done;
  List.iter print_int (snd dp.(n));
  print_newline ()
