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: n, m = (int(i) for i in input().split())
  a = list(int(i) for i in input().split())
  use = []
  t = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
  for i in range(1, 9):
      if i in a:
          use.append([i, t[i]])
  use = sorted(use, key=lambda x: x[1])
  query = ""
  kaisuu = n // use[0][1]
  amari = int(n % use[0][1])
  # 答えのベースを作る
  judge = []
  for i in range(kaisuu):
      query += str(use[0][0])
  if amari == 0:
      judge = [int(query)]
  # あまりに応じて最後尾を変えるか最初を変えるか
  else:
      # 先頭を変えた後のあまりで後ろを変える
      for j, k in enumerate(use):
          tmp2 = query
          amari = int(n % use[0][1])
          if int(k[0]) > int(use[0][0]) and int(k[1]) <= amari + int(use[0][1]):
              tmp2 = str(k[0]) + query[1::]
              amari -= int(k[1])
              amari += int(use[0][1])
              if amari == 0:
                  judge.append(int(tmp2))
          # 後ろ側をあまりが０になるように調整する
          if amari > 0:
              for j, k in enumerate(use):
                  if int(k[1]) == amari + int(use[0][1]):
                      tmp2 = tmp2[:-1] + str(k[0])
                      judge.append(int(tmp2))
  print(max(judge))
  
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"}]