Problem:


Score : 400 points

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

Constraints

All values in input are integers.

2 \leq N \leq 10^4

1 \leq M \leq 9

1 \leq A_i \leq 9

A_i are all different.

There exists an integer that can be formed by exactly N matchsticks under the conditions.

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 ... A_M

Output

Print the largest integer that can be formed with exactly N matchsticks under the conditions in the problem statement.

Sample Input 1

20 4
3 7 8 4

Sample Output 1

777773

The integer 777773 can be formed with 3 + 3 + 3 + 3 + 3 + 5 = 20 matchsticks, and this is the largest integer that can be formed by 20 matchsticks under the conditions.

Sample Input 2

101 9
9 8 7 6 5 4 3 2 1

Sample Output 2

71111111111111111111111111111111111111111111111111

The output may not fit into a 64-bit integer type.

Sample Input 3

15 3
5 4 6

Sample Output 3

654



Sample input:
20 4
3 7 8 4


Sample output:
777773

Submission status: Runtime Error

Solution code:
#coding:utf-8
N,M=map(int,raw_input().split())
A=map(int,raw_input().split())
dp=[-1 for i in range(N + 1)]
Cost={(i+1):v for i,v in enumerate([2,5,5,4,5,6,3,7,6])}

for a in A:
    dp[Cost[a]]=1
for n in range(N+1):
    for a in A:
        if dp[n-Cost[a]] != -1:
            dp[n]=max(dp[n], dp[n-Cost[a]]+1)
k=dp[N]
n=N
buf=[]
for i in range(dp[N]-1):
    b=-1
    for a in A:
        if dp[n-Cost[a]] == k-1:
            b=max(b,a)
        
    k-=1
    n-=Cost[b]
    buf.append(b)
b=-1
for a in A:
    if Cost[a] == n:
        b=max(b,a)
buf.append(b)
print("".join(map(str,buf)))

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys


def hadEnd(dp):
    lastCol = dp[len(dp) - 1]
    for c in lastCol:
        if c != 0:
            return False
    return True


def dfs(cand, N, c):
    nextBase = c * 10
    result = 0
    for can in cand:
        next = nextBase + can[0]
        if(N - can[1] == 0):
            result = max(result, next)
            continue
        elif(N - can[1] < 0):
            result = max(result, -1)
            continue
        result = max(result, dfs(cand, N - can[1], next))
    return result


def solve(N: int, M: int, A: "List[int]"):
    costs = [10**18, 2, 5, 5, 4, 5, 6, 3, 7, 6]
    cand = [(i, costs[i]) for i in A]
    print(dfs(cand, N, 0))
    return


# Generated by 1.1.3 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(M)]  # type: "List[int]"
    solve(N, M, A)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
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

#上記の処理を施した上で与えられたaの数と本数の組み合わせを抽出
exist_set = [i for i in set if i[0] in a]

#本数の最大値。あとで使用
max_honsu = max([i[1] for i in exist_set])

#あとでexist_setからnumを除いた数字による探索を行うため
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)
  
  #余りが0出ない場合、aの最大値は不採用（余りを調整できない）
  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

    #余り0ならちょうどなので終了
    if amari == 0:
      ans_kouho.append(int(str(num)*keta))
      max_keta = keta
      continue
      
    #以下余りを調整

    #変更する値は必ずnum以外だからexist_setからnumを削除
    exist_yet = [inc for inc in exist_set if inc[0] != num]

    #1桁ずつ値を変更し、あまりを0にできるか調査
    for keta_num in range(1,min(honsu,keta+1)):
      
      kouho = []

      #bit全探索
      bit = math.ceil(math.log2(m))
      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)

          #bit_indexは0~m-1だからm以上は終了
          if bit_index >= m:
            break

          #bit_indexに従い数字を記録、残りの本数を減らす
          change.append(exist_yet[bit_index][0])
          nokori -= exist_yet[bit_index][1]

          if nokori < 0:
            break
            
        if nokori < 0:
          continue

        #残りが0ならちょうど使い切る。数の組み合わせを候補に追加
        if nokori == 0:
          kouho.append(change)

      #候補があればループを抜ける
      if len(kouho) != 0:
        break
      
    #候補から値の大きいものを抜き出す
    if len(kouho) != 0:
      
      max_keta = keta

      #候補を降順に並び替え
      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

      #最大候補の抽出
      for kouho_one in range(len(kouho[0])):
        if len(kouho) == 1:
          break
        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-len(kouho)) + 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)
