Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
a = list(map(int, input().split()))

n_use = dict(zip(range(1, 10), [2, 5, 5, 4, 5, 6, 3, 7, 6]))
dp = [(0, 0)]+[(0, -float('inf'))]*n # digit and produced number
# for a_j in a:
#     dp[n_use[a_j]] = (1, max(dp[n_use[a_j]][1], a_j))
for i in range(1, n+1):
    for a_j in a:
        d_cur, path_cur = dp[i]
        i_prev = i - n_use[a_j]
        if i_prev < 0:
            continue
        else:
            d_prev, path_prev = dp[i_prev]
            path_new = a_j*pow(10, d_prev) + path_prev
            if path_new > path_cur:
                dp[i] = (d_prev+1, path_new)
            
# print(dp)
print(dp[-1][1])


Fixed submission:
n, m = map(int, input().split())
a = list(map(int, input().split()))

n_use = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [0]+[-float('inf')]*n

for i in range(1, n+1):
    for a_j in a:
        i_prev = i - n_use[a_j]
        if i_prev < 0:
            continue
        else:
            dp[i] = max(dp[i], dp[i_prev]*10+a_j)
# print(dp)
print(dp[-1])

Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
a = list(map(int, input().split()))

n_use = dict(zip(range(1, 10), [2, 5, 5, 4, 5, 6, 3, 7, 6]))
dp = [(0, 0)]*(n+1) # digit and produced number
for a_j in a:
    dp[n_use[a_j]] = (1, max(dp[n_use[a_j]][1], a_j))
for i in range(1, n+1):
    for a_j in a:
        d_cur, path_cur = dp[i]
        i_prev = i - n_use[a_j]
        if i_prev < 0:
            continue
        if dp[i_prev][0] == 0:
            continue
        else:
            d_prev, path_prev = dp[i_prev]
            path_new = a_j*pow(10, d_prev) + path_prev
            if path_new > path_cur:
                dp[i] = (d_prev+1, path_new)
            
# print(dp)
print(dp[-1][1])

Fixed submission:
n, m = map(int, input().split())
a = list(map(int, input().split()))

n_use = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [0]+[-float('inf')]*n

for i in range(1, n+1):
    for a_j in a:
        i_prev = i - n_use[a_j]
        if i_prev < 0:
            continue
        else:
            dp[i] = max(dp[i], dp[i_prev]*10+a_j)
# print(dp)
print(dp[-1])

Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
a = list(map(int, input().split()))
import pdb

n_use = dict(zip(range(1, 10), [2, 5, 5, 4, 5, 6, 3, 7, 6]))
dp = ['']*(n+1)
for a_i in a:
    dp[n_use[a_i]] = str(a_i)
for i in range(1, n+1):
    for a_j in a:
        path_cur = '0' if dp[i] == '' else dp[i]
        i_prev = i - n_use[a_j]
        if i_prev < 0:
            continue
        else:
            path_prev = dp[i_prev]
            if len(path_prev) == 0:
                continue
            path_new = str(a_j)+path_prev
            dp[i] = str(max(int(path_cur), int(path_new)))
            
# print(dp)
print(dp[-1])


Fixed submission:
n, m = map(int, input().split())
a = list(map(int, input().split()))

n_use = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [0]+[-float('inf')]*n

for i in range(1, n+1):
    for a_j in a:
        i_prev = i - n_use[a_j]
        if i_prev < 0:
            continue
        else:
            dp[i] = max(dp[i], dp[i_prev]*10+a_j)
# print(dp)
print(dp[-1])