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(map(int, input().split()))
  A.sort()
  B = [[] for i in range(0, 10)]
  for i in range(0, M):
      if 1 in A:
          B[2].append(1)
      if 2 in A:
          B[5].append(2)
      if 3 in A:
          B[5].append(3)
      if 4 in A:
          B[4].append(4)
      if 5 in A:
          B[5].append(5)
      if 6 in A:
          B[6].append(6)
      if 7 in A:
          B[3].append(7)
      if 8 in A:
          B[7].append(8)
      if 9 in A:
          B[6].append(9)
  
  
  dp = ["n" for i in range(0, N + 1)]
  if 1 in A:
      dp[2] = 1
  if 2 in A:
      dp[5] = 2
  if 3 in A:
      dp[5] = 3
  if 4 in A:
      dp[4] = 4
  if 5 in A:
      dp[5] = 5
  if 6 in A:
      dp[6] = 6
  if 7 in A:
      dp[3] = 7
  if 8 in A:
      dp[7] = 8
  if 9 in A:
      dp[6] = 9
  for i in range(4, N + 1):
      if i == 4:
          if B[2] != []:
              dp[4] = 11
      if i == 5:
          if B[3] != [] and dp[2] != "n":
              dp[5] = 71
      if i == 6:
          a = 0
          b = 0
          c = 0
          if B[2] != [] and dp[4] != "n":
              a = 1 * 10 ** len(str(dp[4])) + dp[4]
          if B[3] != []:
              b = 77
          if B[4] != [] and dp[2] != "n":
              c = 41
          if dp[6] == "n":
              x = max(a, b, c)
              if x != 0:
                  dp[6] = x
          else:
              dp[6] = max(a, b, c, dp[6])
      if i == 7:
          a = 0
          b = 0
          c = 0
          d = 0
          if B[2] != [] and dp[5] != "n":
              a = 1 * 10 ** len(str(dp[5])) + dp[5]
          if B[3] != [] and dp[4] != "n":
              b = 7 * 10 ** len(str(dp[4])) + dp[4]
          if B[4] != [] and dp[3] != "n":
              c = 47
          if B[5] != [] and dp[2] != "n":
              d = max(B[5]) * 10 + 1
          if dp[7] == "n":
              x = max(a, b, c, d)
              if x != 0:
                  dp[7] = x
          else:
              dp[7] = max(dp[7], a, b, c, d)
      if i > 7:
          a = 0
          b = 0
          c = 0
          d = 0
          e = 0
          f = 0
          if B[2] != [] and dp[i - 2] != "n":
              a = 1 * 10 ** (len(str(dp[i - 2]))) + dp[i - 2]
          if B[3] != [] and dp[i - 3] != "n":
              b = 7 * 10 ** (len(str(dp[i - 3]))) + dp[i - 3]
          if B[4] != [] and dp[i - 4] != "n":
              c = 4 * 10 ** (len(str(dp[i - 4]))) + dp[i - 4]
          if B[5] != [] and dp[i - 5] != "n":
              d = max(B[5]) * 10 ** (len(str(dp[i - 5]))) + dp[i - 5]
          if B[6] != [] and dp[i - 6] != "n":
              e = max(B[6]) * 10 ** (len(str(dp[i - 6]))) + dp[i - 6]
          if B[7] != [] and dp[i - 7] != "n":
              f = 8 * 10 ** (len(str(dp[i - 7]))) + dp[i - 7]
          x = max(a, b, c, d, e, f)
          if x == 0:
              dp[i] = "n"
          else:
              dp[i] = x
  print(dp[N])
  
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"}]