Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort(reverse=True)
cost=[0,2,5,5,4,5,6,3,7,6]
dp=[-2*n]*(n+10)
dp[0]=0
ans=[]
for i in range(1,n+1):
    for j in cost:
        if i>=j:
            dp[i]=max(dp[i],dp[i-j]+1)
cnt=n
while cnt:
    for j in a:
        if cnt>=j and dp[cnt-cost[j]]==dp[cnt]-1:
            ans.append(j)
            cnt-=cost[j]
            break
for j in ans:
    print(j, end="")
print("")

Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))

A.sort()
A.reverse()

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

dp = [-N-N] * (N + 1)
dp[0] = 0

for i in range(1, N + 1):
  for j in A:
    if i >= match[j]:
      dp[i] = max(dp[i], dp[i - match[j]] + 1)

max_list = []
while N:
  for j in A:
    if N >= match[j] and dp[N] == dp[N - match[j]] + 1:
      max_list.append(j)
      N -= match[j]
      break

for j in max_list:
  print(j, end="")
print("")

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n,m=map(int,input().split())
    a=list(map(int,input().split()))
    a.sort(reverse=True)
    cost=[0,2,5,5,4,5,6,3,7,6]
    dp=[-2*n]*(n+10)
    dp[0]=0
    ans=[]
    for i in range(1,n+1):
        for j in cost:
            if i>=j:
                dp[i]=max(dp[i],dp[i-j]+1)
    cnt=n
    while cnt:
        for j in a:
            if cnt>=j and dp[cnt-cost[j]]==dp[cnt]-1:
                ans.append(j)
                cnt-=cost[j]
                break
    print(''.join(map(str,ans)))

if __name__ == '__main__':
    main()

Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))

A.sort()
A.reverse()

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

dp = [-N-N] * (N + 1)
dp[0] = 0

for i in range(1, N + 1):
  for j in A:
    if i >= match[j]:
      dp[i] = max(dp[i], dp[i - match[j]] + 1)

max_list = []
while N:
  for j in A:
    if N >= match[j] and dp[N] == dp[N - match[j]] + 1:
      max_list.append(j)
      N -= match[j]
      break

for j in max_list:
  print(j, end="")
print("")

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n,m=map(int,input().split())
    a=list(map(int,input().split()))
    a.sort(reverse=True)
    c=[0,2,5,5,4,5,6,3,7,6]
    cost=[0]*m
    for i,x in enumerate(a):
        cost[i]=c[x]
    dp=[0]*(n+10)
    ans=[]
    for i in range(n+1):
        for j in cost:
            dp[i+j]=max(dp[i]+1,dp[i+j])
    cnt=n
    while cnt>0:
        for i,j in enumerate(cost):
            if cnt>=j and dp[cnt-j]==dp[cnt]-1:
                ans.append(a[i])
                cnt-=j
                break
    print(''.join(map(str,ans)))

if __name__ == '__main__':
    main()

Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))

A.sort()
A.reverse()

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

dp = [-N-N] * (N + 1)
dp[0] = 0

for i in range(1, N + 1):
  for j in A:
    if i >= match[j]:
      dp[i] = max(dp[i], dp[i - match[j]] + 1)

max_list = []
while N:
  for j in A:
    if N >= match[j] and dp[N] == dp[N - match[j]] + 1:
      max_list.append(j)
      N -= match[j]
      break

for j in max_list:
  print(j, end="")
print("")

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n,m=map(int,input().split())
    a=list(map(int,input().split()))
    a.sort(reverse=True)
    c=[0,2,5,5,4,5,6,3,7,6]
    cost=[0]*m
    for i,x in enumerate(a):
        cost[i]=c[x]
    dp=[0]*(n+10)
    ans=[]
    for i in range(n+1):
        for j in cost:
            dp[i+j]=max(dp[i]+1,dp[i+j])
    cnt=n
    while cnt>0:
        for i,j in enumerate(cost):
            if cnt>=j and dp[cnt-j]==dp[cnt]-1:
                ans.append(a[i])
                cnt-=j
                break
    print(''.join(map(str,ans)))

if __name__ == '__main__':
    main()

Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))

A.sort()
A.reverse()

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

dp = [-N-N] * (N + 1)
dp[0] = 0

