output_description: Print the largest integer that can be formed with exactly N matchsticks under
  the conditions in the problem statement.
  
  * * *
input_description: Input is given from Standard Input in the following format:
  
      
      
      N M
      A_1 A_2 ... A_M
attempt: from itertools import product
  
  e = [10, 2, 5, 5, 4, 5, 6, 3, 7, 6]
  n, m = map(int, input().split())
  a = sorted(map(int, input().split()))
  d = {}
  for i in a:
      if e[i] not in d:
          d[e[i]] = i
      elif d[e[i]] < i:
          d[e[i]] = i
  
  dd = sorted(
      [[i[0] + j[0], i[1] * 10 + j[1]] for i, j in product(d.items(), d.items())]
      + [[k, v] for k, v in d.items()]
  )
  d = {}
  for k, v in dd:
      if k not in d:
          d[k] = v
      elif d[k] < v:
          d[k] = v
  dd = [[k, v] for k, v in sorted(d.items())]
  # [print(k,v) for k,v in dd]
  # print()
  if 2 in d:
      kk = 2
      vv = 1
  elif 3 in d:
      kk = 3
      vv = 7
  else:
      kk = 10000
      vv = 0
      for match_num, num in dd:
          if kk >= match_num and vv < num:
              kk = match_num
              vv = num
          elif kk * 2 > match_num and vv * len(str(vv)) + vv < num:
              kk = match_num
              vv = num
  k = kk * 3
  ans = []
  while n - kk >= k:
      ans.append(vv)
      n -= kk
  
  vvv = 0
  uu = 0
  u = 0
  # print(ans, n)
  for i, j, k in product(dd, dd, dd):
      k_len = len(str(k[1]))
      j_len = len(str(j[1]))
      # if i[0] + j[0] + k[0] == n:
      #    print(i,j,k, i[1] * 10**(j_len+k_len) + j[1] * 10**k_len + k[1])
      if (
          i[0] + j[0] + k[0] == n
          and i[1] * 10 ** (j_len + k_len) + j[1] * 10**k_len + k[1] > vvv
      ):
          vvv = i[1] * 10 ** (j_len + k_len) + j[1] * 10**k_len + k[1]
          uu = i[1] * 10 ** (j_len) + j[1]
          u = k[1]
      if i[0] + j[0] == n and i[1] * 10 ** (j_len) + j[1] > vvv:
          vvv = i[1] * 10 ** (j_len) + j[1]
          uu = i[1]
          u = j[1]
      if i[0] == n and i[1] > vvv:
          vvv = i[1]
          uu = i[1]
  if u == 0:
      print(uu)
  else:
      tmp = "".join(map(str, ans)) if ans else ""
      print(str(uu) + tmp + str(u))
      # print(vv,uu,u)
  
problem_description: 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.
samples: [{"input": "20 4\n    3 7 8 4", "output": "777773\n    \n\nThe integer 777773 can be formed with 3 + 3 + 3 + 3 + 3 + 5 = 20 matchsticks,\nand this is the largest integer that can be formed by 20 matchsticks under the\nconditions.\n\n* * *"}, {"input": "101 9\n    9 8 7 6 5 4 3 2 1", "output": "71111111111111111111111111111111111111111111111111\n    \n\nThe output may not fit into a 64-bit integer type.\n\n* * *"}, {"input": "15 3\n    5 4 6", "output": "654"}]