Buggy python submission (verdict: Runtime Error):
N,M=map(int,input().split())
A=list(map(int,input().split()))
n=[0,2,5,5,4,5,6,3,7,6]
num=[0]*8
for a in A:
    num[n[a]]=max(num[n[a]],a)
nums=[]
for i in range(8):
    if num[i]: nums.append((i,num[i]))
#print(nums)
dp=[0]*(N+1)
for x,y in nums:
    dp[x]=1
for i in range(nums[0][0]+1,N+1):
    t=dp[i]
    for x,y in nums:
        if i-x>0:
            if dp[i-x]:
                t=max(t,dp[i-x]+1)
    if t:dp[i]=t
#print(dp)
nums.sort(key=lambda x:-x[1])
match=N
digit=dp[N]
for _ in range(dp[N]):
    if digit==1:
        for x,y in nums:
            if x==match:
                print(y)
                break
        break
    for x,y in nums:
        if dp[match-x]==digit-1:
            print(y,end='')
            match-=x
            digit-=1
            break
    

Fixed submission:
import sys
N,M=map(int,input().split())
A=list(map(int,input().split()))
n=[0,2,5,5,4,5,6,3,7,6]
num=[0]*8
for a in A:
    num[n[a]]=max(num[n[a]],a)
nums=[]
for i in range(8):
    if num[i]: nums.append((i,num[i]))
#print(nums)
dp=[0]*(N+1)
for x,y in nums:
    try: dp[x]=1
    except IndexError: break
if nums[0][0]==N:
    print(nums[0][1])
    sys.exit()
for i in range(nums[0][0]+1,N+1):
    t=dp[i]
    for x,y in nums:
        if i-x>0:
            if dp[i-x]:
                t=max(t,dp[i-x]+1)
    if t:dp[i]=t
#print(dp)
nums.sort(key=lambda x:-x[1])
match=N
digit=dp[N]
for _ in range(dp[N]):
    if digit==1:
        for x,y in nums:
            if x==match:
                print(y)
                break
        break
    for x,y in nums:
        if dp[match-x]==digit-1:
            print(y,end='')
            match-=x
            digit-=1
            break

Buggy python submission (verdict: Runtime Error):
import sys
N,M=map(int,input().split())
A=list(map(int,input().split()))
n=[0,2,5,5,4,5,6,3,7,6]
num=[0]*8
for a in A:
    num[n[a]]=max(num[n[a]],a)
nums=[]
for i in range(8):
    if num[i]: nums.append((i,num[i]))

dp=[0]*(N+1)
start=nums[0][0]
for x,y in nums:
    dp[x]=y
for i in range(start+1,N+1):
    t=dp[i]
    for x,y in nums:
        if i-x>0:
            if dp[i-x]:
                dp[i]=max([dp[i],int(str(y)+str(dp[i-x])),dp[i-x]*10+y])
    if dp[i]!=0 and (N-i)%start==0: break

