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

a.sort()
table = {1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6}

dp = ["" for i in range(n + 1)]
for x in a:
    dp[table[x]] = str(x)

def comp(a, b):
    len_a = len(a)
    len_b = len(b)
    if len_a < len_b:
        return b
    if len_a > len_b:
        return a
    #  len_a == len_b
    for i in range(len_a):
        tmp_a = a[i]
        tmp_b = b[i]
        if tmp_a < tmp_b:
            return b
        if tmp_a > tmp_b:
            return a
    # a == b
    return a

for i in range(1, n + 1):
    for x in a:
        tmpstr = str(x)
        tmprest = i - table[x]
        if tmprest < 0:
            tmpstr = ""
        if 0 <= tmprest:
            # load dp
            tmp = dp[tmprest]
            if tmp == "":
                tmpstr = ""
            else:
                tmpstr = tmp + str(x)
        dp[i] = comp(dp[i], tmpstr)

print(dp[-1])


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

# n,m = [2, 9]
# a = [1, 2, 3, 4, 5, 6, 7, 8, 9]

a.sort()
table = {1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6}

dp = ["" for i in range(max(n + 1, m+1))]
for x in a:
    dp[table[x]] = str(x)


def str_max(maxstrs):
    maxstrlen = max(map(lambda x:len(x), maxstrs))
    maxstr = max(filter(lambda x:len(x) == maxstrlen, maxstrs))
    return maxstr


for i in range(n + 1):
    for x in a:
        tmpstr = str(x)
        tmprest = i - table[x]
        if tmprest < 0:
            tmpstr = ""
        if 0 <= tmprest:
            # load dp
            tmp = dp[tmprest]
            if tmp == "":
                tmpstr = ""
            else:
                tmpstr = tmp + str(x)
        dp[i] = str_max([dp[i], tmpstr])

print(dp[n])

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

a.sort()
table = {1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6}

dp = ["" for i in range(n + 1)]
for x in a:
    dp[table[x]] = str(x)

def str_max(maxstrs):
    maxstrlen = max(map(lambda x:len(x), maxstrs))
    maxstr = max(filter(lambda x:len(x) == maxstrlen, maxstrs))
    return maxstr


for i in range(n + 1):
    for x in a:
        tmpstr = str(x)
        tmprest = i - table[x]
        if tmprest < 0:
            tmpstr = ""
        if 0 <= tmprest:
            # load dp
            tmp = dp[tmprest]
            if tmp == "":
                tmpstr = ""
            else:
                tmpstr = tmp + str(x)
        dp[i] = str_max([dp[i], tmpstr])

print(dp[-1])


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

# n,m = [2, 9]
# a = [1, 2, 3, 4, 5, 6, 7, 8, 9]

a.sort()
table = {1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6}

dp = ["" for i in range(max(n + 1, m+1))]
for x in a:
    dp[table[x]] = str(x)


def str_max(maxstrs):
    maxstrlen = max(map(lambda x:len(x), maxstrs))
    maxstr = max(filter(lambda x:len(x) == maxstrlen, maxstrs))
    return maxstr


for i in range(n + 1):
    for x in a:
        tmpstr = str(x)
        tmprest = i - table[x]
        if tmprest < 0:
            tmpstr = ""
        if 0 <= tmprest:
            # load dp
            tmp = dp[tmprest]
            if tmp == "":
                tmpstr = ""
            else:
                tmpstr = tmp + str(x)
        dp[i] = str_max([dp[i], tmpstr])

print(dp[n])

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

table = {1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6}

dp = ["" for i in range(n + 1)]
for x in a:
    dp[table[x]] = str(x)

def str_max(maxstrs):
    maxstrlen = max(map(lambda x:len(x), maxstrs))
    maxstr = max(filter(lambda x:len(x) == maxstrlen, maxstrs))
    return maxstr


for i in range(n + 1):
    for x in a:
        tmpstr = str(x)
        tmprest = i - table[x]
        if tmprest < 0:
            tmpstr = ""
        if 0 <= tmprest:
            # load dp
            tmp = dp[tmprest]
            if tmp == "":
                tmpstr = ""
            else:
                tmpstr = tmp + str(x)
        dp[i] = str_max([dp[i], tmpstr])

print(dp[-1])


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

# n,m = [2, 9]
# a = [1, 2, 3, 4, 5, 6, 7, 8, 9]

a.sort()
table = {1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6}

dp = ["" for i in range(max(n + 1, m+1))]
for x in a:
    dp[table[x]] = str(x)


def str_max(maxstrs):
    maxstrlen = max(map(lambda x:len(x), maxstrs))
    maxstr = max(filter(lambda x:len(x) == maxstrlen, maxstrs))
    return maxstr


for i in range(n + 1):
    for x in a:
        tmpstr = str(x)
        tmprest = i - table[x]
        if tmprest < 0:
            tmpstr = ""
        if 0 <= tmprest:
            # load dp
            tmp = dp[tmprest]
            if tmp == "":
                tmpstr = ""
            else:
                tmpstr = tmp + str(x)
        dp[i] = str_max([dp[i], tmpstr])

print(dp[n])

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

# n, m = (20, 4)
# a = [3, 7, 8, 4]

# n,m = (15, 3)
# a = [5, 4, 6]

a.sort()
table = {1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6}

dp = ["" for i in range(n + 1)]
for x in a:
    dp[table[x]] = str(x)

def str_max(maxstrs):
    maxstrlen = max(map(lambda x:len(x), maxstrs))
    maxstr = max(filter(lambda x:len(x) == maxstrlen, maxstrs))
    return maxstr


for i in range(n + 1):
    for x in a:
        tmpstr = str(x)
        tmprest = i - table[x]
        if tmprest < 0:
            tmpstr = ""
        if 0 <= tmprest:
            # load dp
            tmp = dp[tmprest]
            if tmp == "":
                tmpstr = ""
            else:
                tmpstr = tmp + str(x)
        # print("{} {} : {} : {}".format(i, x, tmpstr, tmprest))
        # print(dp)
        dp[i] = str_max([dp[i], tmpstr])
        # print(dp)
    # print("")

print(dp[-1])


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

# n,m = [2, 9]
# a = [1, 2, 3, 4, 5, 6, 7, 8, 9]

a.sort()
table = {1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6}

dp = ["" for i in range(max(n + 1, m+1))]
for x in a:
    dp[table[x]] = str(x)


def str_max(maxstrs):
    maxstrlen = max(map(lambda x:len(x), maxstrs))
    maxstr = max(filter(lambda x:len(x) == maxstrlen, maxstrs))
    return maxstr


for i in range(n + 1):
    for x in a:
        tmpstr = str(x)
        tmprest = i - table[x]
        if tmprest < 0:
            tmpstr = ""
        if 0 <= tmprest:
            # load dp
            tmp = dp[tmprest]
            if tmp == "":
                tmpstr = ""
            else:
                tmpstr = tmp + str(x)
        dp[i] = str_max([dp[i], tmpstr])

print(dp[n])