AtCoder Beginner Contest 118 - Match Matching

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.

Source: atcoder | Difficulty: MEDIUM


Solutions:
(N, M) = map(int, input().split())
A = list(map(int, input().split()))
W = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [-1] * (N + 1)
dp[0] = 0
for i in range(N + 1):
	for a in A:
		if i + W[a] < N + 1:
			dp[i + W[a]] = max(dp[i + W[a]], dp[i] * 10 + a)
print(dp[N])


num = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
dp = [0] * (n + 17)
for i in range(n + 1):
	for j in a:
		if i == 0 or dp[i] > 0:
			dp[i + num[j]] = max(dp[i + num[j]], dp[i] * 10 + j)
print(dp[n])


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


(n, m, *a) = map(int, open(0).read().split())
c = (0, 2, 5, 5, 4, 5, 6, 3, 7, 6)
d = [0] * n * 9
for i in range(n):
	for j in a:
		if d[i] or i < 1:
			d[i + c[j]] = max(d[i + c[j]], d[i] * 10 + j)
print(d[n])


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
d = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
memo = [0] * (n + 1)
memo[0] = 0
memo[1] = -10 ** 10
for i in range(2, n + 1):
	res = -10 ** 10
	for ai in a:
		if d[ai] > i:
			continue
		res = max(res, ai + memo[i - d[ai]] * 10)
	memo[i] = res
print(memo[n])


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
wv = []
for i in a:
	wv.append((b[i], i))
wv.sort()
dp = [-1 for i in range(n + 1)]
dp[0] = 0
for i in range(1, n + 1):
	for (w, v) in wv:
		if i - w < 0:
			continue
		if dp[i - w] == -1:
			continue
		dp[i] = max(dp[i], dp[i - w] * 10 + v)
print(dp[n])


(n, m) = map(int, input().split())
A = list(map(int, input().split()))
li = [2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [-1] * (n + 1)
dp[0] = 0
for i in range(n):
	for j in A:
		if i + li[j - 1] <= n:
			dp[i + li[j - 1]] = max(dp[i + li[j - 1]], dp[i] * 10 + j)
print(dp[-1])


(n, m, *a) = map(int, open(0).read().split())
d = [0] * -~n
for i in range(1, n + 1):
	for (j, k) in zip((1, 2, 3, 4, 5, 6, 7, 8, 9), (2, 5, 5, 4, 5, 6, 3, 7, 6)):
		if i - k >= 0 and (i - k < 1 or d[i - k]) and (j in a):
			d[i] = max(d[i], d[i - k] * 10 + j)
print(d[n])


import sys
sr = lambda : sys.stdin.readline().rstrip()
ir = lambda : int(sr())
lr = lambda : list(map(int, sr().split()))
(N, M) = lr()
A = lr()
dp = [-1] * (N + 1)
dp[0] = 0
for x in range(N):
	for a in A:
		y = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6][a]
		if x + y > N:
			continue
		dp[x + y] = max(dp[x + y], dp[x] * 10 + a)
answer = dp[N]
print(answer)


(N, M) = map(int, input().split())
A = list(map(int, input().split()))
A.sort(reverse=True)
req = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
t = [-1] * (N + 1)
t[0] = 0
for a in A:
	q = req[a]
	for i in range(q, N + 1):
		t[i] = max(t[i], t[i - q] * 10 + a)
print(t[-1])


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


(n, m, *a) = map(int, open(0).read().split())
d = [0] * n + [-1] * 9
for i in range(n):
	d[i + 1] = max((d[i - int('0144345265'[j])] * 10 + j for j in a))
print(d[n])


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
match = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
d = dict()
for e in a:
	d[e] = match[e]
dp = [-1] * (n + 1)
dp[0] = 0
for i in range(1, n + 1):
	for (k, v) in d.items():
		if i - v >= 0 and dp[i - v] != -1:
			dp[i] = max(dp[i], dp[i - v] * 10 + k)
print(dp[n])


import sys
readline = sys.stdin.readline
(N, M) = map(int, readline().split())
A = set(list(map(int, readline().split())))
dic = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
candi = [[], [], [1], [7], [4], [2, 3, 5], [6, 9], [8]]
if 2 in A and 3 in A:
	A.remove(2)
if 2 in A and 5 in A:
	A.remove(2)
if 3 in A and 5 in A:
	A.remove(3)
if 6 in A and 9 in A:
	A.remove(6)
for i in range(len(candi)):
	for j in range(len(candi[i]) - 1, -1, -1):
		if candi[i][j] not in A:
			del candi[i][j]
dp = [-1] * (N + 1)
ans = [''] * (N + 1)
dp[0] = 0

def to_num(x):
	return int(''.join(sorted(x, reverse=True)))
for i in range(len(candi)):
	if not candi[i]:
		continue
	num = str(candi[i][0])
	for j in range(len(dp)):
		if dp[j] == -1:
			continue
		if j + i > N:
			continue
		if dp[j + i] < dp[j] + 1:
			dp[j + i] = dp[j] + 1
			ans[j + i] = ans[j] + num
		elif dp[j + i] == dp[j] + 1:
			if to_num(ans[j + i]) < to_num(ans[j] + num):
				ans[j + i] = ans[j] + num
print(to_num(ans[N]))


(n, m) = map(int, input().split())
List = list(map(int, input().split()))
d = [0] * n * 9 + [-1] * n * 9
for i in range(1, n + 1):
	d[i] = max((d[i - int('0255456376'[a])] * 10 + a for a in List))
print(d[n])


INF = 2 ** 50

def solve(n, m, a):
	c = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
	dp = [-INF] * (n + 10)
	dp[0] = 0
	for d in range(n + 1):
		for i in range(m):
			nd = d + c[a[i]]
			dp[nd] = max(dp[nd], 10 * dp[d] + a[i])
	return dp[n]
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
print(solve(n, m, a))


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) = map(int, input().split())
A = list(map(int, input().split()))
cost = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [-1] * 10010
dp[0] = 0
for i in range(1, n + 1):
	for a in A:
		if i - cost[a] >= 0:
			dp[i] = max(dp[i], a + dp[i - cost[a]] * 10)
print(dp[n])


import sys
import math
from collections import deque
sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda : sys.stdin.readline().strip()
NI = lambda : int(input())
NMI = lambda : map(int, input().split())
NLI = lambda : list(NMI())
SI = lambda : input()

def make_grid(h, w, num):
	return [[int(num)] * w for _ in range(h)]

def main():
	(N, M) = NMI()
	A = NLI()
	match_dict = {i + 1: m for (i, m) in enumerate([2, 5, 5, 4, 5, 6, 3, 7, 6])}
	usable_list = [[a, match_dict[a]] for a in A]
	usable_list.sort(key=lambda x: (x[1], -x[0]))
	dp = [-1] * (N + 10)
	dp[0] = 0
	for i in range(N + 1):
		for (num, m) in usable_list:
			dp[i + m] = max(dp[i + m], dp[i] * 10 + num)
	print(dp[N])

def __starting_point():
	main()
__starting_point()


(n, m, *a) = map(int, open(0).read().split())
d = [0] * n * 9
for i in range(n):
	for j in a:
		c = i + int('0255456376'[j])
		d[c] = max(d[c], (d[i] * 10 + j) * (i < 1 or d[i] > 0))
print(d[n])


(N, M) = map(int, input().split())
A = [int(a) for a in input().split()]
L = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
A.sort(reverse=True)
dp = [0] + [-1] * N
for i in range(1, N + 1):
	for a in A:
		if L[a] <= i:
			dp[i] = max(dp[i], dp[i - L[a]] * 10 + a)
print(dp[-1])


(N, M) = map(int, input().split())
A = list(map(int, input().split()))
B = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
DP = [-1] * (N + 1)
DP[0] = 0
for i in range(N):
	for j in A:
		if i + B[j] <= N:
			DP[i + B[j]] = max(DP[i] * 10 + j, DP[i + B[j]])
print(DP[N])


(n, m) = map(int, input().split())
x = [2, 5, 5, 4, 5, 6, 3, 7, 6]
a = []
for i in map(int, input().split()):
	a.append([i, x[i - 1]])
a.sort(reverse=True)
dp = [0] * (n + 1)
for i in range(m):
	for j in range(n + 1):
		if j == 0 or dp[j] != 0:
			if j + a[i][1] <= n:
				dp[j + a[i][1]] = max(dp[j + a[i][1]], dp[j] * 10 + a[i][0])
print(dp[n])


(n, m) = map(int, input().split())
value = sorted(list(map(int, input().split())), reverse=True)
weight = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [-1] * (n + 1)
dp[0] = 0
for i in range(n + 1):
	for j in value:
		if i + weight[j] < n + 1:
			dp[i + weight[j]] = max(dp[i + weight[j]], dp[i] * 10 + j)
print(dp[n])


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
inf = float('inf')
dp = [-inf] * (n + 1)
dp[0] = 0
for i in range(1, n + 1):
	for j in a:
		cost_j = int('0255456376'[j])
		if i - cost_j >= 0:
			dp[i] = max(dp[i], j + dp[i - cost_j] * 10)
print(dp[n])


match = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
(n, m) = map(int, input().split())
a = [int(_) for _ in input().split()]
dp = [-1] * (n + 100)
dp[0] = 0
for i in range(n + 1):
	if dp[i] == -1:
		continue
	else:
		for j in a:
			dp[i + match[j]] = max(dp[i + match[j]], dp[i] * 10 + j)