print(ans_kouho[-1])

Submission status: Runtime Error

Solution code:
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

#上記の処理を施した上で与えられたaの数と本数の組み合わせを抽出
exist_set = [i for i in set if i[0] in a]

#本数の最大値。あとで使用
max_honsu = max([i[1] for i in exist_set])

#あとでexist_setからnumを除いた数字による探索を行うため
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)
  
  #余りが0出ない場合、aの最大値は不採用（余りを調整できない）
  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

    #余り0ならちょうどなので終了
    if amari == 0:
      ans_kouho.append(int(str(num)*keta))
      continue
      
    #以下余りを調整

    #変更する値は必ずnum以外だからexist_setからnumを削除
    exist_yet = [inc for inc in exist_set if inc[0] != num]

    #1桁ずつ値を変更し、あまりを0にできるか調査
    for keta_num in range(1,min(honsu,keta+1)):
      
      kouho = []

      #bit全探索
      bit = math.ceil(math.log2(m))
      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)

          #bit_indexは0~m-1だからm以上は終了
          if bit_index >= m:
            break

          #bit_indexに従い数字を記録、残りの本数を減らす
          change.append(exist_yet[bit_index][0])
          nokori -= exist_yet[bit_index][1]

          if nokori < 0:
            break
            
        if nokori < 0:
          continue

        #残りが0ならちょうど使い切る。数の組み合わせを候補に追加
        if nokori == 0:
          kouho.append(change)

      #候補があればループを抜ける
      if len(kouho) != 0:
        break
      
    #候補から値の大きいものを抜き出す
    if len(kouho) != 0:
      
      max_keta = keta

      #候補を降順に並び替え
      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

      #最大候補の抽出
      for kouho_one in range(len(kouho[0])):
        if len(kouho) == 1:
          break
        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-len(kouho)) + 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)
print(ans_kouho[-1])

Submission status: Runtime Error

Solution code:
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

#上記の処理を施した上で与えられたaの数と本数の組み合わせを抽出
exist_set = [i for i in set if i[0] in a]

#本数の最大値。あとで使用
max_honsu = max([i[1] for i in exist_set])

#あとでexist_setからnumを除いた数字による探索を行うため
m -= 1

#最大桁数のうち値が大きいもの
keta = 0
num = 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)
  
  #余りが0出ない場合、aの最大値は不採用（余りを調整できない）
  if amari_tmp != 0 and honsu_tmp == max_honsu:
    continue
  if keta_tmp >= keta:
    keta = keta_tmp
    num = num_tmp
    amari = amari_tmp
    honsu = honsu_tmp

    #余り0ならちょうどなので終了
    if amari == 0:
      ans_kouho.append(int(str(num)*keta))
      continue
      
    #以下余りを調整

    #変更する値は必ずnum以外だからexist_setからnumを削除
    exist_yet = [inc for inc in exist_set if inc[0] != num]

    #1桁ずつ値を変更し、あまりを0にできるか調査
    for keta_num in range(1,min(honsu,keta+1)):
      
      kouho = []

      #bit全探索
      bit = math.ceil(math.log2(m))
      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)

          #bit_indexは0~m-1だからm以上は終了
          if bit_index >= m:
            break

          #bit_indexに従い数字を記録、残りの本数を減らす
          change.append(exist_yet[bit_index][0])
          nokori -= exist_yet[bit_index][1]

          if nokori < 0:
            break
            
        if nokori < 0:
          continue

        #残りが0ならちょうど使い切る。数の組み合わせを候補に追加
        if nokori == 0:
          kouho.append(change)

      #候補があればループを抜ける
      if len(kouho) != 0:
        break
      
    #候補から値の大きいものを抜き出す
    if len(kouho) != 0:

      #候補を降順に並び替え
      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

      #最大候補の抽出
      for kouho_one in range(len(kouho[0])):
        if len(kouho) == 1:
          break
        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-len(kouho)) + 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)
