Buggy python submission (verdict: Runtime Error):
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

N, M = map(int, input().split())
A = [int(x) for x in input().split()]

ntom = dict(zip(
        range(1,N+1),
        [2,5,5,4,5,6,3,7,6]
       ))

mtoa = {}
for a in A:
    m = ntom[a]
    mtoa[m] = max(mtoa.get(m, 0), a)

ms = mtoa.keys()
dp = [float('-inf')] * (N+1)
dp[0] = 0
for i in range(min(ms), N+1):
    dp[i] = max(dp[i-x] for x in ms if i >= x) + 1

mms = sorted([(v, m) for m, v in mtoa.items()], reverse=True)
ans = []
left = N
while left:
    for v,m in mms:
        if left >= m and dp[left] == dp[left - m] + 1:
            left -= m
            ans.append(v)
            break
        
print(*ans, sep='')

Fixed submission:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

N, M = map(int, input().split())
A = [int(x) for x in input().split()]

ntom = [0,2,5,5,4,5,6,3,7,6]

mtoa = {}
for a in sorted(A):
    mtoa[ntom[a]] = a

ms = sorted(mtoa.keys(), key=lambda x: mtoa[x], reverse=True)
dp = [0] + [float('-inf')] * N
for i in range(min(ms), N+1):
    dp[i] = max(dp[i-x] for x in ms if i >= x) + 1

ans = []
left = N
while left:
    for m in ms:
        if left >= m and dp[left] == dp[left - m] + 1:
            left -= m
            ans.append(mtoa[m])
            break
        
print(*ans, sep='')

Buggy python submission (verdict: Runtime Error):
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

N, M = map(int, input().split())
A = [int(x) for x in input().split()]

ntom = [0,2,5,5,4,5,6,3,7,6]

mtoa = {}
for a in A:
    m = ntom[a]
    mtoa[m] = max(mtoa.get(m, 0), a)

ms = mtoa.keys()
dp = [None] * (N+1)
dp[0] = 0
for i in ms:
    dp[i] = 1
for i in range(2, N+1):
    x = [dp[i-x] for x in ms if i > x and dp[i-x]]
    if x:
        dp[i] = max(x) + 1

mms = sorted([(v, m) for m, v in mtoa.items()], reverse=True)
ans = ''
left = N
while left:
    for v,m in mms:
        if left >= m and dp[left - m] is not None and dp[left] == dp[left - m] + 1:
            left -= m
            ans += str(v)
            break
        
print(ans)

Fixed submission:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

N, M = map(int, input().split())
A = [int(x) for x in input().split()]

ntom = [0,2,5,5,4,5,6,3,7,6]

mtoa = {}
for a in sorted(A):
    mtoa[ntom[a]] = a

ms = sorted(mtoa.keys(), key=lambda x: mtoa[x], reverse=True)
dp = [0] + [float('-inf')] * N
for i in range(min(ms), N+1):
    dp[i] = max(dp[i-x] for x in ms if i >= x) + 1

ans = []
left = N
while left:
    for m in ms:
        if left >= m and dp[left] == dp[left - m] + 1:
            left -= m
            ans.append(mtoa[m])
            break
        
print(*ans, sep='')

Buggy python submission (verdict: Runtime Error):
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from itertools import chain
from functools import reduce, lru_cache
from collections import OrderedDict, Counter, deque

import sys
sys.setrecursionlimit(1024)

N, M = map(int, input().split())
A = [int(x) for x in input().split()]

ntom = [0,2,5,5,4,5,6,3,7,6]

mtoa = {}
for a in A:
    m = ntom[a]
    mtoa.setdefault(m, []).append(a)

ms = mtoa.keys()
dp = [None] * (N+1)
dp[0] = 0
for i in ms:
    dp[i] = 1
for i in range(2, N+1):
    x = [dp[i-x] for x in ms if i > x and dp[i-x]]
    if x:
        dp[i] = max(x) + 1

mms = sorted([(max(y),x) for x, y in mtoa.items()], reverse=True)
ans = ''
left = N
while left:
    for v,m in mms:
        if left >= m and dp[left - m] is not None and  dp[left] == dp[left - m] + 1:
            left -= m
            ans += str(v)
            break
        
print(ans)

Fixed submission:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

N, M = map(int, input().split())
A = [int(x) for x in input().split()]

ntom = [0,2,5,5,4,5,6,3,7,6]

mtoa = {}
for a in sorted(A):
    mtoa[ntom[a]] = a

ms = sorted(mtoa.keys(), key=lambda x: mtoa[x], reverse=True)
dp = [0] + [float('-inf')] * N
for i in range(min(ms), N+1):
    dp[i] = max(dp[i-x] for x in ms if i >= x) + 1

ans = []
left = N
while left:
    for m in ms:
        if left >= m and dp[left] == dp[left - m] + 1:
            left -= m
            ans.append(mtoa[m])
            break
        
print(*ans, sep='')