print(dp[n])


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


(n, m) = map(int, input().split())
A = list(map(int, input().split()))
A.sort(reverse=1)
use_list = [2, 5, 5, 4, 5, 6, 3, 7, 6]
ans = [-1] * (n + 1)
ans[0] = 0
for a in A:
	ma = use_list[a - 1]
	for i in range(n + 1):
		if ans[i] == -1:
			continue
		if i + ma > n:
			break
		ans[i + ma] = max(ans[i + ma], ans[i] * 10 + a)
print(ans[n])


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, m) = map(int, input().split())
a = list(map(int, input().split()))
maxnum = [-1] * (n + 1)
cost = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
maxnum[0] = 0
for i in range(n + 1):
	for num in a:
		if i + cost[num] < n + 1:
			maxnum[i + cost[num]] = max(maxnum[i + cost[num]], num + maxnum[i] * 10)
print(maxnum[n])


(n, m) = map(int, input().split())
a = set(map(int, input().split()))
dp = [0] + [-float('inf')] * n
f = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
for i in range(n + 1):
	for j in a:
		F = f[j]
		if F <= i:
			dp[i] = max(dp[i - F] * 10 + j, dp[i])
print(dp[n])


(N, M) = map(int, input().split())
A = list(map(int, input().split()))
dp = [-1] * (N + 1)
need = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp[0] = 0
for i in range(N + 1):
	for j in A:
		if i + need[j] > N:
			continue
		dp[i + need[j]] = max(dp[i + need[j]], dp[i] * 10 + j)
print(dp[N])


(N, M) = map(int, input().split())
A = list(reversed(sorted(map(int, input().split()))))
c = [2, 5, 5, 4, 5, 6, 3, 7, 6]
x = [0] * (N + 1)
for i in range(1, N + 1):
	x[i] = max([-1] + [x[i - c[a - 1]] + 1 for a in A if i >= c[a - 1]])
n = N
for _ in range(N):
	for a in A:
		if n >= c[a - 1] and x[n - c[a - 1]] == x[n] - 1:
			print(a, end='')
			n -= c[a - 1]
			break
print('')


(N, M) = map(int, input().split())
A = list(map(int, input().split()))
num = [2, 5, 5, 4, 5, 6, 3, 7, 6]
ans = {}
for i in range(N + 1):
	x = 0
	for j in A:
		k = num[j - 1]
		if i - k < 0:
			continue
		if i == k:
			x = max(x, j)
			continue
		an = ans[i - k]
		if an == 0:
			continue
		x = max(x, an * 10 + j)
	ans[i] = x
print(ans[N])


(N, M) = map(int, input().split())
A = list(map(int, input().split()))
sorted(A)
B = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
C = []
for i in A:
	C.append(B[i])
dp = [-1] * (N + 1)
dp[0] = 0
for i in range(N + 1):
	for a in range(len(C)):
		if i + C[a] <= N and 0 <= dp[i]:
			dp[i + C[a]] = max(dp[i] * 10 + A[a], dp[i + C[a]])
print(dp[-1])


(n, m) = (int(x) for x in input().split())
A = sorted([int(x) for x in input().split()])[::-1]
cost = (0, 2, 5, 5, 4, 5, 6, 3, 7, 6)
dp = [[-1] * (n + 1) for _ in range(m + 1)]
for i in range(m + 1):
	dp[i][0] = 0
for i in range(m):
	for j in range(1, n + 1):
		dp[i + 1][j] = max(dp[i + 1][j], dp[i][j])
		if j >= cost[A[i]]:
			if dp[i + 1][j - cost[A[i]]] == -1:
				dp[i + 1][j] = max(dp[i + 1][j], dp[i][j])
			else:
				dp[i + 1][j] = max(dp[i + 1][j], 10 * dp[i + 1][j - cost[A[i]]] + A[i])
print(dp[m][n])


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
mch = (100, 2, 5, 5, 4, 5, 6, 3, 7, 6)
dp = [-1] * (n + 10)
dp[0] = 0
for i in range(n + 1):
	for aj in a:
		if dp[i - mch[aj]] >= 0:
			dp[i] = max(dp[i], dp[i - mch[aj]] * 10 + aj)
print(dp[n])


import sys
sys.setrecursionlimit(2147483647)
INF = float('inf')
MOD = 10 ** 9 + 7
input = sys.stdin.readline

def resolve():
	W = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
	(n, m) = map(int, input().split())
	A = list(map(int, input().split()))
	dp = [-1] * (n + 1)
	dp[0] = 0
	from itertools import product
	for (i, a) in product(range(n + 1), A):
		if i - W[a] < 0:
			continue
		dp[i] = max(dp[i], dp[i - W[a]] * 10 + a)
	print(dp[n])
resolve()


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


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
ma = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
a = sorted(a)
dp = [-1] * (n + 1)
dp[0] = 0
for i in range(m - 1, -1, -1):
	for j in range(n + 1):
		if dp[j] != -1 and j + ma[a[i]] <= n:
			dp[j + ma[a[i]]] = max(10 * dp[j] + a[i], dp[j + ma[a[i]]])
print(dp[n])


(n, m) = map(int, input().split())
(*a,) = map(int, input().split())
cost = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
dp = [-1] * (n + 1)
v = [0] * (n + 1)
dp[0] = 0
for i in range(1, n + 1):
	for j in range(10)[::-1]:
		if not j in a or i < cost[j]:
			continue
		if dp[i] < dp[i - cost[j]] + 1:
			dp[i] = dp[i - cost[j]] + 1
			v[i] = j
p = n
while p != 0:
	print(v[p], end='')
	p -= cost[v[p]]
print()


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


(n, m) = map(int, input().strip().split())
a = list(map(int, input().strip().split()))
dp = [None] * (n + 1)
dp[0] = 0
x = (0, 2, 5, 5, 4, 5, 6, 3, 7, 6)
for i in range(2, n + 1):
	for v in a:
		if i - x[v] >= 0 and dp[i - x[v]] != None:
			if not dp[i]:
				dp[i] = dp[i - x[v]] * 10 + v
			else:
				dp[i] = max(dp[i], dp[i - x[v]] * 10 + v)
print(dp[n])


(nim, mike) = map(int, input().split())
array = list(map(int, input().split()))
dp = [-1 for i in range(nim + 10)]
l = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp[0] = 0
for i in range(nim - 1):
	for s in array:
		if dp[i] == -1 and i != 0:
			continue
		if dp[i + l[s]] < dp[i] * 10 + s:
			dp[i + l[s]] = dp[i] * 10 + s
print(dp[nim])


(n, m) = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
K = (0, 2, 5, 5, 4, 5, 6, 3, 7, 6)
A.sort()
DP = [0] * (n + 1)
for i in range(n):
	for a in A:
		if i + 1 - K[a] < 0:
			continue
		DP[i + 1] = max(DP[i + 1], DP[i + 1 - K[a]] * 10 + a)
	if DP[i + 1] == 0:
		DP[i + 1] = -1
print(DP[n])


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
c = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
f = [-1] * 10010
f[0] = 0
for i in range(1, n + 1):
	for x in a:
		if i - c[x] >= 0:
			f[i] = max(f[i], f[i - c[x]] * 10 + x)
print(f[n])


(n, m) = map(int, input().split())
l = list(map(int, input().split()))
l.sort()
x = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
d = dict()
for j in l:
	d[x[j]] = j
INF = float('inf')
dp = [-INF for i in range(n + 1)]
dp[0] = 0
l = d.keys()
for i in range(2, n + 1):
	for j in d.keys():
		if j <= i:
			dp[i] = max(dp[i], dp[i - j] * 10 + d[j])
print(dp[n])


(n, m) = map(int, input().split())
lst = list(map(int, input().split()))
weight = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
DP = [-1] * (n + 1)
DP[0] = 0
for i in range(n + 1):
	for x in lst:
		if i + weight[x] < n + 1:
			DP[i + weight[x]] = max(DP[i + weight[x]], DP[i] * 10 + x)
print(DP[n])


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])


matches = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
(n, m) = map(int, input().split())
alist = list(map(int, input().split()))
alist.sort(reverse=True)
dp = [-1 for _ in range(n + 1)]
dp[0] = 0
for a in alist:
	match = matches[a]
	for i in range(n - match + 1):
		if dp[i] == -1:
			continue
		dp[i + match] = max(dp[i + match], dp[i] * 10 + a)
print(dp[-1])


(n, m) = list(map(int, input().split()))
A = sorted(list(map(int, input().split())), reverse=True)
L = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
inf = float('inf')
dp = [-inf] * (n + 1)
dp[0] = 0
for i in range(1, n + 1):
	for j in range(m):
		num = L[A[j]]
		if i >= num:
			dp[i] = max(dp[i], dp[i - num] + 1)
sort_A = sorted(A, reverse=True)
ans = ''
while True:
	if n == 0:
		break
	for i in range(m):
		temp = sort_A[i]
		num = L[temp]
		if n >= num and dp[n - num] >= 0:
			if dp[n - num] == dp[n] - 1:
				ans += str(temp)
				n -= num
				break
print(int(ans))


(N, M) = map(int, input().split())
A = list(map(int, input().split()))
m = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
l = min([m[a] for a in A])
dp = [0] * (N + 1)
for i in range(N):
	for a in A:
		if i > 0 and dp[i] == 0:
			break
		if i + m[a] <= N:
			dp[i + m[a]] = max(dp[i + m[a]], dp[i] * 10 + a)
