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 sys
  from bisect import bisect_left
  
  INF = 1 << 30
  
  
  def main():
      N, M = map(int, input().split())
      A = set(map(str, input().split()))
      number = "174532968"
      necesary_match = (2, 3, 4, 5, 5, 5, 6, 6, 7)
      match = {i: j for i, j in zip(number, necesary_match)}
  
      if "1" in A:
          if N % 2 == 0:
              ans = ["1"] * (N // 2)
          else:
              for num in "75328":
                  if num in A:
                      ans = ["1"] * ((N - match[num]) // 2) + [num]
                      break
      elif "7" in A:
          if N % 3 == 0:
              ans = ["7"] * (N // 3)
          elif N % 3 == 1:
              flag = False
              for num in "48":
                  if num in A:
                      flag = True
                      ans = ["7"] * ((N - match[num]) // 3) + [num]
                      break
              if not flag:
                  for num in "532":
                      if num in A:
                          ans = ["7"] * ((N - 2 * match[num]) // 3) + [num] * 2
                          break
          else:
              flag = False
              for num in "532":
                  if num in A:
                      flag = True
                      ans = ["7"] * ((N - match[num]) // 3) + [num]
                      break
              if not flag:
                  for num in "48":
                      if num in A:
                          ans = ["7"] * ((N - 2 * match[num]) // 3) + [num] * 2
                          break
      elif "4" in A:
          if N % 4 == 0:
              ans = ["4"] * (N // 4)
          elif N % 4 == 1:
              flag = False
              for num in "532":
                  if num in A:
                      flag = True
                      ans = ["4"] * ((N - match[num]) // 4) + [num]
                      break
              if not flag:
                  for num in "96":
                      if num in A:
                          flag = True
                          ans = ["4"] * ((N - match[num] - match["8"]) // 4) + [num, "8"]
                          break
              if not flag:
                  ans = ["4"] * ((N - match["8"] * 3) // 4) + ["8"] * 3
          elif N % 4 == 2:
              flag = False
              for num in "96":
                  if num in A:
                      ans = ["4"] * ((N - match[num]) // 4) + [num]
                      flag = True
                      break
              if not flag:
                  for num in "532":
                      if num in A:
                          ans = ["4"] * ((N - 2 * match[num]) // 4) + [num] * 2
                          flag = True
                          break
              if not flag:
                  ans = ["4"] * ((N - 2 * match["8"]) // 4) + ["8"] * 2
          else:
              flag = False
              if "8" in A:
                  flag = True
                  ans = ["4"] * ((N - match["8"]) // 4) + ["8"]
              if not flag:
                  x = ""
                  for num in "96":
                      if num in A:
                          x = num
                          break
                  y = ""
                  for num in "532":
                      if num in A:
                          y = num
                          break
                  if x:
                      ans = ["4"] * ((N - match[x] - match[y]) // 4) + [x, y]
                  else:
                      ans = ["4"] * ((N - 3 * match[y]) // 4) + [y] * 3
      else:
          x = ""
          for num in "532":
              if num in A:
                  x = num
                  break
          if x:
              if N % 5 == 0:
                  ans = [x] * (N // 5)
              elif N & 5 == 1:
                  flag = False
                  for num in "96":
                      if num in A:
                          flag = True
                          ans = [x] * ((N - match[num]) // 5) + [num]
                          break
                  if not flag:
                      ans = [x] * ((N - 3 * match["8"]) // 5) + ["8"] * 3
              elif N % 5 == 2:
                  if "8" in A:
                      ans = [x] * ((N - match["8"]) // 5) + ["8"]
                  else:
                      for num in "96":
                          if num in A:
                              ans = [x] * ((N - 2 * match[num]) // 5) + [num] * 2
                              break
              elif N % 5 == 3:
                  if "8" in A:
                      flag = False
                      for num in "96":
                          if num in A:
                              flag = True
                              ans = [x] * ((N - match[num] - match["8"]) // 5) + [
                                  "8",
                                  num,
                              ]
                              break
                      if not flag:
                          ans = [x] * ((N - 4 * match["8"]) // 5) + ["8"] * 4
              elif N % 5 == 4:
                  if "8" in A:
                      ans = [x] * ((N - 2 * match["8"]) // 5) + ["8"] * 2
                  else:
                      for num in "96":
                          if num in A:
                              ans = [x] * ((N - 4 * match[num]) // 5) + [num] * 4
                              break
  
          else:
              y = ""
              for num in "96":
                  if num in A:
                      y = num
                      break
              if y:
                  if N % 6 == 0:
                      ans = [y] * (N // 6)
                  else:
                      k = N % 6
                      ans = [y] * ((N - k * match["8"]) // 6) + ["8"] * k
              else:
                  ans = ["8"] * (N // 7)
      ans.sort(reverse=True)
      print("".join(ans))
  
  
  if __name__ == "__main__":
      main()
  
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"}]