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 numpy as np
  
  n, m = list(map(int, input().split()))
  a = list(map(int, input().split()))
  
  need_lis = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
  
  use_lis = [need_lis[i] for i in a]
  
  use_lis.sort()
  
  use_num_lis = []
  for i in use_lis:
      if i == 2:
          use_num_lis.append(1)
      if i == 3:
          use_num_lis.append(7)
      if i == 4:
          use_num_lis.append(4)
      if i == 5:
          if 5 in a:
              use_num_lis.append(5)
          elif 3 in a:
              use_num_lis.append(3)
          else:
              use_num_lis.append(2)
      if i == 6:
          if 9 in a:
              use_num_lis.append(9)
          else:
              use_num_lis.append(6)
      if i == 7:
          use_num_lis.append(8)
  
  ok_lis = []
  for i in range(use_lis[0] ** (len(use_lis) - 1)):
      ind_lis = []
      for j in range(len(use_lis) - 2, -1, -1):
          # print(j)
          ind_lis.append(i // (use_lis[0] ** j))
          i -= (i // (use_lis[0] ** j)) * (use_lis[0] ** j)
      # print(ind_lis)
  
      count = 0
      for i in range(len(use_lis) - 1):
          count += use_lis[i + 1] * ind_lis[i]
      # print(count)
      if (n - count) % use_lis[0] == 0:
          # print(n-count)
          rest = n - count
          ok_lis.append([int(rest / use_lis[0])] + ind_lis)
  
  res = []
  for i in ok_lis:
      res.append(sum(i))
  max_lis = [i for i in range(len(res)) if res[i] == max(res)]
  
  answers_lis = []
  for i in max_lis:
      answer = ""
      use = ok_lis[i]
      num_lis = use_num_lis
  
      for k in range(len(use)):
  
          ind = np.argmax(np.array(num_lis))
          # print(ind)
          if use[ind] > 0:
              count = 0
              for j in range(use[ind]):
                  # print(type(count))
                  count += (10**j) * num_lis[ind]
              count = str(count)
              # print(type(count))
              answer += count
          num_lis[ind] = 0
      answers_lis.append(answer)
  
      use_num_lis = []
      for i in use_lis:
          if i == 2:
              use_num_lis.append(1)
          if i == 3:
              use_num_lis.append(7)
          if i == 4:
              use_num_lis.append(4)
          if i == 5:
              if 5 in a:
                  use_num_lis.append(5)
              elif 3 in a:
                  use_num_lis.append(3)
              else:
                  use_num_lis.append(2)
          if i == 6:
              if 9 in a:
                  use_num_lis.append(9)
              else:
                  use_num_lis.append(6)
          if i == 7:
              use_num_lis.append(8)
  
  
  print(int(max(answers_lis)))
  
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"}]