print(dp[-1])


(n, m) = map(int, input().split())
a = [int(i) for i in input().split()]
cost = [2, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [0 for i in range(n + 1)]
for i in range(1, n + 1):
	p = 0
	for j in a:
		if i - cost[j] >= 0 and (dp[i - cost[j]] != 0 or i - cost[j] == 0):
			p = max(p, int(dp[i - cost[j]] * 10 + j))
	dp[i] = p
print(dp[n])


from functools import lru_cache
(N, _) = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
costs = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]

@lru_cache(None)
def f(n):
	if n == 0:
		return 0
	values = [f(n - costs[a]) * 10 + a for a in A if n >= costs[a]]
	if len(values) > 0:
		return max(values)
	return -1
[f(i) for i in range(N)]
print(f(N))


import sys
input = sys.stdin.readline
INF = float('inf')

def main():
	(n, m) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	a.sort(reverse=True)
	cost = [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6]
	dp = [-1] * (n + 1)
	dp[0] = 0
	for i in range(1, n + 1):
		for aj in a:
			if i - cost[aj] < 0:
				continue
			dp[i] = max(dp[i], dp[i - cost[aj]] + 1)
	digits = dp[n]
	ans = ''
	rest = n
	while digits:
		for aj in a:
			if rest - cost[aj] < 0:
				continue
			if dp[rest - cost[aj]] == digits - 1:
				ans += str(aj)
				rest -= cost[aj]
				digits -= 1
				break
	print(ans)

def __starting_point():
	main()
__starting_point()


(n, m) = map(int, input().split())
b = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
a = [0] + list(map(int, input().split()))
f = [-1] * (n + 1)
f[0] = 0
for i in range(1, n + 1):
	for j in range(1, m + 1):
		x = a[j]
		if i - b[x] < 0:
			continue
		f[i] = max(f[i], f[i - b[x]] * 10 + x)
print(f[n])


import sys
input = sys.stdin.readline
(N, M) = map(int, input().split())
A = list(map(int, input().split()))
cnt = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [0] * (N + 1)
for i in range(N):
	if i > 0 and dp[i] == 0:
		continue
	for a in A:
		if i + cnt[a] > N:
			continue
		dp[i + cnt[a]] = max(dp[i + cnt[a]], dp[i] * 10 + a)
print(dp[N])


num_dic = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
(N, M) = list(map(int, input().split()))
t_dic = {}
A = list(map(int, input().split()))
A = sorted(A)[::-1]
for a in A:
	t_dic[a] = num_dic[a]
min_v = min(t_dic.values())
dp = [0] * (N + 8)
for i in range(N):
	for a in A:
		num = t_dic[a]
		if i != 0 and dp[i] == 0:
			continue
		dp[i + num] = max(10 * dp[i] + a, dp[i + num])
print(dp[N])


def main():
	(N, M, *A) = map(int, open(0).read().split())
	C = [0, 1, 4, 4, 3, 4, 5, 2, 6, 5]
	dp = [0] * N + [-1] * 6
	for i in range(N):
		dp[i + 1] = max((dp[i - C[a]] * 10 + a for a in A))
	print(dp[N])
main()


(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
matchlist = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
numberlist = []
for i in range(m):
	numberlist.append(matchlist[a[i]])
numberlist = set(numberlist)
dp = [-10000000] * (n + 1)
dp[0] = 0
for i in range(n):
	for j in numberlist:
		if i + j <= n:
			dp[i + j] = max(dp[i + j], dp[i] + 1)
ans = []
d = dp[n]
a.sort(reverse=True)
for i in range(d):
	for j in a:
		if n - matchlist[j] >= 0:
			if dp[n] == dp[n - matchlist[j]] + 1:
				n -= matchlist[j]
				ans.append(j)
				break
print(''.join(map(str, ans)))


L = [6, 2, 5, 5, 4, 5, 6, 3, 7, 6]
(N, M) = map(int, input().split())
A = [int(a) for a in input().split()]
B = [(A[i], L[A[i]]) for i in range(M)]
X = [0] + [-1] * N
for i in range(1, N + 1):
	for b in B:
		if b[1] <= i:
			X[i] = max(X[i], X[i - b[1]] * 10 + b[0])
print(X[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)))


import sys
input = sys.stdin.readline

def main():
	(N, M) = [int(x) for x in input().split()]
	A = [int(x) for x in input().split()]
	match = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
	dp = [0] * (N + 1)
	for i in range(1, N + 1):
		for a in A:
			if match[a] <= i:
				if match[a] == i or dp[i - match[a]] != 0:
					dp[i] = max(dp[i], dp[i - match[a]] * 10 + a)
	print(dp[-1])

def __starting_point():
	main()
__starting_point()


(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) = map(int, input().split())
a = list(map(int, input().split()))
table = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
dp = [-1] * (n + 1)
dp[0] = 0
for i in range(1, n + 1):
	mx = -1
	for j in a:
		if not 0 <= i - table[j] < n + 1:
			continue
		if dp[i - table[j]] < 0:
			continue
		if mx < dp[i - table[j]] * 10 + j:
			mx = dp[i - table[j]] * 10 + j
	dp[i] = mx
print(dp[n])


(n, m) = map(int, input().split())
a = [int(i) for i in input().split()]
l = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
inf = float('INF')
dp = [-inf] * (n + 10)
d = [-inf] * (n + 10)
dp[0] = 0
d[0] = 0
for i in range(n):
	if dp[i] == -inf:
		continue
	tmp = dp[i]
	k = d[i]
	for ai in a:
		dp[l[ai] + i] = max(dp[l[ai] + i], dp[i] + k * ai, dp[i] * 10 + ai)
		d[i + l[ai]] = max(d[i + l[ai]], k + 1)
print(dp[n])


