Problem: 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.

Submission attempts (in order):
import math

n,m = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]

set = [[1,2],[7,3],[4,4],[5,5],[3,5],[2,5],[9,6],[6,6],[8,7]]

if 3 in a:
  if 5 in a:
    a.remove(3)
    m -= 1
  a.remove(2)
  m -= 1

if 9 in a:
  a.remove(6)
  m -= 1
  
exist = [i for i in set if i[0] in a]

num = 0
keta = 0
for i in exist:
  if i[0] > num and n//i[1] >= keta:
    num = i[0]
    honsu = i[1]
    keta = n//honsu
    amari = n%honsu
    break

for l in range(keta-1):
  exist_tmp = [i for i in exist if i[0] != num]
  bit = math.ceil(math.log2(m-1))
  for i in range(1,keta+1):
    kouho = []
    for j in range((2**bit)**i):
      nokori = amari + honsu * i
      change = []
      for k in range(i):
        tmp = (j >> k*bit) & (2**bit-1)
        if tmp >= m-1:
          continue
        nokori -= exist_tmp[tmp][1]
        change.append(exist_tmp[tmp][0])
        if nokori < 0:
          break
      if nokori == 0:
        kouho.append(change)
    if len(kouho) != 0:
      break
  if len(kouho) != 0:
    break
  else:
    keta -= 1
    num = 0
    for i in exist:
      if i[0] > num and n//i[1] >= keta:
        num = i[0]
        honsu = i[1]
        amari = n%honsu
        break

kouho = [sorted(i,reverse=True) for i in kouho]
kouho_tmp = []
for i in kouho:
  if i not in kouho_tmp:
    kouho_tmp.append(i)
kouho = kouho_tmp
kouho.sort(reverse=True)

if len(kouho) > 1:
  for i in range(len(kouho[0])):
    max_num = 0
    for j in range(len(kouho)):
      if kouho[j][i] > max_num:
        max_num = kouho[j][i]
    kouho = [k for k in kouho if k[i] == max_num]
    if len(kouho) == 1:
      break
      
ans = [num]*(keta-len(kouho[0])) + kouho[0]
ans.sort(reverse=True)

ans_txt = ''
for i in ans:
  ans_txt += str(i)
print(ans_txt)

import math

n,m = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]


set = [[1,2],[7,3],[4,4],[5,5],[3,5],[2,5],[9,6],[6,6],[8,7]]


if 2 in a:
  if 3 in a or 5 in a:
    a.remove(2)
    m -= 1

if 3 in a:
  if 5 in a:
    a.remove(3)
    m -= 1
  
if 6 in a:
  if 9 in a:
    a.remove(6)
    m -= 1


max_a = max(a)


exist_set = [i for i in set if i[0] in a]

ans_kouho = []

keta = 0
num = 0
for i in exist_set:
  keta_tmp = n//i[1]
  num_tmp = i[0]
  amari_tmp = n % i[1]
  
  if amari_tmp != 0 and num_tmp == max_a:
    continue
  if keta_tmp >= keta and num_tmp > num:
    keta = keta_tmp
    num = num_tmp
    amari = amari_tmp
    honsu = i[1]

    
    if amari == 0:
      ans_kouho.append(int(str(num)*keta))
      continue
      
    

    
    exist_yet = [i for i in exist_set if i[0] != num]
    m -= 1

    
    for i in range(1,honsu):

      
      bit = math.ceil(math.log2(m))
      kouho = []
      for j in range((2**m)*i):
        change = []
        nokori = amari + honsu * i
        for k in range(i):
          bit_index =  (j >> k*bit) & (2**m-1)

          
          if bit_index >= m:
            break

          
          change.append(exist_yet[bit_index][0])
          nokori -= exist_yet[bit_index][1]

          if nokori < 0:
            break

        
        if nokori == 0:
          kouho.append(change)
      
      
      if len(kouho) != 0:
        break

    if len(kouho) == 0:
      continue
 
    

    
    kouho = [sorted(i,reverse=True) for i in kouho]

    
    kouho_tmp = []
    for i in kouho:
      if i not in kouho_tmp:
        kouho_tmp.append(i)
    kouho = kouho_tmp

    
    for i in range(len(kouho[0])):
      if len(kouho) == 1:
        break
      max_i = max([j[i] for j in kouho])
      kouho = [j for j in kouho if j[i] == max_i]
    kouho = kouho[0]

    ans = [num]*(keta-len(kouho)) + kouho
    ans.sort(reverse=True)
    ans_str = ''
    for i in ans:
      ans_str += str(i)
    ans_kouho.append(int(ans_str))

ans_kouho.sort()
print(ans_kouho)
print(ans_kouho[-1])

import math

n,m = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]

set = [[1,2],[7,3],[4,4],[5,5],[3,5],[2,5],[9,6],[6,6],[8,7]]

if 3 in a:
  if 5 in a:
    a.remove(3)
  a.remove(2)

if 9 in a:
  a.remove(6)
  
exist = [i for i in set if i[0] in a]

