Buggy python submission (verdict: Time Limit Exceeded):
def main():
	n, m = list(map(int, input().split()))
	nums = list(map(int, input().split()))
	
	d = {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6}
	num2match = {key:val for key, val in d.items() if key in nums}
	
	dp = [-1 for i in range(n + 1)]

	dp[0] = 0
	val_set = set(num2match.values())
	match2num = {v:max([int(key) for key, val in num2match.items() if val == v]) for v in val_set}
 
	for i in range(2, n + 1):
		for val in val_set:
			if i >= val and dp[i - val] > 0:
				tmp = match2num[val] * (10 ** len(str(dp[i - val]))) + dp[i - val]
				dp[i] = max(dp[i], tmp)
			elif i >= val and dp[i - val] == 0:
				tmp = match2num[val]
				dp[i] = max(dp[i], tmp)
	
	#print(num2match)
	#print(match2num)
	#print(dp)
	print(int(dp[n]))
 
if __name__ == '__main__':
	main()

Fixed submission:
import math
def main():
	n, m = list(map(int, input().split()))
	nums = list(map(int, input().split()))
	
	d = {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6}
	num2match = {key:val for key, val in d.items() if key in nums}
	
	dp = [-1 for i in range(n + 1)]
	dp_keta = [0 for i in range(n + 1)]

	dp[0] = 0
	val_set = list(set(num2match.values()))
	match2num = {v:max([int(key) for key, val in num2match.items() if val == v]) for v in val_set}

	cnt = 0
	for i in range(2, n + 1):
		for val in val_set:
			#print(val)
			cnt += 1
			if i >= val and dp[i - val] >= 0:
				tmp = match2num[val] * (10 ** dp_keta[i - val]) + dp[i - val]
				dp[i] = max(dp[i], tmp)
				dp_keta[i] = max(dp_keta[i], dp_keta[i - val] + 1)

	#print(num2match)
	#print(match2num)
	#print(dp)
	#print(dp)
	print(int(dp[n]))
	#print(cnt)
 
if __name__ == '__main__':
	main()

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

def main():
	n, m = list(map(int, input().split()))
	nums = list(map(int, input().split()))

	num2match = {key:val for key, val in {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6}.items() if key in nums}
	dp = ["na" for i in range(n + 1)] # マッチをn本使った時に作れる最大の数

	dp[0] = ""
	val_set = set(num2match.values())
	match2num = {v:max([key for key, val in num2match.items() if val == v]) for v in val_set}

	for i in range(2, n + 1):
		for val in val_set:
			if i >= val and dp[i - val] != "na":
				tmp = str(match2num[val]) + str(dp[i - val])
				if dp[i] == "na" or int(tmp) >= int(dp[i]):
					dp[i] = tmp

	print(int(dp[n]))

if __name__ == '__main__':
	main()

Fixed submission:
import math
def main():
	n, m = list(map(int, input().split()))
	nums = list(map(int, input().split()))
	
	d = {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6}
	num2match = {key:val for key, val in d.items() if key in nums}
	
	dp = [-1 for i in range(n + 1)]
	dp_keta = [0 for i in range(n + 1)]

	dp[0] = 0
	val_set = list(set(num2match.values()))
	match2num = {v:max([int(key) for key, val in num2match.items() if val == v]) for v in val_set}

	cnt = 0
	for i in range(2, n + 1):
		for val in val_set:
			#print(val)
			cnt += 1
			if i >= val and dp[i - val] >= 0:
				tmp = match2num[val] * (10 ** dp_keta[i - val]) + dp[i - val]
				dp[i] = max(dp[i], tmp)
				dp_keta[i] = max(dp_keta[i], dp_keta[i - val] + 1)

	#print(num2match)
	#print(match2num)
	#print(dp)
	#print(dp)
	print(int(dp[n]))
	#print(cnt)
 
if __name__ == '__main__':
	main()