(N, M) = map(int, input().split())
A = list(map(int, input().split()))
cost = [2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [-1 for _ in [0] * (N + 10)]
dp[0] = 0
for i in range(N + 1):
	for a in A:
		if i - cost[a - 1] < 0 or dp[i - cost[a - 1]] < 0:
			continue
		if dp[i - cost[a - 1]] * 10 + a > dp[i]:
			dp[i] = dp[i - cost[a - 1]] * 10 + a
print(dp[N])


import sys
sys.setrecursionlimit(10 ** 4)
from fractions import gcd
num = [10 ** 9, 2, 5, 5, 4, 5, 6, 3, 7, 6]

def main():
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	a.sort()
	dp = [[''] * (n + 1) for _ in range(len(a))]
	dp[0][num[a[0]]] = str(a[0])
	for i in range(num[a[0]], n + 1):
		if len(dp[0][i - num[a[0]]]) > 0:
			dp[0][i] = dp[0][i - num[a[0]]] + str(a[0])
	for i in range(1, len(a)):
		for j in range(n + 1):
			if j < num[a[i]]:
				dp[i][j] = dp[i - 1][j]
			elif j == num[a[i]] or dp[i][j - num[a[i]]] != '':
				buf = dp[i][j - num[a[i]]] + str(a[i])
				if len(buf) < len(dp[i - 1][j]):
					dp[i][j] = dp[i - 1][j]
				else:
					dp[i][j] = buf
			else:
				dp[i][j] = dp[i - 1][j]
	ans = list(dp[len(a) - 1][n])
	ans.sort(reverse=True)
	print(''.join(ans))

def __starting_point():
	main()
__starting_point()


(n, m) = list(map(int, input().split()))
A = list(map(int, input().split()))
L = [2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [-float('inf')] * (n + 1)
dp[0] = 0
for i in range(1, n + 1):
	for j in range(m):
		if i - L[A[j] - 1] < 0:
			None
		else:
			dp[i] = max(dp[i - L[A[j] - 1]] + 1, dp[i])
A.sort()
A.reverse()
cur = n
ans = ''
while cur != 0:
	for i in range(m):
		temp = A[i]
		if cur - L[temp - 1] >= 0:
			if dp[cur - L[temp - 1]] == dp[cur] - 1:
				ans = ans + str(temp)
				cur -= L[temp - 1]
				break
print(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 main():
	(N, M) = map(int, input().split())
	alist = list(map(int, input().split()))
	alist.sort(reverse=True)
	nums = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
	dp = [-1 for _ in range(N + 1)]
	dp[0] = 0
	for a in alist:
		n = nums[a]
		for i in range(N - n + 1):
			if dp[i] == -1:
				continue
			dp[i + n] = max(dp[i + n], dp[i] * 10 + a)
	print(dp[-1])
main()


(n, m) = map(int, input().split())
A = list(map(int, input().split()))
C = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [-1] * (n + 1)
dp[0] = 0
for n_i in range(n):
	for a_i in A:
		if n_i + C[a_i] <= n:
			dp[n_i + C[a_i]] = max(dp[n_i + C[a_i]], dp[n_i] * 10 + a_i)
print(dp.pop())


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


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
a.sort(reverse=True)
l = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [-1 ** 10] * (n + 1)
dp[0] = 0
for i in range(1, n + 1):
	for j in range(m):
		if i >= l[a[j]] and dp[i] < dp[i - l[a[j]]] + 1:
			dp[i] = dp[i - l[a[j]]] + 1
ans = []
remain = dp[-1]
now = n
for i in a:
	x = l[i]
	while now - x >= 0 and dp[now] == dp[now - x] + 1:
		ans.append(i)
		now -= x
print(*ans, sep='')


(N, M) = map(int, input().split())
A = map(int, input().split())
ok = [0] * 10
for a in A:
	ok[a] = 1
dp = [-1] * (N + 1)
dp[0] = 0
cost = [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6]
for i in range(N):
	for num in range(1, 10):
		c = cost[num]
		if ok[num] == 0 or i + c > N:
			continue
		dp[i + c] = max(dp[i + c], dp[i] * 10 + num)
ans = dp[N]
print(ans)


def main():
	(N, M) = map(int, input().split())
	A = list(map(int, input().split()))
	dp = [2, 5, 5, 4, 5, 6, 3, 7, 6]
	p = []
	q = []
	for i in range(M):
		p.append(dp[A[i] - 1])
		q.append(A[i])
	bp = [[0, 0] for i in range(N + 1)]
	for i in range(N - 1):
		for j in range(len(p)):
			if i + p[j] > N:
				continue
			if bp[i][0] == 0:
				if i != 0:
					continue
			bp[i + p[j]][0] = max(bp[i + p[j]][0], bp[i][0] + 10 ** bp[i][1] * q[j])
			bp[i + p[j]][1] = bp[i][1] + 1
	print(bp[N][0])

def __starting_point():
	main()
__starting_point()


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
p = [2, 5, 5, 4, 5, 6, 3, 7, 6]
num = []
for x in a:
	num.append(p[x - 1])
inf = 10 ** 8
dp = [-inf] * (n + 1)
dp[0] = 0
for i in range(n):
	for j in range(m):
		if i + num[j] <= n and dp[i] != -inf:
			dp[i + num[j]] = max(dp[i + num[j]], dp[i] + 1)
ans = ''
now = n
for i in range(dp[n]):
	for j in range(m - 1, -1, -1):
		if now - num[j] < 0:
			continue
		if dp[now - num[j]] == dp[now] - 1:
			now -= num[j]
			ans += str(a[j])
			break
print(ans)


match = [2, 5, 5, 4, 5, 6, 3, 7, 6]
(n, m) = map(int, input().split())
aa = list(map(int, input().split()))
dp = [-1] * (n + 10)
dp[0] = 0
for i in range(n + 1):
	for nums in aa:
		if dp[i] == -1:
			continue
		dp[i + match[nums - 1]] = max(dp[i] * 10 + nums, dp[i + match[nums - 1]])
print(dp[n])


(N, M) = map(int, input().split())
A = list(map(int, input().split()))
C = [0, 1, 4, 4, 3, 4, 5, 2, 6, 5]
dp = N * [0] + 9 * [-1]
for n in range(N):
	dp[n + 1] = max((a + 10 * dp[n - C[a]] for a in A))
print(dp[N])


import math

def inpul():
	return list(map(int, input().split()))
(N, M) = inpul()
A = inpul()
c = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
f = [-1] * 10010
f[0] = 0
for i in range(1, N + 1):
	for x in A:
		if i - c[x] >= 0:
			f[i] = max(f[i], f[i - c[x]] * 10 + x)
print(f[N])


(n, m) = map(int, input().split())
dp = [0] + [-2 ** 30 for i in range(n)]
(a, b) = (sorted(list(map(int, input().split())), reverse=True), [0, 2, 5, 5, 4, 5, 6, 3, 7, 6])
for i in range(n):
	for j in range(m):
		if i + b[a[j]] > n:
			continue
		dp[i + b[a[j]]] = max(dp[i + b[a[j]]], dp[i] + 1)
ans = ''
while n:
	for j in range(m):
		if n - b[a[j]] < 0:
			continue
		if dp[n - b[a[j]]] == dp[n] - 1:
			ans += str(a[j])
			n -= b[a[j]]
			break
print(ans)


(N, M) = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
if 1 in A:
	if N % 2 == 0:
		print('1' * (N // 2))
	elif 7 in A:
		print('7' + '1' * (N // 2 - 1))
	elif 5 in A:
		print('5' + '1' * (N // 2 - 2))
	elif 3 in A:
		print('3' + '1' * (N // 2 - 2))
	elif 2 in A:
		print('2' + '1' * (N // 2 - 2))
	else:
		print('8' + '1' * (N // 2 - 3))
elif 7 in A:
	if N % 3 == 0:
		print('7' * (N // 3))
	elif N % 3 == 1:
		if 4 in A:
			print('7' * (N // 3 - 1) + '4')
		elif 8 in A:
			print('8' + '7' * (N // 3 - 2))
		elif 5 in A:
			print('7' * (N // 3 - 3) + '55')
		elif 3 in A:
			print('7' * (N // 3 - 3) + '33')
		elif 2 in A:
			print('7' * (N // 3 - 3) + '22')
	elif 5 in A:
		print('7' * (N // 3 - 1) + '5')
	elif 3 in A:
		print('7' * (N // 3 - 1) + '3')
	elif 2 in A:
		print('7' * (N // 3 - 1) + '2')
	elif 4 in A:
		print('7' * (N // 3 - 2) + '44')
	elif 8 in A:
		print(print('7' * (N // 3 - 4) + '88'))
else:
	B = [[] for i in range(0, 10)]
	for i in range(0, M):
		if 2 in A:
			B[5].append(2)
		if 3 in A:
			B[5].append(3)
		if 4 in A:
			B[4].append(4)
		if 5 in A:
			B[5].append(5)
		if 6 in A:
			B[6].append(6)
		if 8 in A:
			B[7].append(8)
		if 9 in A:
			B[6].append(9)
	dp = ['n' for i in range(0, max(10, N + 1))]
	for i in range(0, min(N + 1, 10)):
		if i == 2:
			if 2 in A:
				dp[5] = '2'
		if i == 3:
			if 3 in A:
				dp[5] = '3'
		if i == 4:
			if 4 in A:
				dp[4] = '4'
		if i == 5:
			if 5 in A:
				dp[5] = '5'
		if i == 6:
			if 6 in A:
				dp[6] = '6'
		if i == 8:
			if 8 in A:
				dp[7] = '8'
		if i == 9:
			if 9 in A:
				dp[6] = '9'
	if N >= 6:
		a = '0'
		b = '0'
		c = '0'
		if dp[6] == 'n':
			a = int(a)
			b = int(b)
			c = int(c)
			x = max(a, b, c)
			if x != 0:
				dp[6] = str(x)
		else:
			a = int(a)
			b = int(b)
			c = int(c)
			d = int(dp[6])
			dp[6] = str(max(a, b, c, d))
	if N >= 7:
		a = '0'
		b = '0'
		c = '0'
		d = '0'
		if dp[7] == 'n':
			a = int(a)
			b = int(b)
			c = int(c)
			d = int(d)
			x = max(a, b, c, d)
			if x != 0:
				dp[7] = str(x)
		else:
			a = int(a)
			b = int(b)
			c = int(c)
			d = int(d)
			e = int(dp[7])
			dp[7] = str(max(e, a, b, c, d))
	for i in range(8, N + 1):
		a = '0'
		b = '0'
		c = '0'
		d = '0'
		e = '0'
		f = '0'
		if B[4] and dp[i - 4] != 'n':
			c = '4' + dp[i - 4]
		if B[5] and dp[i - 5] != 'n':
			d = str(max(B[5])) + dp[i - 5]
		if B[6] and dp[i - 6] != 'n':
			e = str(max(B[6])) + dp[i - 6]
		if B[7] and dp[i - 7] != 'n':
			f = '8' + dp[i - 7]
		a = int(a)
		b = int(b)
		c = int(c)
		d = int(d)
		e = int(e)
		f = int(f)
		x = max(a, b, c, d, e, f)
		if x == 0:
			dp[i] = 'n'
		else:
			dp[i] = str(x)
	print(int(dp[N]))


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


costs = [2, 5, 5, 4, 5, 6, 3, 7, 6]

def main():
	(N, M) = map(int, input().split())
	A = list(map(int, input().split()))
	dp = [-1 for _ in range(N + 1)]
	dp[0] = 0
	for i in range(1, N + 1):
		for a in A:
			if i < costs[a - 1]:
				continue
			dp[i] = max(dp[i], dp[i - costs[a - 1]] * 10 + a)
	print(dp[N])
main()


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) = map(int, input().split())
a = list(map(int, input().split()))
d = [2, 5, 5, 4, 5, 6, 3, 7, 6]
a.sort()
dic = {}
for ai in a:
	dic[d[ai - 1]] = ai
dp = [''] + ['0'] * n
for i in range(n + 1):
	for (j, k) in dic.items():
		if i - j >= 0 and dp[i - j] != '0':
			temp = str(k) + dp[i - j]
			if len(temp) >= len(dp[i]) and temp > dp[i]:
				dp[i] = temp
print(dp[-1])


from collections import defaultdict
(N, M, *A) = map(int, open(0).read().split())
ls = [0] * 10
ls[1] = 2
ls[2] = 5
ls[3] = 5
ls[4] = 4
ls[5] = 5
ls[6] = 6
ls[7] = 3
ls[8] = 7
ls[9] = 6
dic = defaultdict(int)
for a in A:
	k = ls[a]
	dic[k] = max(dic[k], a)
dp = [-1] * (N + 1)
for k in dic.keys():
	if k > N:
		continue
	dp[k] = dic[k]
for i in range(N + 1):
	for k in dic.keys():
		if i - k > 0 and dp[i - k] != -1:
			m = dp[i - k] * 10 + dic[k]
			dp[i] = max(dp[i], m)
print(dp[N])


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
use = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [None for _ in range(n + 1)]
dp[0] = 0
for i in range(0, n + 1):
	for j in a:
		if i + use[j] < n + 1 and dp[i] != None:
			if dp[i + use[j]] == None:
				dp[i + use[j]] = dp[i] * 10 + j
			else:
				dp[i + use[j]] = max(dp[i] * 10 + j, dp[i + use[j]])
print(dp[n])


INF = float('inf')
cost = [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6]
(N, M) = map(int, input().split())
A = list(map(int, input().split()))
dp = [-INF] * (N + 1)
dp[0] = 0
for i in range(1, N + 1):
	for a in A:
		if i - cost[a] >= 0:
			dp[i] = max(dp[i], dp[i - cost[a]] * 10 + a)
print(dp[N])


(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) = map(int, input().split())
a = list(map(int, input().split()))
needs = [999, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [-1] * (n + 10)
dp[0] = 0
for i in range(n):
	if dp[i] != -1:
		for k in sorted(a, reverse=True):
			matchs = needs[k]
			dp[i + matchs] = max(dp[i + matchs], dp[i] * 10 + k)
print(dp[n])


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
w = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [-1] * (n + 1)
dp[0] = 0
for i in range(n + 1):
	for x in a:
		if i + w[x] < n + 1:
			dp[i + w[x]] = max(dp[i + w[x]], dp[i] * 10 + x)
print(dp[n])


(N, M) = map(int, input().split())
A = list(map(int, input().split()))
C = [None, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [-1 for _ in range(N + 7)]
dp[0] = 0
for i in range(N + 1):
	for j in A:
		if C[j] <= i:
			dp[i] = max(dp[i - C[j]] * 10 + j, dp[i])
print(dp[N])


(N, M) = map(int, input().split())
A = tuple(map(int, input().split()))
M_lst = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [-1] * (N + 10)
dp[0] = 0
for i in range(N + 1):
	if dp[i] == -1:
		continue
	for a in A:
		dp[i + M_lst[a]] = max(dp[i + M_lst[a]], dp[i] * 10 + a)
print(dp[N])


(n, m) = map(int, input().split())
needs = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [-1] * (n + 8)
dp[0] = 0
for a in sorted(map(int, input().split()), reverse=True):
	na = needs[a]
	for i in range(n + 1):
		if dp[i] == -1:
			continue
		dp[i + na] = max(dp[i + na], dp[i] * 10 + a)
print(dp[n])


(w, n) = map(int, input().split())
A0 = list(map(int, input().split()))
C = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
A = []
for i in range(n):
	A.append([A0[i], C[A0[i]]])
dp = [-1] * (w + 1)
dp[0] = 0
for i in range(w + 1):
	for j in range(n):
		if A[j][1] <= i:
			dp[i] = max(dp[i], dp[i - A[j][1]] * 10 + A[j][0])
print(dp[w])


(N, M) = map(int, input().split())
A = [int(i) for i in input().split()]
h = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
dp = [-1] * (N + 1)
dp[0] = 0
for i in range(N + 1):
	for a in A:
		if i - h[a] < 0:
			continue
		dp[i] = max(10 * dp[i - h[a]] + a, dp[i])
print(dp[N])


(n, _) = map(int, input().split())
L = list(map(int, input().split()))
weight = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [-1] * (n + 1)
dp[0] = 0
for i in range(n + 1):
	for a in L:
		if i + weight[a] < n + 1:
			dp[i + weight[a]] = max(dp[i + weight[a]], dp[i] * 10 + a)
print(dp[n])


def main():
	C = [2, 5, 5, 4, 5, 6, 3, 7, 6]
	(N, M) = list(map(int, input().split()))
	A = list(map(int, input().split()))
	dp = [0] + [-1] * N
	K = {a: C[a - 1] for a in A}
	for i in range(min(K.values()), N + 1):
		try:
			dp[i] = max((dp[i - v] * 10 + k for (k, v) in list(K.items()) if i - v >= 0 and dp[i - v] >= 0))
		except ValueError:
			dp[i] = -1
	print(dp[N])
main()


(n, m) = map(int, input().split())
aa = reversed(sorted(map(int, input().split())))
t = ['_', 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [-1 for _ in range(n + 1)]
dp[0] = 0
for a in aa:
	for i in range(len(dp)):
		if dp[i] >= 0 and i + t[a] < len(dp):
			dp[i + t[a]] = max(dp[i + t[a]], dp[i] * 10 + a)
print(dp[n])


(N, M) = map(int, input().split())
A = list(map(int, input().split()))
c = [2, 5, 5, 4, 5, 6, 3, 7, 6]
A.sort(reverse=True)
dp = [-1 for _ in range(N + 1)]
dp[0] = 0
for a in A:
	c_ = c[a - 1]
	for i in range(c_, N + 1):
		if dp[i - c_] == -1:
			continue
		dp[i] = max(dp[i], dp[i - c_] * 10 + a)
print(dp[-1])


(n, _, *a) = map(int, open(0).read().split())
ref = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
dp = [0] + [-float('inf')] * (n + 9)
for i in range(n + 1):
	for x in a:
		if dp[i] < dp[i - ref[x]] * 10 + x:
			dp[i] = dp[i - ref[x]] * 10 + x
print(dp[n])


(N, M) = map(int, input().split())
A = list(map(int, input().split()))
A.sort(reverse=True)
honsuu = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]

def ketadp(N, M, A):
	dp = [0] * (N + 1)
	for i in range(N):
		for j in A:
			if i + 1 - honsuu[j] == 0 or (i + 1 - honsuu[j] > 0 and dp[i + 1 - honsuu[j]] != 0):
				dp[i + 1] = max(dp[i + 1], dp[i + 1 - honsuu[j]] + 1)
	return dp
ans = ''
keta = ketadp(N, M, A)
remain = keta[N]
match = N
while match > 0:
	for i in A:
		if match - honsuu[i] < 0:
			continue
		if keta[match - honsuu[i]] == remain - 1 and (not (match - honsuu[i] != 0 and remain == 1)):
			ans += str(i)
			remain -= 1
			match -= honsuu[i]
			break
print(ans)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
match_num = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
dp = [-1] * (n + 10)
dp[0] = 0
for i in range(n):
	for j in range(m):
		nxt = match_num[a[j]]
		if dp[i] >= 0:
			dp[i + nxt] = max(dp[i + nxt], dp[i] * 10 + a[j])
print(dp[n])


(n, m) = map(int, input().split())
a = sorted(map(int, input().split()))[::-1]
c = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [-float('inf')] * (n + 1)
dp[0] = 0
for i in range(2, n + 1):
	for j in a:
		if i >= c[j]:
			dp[i] = max(dp[i], dp[i - c[j]] + 1)
r = []
while n:
	i = max((i for i in a if n >= c[i]), key=lambda x: dp[n - c[x]])
	r.append(i)
	n -= c[i]
print(''.join(map(str, r)))


from collections import defaultdict

def f(a, b):
	for i in range(9, 0, -1):
		if a[i] == b[i]:
			continue
		elif a[i] > b[i]:
			return True
		else:
			return False
	return False
(N, M) = map(int, input().split())
se = set()
A = list(map(int, input().split()))
toNum = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [[] for i in range(N + 1)]
done = [False for i in range(N + 10)]
done[N] = True
dics = [defaultdict(int) for i in range(N + 1)]
for i in range(N - 1, -1, -1):
	for a in A:
		num = toNum[a]
		if not done[i + num]:
			continue
		l = len(dp[i + num]) + 1
		d = dics[i + num].copy()
		d[a] += 1
		if l > len(dp[i]) or (l == len(dp[i]) and f(d, dics[i])):
			dp[i] = dp[i + num] + [a]
			done[i] = True
			dics[i] = d
dp[0].sort(reverse=True)
print(*dp[0], sep='')


import sys
(N, M) = map(int, sys.stdin.readline().rstrip().split())
A = list(map(int, sys.stdin.readline().rstrip().split()))
from collections import defaultdict
d = defaultdict(int)
for i in range(M):
	if A[i] == 1:
		d[2] = 1
	elif A[i] == 7:
		d[3] = 7
	elif A[i] == 4:
		d[4] = 4
	elif A[i] in [2, 3, 5]:
		d[5] = max(d[5], A[i])
	elif A[i] in [6, 9]:
		d[6] = max(d[6], A[i])
	else:
		d[7] = 8
B = list(d.keys())
B.sort()
a = B[0]
B[0] = 0
if N % a == 0:
	print(''.join([str(d[a])] * (N // a)))
	return
from itertools import combinations_with_replacement
C = []
for i in range(N // a, max(-1, N // a - 7), -1):
	x = N - a * i
	for u in list(combinations_with_replacement(B, 5)):
		if sum(u) == x:
			s = []
			for j in range(5):
				if u[j] != 0:
					s.append(u[j])
			C.append((i, s))
D = []
for (i, j) in C:
	if i + len(j) == C[0][0] + len(C[0][1]):
		D.append((i, j))
m = min((D[i][0] for i in range(len(D))))
(q, r, s) = (0, 0, 0)
for (i, j) in D:
	X = [d[a]] * (i - m) + [d[k] for k in j]
	X.sort(reverse=True)
	M = ''
	for k in X:
		M += str(k)
	M = int(M)
	if q < M:
		(q, r, s) = (M, i, j)
E = [d[a]] * r + [d[i] for i in s]
E.sort(reverse=True)
ans = ''
for i in E:
	ans += str(i)
print(ans)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
t = [2, 5, 5, 4, 5, 6, 3, 7, 6]
cost = {ai: t[ai - 1] for ai in a}
dp = [0] + [-1] * n
for i in range(1, n + 1):
	dpi = -1
	for (ai, ci) in cost.items():
		if i - ci >= 0 and dp[i - ci] >= 0:
			dpi = max(dp[i - ci] * 10 + ai, dpi)
	dp[i] = dpi
print(dp[n])


c = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
I = lambda : map(int, input().split())
(N, M) = I()
A = sorted(I())[::-1]
d = [0]
for i in range(N):
	l = [d[i + 1 - c[a]] for a in A if i + 2 > c[a]]
	d += [-1 if len(l) == 0 else max(l) + 1]
S = ''
T = 0
for i in range(d[N]):
	for a in A:
		b = c[a]
		if N - T - b >= 0 and d[N - T - b] == d[N] - 1 - i:
			S += str(a)
			T += b
			break
print(S)


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)
	b = {}
	for i in a:
		x = t[i]
		if b.get(x) is not None:
			a.remove(i)
		else:
			b[x] = 0
	b = [t[i] for i in a]
	dp = [-float('Inf')] * (n + 10)
	dp[0] = 0
	for i in range(2, n + 1):
		dp[i] = max((dp[i - j] + 1 for j in b))
	nums = []
	for i in range(dp[n], 0, -1):
		for x in a:
			if dp[n - t[x]] == i - 1:
				nums.append(x)
				n -= t[x]
				break
	ans = ''.join(map(str, nums))
	print(ans)

def __starting_point():
	main()
__starting_point()


(n, m) = map(int, input().split())
match = [2, 5, 5, 4, 5, 6, 3, 7, 6]
a = [int(i) for i in input().split()]
dp = [0] + [-1] * (n + 1)
for i in range(1, n + 1):
	for j in a:
		honsu = match[j - 1]
		if i - honsu >= 0:
			dp[i] = max(dp[i - honsu] * 10 + j, dp[i])
print(dp[n])


INF = float('inf')
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
cost = [2, 5, 5, 4, 5, 6, 3, 7, 6]
l = [[x, cost[x - 1]] for x in a]
l = sorted(l, key=lambda x: x[1] * 10 - x[0])
r = l[0]
_max = [0] + [-INF] * n
for i in range(1, n + 1):
	for x in l:
		ind = i - x[1]
		if ind >= 0:
			_max[i] = max(_max[i], _max[ind] * 10 + x[0])
ans = str(_max[n])
print(ans)


def main():
	import sys
	input = sys.stdin.readline
	sys.setrecursionlimit(10 ** 7)
	from collections import Counter, deque
	from itertools import combinations, permutations, accumulate, groupby
	from bisect import bisect_left, bisect_right
	from heapq import heapify, heappop, heappush
	from math import floor, ceil
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	b = set()
	matti = [2, 5, 5, 4, 5, 6, 3, 7, 6]
	for i in a:
		b.add(matti[i - 1])
	dp = [-1] * (n + 1)
	dp[0] = 0
	for i in range(n + 1):
		for j in b:
			if i >= j:
				dp[i] = max(dp[i - j] + 1, dp[i])
	keta = dp[-1]
	res = []
	a.sort(reverse=True)
	cnt = n
	while cnt > 0:
		for i in a:
			if cnt >= matti[i - 1]:
				if dp[cnt] - dp[cnt - matti[i - 1]] == 1:
					res.append(str(i))
					cnt -= matti[i - 1]
					break
	print(''.join(res))

def __starting_point():
	main()
__starting_point()


l = [6, 2, 5, 5, 4, 5, 6, 3, 7, 6]
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
a.sort(reverse=True)
dp = [-1] * (n + 10)
dp[0] = 0
for i in range(n):
	for num in a:
		dp[i + l[num]] = max(dp[i + l[num]], dp[i] * 10 + num)
print(dp[n])


cost = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
(N, M) = map(int, input().split())
A = sorted(map(int, input().split()))[::-1]
dp = [0]
for i in range(1, N + 1):
	l = [dp[i - cost[a]] for a in A if i >= cost[a]]
	if len(l) == 0:
		dp += [-1]
	else:
		dp += [max(l) + 1]
S = ''
Sum = 0
for i in range(dp[N]):
	for a in A:
		if N - Sum - cost[a] >= 0 and dp[N - Sum - cost[a]] == dp[N] - 1 - i:
			S += str(a)
			Sum += cost[a]
			break
print(S)


(N, M) = map(int, input().split())
e = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [-1] * (N + 8)
dp[0] = 0
A = list(map(int, input().split()))
for n in range(N):
	if dp[n] == -1:
		continue
	for a in A:
		dp[n + e[a]] = max(dp[n + e[a]], dp[n] * 10 + a)
print(dp[N])


def resolve():
	(N, M) = list(map(int, input().split()))
	A = list(map(int, input().split()))
	required = [None] + [2, 5, 5, 4, 5, 6, 3, 7, 6]
	dp = [0 for _ in range(max(10, N + 1))]
	candidates = {}
	for a in A:
		req = required[a]
		if dp[req] < a:
			dp[req] = a
			candidates[req] = a
	for i in range(N + 1):
		for (n_match, num) in candidates.items():
			if i - n_match >= 0 and dp[i - n_match] != 0:
				dp[i] = max(dp[i], dp[i - n_match] * 10 + num)
	print(dp[N])
if '__main__' == __name__:
	resolve()


(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
all_weights = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
weights = [all_weights[i] for i in a]
dp = [-1 for _ in range(n + 1)]
dp[0] = 0
for i in range(1, n + 1):
	for j in range(len(weights)):
		if i - weights[j] >= 0:
			dp[i] = max(dp[i - weights[j]] * 10 + a[j], dp[i])
print(dp[-1])


import sys
try:
	from typing import Dict, List
except ImportError:
	pass

def solve(N: int, M: int, A: 'List[int]'):
	mn = [6, 2, 5, 5, 4, 5, 6, 3, 7, 6]
	A.sort(reverse=True)
	dp = [-1] * (N + 1)
	dp[0] = 0
	for i in range(1, N + 1):
		m = -1
		for Ai in A:
			if i - mn[Ai] >= 0 and dp[i - mn[Ai]] >= 0:
				m = max(m, dp[i - mn[Ai]] + 1)
		if m >= 0:
			dp[i] = m
	assert dp[N] >= 0
	k = N
	res = []
	while dp[k]:
		for Ai in A:
			if k - mn[Ai] >= 0 and dp[k - mn[Ai]] == dp[k] - 1:
				res.append(Ai)
				k -= mn[Ai]
				break
	print(''.join((str(d) for d in res)))

def main():

	def iterate_tokens():
		for line in sys.stdin:
			for word in line.split():
				yield word
	tokens = iterate_tokens()
	N = int(next(tokens))
	M = int(next(tokens))
	A = [int(next(tokens)) for _ in range(M)]
	solve(N, M, A)

def __starting_point():
	main()
__starting_point()


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


(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
a.sort(reverse=True)
num_of_matches = [6, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [-1] * (n + 1)
dp[0] = 0
for i in range(1, n + 1):
	val = -1
	for aa in a:
		if i - num_of_matches[aa] < 0:
			continue
		if i - num_of_matches[aa] == 0:
			val = 1
		elif dp[i - num_of_matches[aa]] > 0:
			val = max(val, dp[i - num_of_matches[aa]] + 1)
	dp[i] = val
res = ''
remain = dp[n]
match = n
while match > 0:
	for i in range(m):
		if match - num_of_matches[a[i]] >= 0 and remain - 1 == dp[match - num_of_matches[a[i]]]:
			match -= num_of_matches[a[i]]
			remain -= 1
			res += str(a[i])
			break
print(res)


(N, M) = map(int, input().split())
am = list(map(int, input().split()))
c = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
f = [-1] * 10010
f[0] = 0
for i in range(1, N + 1):
	for x in am:
		if i - c[x] >= 0:
			f[i] = max(f[i], f[i - c[x]] * 10 + x)
print(f[N])


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
c = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
d = [-1] * (n + 1)
d[0] = 0
for i in range(n + 1):
	for ai in a:
		if i - c[ai] >= 0:
			d[i] = max(d[i], d[i - c[ai]] * 10 + ai)
print(d[-1])


import sys
sys.setrecursionlimit(10000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15
from copy import deepcopy

def compare(A, B):
	tot_a = sum(A)
	tot_b = sum(B)
	if tot_a > tot_b:
		return A
	if tot_b > tot_a:
		return B
	for i in range(9, 0, -1):
		if A[i] > B[i]:
			return A
		elif A[i] < B[i]:
			return B

def main():
	(N, M) = map(int, input().split())
	A = list(map(int, input().split()))
	match = (-1, 2, 5, 5, 4, 5, 6, 3, 7, 6)
	dp = [[0] * 10 for _ in range(1 + N)]
	changed = [0] * (N + 1)
	changed[0] = 1
	for i in range(N):
		for j in A:
			if i + 1 - match[j] >= 0 and changed[i + 1 - match[j]]:
				B = deepcopy(dp[i + 1 - match[j]])
				B[j] += 1
				if compare(dp[i + 1], B) == B:
					dp[i + 1] = B
					changed[i + 1] = 1
	ans = ''
	for i in range(9, 0, -1):
		ans += str(i) * dp[-1][i]
	print(ans)

def __starting_point():
	main()
__starting_point()


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
dp = [-1] * (n * 18)
for i in range(n * 9):
	dp[i] = 0
lis = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
for i in range(n):
	cnt = -float('inf')
	for j in a:
		cnt = max(cnt, dp[i + 1 - lis[j]] * 10 + j)
	dp[i + 1] = cnt
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) = map(int, input().split())
(*a,) = map(int, input().split())
needs = [9, 2, 5, 5, 4, 5, 6, 3, 7, 6]
table = [-1] * (n + 10)
table[0] = 0
for i in range(n):
	if table[i] != -1:
		for j in a:
			table[i + needs[j]] = max(table[i] * 10 + j, table[i + needs[j]])
print(table[n])


(n, m) = map(int, input().split())
p = [-1] * (n + 1)
a = list(map(int, input().split()))
e = [2, 5, 5, 4, 5, 6, 3, 7, 6]
p[0] = 0
for i in range(n + 1):
	for j in a:
		if i - e[j - 1] >= 0:
			p[i] = max(p[i], p[i - e[j - 1]] * 10 + j)
print(p[-1])


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
n_use = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [0] + [-float('inf')] * n
for i in range(1, n + 1):
	for a_j in a:
		i_prev = i - n_use[a_j]
		if i_prev < 0:
			continue
		else:
			dp[i] = max(dp[i], dp[i_prev] * 10 + a_j)
print(dp[-1])


(N, M) = map(int, input().split())
A = list(map(int, input().split()))
Matches = {num: c for (num, c) in zip([1, 2, 3, 4, 5, 6, 7, 8, 9], [2, 5, 5, 4, 5, 6, 3, 7, 6])}
dp = [-1 for _ in range(N + 1)]
dp[0] = 0
for a in sorted(A, reverse=True):
	c = Matches[a]
	for i in range(N - c + 1):
		if dp[i] == -1:
			continue
		dp[i + c] = max(dp[i + c], dp[i] * 10 + a)
print(dp[N])


import sys
from collections import defaultdict as dd
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.buffer.readline
(N, M) = map(int, input().split())
nums = list(map(int, input().split()))
ms = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [0] * (N + 1)
for i in range(N):
	for n in nums:
		if i + ms[n] > N:
			continue
		if i != 0 and dp[i] == 0:
			continue
		dp[i + ms[n]] = max(dp[i] * 10 + n, dp[i + ms[n]])
print(dp[N])


(N, M) = map(int, input().split())
A = list(map(int, input().split()))
dp = [0] * N + [-1] * 9
for i in range(1, N + 1):
	dp[i] = max((dp[i - int('0255456376'[a])] * 10 + a for a in A))
print(dp[N])


(N, M) = map(int, input().split())
A = list(map(int, input().split()))
dp = [-1] * (N + 1)
dp[0] = 0
match = [2, 5, 5, 4, 5, 6, 3, 7, 6]
mn = []
for a in A:
	mn.append([match[a - 1], a])
for i in range(2, N + 1):
	for (m, n) in mn:
		if i - m >= 0:
			dp[i] = max(dp[i], dp[i - m] * 10 + n)
print(dp[-1])


(n, m) = map(int, input().split())
A = list(map(int, input().split()))
d = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
INF = 10 ** 18
dp = [-INF] * (n + 1)
dp[n] = 0
for i in reversed(range(1, n + 1)):
	for a in A:
		j = i - d[a]
		if j >= 0:
			dp[j] = max(dp[j], dp[i] * 10 + a)
print(dp[0])


import sys
input = sys.stdin.readline

def main():
	(n, m) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	key = dict()
	if 1 in a:
		key[2] = 1
	for v in [5, 3, 2]:
		if v in a:
			key[5] = v
			break
	if 4 in a:
		key[4] = 4
	for v in [9, 6]:
		if v in a:
			key[6] = v
			break
	if 7 in a:
		key[3] = 7
	if 8 in a:
		key[7] = 8
	dp = [0] * (n + 1)
	for i in range(n):
		if i != 0 and dp[i] == 0:
			continue
		for (k, v) in list(key.items()):
			if i + k <= n:
				dp[i + k] = max(dp[i + k], 10 * dp[i] + v)
	print(dp[n])

def __starting_point():
	main()
__starting_point()


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


(N, M) = map(int, input().split())
X = list(map(int, input().split()))
X.sort(reverse=True)
num = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [-1] * (N + 1)
dp[0] = 0
for a in X:
	for i in range(N + 1):
		if dp[i] == -1 or i + num[a] > N:
			continue
		dp[i + num[a]] = max(dp[i + num[a]], dp[i] * 10 + a)
print(dp[-1])


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


ch = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
dp = [-1 for _ in range(n + 1)]
dp[0] = 0
for i in range(1, n + 1):
	res = -1
	for x in a:
		if i - ch[x] >= 0:
			res = max(res, dp[i - ch[x]] * 10 + x)
	dp[i] = res
print(dp[n])


(N, M) = map(int, input().split())
l = list(map(int, input().split()))
w = [2, 5, 5, 4, 5, 6, 3, 7, 6]
l.sort()
dp = [0] * max(N + 1, 8)
for j in l:
	dp[w[j - 1]] = j
for i in range(N):
	if dp[i] != 0:
		for j in l:
			if w[j - 1] + i <= N:
				dp[i + w[j - 1]] = max(dp[i + w[j - 1]], dp[i] * 10 + j)
print(dp[N])


(N, M) = map(int, input().split())
A = list(map(int, input().split()))
weight = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
li = [-100] * (N + 1)
li[0] = 0
for i in range(N + 1):
	for a in A:
		if weight[a] + i <= N:
			li[weight[a] + i] = max(li[weight[a] + i], a + 10 * li[i])
print(li[N])


(N, M) = map(int, input().split())
A = list(map(int, input().split()))
A.sort(reverse=True)
match = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [0] + [-1] * N
for i in A:
	for j in range(0, N + 1):
		if j - match[i] >= 0:
			dp[j] = max(dp[j - match[i]] * 10 + i, dp[j])
print(dp[-1])


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


(N, M) = [int(x) for x in input().split()]
X = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
A = [int(x) for x in input().split()]
ANS = [0]
for i in range(1, N + 1):
	ans = 0
	for j in A:
		if i == X[j]:
			ans = max(j, ans)
		elif i >= X[j]:
			temp = ANS[i - X[j]]
			if temp != 0:
				ans = max(ans, 10 * temp + j)
	ANS.append(ans)
print(ANS[-1])


(n, m) = map(int, input().split())
mt = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
a = sorted(map(int, input().split()))[::-1]
dp = [-1] * (n + 1)
dp[0] = 0
for i in range(n + 1):
	if dp[i] == -1:
		continue
	for j in a:
		if i + mt[j] < n + 1:
			dp[i + mt[j]] = max(dp[i] * 10 + j, dp[i + mt[j]])
print(dp[-1])


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
w = [2, 5, 5, 4, 5, 6, 3, 7, 6]
min_w = 10
for i in a:
	min_w = min(min_w, w[i - 1])
dp = [0 for i in range(n + 1)]
for i in range(0, n):
	if 1 <= i < min_w:
		dp[i] = -1
	for j in a:
		if i + 1 - w[j - 1] >= 0 and dp[i + 1 - w[j - 1]] != -1:
			dp[i + 1] = max(dp[i + 1], dp[i + 1 - w[j - 1]] * 10 + j)
print(dp[n])


(N, M) = map(int, input().split())
listA = list(map(int, input().split()))
c = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [-1] * 10010
dp[0] = 0
for j in range(1, N + 1):
	for a in listA:
		if j - c[a] >= 0:
			dp[j] = max(dp[j], dp[j - c[a]] * 10 + a)
print(dp[N])


import time
(n, m) = map(int, input().split())
numbers = list(map(int, input().split()))
costs = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
f = [-1] * 10010
f[0] = 0
for i in range(1, n + 1):
	for x in numbers:
		if i - costs[x] >= 0:
			f[i] = max(f[i], f[i - costs[x]] * 10 + x)
print(f[n])


B = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
(N, M) = map(int, input().split())
A = list(map(int, input().split()))
dic = {i: B[i] for i in A}
dp = [0] * (N + 1)
for i in range(N + 1):
	for j in A:
		if i > 0 and dp[i] == 0:
			break
		if i + dic[j] <= N:
			dp[i + dic[j]] = max(dp[i] * 10 + j, dp[i + dic[j]])
print(dp[N])


inf = 1 << 60
(N, M) = map(int, input().split())
A = list(map(int, input().split()))
use = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [-inf for i in range(N + 1)]
dp[0] = 0
for i in range(N + 1):
	for a in A:
		if i < use[a]:
			continue
		dp[i] = max(dp[i], dp[i - use[a]] * 10 + a)
print(dp[N])


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])))


(n, m) = map(int, input().split())
cost = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
a = list(map(int, input().split()))
dp = [-1 for _ in range(n + 1)]
dp[0] = 0
for i in range(n + 1):
	if dp[i] == -1:
		continue
	for j in a:
		if i + cost[j] <= n:
			dp[i + cost[j]] = max(dp[i + cost[j]], dp[i] * 10 + j)
print(dp[-1])


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


(N, M) = map(int, input().split())
A = list((int(x) for x in input().split()))
match_list = [('0', 6), ('1', 2), ('2', 5), ('3', 5), ('4', 4), ('5', 5), ('6', 6), ('7', 3), ('8', 7), ('9', 6)]
MA = []
for a in A:
	MA.append(match_list[a])
MA.sort(key=lambda x: x[0], reverse=True)
dp = [-1] * (N + 1)
dp[0] = 0
for i in range(N + 1):
	for j in range(M):
		if i - MA[j][1] >= 0:
			dp[i] = max(dp[i], dp[i - MA[j][1]] + 1)
remain_keta = dp[N]
remain_match = N
ans = ''
for i in range(dp[N]):
	for ma in MA:
		if remain_match >= ma[1] and dp[remain_match - ma[1]] == remain_keta - 1:
			ans += ma[0]
			remain_keta -= 1
			remain_match -= ma[1]
			break
print(ans)


(N, M) = map(int, input().split())
A = list(map(int, input().split()))
dp = [-1 for _ in range(10010)]
dp[0] = 0
MatchNum = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
for i in range(1, 10005):
	for x in A:
		if i - MatchNum[x] >= 0:
			tmp = dp[i - MatchNum[x]] * 10 + x
			dp[i] = max(tmp, dp[i])
print(dp[N])


(match, _) = [int(item) for item in input().split()]
kind = sorted([int(item) for item in input().split()])
use = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [-1] * int(match + 1)
dp[0] = 0
for i in range(match + 1):
	for k in kind:
		if i < use[k]:
			continue
		dp[i] = max(dp[i], dp[i - use[k]] * 10 + k)
print(dp[match])


(N, M) = map(int, input().split())
A = list(map(int, input().split()))
dict = dict(zip([i for i in range(1, 10)], [2, 5, 5, 4, 5, 6, 3, 7, 6]))
INF = 10 ** (N // 2 + 1)
dp = [-INF for i in range(N + 21)]
for a in A:
	dp[dict[a]] = max(dp[dict[a]], a)
for i in range(N + 1):
	for j in range(1, 10):
		dp[i] = max(dp[i], dp[i - j] * 10 + dp[j], dp[j] * 10 + dp[i - j])
print(dp[N])


c = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
(N, M) = map(int, input().split())
A = list(map(int, input().split()))
INF = 100010
dp = [-INF for _ in range(N + 1)]
dp[0] = 0
for i in range(N + 1):
	for a in A:
		if i - c[a] >= 0:
			dp[i] = max(dp[i], dp[i - c[a]] * 10 + a)
print(dp[N])


B_arr = [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6]
(N, M) = (int(x) for x in input().split())
A_arr = [int(x) for x in input().split()]
d = [-1] * (10 * N)
d[0] = 0
for i in range(1, N + 1):
	for A in A_arr:
		d[i] = max(d[i], d[i - B_arr[A]] * 10 + A)
print(d[N])


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])


import sys
input = sys.stdin.readline
(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
d = dict()
need = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
for i in range(m):
	if need[a[i]] not in d:
		d[need[a[i]]] = a[i]
	elif a[i] > d[need[a[i]]]:
		d[need[a[i]]] = a[i]
d = list(d.items())
d.sort()
m = len(d)
dp = [[-1] * 10 for _ in range(n + 1)]
dp[0] = [0] * 10
for i in range(n + 1):
	for j in range(m):
		if i - d[j][0] >= 0 and dp[i - d[j][0]][0] != -1:
			if dp[i][0] == dp[i - d[j][0]][0] + 1:
				ok = False
				for k in reversed(list(range(1, 10))):
					if k == d[j][1]:
						if dp[i][k] < dp[i - d[j][0]][k] + 1:
							ok = True
							break
						if dp[i][k] > dp[i - d[j][0]][k] + 1:
							break
					else:
						if dp[i][k] < dp[i - d[j][0]][k]:
							ok = True
							break
						if dp[i][k] > dp[i - d[j][0]][k]:
							break
				if ok:
					for k in range(10):
						dp[i][k] = dp[i - d[j][0]][k]
					dp[i][d[j][1]] += 1
					dp[i][0] += 1
			elif dp[i][0] < dp[i - d[j][0]][0] + 1:
				for k in range(10):
					dp[i][k] = dp[i - d[j][0]][k]
				dp[i][d[j][1]] += 1
				dp[i][0] += 1
ans = str()
for i in reversed(list(range(1, 10))):
	ans += str(i) * dp[-1][i]
print(ans)


def inpl():
	return list(map(int, input().split()))
(N, M) = inpl()
A = inpl()
A.sort(reverse=True)
match = (0, 2, 5, 5, 4, 5, 6, 3, 7, 6)
dp = [0] + [-float('inf')] * N
for i in range(1, N + 1):
	dp[i] = max([10 * dp[max(-1, i - match[a])] + a for a in A])
print(''.join(sorted(str(dp[-1]), reverse=True)))


mnum = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
(N, M) = map(int, input().split())
A = list(map(int, input().split()))
dp = [-1 for _ in range(N + 100)]
dp[0] = 0
for i in range(N):
	for m in A:
		need = mnum[m]
		if dp[i] >= 0:
			dp[i + need] = max(dp[i + need], dp[i] * 10 + m)
print(dp[N])


(N, M) = map(int, input().split())
A = list(map(int, input().split()))
A.sort(reverse=True)
d = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
S = set()
for a in A:
	S.add(d[a])
INF = 10 ** 8
dp = [-INF for _ in range(N + 1)]
dp[0] = 0
for i in range(N + 1):
	for s in S:
		if i - s >= 0:
			dp[i] = max(dp[i], dp[i - s] + 1)
ans = 0
digit = dp[N]
for i in range(digit - 1, -1, -1):
	if i != 0:
		for a in A:
			if dp[N - d[a]] == i:
				ans = ans * 10 + a
				N -= d[a]
				break
	else:
		for a in A:
			if N == d[a]:
				ans = ans * 10 + a
				break
print(ans)


(N, M) = (int(i) for i in input().split())
A = [int(i) for i in input().split()]
num = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
DP = [-1] * (N + 1)
DP[0] = 0
for i in range(N + 1):
	for j in range(M):
		n = num[A[j]]
		if i - n >= 0 and DP[i - n] != -1:
			DP[i] = max(DP[i], DP[i - n] * 10 + A[j])
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)


(N, M) = map(int, input().split())
A_list = list(map(int, input().split()))
needs_list = [6, 2, 5, 5, 4, 5, 6, 3, 7, 6]
DP = [-1] * (N + 8)
DP[0] = 0
for i in range(N):
	for a in A_list:
		j = i + needs_list[a]
		DP[j] = max(DP[j], DP[i] * 10 + a)
print(DP[N])


(N, M) = map(int, input().split())
A = list(map(int, input().split()))
dp = [-1 for i in range(N + 10)]
l = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp[0] = 0
for i in range(N - 1):
	for s in A:
		if dp[i] == -1 and i != 0:
			continue
		if dp[i + l[s]] < dp[i] * 10 + s:
			dp[i + l[s]] = dp[i] * 10 + s
print(dp[N])


import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7

def resolve():
	(n, m) = list(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)]
	B = []
	for (num, cost) in L:
		if num in A:
			B.append([num, cost])
	B.sort(key=lambda x: -x[0])
	dp = [-f_inf] * (n + 1)
	dp[0] = 0
	for i in range(n + 1):
		for (_, cost) in B:
			if i - cost == 0:
				dp[i] = max(dp[i], dp[0] + 1)
			elif i - cost > 0 and dp[i - cost] != 0:
				dp[i] = max(dp[i], dp[i - cost] + 1)
	j = 0
	k = n
	res = ''
	while k > 0 and j < m:
		(num, cost) = B[j]
		if k - cost >= 0 and dp[k] - dp[k - cost] == 1:
			res += str(num)
			k -= cost
		else:
			j += 1
	print(res)

def __starting_point():
	resolve()
__starting_point()


nums = [2, 5, 5, 4, 5, 6, 3, 7, 6]
(n, m) = map(int, input().split())
digits = list(map(int, input().split()))
dp = [-1] * (n + 10)
dp[0] = 0
for i in range(n + 1):
	if dp[i] == -1:
		continue
	else:
		for digit in digits:
			dp[i + nums[digit - 1]] = max(dp[i + nums[digit - 1]], dp[i] * 10 + digit)
print(dp[n])


import sys

def input():
	return sys.stdin.readline().strip()

def mapint():
	return map(int, input().split())
sys.setrecursionlimit(10 ** 9)
(N, M) = mapint()
As = list(mapint())
num_lis = [2, 5, 5, 4, 5, 6, 3, 7, 6]
need_m = [num_lis[a - 1] for a in As]
dp = [-1] * (N + 1)
dp[0] = 0
for i in range(1, N + 1):
	for (a, need) in zip(As, need_m):
		if need > i:
			continue
		elif dp[i - need] == -1:
			continue
		elif dp[i - need] == 0:
			dp[i] = max(dp[i], a)
		else:
			dp[i] = max(dp[i], dp[i - need] * 10 + a)
print(dp[-1])