num = 0
keta = 0
for i in exist:
  if i[0] > num and n//i[1] >= keta:
    num = i[0]
    honsu = i[1]
    keta = n//honsu
    amari = n%honsu
    break

for l in range(keta-1):
  bit = math.ceil(math.log2(m-1))
  exist_tmp = [i for i in exist if i[0] != num]
  for i in range(1,keta+1):
    kouho = []
    for j in range((2**bit)**i):
      nokori = amari + honsu * i
      change = []
      for k in range(i):
        tmp = (j >> k*bit) & (2**bit-1)
        if tmp >= m:
          continue
        nokori -= exist_tmp[tmp][1]
        change.append(exist_tmp[tmp][0])
        if nokori < 0:
          break
      if nokori == 0:
        kouho.append(change)
    if len(kouho) != 0:
      break
  if len(kouho) != 0:
    break
  else:
    keta -= 1
    num = 0
    for i in exist:
      if i[0] > num and n//i[1] >= keta:
        num = i[0]
        honsu = i[1]
        amari = n%honsu
        break

kouho = [sorted(i,reverse=True) for i in kouho]
kouho_tmp = []
for i in kouho:
  if i not in kouho_tmp:
    kouho_tmp.append(i)
kouho = kouho_tmp
kouho.sort(reverse=True)

if len(kouho) > 1:
  for i in range(len(kouho[0])):
    max_num = 0
    for j in range(len(kouho)):
      if kouho[j][i] > max_num:
        max_num = kouho[j][i]
    kouho = [k for k in kouho if k[i] == max_num]
    if len(kouho) == 1:
      break
      
ans = [num]*(keta-len(kouho[0])) + kouho[0]
ans.sort(reverse=True)

ans_txt = ''
for i in ans:
  ans_txt += str(i)
print(ans_txt)

import math

n,m = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]


set = [[1,2],[7,3],[4,4],[5,5],[3,5],[2,5],[9,6],[6,6],[8,7]]


if 2 in a:
  if 3 in a or 5 in a:
    a.remove(2)
    m -= 1

if 3 in a:
  if 5 in a:
    a.remove(3)
    m -= 1
  
if 6 in a:
  if 9 in a:
    a.remove(6)
    m -= 1


exist_set = [i for i in set if i[0] in a]


max_honsu = max([i[1] for i in exist_set])


m -= 1


max_keta = 0
ans_kouho = []

for exist_one in exist_set:
  keta_tmp = n//exist_one[1]
  num_tmp = exist_one[0]
  amari_tmp = n % exist_one[1]
  honsu_tmp = exist_one[1]
  
  ###print(keta_tmp,num_tmp)
  
  
  if amari_tmp != 0 and honsu_tmp == max_honsu:
    continue
  if keta_tmp >= max_keta:
    keta = keta_tmp
    num = num_tmp
    amari = amari_tmp
    honsu = honsu_tmp

    
    if amari == 0:
      ans_kouho.append(int(str(num)*keta))
      max_keta = keta
      continue
      
    

    
    exist_yet = [inc for inc in exist_set if inc[0] != num]

    
    for keta_num in range(1,min(max_honsu,keta+1)):
      
      kouho = []

      
      
      bit = math.ceil(math.log2(m+1))
      for all_bit in range((2**m)*keta_num):
        change = []
        nokori = amari + honsu * keta_num
        for bit_conf in range(keta_num):
          bit_index =  (all_bit >> bit_conf*bit) & (2**m-1)

          
          if bit_index > m:
            break
            
          
          if bit_index == m:
            change.append(0)
            continue

          
          change.append(exist_yet[bit_index][0])
          nokori -= exist_yet[bit_index][1]

          if nokori < 0:
            break
            
        if bit_index > m or nokori < 0:
          continue

        
        if nokori == 0:
          kouho.append(change)
      
      
      if len(kouho) != 0:

        
        del_len = len(kouho[0])

        
        kouho = [[inc_1 for inc_1 in inc_2 if inc_1 != 0] for inc_2 in kouho]

        
        max_len = max([len(inc) for inc in kouho])
        kouho = [inc for inc in kouho if len(inc) == max_len]
        
        max_keta = keta - del_len + max_len

        
        kouho = [sorted(kouho_one,reverse=True) for kouho_one in kouho]

        
        kouho_tmp = []
        for kouho_one in kouho:
          if kouho_one not in kouho_tmp:
            kouho_tmp.append(kouho_one)
        kouho = kouho_tmp

        
        if len(kouho) != 1:
          for kouho_one in range(len(kouho[0])):
            max_i = max([inc[kouho_one] for inc in kouho])
            kouho = [inc for inc in kouho if inc[kouho_one] == max_i]
        kouho = kouho[0]

        ans = [num]*(keta-del_len) + kouho
        ans.sort(reverse=True)
        ans_str = ''
        for ans_one in ans:
          ans_str += str(ans_one)
        ans_kouho.append(int(ans_str))

ans_kouho.sort()

print(ans_kouho[-1])


Verdicts: Runtime Error

Runtime Error

Runtime Error

Accepted