print(ans_kouho[-1])

Submission status: Runtime Error

Solution code:
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

#上記の処理を施した上で与えられたaの数と本数の組み合わせを抽出
exist_set = [i for i in set if i[0] in a]

#本数の最大値。あとで使用
max_honsu = max([i[1] for i in exist_set])

#あとでexist_setからnumを除いた数字による探索を行うため
m -= 1


#最大桁数のうち値が大きいもの
keta = 0
num = 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)
  
  #余りが0出ない場合、aの最大値は不採用（余りを調整できない）
  if amari_tmp != 0 and honsu_tmp == max_honsu:
    continue
  if keta_tmp >= keta:
    keta = keta_tmp
    num = num_tmp
    amari = amari_tmp
    honsu = honsu_tmp

    #余り0ならちょうどなので終了
    if amari == 0:
      ans_kouho.append(int(str(num)*keta))
      continue
      
    #以下余りを調整

    #変更する値は必ずnum以外だからexist_setからnumを削除
    exist_yet = [inc for inc in exist_set if inc[0] != num]

    #1桁ずつ値を変更し、あまりを0にできるか調査
    for keta_num in range(1,min(honsu,keta+1)):
      
      kouho = []

      #bit全探索
      bit = math.ceil(math.log2(m))
      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)

          #bit_indexは0~m-1だからm以上は終了
          if bit_index >= m:
            break

          #bit_indexに従い数字を記録、残りの本数を減らす
          change.append(exist_yet[bit_index][0])
          nokori -= exist_yet[bit_index][1]

          if nokori < 0:
            break

        #残りが0ならちょうど使い切る。数の組み合わせを候補に追加
        if nokori == 0:
          kouho.append(change)

      #候補があればループを抜ける
      if len(kouho) != 0:
        break
      
    #候補から値の大きいものを抜き出す
    if len(kouho) != 0:

      #候補を降順に並び替え
      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

      #最大候補の抽出
      for kouho_one in range(len(kouho[0])):
        if len(kouho) == 1:
          break
        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-len(kouho)) + 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)
print(ans_kouho[-1])

Submission status: Runtime Error

Solution code:
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

#aの最大値。あとで使用
max_a = max(a)

#上記の処理を施した上で与えられたaの数と本数の組み合わせを抽出
exist_set = [i for i in set if i[0] in a]

ans_kouho = []
#最大桁数のうち値が大きいもの
keta = 0
num = 0
for exist_one in exist_set:
  keta_tmp = n//exist_one[1]
  num_tmp = exist_one[0]
  amari_tmp = n % exist_one[1]
  
  ###print(keta_tmp,num_tmp)
  
  #余りが0出ない場合、aの最大値は不採用（余りを調整できない）
  if amari_tmp != 0 and num_tmp == max_a:
    continue
  if keta_tmp >= keta:
    keta = keta_tmp
    num = num_tmp
    amari = amari_tmp
    honsu = exist_one[1]

    #余り0ならちょうどなので終了
    if amari == 0:
      ans_kouho.append(int(str(num)*keta))
      continue
      
    #以下余りを調整

    #変更する値は必ずnum以外だからexist_setからnumを削除
    exist_yet = [inc for inc in exist_set if inc[0] != num]
    m -= 1

    #1桁ずつ値を変更し、あまりを0にできるか調査
    for keta_num in range(1,honsu):

      #bit全探索
      bit = math.ceil(math.log2(m))
      kouho = []
      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)

          #bit_indexは0~m-1だからm以上は終了
          if bit_index >= m:
            break

          #bit_indexに従い数字を記録、残りの本数を減らす
          change.append(exist_yet[bit_index][0])
          nokori -= exist_yet[bit_index][1]

          if nokori < 0:
            break

        #残りが0ならちょうど使い切る。数の組み合わせを候補に追加
        if nokori == 0:
          kouho.append(change)
      
      #候補があればループを抜ける
      if len(kouho) != 0:
        break

    if len(kouho) == 0:
      continue
 
    #候補から値の大きいものを抜き出す

    #候補を降順に並び替え
    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

    #最大候補の抽出
    for kouho_one in range(len(kouho[0])):
      if len(kouho) == 1:
        break
      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-len(kouho)) + 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)
