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 ds = Array.make (n + 1) 0 in
  let dp = Array.make (n + 1) [] in
  Array.iter (fun a ->
    ds.(cost (a - 1)) <- 1;
    dp.(cost (a - 1)) <- [a]) as_;
  for i = 0 to n do
    Array.iter (fun a ->
      if
        0 < ds.(i) &&
        i + cost (a - 1) <= n &&
        (ds.(i + cost (a - 1)), dp.(i + cost (a - 1))) < (1 + ds.(i), a :: dp.(i))
      then begin
        ds.(i + cost (a - 1)) <- 1 + ds.(i);
        dp.(i + cost (a - 1)) <- a :: dp.(i)
      end) as_
  done;
  List.iter print_int dp.(n);
  print_newline ()
