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 = map(int, input().split())
  A = list(input().split())
  if "1" in A:
      if N % 2 == 0:
          print("1" * (N // 2))
      else:
          if "7" in A:
              print("7" + "1" * (N // 2 - 1))
          else:
              print("1" * (N // 2))
  else:
      if "7" in A:
          if N % 3 == 0:
              print("7" * (N // 3))
          elif N % 3 == 1:
              if "4" in A:
                  print("7" * (N // 3 - 1) + "4")
              elif "8" in A:
                  print("8" + "7" * (N // 3 - 2))
          elif N % 3 == 2:
              if "5" in A:
                  print("7" * (N // 3 - 1) + "5")
              elif "3" in A:
                  print("7" * (N // 3 - 1) + "3")
              elif "2" in A:
                  print("7" * (N // 3 - 1) + "2")
      elif "4" in A:
          if N % 3 == 3:
              if N >= 11 and "9" in A and "5" in A:
                  print("95" + "4" * (N // 4 - 2))
              elif N >= 11 and "9" in A and "3" in A:
                  print("9" + "4" * (N // 4 - 2) + "3")
              elif N >= 11 and "9" in A and "2" in A:
                  print("9" + "4" * (N // 4 - 2) + "2")
              elif "8" in A:
                  print("8" + "4" * (N // 4 - 1))
              elif N >= 11 and "6" in A and "5" in A:
                  print("65" + "4" * (N // 4 - 2))
              elif N >= 11 and "6" in A and "3" in A:
                  print("6" + "4" * (N // 4 - 2) + "3")
              elif N >= 11 and "6" in A and "2" in A:
                  print("6" + "4" * (N // 4 - 2) + "2")
          elif N % 4 == 2 and "9" in A:
              print("9" + "4" * (N // 4 - 1))
          elif N % 4 == 2 and "6" in A:
              print("6" + "4" * (N // 4 - 1))
          elif N % 4 != 0 and "5" in A:
              print("5" * min(N % 4, N // 5) + "4" * (N // 4 - min(N % 4, N // 5)))
          elif N % 4 != 0 and "3" in A:
              print("4" * (N // 4 - min(N % 4, N // 5)) + "3" * min(N % 4, N // 5))
          elif N % 4 != 0 and "5" in A:
              print("4" * (N // 4 - min(N % 4, N // 5)) + "2" * min(N % 4, N // 5))
          else:
              print("4" * (N // 4))
      elif "5" in A:
          if N % 5 != 0 and "9" in A:
              print("9" * min(N % 5, N // 6) + "5" * (N // 5 - min(N % 5, N // 6)))
          elif N % 5 == 2 and "8" in A:
              print("8" + "5" * (N // 5 - 1))
          elif N % 5 != 0 and "6" in A:
              print("6" * min(N % 5, N // 6) + "5" * (N // 5 - min(N % 5, N // 6)))
          else:
              print("5" * (N // 5))
      elif "3" in A:
          if N % 5 != 0 and "9" in A:
              print("9" * min(N % 5, N // 6) + "3" * (N // 5 - min(N % 5, N // 6)))
          elif N % 5 == 2 and "8" in A:
              print("8" + "3" * (N // 5 - 1))
          elif N % 5 != 0 and "6" in A:
              print("6" * min(N % 5, N // 6) + "3" * (N // 5 - min(N % 5, N // 6)))
          else:
              print("3" * (N // 5))
      elif "2" in A:
          if N % 5 != 0 and "9" in A:
              print("9" * min(N % 5, N // 6) + "2" * (N // 5 - min(N % 5, N // 6)))
          elif N % 5 == 2 and "8" in A:
              print("8" + "2" * (N // 5 - 1))
          elif N % 5 != 0 and "6" in A:
              print("6" * min(N % 5, N // 6) + "2" * (N // 5 - min(N % 5, N // 6)))
          else:
              print("2" * (N // 5))
      elif "9" in A:
          print("9" * (N // 6))
      elif "6" in A:
          if N % 6 != 0 and "8" in A:
              print("8" * min(N % 6, N // 7) + "6" * (N // 6 - min(N % 6, N // 7)))
          else:
              print("6" * (N // 6))
      else:
          if "8" in A:
              print("8" * (N // 7))
  
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"}]