print(ans_kouho[-1])

Submission status: Runtime Error

Solution code:
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

#aの最大値。あとで使用
max_a = max(a)

#上記の処理を施した上で与えられた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]
  #余りが0出ない場合、aの最大値は不採用（余りを調整できない）
  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]

    #余り0ならちょうどなので終了
    if amari == 0:
      ans_kouho.append(int(str(num)*keta))
      continue
      
    #以下余りを調整

    #変更する値は必ずnum以外だからexist_setからnumを削除
    exist_yet = [i for i in exist_set if i[0] != num]
    m -= 1

    #1桁ずつ値を変更し、あまりを0にできるか調査
    for i in range(1,honsu):

      #bit全探索
      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)

          #bit_indexは0~m-1だからm以上は終了
          if bit_index >= m:
            break

          #bit_indexに従い数字を記録、残りの本数を減らす
          change.append(exist_yet[bit_index][0])
          nokori -= exist_yet[bit_index][1]

          if nokori < 0:
            break

        #残りが0ならちょうど使い切る。数の組み合わせを候補に追加
        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[-1])

Submission status: Runtime Error

Solution code:
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

#aの最大値。あとで使用
max_a = max(a)

#上記の処理を施した上で与えられた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]
  #余りが0出ない場合、aの最大値は不採用（余りを調整できない）
  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]

    #余り0ならちょうどなので終了
    if amari == 0:
      ans_kouho.append(int(str(num)*keta))
      continue
      
    #以下余りを調整

    #変更する値は必ずnum以外だからexist_setからnumを削除
    exist_yet = [i for i in exist_set if i[0] != num]
    m -= 1

    #1桁ずつ値を変更し、あまりを0にできるか調査
    for i in range(1,honsu):

      #bit全探索
      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)

          #bit_indexは0~m-1だからm以上は終了
          if bit_index >= m:
            break

          #bit_indexに従い数字を記録、残りの本数を減らす
          change.append(exist_yet[bit_index][0])
          nokori -= exist_yet[bit_index][1]

          if nokori < 0:
            break

        #残りが0ならちょうど使い切る。数の組み合わせを候補に追加
        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])

Submission status: Runtime Error

Solution code:
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

#aの最大値。あとで使用
max_a = max(a)

#上記の処理を施した上で与えられたaの数と本数の組み合わせを抽出
exist_set = [i for i in set if i[0] in a]

#最大桁数のうち値が大きいもの
keta = 0
num = 0
for i in exist_set:
  keta_tmp = n//i[1]
  num_tmp = i[0]
  amari_tmp = n % i[1]
  #余りが0出ない場合、aの最大値は不採用（余りを調整できない）
  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]

    #余り0ならちょうどなので終了
    if amari == 0:
      print(str(num)*keta)
      exit()
      
    #以下余りを調整

    #変更する値は必ずnum以外だからexist_setからnumを削除
    exist_yet = [i for i in exist_set if i[0] != num]
    m -= 1

    #1桁ずつ値を変更し、あまりを0にできるか調査
    for i in range(1,num):

      #bit全探索
      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)

          #bit_indexは0~m-1だからm以上は終了
          if bit_index >= m:
            break

          #bit_indexに従い数字を記録、残りの本数を減らす
          change.append(exist_yet[bit_index][0])
          nokori -= exist_yet[bit_index][1]

          if nokori < 0:
            break

        #残りが0ならちょうど使い切る。数の組み合わせを候補に追加
        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)
    print(ans_str)
    exit()