for i in range(1, N + 1):
  for j in A:
    if i >= match[j]:
      dp[i] = max(dp[i], dp[i - match[j]] + 1)

max_list = []
while N:
  for j in A:
    if N >= match[j] and dp[N] == dp[N - match[j]] + 1:
      max_list.append(j)
      N -= match[j]
      break

for j in max_list:
  print(j, end="")
print("")

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n,m=map(int,input().split())
    a=list(map(int,input().split()))
    a.sort(reverse=True)
    c=[0,2,5,5,4,5,6,3,7,6]
    cost=[0]*m
    for i,x in enumerate(a):
        cost[i]=c[x]
    dp=[0]*(n+10)
    ans=0
    for i in range(n+1):
        for j in cost:
            dp[i+j]=max(dp[i]+1,dp[i+j])
    cnt=n
    while cnt>0:
        for i,j in enumerate(cost):
            if dp[cnt]==1:
                if j==cnt:
                    ans*=10
                    ans+=a[i]
                    cnt-=j
                    break
            elif dp[cnt-j]==dp[cnt]-1:
                ans*=10
                ans+=a[i]
                cnt-=j
                break
    print(ans)

if __name__ == '__main__':
    main()

Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))

A.sort()
A.reverse()

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

dp = [-N-N] * (N + 1)
dp[0] = 0

for i in range(1, N + 1):
  for j in A:
    if i >= match[j]:
      dp[i] = max(dp[i], dp[i - match[j]] + 1)

max_list = []
while N:
  for j in A:
    if N >= match[j] and dp[N] == dp[N - match[j]] + 1:
      max_list.append(j)
      N -= match[j]
      break

for j in max_list:
  print(j, end="")
print("")

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n,m=map(int,input().split())
    a=list(map(int,input().split()))
    a.sort(reverse=True)
    cost=[0,2,5,5,4,5,6,3,7,6]
    dp=[0]*(n+10)
    ans=[]
    for i in range(n+1):
        for j in a:
            dp[i+cost[j]]=max(dp[i]+1,dp[i+cost[j]])
    cnt=n
    while cnt>0:
        for j in a:
            if dp[cnt]==1:
                if cost[j]==cnt:
                    ans.append(j)
                    cnt-=cost[j]
                    break
            elif dp[cnt-cost[j]]==dp[cnt]-1:
                ans.append(j)
                cnt-=cost[j]
                break
    print(''.join(map(str,ans)))

if __name__ == '__main__':
    main()

Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))

A.sort()
A.reverse()

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

dp = [-N-N] * (N + 1)
dp[0] = 0

for i in range(1, N + 1):
  for j in A:
    if i >= match[j]:
      dp[i] = max(dp[i], dp[i - match[j]] + 1)

max_list = []
while N:
  for j in A:
    if N >= match[j] and dp[N] == dp[N - match[j]] + 1:
      max_list.append(j)
      N -= match[j]
      break

for j in max_list:
  print(j, end="")
print("")

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n,m=map(int,input().split())
    a=list(map(int,input().split()))
    a.sort(reverse=True)
    cost=[0,2,5,5,4,5,6,3,7,6]
    dp=[0]*(n+10)
    ans=[]
    for i in range(n+1):
        for j in a:
            dp[i+cost[j]]=max(dp[i]+1,dp[i+cost[j]])
    cnt=n
    while cnt>0:
        for j in a:
            if dp[cnt]==1:
                if cost[j]==cnt:
                    ans.append(j)
                    cnt-=cost[j]
                    break
            elif dp[cnt-cost[j]]==dp[cnt]-1:
                ans.append(j)
                cnt-=cost[j]
                break
    print(''.join(map(str,ans)))

if __name__ == '__main__':
    main()

Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))

A.sort()
A.reverse()

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

dp = [-N-N] * (N + 1)
dp[0] = 0

for i in range(1, N + 1):
  for j in A:
    if i >= match[j]:
      dp[i] = max(dp[i], dp[i - match[j]] + 1)

max_list = []
while N:
  for j in A:
    if N >= match[j] and dp[N] == dp[N - match[j]] + 1:
      max_list.append(j)
      N -= match[j]
      break

for j in max_list:
  print(j, end="")
print("")