Find the largest integer that can be formed with exactly N matchsticks, under the following conditions:
 - Every digit in the integer must be one of the digits A_1, A_2, ..., A_M (1 \leq A_i \leq 9).
 - The number of matchsticks used to form digits 1, 2, 3, 4, 5, 6, 7, 8, 9 should be 2, 5, 5, 4, 5, 6, 3, 7, 6, respectively.

-----Constraints-----
 - All values in input are integers.
 - 2 \leq N \leq 10^4
 - 1 \leq M \leq 9
 - 1 \leq A_i \leq 9
 - A_i are all different.
 - There exists an integer that can be formed by exactly N matchsticks under the conditions.

-----Input-----
Input is given from Standard Input in the following format:
N M
A_1 A_2 ... A_M

-----Output-----
Print the largest integer that can be formed with exactly N matchsticks under the conditions in the problem statement.

-----Sample Input-----
20 4
3 7 8 4

-----Sample Output-----
777773

The integer 777773 can be formed with 3 + 3 + 3 + 3 + 3 + 5 = 20 matchsticks, and this is the largest integer that can be formed by 20 matchsticks under the conditions.

Difficulty: MEDIUM

Solutions:
(n, m) = map(int, input().split())
D = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
A = list(map(int, input().split()))
C = [(a, D[a]) for a in A]
C.sort(reverse=True)
dp = [-1] * (n + 1)
dp[0] = 0
for i in range(1, n + 1):
	for (a, c) in C:
		if i >= c and dp[i - c] >= 0:
			dp[i] = max(dp[i - c] + 1, dp[i])
now = n
ans = ''
while now:
	for (a, c) in C:
		if now - c >= 0 and dp[now] == dp[now - c] + 1:
			ans += str(a)
			now -= c
			break
print(ans)


def main():
	(n, m) = map(int, input().split())
	a = set(map(int, input().split()))

	def bigger(x, y):
		if len(x) > len(y):
			return x
		elif len(x) < len(y):
			return y
		elif x < y:
			return y
		else:
			return x
	l = []
	if 1 in a:
		l.append((2, '1'))
	if 7 in a:
		l.append((3, '7'))
	if 4 in a:
		l.append((4, '4'))
	if 5 in a:
		l.append((5, '5'))
	elif 3 in a:
		l.append((5, '3'))
	elif 2 in a:
		l.append((5, '2'))
	if 9 in a:
		l.append((6, '9'))
	elif 6 in a:
		l.append((6, '6'))
	if 8 in a:
		l.append((7, '8'))
	dp = [None] * (n + 1)
	dp[0] = ''
	for i in range(1, n + 1):
		for (cost, char) in l:
			if i - cost < 0 or dp[i - cost] is None:
				continue
			candidate = max(dp[i - cost] + char, char + dp[i - cost])
			if dp[i] is None:
				dp[i] = candidate
			else:
				dp[i] = bigger(dp[i], candidate)
	print(dp[n])
main()


(N, M) = list(map(int, input().split()))
A = list(map(int, input().split()))
A.sort(reverse=True)
INF = float('inf')
dp = [-INF] * (N + 5)
needs = [2, 5, 5, 4, 5, 6, 3, 7, 6]
can_use = {k: needs[k - 1] for k in A}
dp[0] = 0
for i in range(2, N + 1):
	for v in list(can_use.values()):
		if i - v < 0:
			continue
		dp[i] = max(dp[i], dp[i - v] + 1)
ans = ''
i = N
while i != 0:
	for (k, v) in list(can_use.items()):
		if i - v < 0:
			continue
		while dp[i] == dp[i - v] + 1:
			ans += str(k)
			i -= v
print(ans)


def main():
	(n, m, *a) = list(map(int, open(0).read().split()))
	t = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
	a.sort(reverse=True)
	dp = [-float('Inf')] * (n + 10)
	dp[0] = 0
	for i in range(2, n + 1):
		for j in a:
			dp[i] = max(dp[i], dp[i - t[j]] + 1)
	nums = []
	k = dp[n]
	while n:
		for x in a:
			if dp[n - t[x]] == k - 1:
				nums.append(x)
				n -= t[x]
				break
		k -= 1
	ans = ''.join(map(str, nums))
	print(ans)

