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: l = [[0, 0], [1, 2], [2, 5], [3, 5], [4, 4], [5, 5], [6, 6], [7, 3], [8, 7], [9, 6]]
  n, m = map(int, input().split())
  a = list(map(int, input().split()))
  l0 = []
  for i in range(m):
      l0.append(l[a[i]])
  l0.sort(key=lambda x: x[0], reverse=True)
  l0.sort(key=lambda x: x[1])
  p, q = l0[0]
  count = n - (n // q) * q
  c = 1
  ans = 0
  for i in range(n // q):
      ans += p * c
      c *= 10
  c = c // 10
  l0.sort(key=lambda x: x[0], reverse=True)
  i = 0
  while count > 0 and i < m:
      p1, q1 = l0[i]
      if q1 > q:
          q1 -= q
      # print(count)
      if p1 > p:
          while count >= q1:
              ans -= p * c
              ans += p1 * c
              count -= q1
              c = c // 10
          i += 1
      else:
          i += 1
  i = 0
  c = 1
  l0.sort(key=lambda x: x[1], reverse=True)
  while count > 0 and i < m:
      p1, q1 = l0[i]
      if q1 > q:
          q1 -= q
      if p1 < p:
          while count >= q1:
              ans -= p * c
              ans += p1 * c
              count -= q1
              c *= 10
          i += 1
      else:
          i += 1
  if count == 0:
      print(ans)
  else:
      x = n // q - 1
      for i in range(n // q - 1):
          count = n - (x) * q
          c = 1
          ans = 0
          for i in range(x):
              ans += p * c
              c *= 10
          c = c // 10
          l0.sort(key=lambda x: x[0], reverse=True)
          i = 0
          while count > 0 and i < m:
              p1, q1 = l0[i]
              if q1 > q:
                  q1 -= q
              # print(count)
              if p1 > p:
                  while count >= q1:
                      ans -= p * c
                      ans += p1 * c
                      count -= q1
                      c = c // 10
                  i += 1
              else:
                  i += 1
          i = 0
          c = 1
          l0.sort(key=lambda x: x[1], reverse=True)
          while count > 0 and i < m:
              p1, q1 = l0[i]
              if q1 > q:
                  q1 -= q
              if p1 < p:
                  while count >= q1:
                      ans -= p * c
                      ans += p1 * c
                      count -= q1
                      c *= 10
                  i += 1
              else:
                  i += 1
          if count == 0:
              print(ans)
              break
          else:
              x += 1
  
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"}]