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: import queue
  from collections import defaultdict
  import copy
  
  N, M = map(int, input().split())
  A = list(map(int, input().split()))
  ma = [2, 5, 5, 4, 5, 6, 3, 7, 6]
  BA = [[0, 0] for _ in range(M)]  # (必要本数，-1*数字)
  for i in range(M):
      BA[i][1] = -1 * A[i]
      BA[i][0] = ma[A[i] - 1]
  
  BA.sort()
  
  # 同じ本数使うものを消去
  can = []
  ii = 0
  while ii < len(BA):
      if BA[ii][0] in can:
          BA.pop(ii)
      else:
          can.append(BA[ii][0])
          ii += 1
  M = len(BA)  # Mの書き換え
  
  diff = [0] * M  # 数字変更の際に追加で必要な本数
  for i in range(M):
      diff[i] = BA[i][0] - BA[0][0]
      BA[i][1] = BA[i][1] * -1  # (必要本数，数字)に直しておく
  
  fi = BA[0]  ###
  # BA.sort(key=lambda x: x[1]*-1)#数字の大きいもの順
  
  
  p = [1] * (N + 1)
  for i in range(N):
      p[i + 1] = p[i] * 10
  
  
  # 数字のセットL(sort済み)から作れる最大の数値を出力
  def set_to_num(L):
      ans = 0
      for i in range(len(L)):
          ans += L[i] * p[i]
      return ans
  
  
  # 2つの配列を合わせる
  def make_set(temp, L):
      ori = copy.deepcopy(temp)
      for i in range(len(L)):
          ori[i] = L[i]
      ori.sort()
      return ori
  
  
  # dd用hash
  def make_h(L):
      h = 0
      for i in range(len(L)):
          h += p[i] * L[i]
      return h
  
  
  ans = -1
  Max_dig = N // fi[0]
  
  for d in range(Max_dig):  # 減らす桁数
      dig = Max_dig - d
      rem = N - fi[0] * dig
      temp = [fi[1]] * dig
      dd = defaultdict(int)
      q = queue.Queue()
      q.put((rem, []))  # 残り本数，入れ替える数字
  
      while not q.empty():
          rem2, L = q.get()
          if rem2 == 0:
              # print(temp,L)
              LLL = make_set(temp, L)
              # print(LLL)
              ans = max(ans, set_to_num(LLL))
          else:
              for i in range(1, M):  # 変更候補の数字を見ていく.0番は自身のためのぞく
                  if rem2 >= diff[i]:
                      LL = copy.deepcopy(L)
                      LL.append(BA[i][1])
                      if len(LL) <= len(temp):
                          LL.sort()
                          h = make_h(LL)
                          if dd[h] == 0:
                              q.put((rem2 - diff[i], LL))
                              dd[h] = 1
      if ans != -1:
          break
  
  print(ans)
  
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"}]