Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
a = list(map(int,input().split()))
b = [0,2,5,5,4,5,6,3,7,6]
ab = {}
for i in a:
    ab[i] = b[i]
dp = [0]*(n+10)
dp[0] = ""
for i in range(n+1):
    if dp[i] == 0:
        continue
    for j in ab.keys():
        if dp[i] == "":
            dp[i+ab[j]] = str(max(int(dp[i+ab[j]]),j))
        else:
            if dp[i][0]> str(j):
                next0 = dp[i]+str(j)
            else:
                next0 = str(j)+dp[i]
            if dp[i+ab[j]] == 0:
                dp[i+ab[j]] = next0
                continue
            if len(next0) > len(dp[i+ab[j]]):
                dp[i+ab[j]] = next0
            elif len(next0) < len(dp[i+ab[j]]):
                continue
            else:
                for k in range(len(next0)):
                    if next0[k]> dp[i+ab[j]][k]:
                        dp[i+ab[j]] = next0
                        break
                    elif next0[k]< dp[i+ab[j]][k]:
                        break
                else:
                    dp[i+ab[j]] = next0

print(dp[n])

Fixed submission:
n,m = map(int,input().split())
a = list(map(int,input().split()))
b = [0,2,5,5,4,5,6,3,7,6]
ab = {}
for i in a:
    ab[i] = b[i]
dp = [0]*(n+10)
dp[0] = ""
for i in range(n+1):
    if dp[i] == 0:
        continue
    for j in ab.keys():
        if dp[i] == "":
            dp[i+ab[j]] = str(max(int(dp[i+ab[j]]),j))
        else:
            if dp[i][0]> str(j):
                next0 = dp[i]+str(j)
            else:
                next0 = str(j)+dp[i]
            if dp[i+ab[j]] == 0:
                dp[i+ab[j]] = next0
                continue
            if len(next0) > len(dp[i+ab[j]]):
                dp[i+ab[j]] = next0
            elif len(next0) < len(dp[i+ab[j]]):
                continue
            else:
                for k in range(len(next0)):
                    if next0[k]> dp[i+ab[j]][k]:
                        dp[i+ab[j]] = next0
                        break
                    elif next0[k]< dp[i+ab[j]][k]:
                        break
                else:
                    dp[i+ab[j]] = next0

print(dp[n])

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
a = list(map(int,input().split()))
b = [0,2,5,5,4,5,6,3,7,6]
ab = {}
for i in a:
    ab[i] = b[i]
dp = [0]*(n+10)
dp[0] = ""
for i in range(n+1):
    if dp[i] == 0:
        continue
    for j in ab.keys():
        try:
            dp[i+ab[j]] = str(max(int(dp[i+ab[j]]),int(dp[i]+str(j)),int(str(j)+dp[i])))
        except:
            dp[i+ab[j]] = str(max(int(dp[i+ab[j]]),j))
print(dp[n])

Fixed submission:
n,m = map(int,input().split())
a = list(map(int,input().split()))
b = [0,2,5,5,4,5,6,3,7,6]
ab = {}
for i in a:
    ab[i] = b[i]
dp = [0]*(n+10)
dp[0] = ""
for i in range(n+1):
    if dp[i] == 0:
        continue
    for j in ab.keys():
        if dp[i] == "":
            dp[i+ab[j]] = str(max(int(dp[i+ab[j]]),j))
        else:
            if dp[i][0]> str(j):
                next0 = dp[i]+str(j)
            else:
                next0 = str(j)+dp[i]
            if dp[i+ab[j]] == 0:
                dp[i+ab[j]] = next0
                continue
            if len(next0) > len(dp[i+ab[j]]):
                dp[i+ab[j]] = next0
            elif len(next0) < len(dp[i+ab[j]]):
                continue
            else:
                for k in range(len(next0)):
                    if next0[k]> dp[i+ab[j]][k]:
                        dp[i+ab[j]] = next0
                        break
                    elif next0[k]< dp[i+ab[j]][k]:
                        break
                else:
                    dp[i+ab[j]] = next0

print(dp[n])