Problem:
Score : 400 points

Problem Statement

Find the largest integer that can be formed with exactly N matchsticks, under the following conditions:

Every digit in the integer must be one of the digits A_1, A_2, ..., A_M (1 \leq A_i \leq 9).

The number of matchsticks used to form digits 1, 2, 3, 4, 5, 6, 7, 8, 9 should be 2, 5, 5, 4, 5, 6, 3, 7, 6, respectively.

Constraints

All values in input are integers.

2 \leq N \leq 10^4

1 \leq M \leq 9

1 \leq A_i \leq 9

A_i are all different.

There exists an integer that can be formed by exactly N matchsticks under the conditions.

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 ... A_M

Output

Print the largest integer that can be formed with exactly N matchsticks under the conditions in the problem statement.

Sample Input 1

20 4
3 7 8 4

Sample Output 1

777773

The integer 777773 can be formed with 3 + 3 + 3 + 3 + 3 + 5 = 20 matchsticks, and this is the largest integer that can be formed by 20 matchsticks under the conditions.

Sample Input 2

101 9
9 8 7 6 5 4 3 2 1

Sample Output 2

71111111111111111111111111111111111111111111111111

The output may not fit into a 64-bit integer type.

Sample Input 3

15 3
5 4 6

Sample Output 3

654

Solution in LuaJIT (2.0.4) (judge verdict: Accepted)

local n, m = io.read("*n", "*n")
local a = {}
for i = 1, 9 do a[i] = false end
local num_to_line = {2, 5, 5, 4, 5, 6, 3, 7, 6}
local topval = {0, 0, 0, 0, 0, 0, 0}
for i = 1, m do
  local tmp = io.read("*n")
  a[tmp] = true
  local line = num_to_line[tmp]
  topval[line] = math.max(topval[line], tmp)
end
local r = {}
local ins = table.insert
if a[1] then
  if n % 2 == 1 then
    local tmp = {7, 5, 3, 2, 8}
    for i = 1, 5 do
      if a[tmp[i]] then
        ins(r, tmp[i])
        n = n - num_to_line[tmp[i]]
        break
      end
    end
  end
  for i = 1, n / 2 do ins(r, 1) end
elseif a[7] then
  if n % 3 == 1 then
    if a[4] then ins(r, 4) n = n - 4
    elseif a[8] then ins(r, 8) n = n - 7
    else ins(r, topval[5]) ins(r, topval[5]) n = n - 10
    end
  elseif n % 3 == 2 then
    if 0 < topval[5] then ins(r, topval[5]) n = n - 5
    elseif a[4] then ins(r, 4) ins(r, 4) n = n - 8
    else ins(r, 8) ins(r, 8) n = n - 14
    end
  end
  for i = 1, n / 3 do ins(r, 7) end
elseif a[4] then
  if n % 4 == 1 then
    if 0 < topval[5] then ins(r, topval[5]) n = n - 5
    elseif 0 < topval[6] then ins(r, topval[6]) ins(r, 8) n = n - 13
    else ins(r, 8) ins(r, 8) ins(r, 8) n = n - 21
    end
  elseif n % 4 == 2 then
    if 0 < topval[6] then ins(r, topval[6]) n = n - 6
    elseif 0 < topval[5] then ins(r, topval[5]) ins(r, topval[5]) n = n - 10
    else ins(r, 8) ins(r, 8) n = n - 14
    end
  elseif n % 4 == 3 then
    if a[9] and 0 < topval[5] then ins(r, 9) ins(r, topval[5]) n = n - 11
    elseif a[8] then ins(r, 8) n = n - 7
    elseif a[6] then ins(r, 6) ins(r, topval[5]) n = n - 11
    else ins(r, topval[5]) ins(r, topval[5]) ins(r, topval[5]) n = n - 15
    end
  end
  for i = 1, n / 4 do ins(r, 4) end
elseif 0 < topval[5] then
  if n % 5 == 1 then
    if 0 < topval[6] then ins(r, topval[6]) n = n - 6
    else ins(r, 8) ins(r, 8) ins(r, 8) n = n - 21
    end
  elseif n % 5 == 2 then
    if a[9] then ins(r, 9) ins(r, 9) n = n - 12
    elseif a[8] then ins(r, 8) n = n - 7
    else ins(r, 6) ins(r, 6) n = n - 12
    end
  elseif n % 5 == 3 then
    if 0 < topval[6] and a[8] then ins(r, topval[6]) ins(r, 8) n = n - 13
    elseif 0 < topval[6] then ins(r, topval[6]) ins(r, topval[6]) ins(r, topval[6]) n = n - 18
    else ins(r, 8) ins(r, 8) ins(r, 8) ins(r, 8) n = n - 28
    end
  elseif n % 5 == 4 then
    if a[9] then
      for i = 1, 4 do ins(r, 9) end
      n = n - 24
    elseif a[8] then ins(r, 8) ins(r, 8) n = n - 14
    else
      for i = 1, 4 do ins(r, 6) end
      n = n - 24
    end
  end
  for i = 1, n / 5 do ins(r, topval[5]) end
elseif 0 < topval[6] then
  for i = 1, n % 6 do ins(r, 8) end
  n = n - 7 * (n % 6)
  for i = 1, n / 6 do ins(r, topval[6]) end
else
  for i = 1, n / 7 do ins(r, 8) end
end
table.sort(r, function(a, b) return a > b end)
print(table.concat(r, ""))