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())
  arr = list(map(int, input().split()))
  dic = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
  cand = [[""] for _ in range(6)]
  for val in arr:
      cand[dic[val] - 2].append(str(val))
  pos = 6
  for i in range(5, -1, -1):
      cand[i] = sorted(cand[i], reverse=True)
      if len(cand[i]) > 1:
          pos = i
  ans = []
  for c1 in range(n // (pos + 2), max(-1, n // (pos + 2) - 11), -1):
      tmp = [[cand[pos][0], c1]]
      if pos == 5 and (pos + 2) * c1 == n:
          t = ""
          tmp = sorted(tmp, reverse=True, key=lambda x: x[0])
          for char, val in tmp:
              t += char * val
          ans.append(t)
      for c2 in range(11):
          if pos >= 5:
              continue
          tmp2 = tmp + [[cand[pos + 1][0], c2]]
          if pos == 4 and (pos + 2) * c1 + (pos + 2 + 1) * c2 == n:
              t = ""
              tmp2 = sorted(tmp2, reverse=True, key=lambda x: x[0])
              for char, val in tmp2:
                  t += char * val
              ans.append(t)
          for c3 in range(11):
              if pos >= 4:
                  continue
              tmp3 = tmp2 + [[cand[pos + 2][0], c3]]
              if (
                  pos == 3
                  and (pos + 2) * c1 + (pos + 2 + 1) * c2 + (pos + 2 + 2) * c3 == n
              ):
                  t = ""
                  tmp3 = sorted(tmp3, reverse=True, key=lambda x: x[0])
                  for char, val in tmp3:
                      t += char * val
                  ans.append(t)
              for c4 in range(11):
                  if pos >= 3:
                      continue
                  tmp4 = tmp3 + [[cand[pos + 3][0], c4]]
                  if (
                      pos == 2
                      and (pos + 2) * c1
                      + (pos + 2 + 1) * c2
                      + (pos + 2 + 2) * c3
                      + (pos + 2 + 3) * c4
                      == n
                  ):
                      t = ""
                      tmp4 = sorted(tmp4, reverse=True, key=lambda x: x[0])
                      for char, val in tmp4:
                          t += char * val
                      ans.append(t)
                  for c5 in range(11):
                      if pos >= 2:
                          continue
                      tmp5 = tmp4 + [[cand[pos + 4][0], c5]]
                      if (
                          pos == 1
                          and (pos + 2) * c1
                          + (pos + 2 + 1) * c2
                          + (pos + 2 + 2) * c3
                          + (pos + 2 + 3) * c4
                          + (pos + 2 + 4) * c5
                          == n
                      ):
                          t = ""
                          tmp5 = sorted(tmp5, reverse=True, key=lambda x: x[0])
                          for char, val in tmp5:
                              t += char * val
                          ans.append(t)
                      for c6 in range(11):
                          if pos >= 1:
                              continue
                          tmp6 = tmp5 + [[cand[pos + 5][0], c6]]
                          if (
                              pos == 0
                              and (pos + 2) * c1
                              + (pos + 2 + 1) * c2
                              + (pos + 2 + 2) * c3
                              + (pos + 2 + 3) * c4
                              + (pos + 2 + 4) * c5
                              + (pos + 2 + 5) * c6
                              == n
                          ):
                              t = ""
                              tmp6 = sorted(tmp6, reverse=True, key=lambda x: x[0])
                              for char, val in tmp6:
                                  t += char * val
                              ans.append(t)
  ans = sorted(ans, reverse=True, key=lambda x: len(x))
  print(ans[0])
  
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"}]