def __starting_point():
	main()
__starting_point()


(N, _) = map(int, input().split())
A = [int(i) for i in input().split()]
INF = '-'

def chmax(a: str, b: str):
	if a == INF:
		return b
	elif len(a) < len(b):
		return b
	elif len(a) == len(a):
		if a < b:
			return b
	return a
cnt = dict(zip(range(1, 10), [2, 5, 5, 4, 5, 6, 3, 7, 6]))
dp = [INF] * 10 ** 5
dp[0] = ''
for i in range(N):
	if dp[i] == INF:
		continue
	for a in A:
		dp[i + cnt[a]] = chmax(dp[i + cnt[a]], dp[i] + chr(ord('0') + a))
print(dp[N])


def main():
	(n, m) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	nm = [999, 2, 5, 5, 4, 5, 6, 3, 7, 6]
	dig = [None for i in range(8)]
	for x in sorted(a):
		dig[nm[x]] = x
	dp = [None for j in range(n + 1)]
	dp[0] = (0, None, None)
	for j in range(1, n + 1):
		for use in [2, 3, 4, 5, 6, 7]:
			jp = j - use
			if jp < 0:
				continue
			if dig[use] is None:
				continue
			if dp[jp] is None:
				continue
			d = dp[jp][0] + 1
			tup = (d, jp, dig[use])
			if dp[j] is None or dp[j][0] < d or (dp[j][0] == d and dp[j][2] < dig[use]):
				dp[j] = tup
	res = []
	j = n
	while True:
		(d, jp, i) = dp[j]
		if jp is None:
			break
		res.append(str(i))
		j = jp
	assert len(res) == dp[n][0]
	print(''.join(res))

def __starting_point():
	main()
__starting_point()