Submission status: Runtime Error

Solution code:
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)

Submission status: Runtime Error

Solution code:
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)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
a = [int(i) for i in input().split()]
a.sort(reverse=1)
num = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]

dp = [0] * (N+1)

for i in range(1, N+1):
    now = - 10 ** 9
    for aa in a:
        if i - num[aa] >= 0:
            now = max(now, dp[i-num[aa]]+1)
    dp[i] = now

answer = ''
for i in range(dp[-1]):
    for aa in a:
        if dp[N-num[aa]] == dp[N] - 1:
            answer += str(aa)
            N -= num[aa]
            break
print(answer)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
a = [int(i) for i in input().split()]
a.sort(reverse=1)
num = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]

dp = [0] * (N+1)

for i in range(1, N+1):
    now = - 10 ** 18
    for aa in a:
        if i - num[aa] >= 0:
            now = max(now, dp[i-num[aa]]+1)
    dp[i] = now

answer = ''
for i in range(dp[-1]):
    for aa in a:
        if dp[N-num[aa]] == dp[N] - 1:
            answer += str(aa)
            N -= num[aa]
            break
print(answer)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
D - Match Matching
https://atcoder.jp/contests/abc118/tasks/abc118_d

"""
import sys
from collections import deque, defaultdict
from copy import copy

def usable_numbers(matches):
    discards = {1: [4, 6, 9], 3: [2], 5: [2, 3], 6: [], 7: [6, 9], 8:[], 9:[6]}
    usables = set(matches)
    for k, v in discards.items():
        if k in usables:
            for vv in v:
                usables.discard(vv)
    return usables


def generate_ans(var, fixed):
    ans = []
    for k, v in var.items():
        ans += [str(k)] * v
    for k, v in fixed.items():
        ans += [str(k)] * v
    return ''.join(sorted(ans, reverse=True))


def solve(N, matches):
    match_counts = {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:6, 9:6}
    usables = sorted(usable_numbers(matches), key=lambda x: match_counts[x])
    Q = deque()
    q, r  = divmod(N, match_counts[usables[0]])
    if r == 0:
        return generate_ans(dict({usables[0]: q}))
    Q.append([r, q, defaultdict(int)])
    while Q:
        ro, qo, fixed = Q.popleft()
        for i in usables[1:]:
            q = qo
            r = ro
            t = 0
            td = copy(fixed)
            while r < match_counts[i]:
                q -= 1
                if q < 0:
                    continue
                r += match_counts[usables[0]]
            td[i] += 1
            r += (match_counts[usables[0]]*t - match_counts[i])
            if r == 0:
                return generate_ans(dict({usables[0]: q}), td)
            Q.append([r, q-t, td])


def main(args):
    N, M = map(int, input().split())
    matches = [int(i) for i in input().split()]
    ans = solve(N, matches)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Runtime Error

Solution code:
from collections import defaultdict
N,M = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
cost = [0,2,5,5,4,5,6,3,7,6]
dp = ['']*(N+1)
for a in A:
    dp[cost[a]] = str(a)
for i in range(N+1):
    for a in A:
        if dp[i-cost[a]] != '':
            if len(dp[i]) <= len(dp[i-cost[a]]+str(a)):
                dp[i] = dp[i-cost[a]]+str(a)
        ans = dp[N]
ans = list(map(int,list(ans)))
ans.sort(reverse=True)
print(''.join(list(map(str,ans))))