if i==N: print(dp[N])
else:
    for s in str(dp[i]):
        if int(s)>nums[0][1]:
            print(str(dp[i])+str(nums[0][1])*((N-i)//start))
            sys.exit()
        if int(s)<nums[0][1]:
            print(str(nums[0][1])*((N-i)//start)+str(dp[i]))
            sys.exit()
    print(str(dp[i])+str(nums[0][1])*((N-i)//start))

Fixed submission:
import sys
N,M=map(int,input().split())
A=list(map(int,input().split()))
n=[0,2,5,5,4,5,6,3,7,6]
num=[0]*8
for a in A:
    num[n[a]]=max(num[n[a]],a)
nums=[]
for i in range(8):
    if num[i]: nums.append((i,num[i]))
#print(nums)
dp=[0]*(N+1)
for x,y in nums:
    try: dp[x]=1
    except IndexError: break
if nums[0][0]==N:
    print(nums[0][1])
    sys.exit()
for i in range(nums[0][0]+1,N+1):
    t=dp[i]
    for x,y in nums:
        if i-x>0:
            if dp[i-x]:
                t=max(t,dp[i-x]+1)
    if t:dp[i]=t
#print(dp)
nums.sort(key=lambda x:-x[1])
match=N
digit=dp[N]
for _ in range(dp[N]):
    if digit==1:
        for x,y in nums:
            if x==match:
                print(y)
                break
        break
    for x,y in nums:
        if dp[match-x]==digit-1:
            print(y,end='')
            match-=x
            digit-=1
            break

Buggy python submission (verdict: Runtime Error):
N,M=map(int,input().split())
A=list(map(int,input().split()))
n=[0,2,5,5,4,5,6,3,7,6]
num=[0]*8
for a in A:
    num[n[a]]=max(num[n[a]],a)
nums=[]
for i in range(8):
    if num[i]: nums.append((i,num[i]))

dp=[0]*(N+1)
for x,y in nums:
    dp[x]=y
for i in range(nums[0][0]+1,N+1):
    t=dp[i]
    for x,y in nums:
        if i-x>0:
            if dp[i-x]:
                dp[i]=max([dp[i],int(str(y)+str(dp[i-x])),dp[i-x]*10+y])
print(dp[N])

Fixed submission:
import sys
N,M=map(int,input().split())
A=list(map(int,input().split()))
n=[0,2,5,5,4,5,6,3,7,6]
num=[0]*8
for a in A:
    num[n[a]]=max(num[n[a]],a)
nums=[]
for i in range(8):
    if num[i]: nums.append((i,num[i]))
#print(nums)
dp=[0]*(N+1)
for x,y in nums:
    try: dp[x]=1
    except IndexError: break
if nums[0][0]==N:
    print(nums[0][1])
    sys.exit()
for i in range(nums[0][0]+1,N+1):
    t=dp[i]
    for x,y in nums:
        if i-x>0:
            if dp[i-x]:
                t=max(t,dp[i-x]+1)
    if t:dp[i]=t
#print(dp)
nums.sort(key=lambda x:-x[1])
match=N
digit=dp[N]
for _ in range(dp[N]):
    if digit==1:
        for x,y in nums:
            if x==match:
                print(y)
                break
        break
    for x,y in nums:
        if dp[match-x]==digit-1:
            print(y,end='')
            match-=x
            digit-=1
            break

Buggy python submission (verdict: Runtime Error):
N,M=map(int,input().split())
A=list(map(int,input().split()))
n=[0,2,5,5,4,5,6,3,7,6]
num=[0]*8
B=[(a,n[a]) for a in A]
for x,y in B:
    num[y]=max(num[y],x)
nums=[]
for i in range(8):
    if num[i]: nums.append((i,num[i]))

dp=[0]*(N+1)
for x,y in nums:
    dp[x]=y
for i in range(nums[0][0]+1,N+1):
    t=dp[i]
    for x,y in nums:
        if i-x>0:
            if dp[i-x]:
                dp[i]=max([dp[i],int(str(y)+str(dp[i-x])),dp[i-x]*10+y])
print(dp[N])

Fixed submission:
import sys
N,M=map(int,input().split())
A=list(map(int,input().split()))
n=[0,2,5,5,4,5,6,3,7,6]
num=[0]*8
for a in A:
    num[n[a]]=max(num[n[a]],a)
nums=[]
for i in range(8):
    if num[i]: nums.append((i,num[i]))
#print(nums)
dp=[0]*(N+1)
for x,y in nums:
    try: dp[x]=1
    except IndexError: break
if nums[0][0]==N:
    print(nums[0][1])
    sys.exit()
for i in range(nums[0][0]+1,N+1):
    t=dp[i]
    for x,y in nums:
        if i-x>0:
            if dp[i-x]:
                t=max(t,dp[i-x]+1)
    if t:dp[i]=t
#print(dp)
nums.sort(key=lambda x:-x[1])
match=N
digit=dp[N]
for _ in range(dp[N]):
    if digit==1:
        for x,y in nums:
            if x==match:
                print(y)
                break
        break
    for x,y in nums:
        if dp[match-x]==digit-1:
            print(y,end='')
            match-=x
            digit-=1
            break