(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('')


def max_for_str(a, b):
	if len(a) > len(b):
		return a
	elif len(a) < len(b):
		return b
	else:
		for (c1, c2) in zip(a, b):
			if c1 > c2:
				return a
			elif c1 < c2:
				return b
		return a
match = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
(N, M) = map(int, input().split())
As = list(input().split())
dp = [None] * (N + 1)
dp[0] = ''
for i in range(N + 1):
	for A in As:
		if i - match[int(A)] >= 0 and (not dp[i - match[int(A)]] is None):
			if dp[i] is None:
				dp[i] = A + dp[i - match[int(A)]]
			else:
				dp[i] = max_for_str(dp[i], A + dp[i - match[int(A)]])
print(dp[N])


from collections import defaultdict
(N, M) = map(int, input().split())
A = list(map(int, input().split()))
L = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
D = defaultdict(int)
for i in A:
	if D[L[i]] < i:
		D[L[i]] = i
INF = float('inf')
dp = [-INF] * (N + 1)
dp[0] = 0
for i in range(1, N + 1):
	for j in D.keys():
		if i - j >= 0:
			dp[i] = max(dp[i], dp[i - j] + 1)
		else:
			dp[i] = max(dp[i], -INF)
ans = ''
temp = N
for (i, x) in sorted(D.items(), key=lambda x: x[1], reverse=True):
	while temp - i >= 0 and dp[temp - i] == dp[temp] - 1:
		ans += str(x)
		temp -= i
print(ans)


(n, m) = list(map(int, input().split()))
sz = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
l = []
dp = [0] * (n + 1)
l = list(map(int, input().split()))
l.sort(reverse=True)
dp[0] = 1
for i in range(1, n + 1):
	for j in l:
		if i - sz[j] >= 0 and dp[i - sz[j]] > 0:
			dp[i] = max(dp[i], 1 + dp[i - sz[j]])
ans = []
cur = n
while cur > 0:
	for j in l:
		if cur - sz[j] >= 0:
			if dp[cur] == 1 + dp[cur - sz[j]] and dp[cur - sz[j]] > 0:
				ans.append(j)
				cur -= sz[j]
				break
ans.sort(reverse=True)
print(''.join(map(str, ans)))


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
d = [10 ** 5, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [(0, 0)] * (n + 1)
for i in range(n):
	if i > 0 and dp[i][0] == 0:
		continue
	(k, _) = dp[i]
	for ai in a:
		if i + d[ai] <= n:
			dp[i + d[ai]] = max(dp[i + d[ai]], (k + 1, ai))
ans = []
i = n
for _ in range(dp[n][0]):
	ans.append(str(dp[i][1]))
	i -= d[dp[i][1]]
print(''.join(ans))


(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
num_of_matches = [6, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = ['x'] * (n + 8)
dp[0] = ''
for i in range(n + 1):
	if dp[i] == 'x':
		continue
	for aa in a:
		x = dp[i + num_of_matches[aa]]
		y = dp[i] + str(aa)
		if x == 'x':
			dp[i + num_of_matches[aa]] = y
		elif len(x) < len(y):
			dp[i + num_of_matches[aa]] = y
		elif len(x) == len(y):
			if x < y:
				dp[i + num_of_matches[aa]] = y
print(dp[n])


def num_to_match(n):
	if n == 1:
		return 2
	elif n == 7:
		return 3
	elif n == 4:
		return 4
	elif n in [2, 3, 5]:
		return 5
	elif n in [6, 9]:
		return 6
	else:
		return 7

def max_length(N, match):
	match = set(match)
	INF = 10 ** 15
	dp = [-INF for _ in range(N + 1)]
	dp[0] = 0
	for n in range(1, N + 1):
		max_l = dp[n]
		for m in match:
			if n - m >= 0 and dp[n - m] >= 0:
				max_l = max([max_l, 1 + dp[n - m]])
		dp[n] = max_l
	return dp

def main():
	(N, _) = list(map(int, input().split(' ')))
	A = list(map(int, input().split(' ')))
	A.sort(reverse=True)
	match = list(map(num_to_match, A))
	max_l = max_length(N, match)
	digits = [0 for _ in range(max_l[N])]
	n_match = N
	for i in range(max_l[N]):
		for a in A:
			ma = num_to_match(a)
			if n_match - ma < 0:
				continue
			if max_l[n_match - ma] == max_l[n_match] - 1:
				digits[i] = a
				n_match -= ma
				break
	print(''.join(map(str, digits)))

def __starting_point():
	main()
__starting_point()


import heapq
(N, M) = map(int, input().split())
A = list(map(int, input().split()))
B = [6, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [-1 for _ in range(N + 10)]
dp[0] = 0
A.sort()
visited = [False for _ in range(N + 10)]
h = []
heapq.heappush(h, (0, 0))
while h:
	(now_n, keta) = heapq.heappop(h)
	if visited[now_n]:
		continue
	visited[now_n] = True
	for a in A:
		nxt_n = now_n + B[a]
		if nxt_n >= N + 10:
			continue
		dp[nxt_n] = max(dp[nxt_n], dp[now_n] + 1)
		heapq.heappush(h, (nxt_n, dp[now_n] + 1))
A.sort(reverse=True)
ANS = []
zan = N
for i in range(dp[N], 0, -1):
	bugf = True
	for a in A:
		if dp[zan - B[a]] == dp[zan] - 1:
			ANS.append(str(a))
			zan = zan - B[a]
			bugf = False
			break
	if bugf:
		print('bug...')
print(''.join(ANS))


ma = lambda : map(int, input().split())
lma = lambda : list(map(int, input().split()))
tma = lambda : tuple(map(int, input().split()))
ni = lambda : int(input())
yn = lambda fl: print('Yes') if fl else print('No')
import collections
import math
import itertools
import heapq as hq
cnt = [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6]
(n, m) = ma()
A = lma()
use = [(cnt[a], a) for a in A]
use.sort(key=lambda x: x[1], reverse=True)
INF = 10 ** 9
dp = [0] + [-INF] * (n + 10)
for i in range(n):
	if dp[i] == -INF:
		continue
	for (c, a) in use:
		dp[i + c] = max(dp[i + c], dp[i] + 1)
l = dp[n]
ans = ['0'] * l
num = n
for i in range(l):
	for (c, a) in use:
		if dp[num - c] == dp[num] - 1:
			ans[i] = str(a)
			num -= c
			break
print(''.join(ans))


(N, M) = list(map(int, input().split()))
A = list(map(int, input().split()))
A.sort(reverse=True)
match = [10 ** 18, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [-10 ** 18] * (N + 1)
dp[0] = 1
for i in range(N):
	for a in A:
		m = match[a]
		if i + m <= N and dp[i + m] < dp[i] + 1:
			dp[i + m] = dp[i] + 1
ans = []
while N > 0:
	for a in A:
		m = match[a]
		if N - m >= 0 and dp[N - m] + 1 == dp[N]:
			ans.append(a)
			N -= m
			break
ans.sort(reverse=True)
print(''.join(map(str, ans)))


(N, M) = list(map(int, input().split()))
A = list(map(int, input().split()))
f = [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [None] * (N + 1)
dp[0] = ''

def max_str(s1: str, s2: str) -> str:
	(l1, l2) = (len(s1), len(s2))
	if l1 != l2:
		return s1 if l1 > l2 else s2
	if l1 == l2:
		return s1 if s1 > s2 else s2
for i in range(2, N + 1):
	nxt = ''
	for aj in A:
		if i - f[aj] < 0 or dp[i - f[aj]] == None:
			continue
		nxt = max_str(nxt, dp[i - f[aj]] + str(aj))
	if nxt != '':
		dp[i] = nxt
print(dp[N])


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 = [-n - n] * (n + 1)
	dp[0] = 0
	ans = []
	for i in range(1, n + 1):
		for j in a:
			if i >= cost[j]:
				dp[i] = max(dp[i], dp[i - cost[j]] + 1)
	cnt = n
	while cnt:
		for j in a:
			if cnt >= cost[j] and dp[cnt - cost[j]] == dp[cnt] - 1:
				ans.append(j)
				cnt -= cost[j]
				break
	for j in ans:
		print(j, end='')
	print('')

def __starting_point():
	main()
__starting_point()


(N, M) = list(map(int, input().split()))
A = list(map(int, input().split()))
A.sort(reverse=True)
f = [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [-float('inf')] * (N + 1)
dp[0] = 0
for i in range(N + 1):
	nxt = dp[i]
	for aj in A:
		if i - f[aj] < 0:
			continue
		nxt = max(nxt, dp[i - f[aj]] + 1)
	dp[i] = nxt
keta = dp[N]
ans = ''
matchs = N
while keta > 0:
	for aj in A:
		if matchs - f[aj] < 0:
			continue
		if dp[matchs - f[aj]] == keta - 1:
			ans += str(aj)
			matchs -= f[aj]
			keta -= 1
			break
print(ans)


(N, M) = map(int, input().split())
A = list(map(int, input().split()))
T = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
B = []
for i in A:
	B.append(T[i])
B = sorted(list(set(B)))
dp = [0 for i in range(N + 1)]
AB = [[i, T[i]] for i in A]
AB = sorted(AB, reverse=True)
for i in range(1, N + 1):
	for j in B:
		if i - j >= 0 and dp[i - j] != -1:
			dp[i] = max(dp[i], dp[i - j] + 1)
	if dp[i] == 0:
		dp[i] = -1
c = N
U = [0 for i in range(8)]
while c > 0:
	for i in range(len(AB)):
		if c - AB[i][1] >= 0 and dp[c] == dp[c - AB[i][1]] + 1:
			U[AB[i][1]] += 1
			c -= AB[i][1]
			break
ans = ''
if 9 in A:
	ans += '9' * U[6]
	U[6] = 0
if 8 in A:
	ans += '8' * U[7]
if 7 in A:
	ans += '7' * U[3]
if 6 in A:
	ans += '6' * U[6]
if 5 in A:
	ans += '5' * U[5]
	U[5] = 0
if 4 in A:
	ans += '4' * U[4]
if 3 in A:
	ans += '3' * U[5]
	U[5] = 0
if 2 in A:
	ans += '2' * U[5]
if 1 in A:
	ans += '1' * U[2]
print(ans)


import sys
input = sys.stdin.readline
(N, M) = map(int, input().split())
A = list(map(int, input().split()))
num = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [-float('inf')] * (N + 1)
dp[0] = 0
for a in A:
	if num[a] <= N:
		dp[num[a]] = 1
for i in range(1, N + 1):
	for a in A:
		if i - num[a] >= 0:
			dp[i] = max(dp[i], dp[i - num[a]] + 1)
A.sort(reverse=True)
ans = ''
for i in range(dp[N]):
	for a in A:
		if N - num[a] >= 0 and dp[N - num[a]] == dp[N] - 1:
			ans += str(a)
			N -= num[a]
			break
print(ans)


v = [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6]
(N, M) = map(int, input().split())
A = sorted(map(int, input().split()), reverse=True)
dp = [0] * (N + 999)
mp = [[] for _ in range(N + 999)]
for a in reversed(A):
	dp[v[a]] = 1
	mp[v[a]] = [a]
for i in range(N + 1):
	for a in A:
		if i - v[a] >= 0 and dp[i - v[a]] > 0 and (dp[i] < 1 + dp[i - v[a]]):
			dp[i] = 1 + dp[i - v[a]]
			mp[i] = [a] + mp[i - v[a]]
print(''.join(map(str, mp[N])))


def main():
	(N, M) = map(int, input().split())
	A = set(map(int, input().split()))
	cost = [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6]
	B = []
	for i in range(1, 10):
		if i in A:
			B.append((cost[i], i))
	B = B[::-1]
	dp = [[-1, -1] for i in range(N + 1)]
	dp[0][0] = 0
	for i in range(1, N + 1):
		for (x, y) in B:
			if i >= x and dp[i - x][0] >= 0:
				if dp[i][0] < dp[i - x][0] + 1:
					dp[i][0] = dp[i - x][0] + 1
					dp[i][1] = y
	id = N
	ans = []
	while id > 0:
		ans.append(dp[id][1])
		id -= cost[dp[id][1]]
	ans.sort(reverse=True)
	print(''.join(map(str, ans)))
main()


cost = [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6]
(N, M) = map(int, input().split())
A = list(map(int, input().split()))
dp = [None] * (N + 1)
dp[0] = ''
for i in range(1, N + 1):
	for a in A:
		if i - cost[a] >= 0 and dp[i - cost[a]] != None:
			s = dp[i - cost[a]] + str(a)
			if dp[i] == None or len(dp[i]) < len(s):
				dp[i] = s
			elif len(dp[i]) > len(s):
				continue
			else:
				dp[i] = max(dp[i], s)
print(dp[N])


(n, m) = map(int, input().split())
A = reversed(sorted(map(int, input().split())))
M = [2, 5, 5, 4, 5, 6, 3, 7, 6]
D = {}
E = {}
for a in A:
	if M[a - 1] in D:
		continue
	else:
		D[M[a - 1]] = a
		E[a] = M[a - 1]
S = list(reversed(sorted(E)))
dp = [0] * (n + 2)
for i in range(1, n + 2):
	cnt = -1
	for d in D:
		if i - d >= 0:
			if dp[i - d] != -1:
				cnt = max(dp[i - d] + 1, cnt)
	dp[i] = cnt
k = dp[n]
ans = ''
while k > 0:
	for s in S:
		x = E[s]
		if n - x < 0:
			continue
		if dp[n - x] == k - 1:
			ans += str(s)
			k -= 1
			n -= x
			break
print(ans)