n, _ = list(map(int, input().split()))

numbers = list(map(int, input().split()))

reqs = {

    1: 2,

    2: 5,

    3: 5,

    4: 4,

    5: 5,

    6: 6,

    7: 3,

    8: 7,

    9: 6,

}



memo = {}





def max_counts(a, b):

    """

    a と b 大きい方を返す

    :param a: solve() の返り値

    :param b: solve() の返り値

    :return:

    """

    if a is None:

        return b

    if b is None:

        return a

    # 桁数は大きいほうが大きい

    if sum(a) > sum(b):

        return a

    if sum(a) < sum(b):

        return b

    # 桁数が同じなら、大きい数字の多いほうが大きい

    for a_c, b_c in zip(reversed(a), reversed(b)):

        if a_c > b_c:

            return a

        if a_c < b_c:

            return b

    return a





# https://atcoder.jp/contests/abc118/submissions/4911280

# DP 版

memo[0] = [0 for _ in range(10)]

for i in range(n):

    if i in memo:

        for num in numbers:

            next_n = i + reqs[num]

            next_counts = memo[next_n] if next_n in memo else None

            if next_n <= n:

                memo[next_n] = max_counts(

                    next_counts,

                    memo[i][:num] + [memo[i][num] + 1] + memo[i][num + 1:]

                )



ans = ''

for num in range(9, 0, -1):

    for _ in range(memo[n][num]):

        ans += str(num)

print(ans)

import os

import sys



if os.getenv("LOCAL"):

    sys.stdin = open("_in.txt", "r")



sys.setrecursionlimit(10 ** 9)

INF = float("inf")

IINF = 10 ** 18

MOD = 10 ** 9 + 7

# MOD = 998244353



N, M = list(map(int, sys.stdin.buffer.readline().split()))

A = list(map(int, sys.stdin.buffer.readline().split()))



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



# dp[i][0]: i 個のマッチ棒を使って作れる整数の最大の桁数

# dp[i][1][-j]: j を何個作るか

dp = [(0, [0] * 9) for _ in range(N + 10)]

for i in range(N):

    d, counts = dp[i]

    if i > 0 and d == 0:

        continue

    for a in A:

        cnts = counts[:]

        cnts[-a] += 1

        dp[i + requires[a]] = max(dp[i + requires[a]], (d + 1, cnts))

ans = ''

for i, cnt in enumerate(dp[N][1]):

    ans += str(9 - i) * cnt

print(ans)

n, _ = list(map(int, input().split()))
numbers = list(map(int, input().split()))
reqs = {
    1: 2,
    2: 5,
    3: 5,
    4: 4,
    5: 5,
    6: 6,
    7: 3,
    8: 7,
    9: 6,
}
memo = {}


def max_counts(a, b):
    """
    a と b 大きい方を返す
    :param a: solve() の返り値
    :param b: solve() の返り値
    :return:
    """
    if a is None:
        return b
    if b is None:
        return a
    # 桁数は大きいほうが大きい
    if sum(a) > sum(b):
        return a
    if sum(a) < sum(b):
        return b
    # 桁数が同じなら、大きい数字の多いほうが大きい
    for a_c, b_c in zip(reversed(a), reversed(b)):
        if a_c > b_c:
            return a
        if a_c < b_c:
            return b
    return a


# https://atcoder.jp/contests/abc118/submissions/4911280
# DP 版
memo[0] = [0 for _ in range(10)]
for i in range(n):
    if i in memo:
        for num in numbers:
            next_n = i + reqs[num]
            next_counts = memo[next_n] if next_n in memo else None
            if next_n <= n:
                memo[next_n] = max_counts(
                    next_counts, memo[i][:num] + [memo[i][num] + 1] + memo[i][num + 1 :]
                )
ans = ""
for num in range(9, 0, -1):
    for _ in range(memo[n][num]):
        ans += str(num)
print(ans)

import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")
sys.setrecursionlimit(10**9)
INF = float("inf")
IINF = 10**18
MOD = 10**9 + 7
# MOD = 998244353
N, M = list(map(int, sys.stdin.buffer.readline().split()))
A = list(map(int, sys.stdin.buffer.readline().split()))
requires = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
# dp[i][0]: i 個のマッチ棒を使って作れる整数の最大の桁数
# dp[i][1][-j]: j を何個作るか
dp = [(0, [0] * 9) for _ in range(N + 10)]
for i in range(N):
    d, counts = dp[i]
    if i > 0 and d == 0:
        continue
    for a in A:
        cnts = counts[:]
        cnts[-a] += 1
        dp[i + requires[a]] = max(dp[i + requires[a]], (d + 1, cnts))
ans = ""
for i, cnt in enumerate(dp[N][1]):
    ans += str(9 - i) * cnt
print(ans)

-n, _ = list(map(int, input().split())) -numbers = list(map(int, input().split())) -reqs = { -    1: 2, -    2: 5, -    3: 5, -    4: 4, -    5: 5, -    6: 6, -    7: 3, -    8: 7, -    9: 6, -} -memo = {} +import os +import sys - -def max_counts(a, b): -    """ -    a と b 大きい方を返す -    :param a: solve() の返り値 -    :param b: solve() の返り値 -    :return: -    """ -    if a is None: -        return b -    if b is None: -        return a -    # 桁数は大きいほうが大きい -    if sum(a) > sum(b): -        return a -    if sum(a) < sum(b): -        return b -    # 桁数が同じなら、大きい数字の多いほうが大きい -    for a_c, b_c in zip(reversed(a), reversed(b)): -        if a_c > b_c: -            return a -        if a_c < b_c: -            return b -    return a - - -# https://atcoder.jp/contests/abc118/submissions/4911280 -# DP 版 -memo[0] = [0 for _ in range(10)] -for i in range(n): -    if i in memo: -        for num in numbers: -            next_n = i + reqs[num] -            next_counts = memo[next_n] if next_n in memo else None -            if next_n <= n: -                memo[next_n] = max_counts( -                    next_counts, memo[i][:num] + [memo[i][num] + 1] + memo[i][num + 1 :] -                ) +if os.getenv("LOCAL"): +    sys.stdin = open("_in.txt", "r") +sys.setrecursionlimit(10**9) +INF = float("inf") +IINF = 10**18 +MOD = 10**9 + 7 +# MOD = 998244353 +N, M = list(map(int, sys.stdin.buffer.readline().split())) +A = list(map(int, sys.stdin.buffer.readline().split())) +requires = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] +# dp[i][0]: i 個のマッチ棒を使って作れる整数の最大の桁数 +# dp[i][1][-j]: j を何個作るか +dp = [(0, [0] * 9) for _ in range(N + 10)] +for i in range(N): +    d, counts = dp[i] +    if i > 0 and d == 0: +        continue +    for a in A: +        cnts = counts[:] +        cnts[-a] += 1 +        dp[i + requires[a]] = max(dp[i + requires[a]], (d + 1, cnts)) -for num in range(9, 0, -1): -    for _ in range(memo[n][num]): -        ans += str(num) +for i, cnt in enumerate(dp[N][1]): +    ans += str(9 - i) * cnt