N,M=list(map(int,input().split()))

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

 

m=[[1,2],[2,5],[3,5],[4,4],[5,5],[6,6],[7,3],[8,7],[9,6]]

B=[]

    

for a in A:

    B.append(m[a-1])



if [5,5] in B:

    if [2,5] in B:

        B.remove([2,5])

    if [3,5] in B:    

        B.remove([3,5])



if [3,5] in B and [2,5] in B:

    B.remove([2,5])



if [7,3] in B:

    if [6,6] in B:

        B.remove([6,6])

    if [9,6] in B:

        B.remove([9,6])

    

if [9,6] in B and [6,6] in B:

    B.remove([6,6])



if [1,2] in B and [7,3] in B:

    if [2,5] in B:

        B.remove([2,5])

    if [3,5] in B:

        B.remove([3,5])

    if [5,5] in B:

        B.remove([5,5])

    if [8,7] in B:

        B.remove([8,7])



if [1,2] in B and ([2,5] in B or [3,5] in B or [5,5] in B) and [8,7] in B:

    B.remove([8,7])

    

if [4,4] in B and [7,3] in B and [8,7] in B:

    B.remove([8,7])

    

if [1,2] in B and [4,4] in B:

    B.remove([4,4])



maxlist=[""]*(N+1)

 

for i in range(N+1):

    if i!=0 and maxlist[i]=="":

        continue

    else:

        for b in B:

            if i+b[1]>N:

                continue

            ans=maxlist[i]+str(b[0])

            _max=maxlist[i+b[1]]

            if len(ans)<len(_max):

                continue

            elif len(ans)>len(_max):

                maxlist[i+b[1]] = ans

            else:

                for j in range(len(ans)):

                    if ans[j]<_max[j]:

                        break

                    if ans[j]>_max[j]:

                        maxlist[i+b[1]] = ans

                        break

 

print((maxlist[N]))
def comp(a,b):

    if sum(a)<sum(b):

        return "right"

    elif sum(a)>sum(b):

        return "left"

    else:

        for i in range(8,-1,-1):

            if a[i]<b[i]:

                return "right"

            elif a[i]>b[i]:

                return "left"

        else:

            return "equal"



N,M=map(int,input().split())

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

 

m=[[1,2],[2,5],[3,5],[4,4],[5,5],[6,6],[7,3],[8,7],[9,6]]

B=[]

    

for a in A:

    B.append(m[a-1])



if [5,5] in B:

    if [2,5] in B:

        B.remove([2,5])

    if [3,5] in B:    

        B.remove([3,5])



if [3,5] in B and [2,5] in B:

    B.remove([2,5])



if [7,3] in B:

    if [6,6] in B:

        B.remove([6,6])

    if [9,6] in B:

        B.remove([9,6])

    

if [9,6] in B and [6,6] in B:

    B.remove([6,6])



if [1,2] in B and [7,3] in B:

    if [2,5] in B:

        B.remove([2,5])

    if [3,5] in B:

        B.remove([3,5])

    if [5,5] in B:

        B.remove([5,5])

    if [8,7] in B:

        B.remove([8,7])



if [1,2] in B and ([2,5] in B or [3,5] in B or [5,5] in B) and [8,7] in B:

    B.remove([8,7])

    

if [4,4] in B and [7,3] in B and [8,7] in B:

    B.remove([8,7])

    

if [1,2] in B and [4,4] in B:

    B.remove([4,4])

    

maxlist=[[0]*9 for _ in range(N+1)]

 

for i in range(N+1):

    if i!=0 and sum(maxlist[i])==0:

        continue

    else:

        for b in B:

            if i+b[1]>N:

                continue

            #ans=maxlist[i]+str(b[0])

            ans=[maxlist[i][k] for k in range(9)]

            ans[b[0]-1]+=1

            _max=maxlist[i+b[1]]

            if comp(ans,_max) == "left":

                maxlist[i+b[1]]=[ans[k] for k in range(9)]

 

for i in range(8,-1,-1):

    print(str(i+1)*maxlist[N][i],end="")

print()

N, M = list(map(int, input().split()))
A = list((list(map(int, input().split()))))
m = [[1, 2], [2, 5], [3, 5], [4, 4], [5, 5], [6, 6], [7, 3], [8, 7], [9, 6]]
B = []
for a in A:
    B.append(m[a - 1])
if [5, 5] in B:
    if [2, 5] in B:
        B.remove([2, 5])
    if [3, 5] in B:
        B.remove([3, 5])
if [3, 5] in B and [2, 5] in B:
    B.remove([2, 5])
if [7, 3] in B:
    if [6, 6] in B:
        B.remove([6, 6])
    if [9, 6] in B:
        B.remove([9, 6])
if [9, 6] in B and [6, 6] in B:
    B.remove([6, 6])
if [1, 2] in B and [7, 3] in B:
    if [2, 5] in B:
        B.remove([2, 5])
    if [3, 5] in B:
        B.remove([3, 5])
    if [5, 5] in B:
        B.remove([5, 5])
    if [8, 7] in B:
        B.remove([8, 7])
if [1, 2] in B and ([2, 5] in B or [3, 5] in B or [5, 5] in B) and [8, 7] in B:
    B.remove([8, 7])
if [4, 4] in B and [7, 3] in B and [8, 7] in B:
    B.remove([8, 7])
if [1, 2] in B and [4, 4] in B:
    B.remove([4, 4])
maxlist = [""] * (N + 1)
for i in range(N + 1):
    if i != 0 and maxlist[i] == "":
        continue
    else:
        for b in B:
            if i + b[1] > N:
                continue
            ans = maxlist[i] + str(b[0])
            _max = maxlist[i + b[1]]
            if len(ans) < len(_max):
                continue
            elif len(ans) > len(_max):
                maxlist[i + b[1]] = ans
            else:
                for j in range(len(ans)):
                    if ans[j] < _max[j]:
                        break
                    if ans[j] > _max[j]:
                        maxlist[i + b[1]] = ans
                        break
print((maxlist[N]))

def comp(a, b):
    if sum(a) < sum(b):
        return "right"
    elif sum(a) > sum(b):
        return "left"
    else:
        for i in range(8, -1, -1):
            if a[i] < b[i]:
                return "right"
            elif a[i] > b[i]:
                return "left"
        else:
            return "equal"


N, M = map(int, input().split())
A = list((map(int, input().split())))
m = [[1, 2], [2, 5], [3, 5], [4, 4], [5, 5], [6, 6], [7, 3], [8, 7], [9, 6]]
B = []
for a in A:
    B.append(m[a - 1])
if [5, 5] in B:
    if [2, 5] in B:
        B.remove([2, 5])
    if [3, 5] in B:
        B.remove([3, 5])
if [3, 5] in B and [2, 5] in B:
    B.remove([2, 5])
if [7, 3] in B:
    if [6, 6] in B:
        B.remove([6, 6])
    if [9, 6] in B:
        B.remove([9, 6])
if [9, 6] in B and [6, 6] in B:
    B.remove([6, 6])
if [1, 2] in B and [7, 3] in B:
    if [2, 5] in B:
        B.remove([2, 5])
    if [3, 5] in B:
        B.remove([3, 5])
    if [5, 5] in B:
        B.remove([5, 5])
    if [8, 7] in B:
        B.remove([8, 7])
if [1, 2] in B and ([2, 5] in B or [3, 5] in B or [5, 5] in B) and [8, 7] in B:
    B.remove([8, 7])
if [4, 4] in B and [7, 3] in B and [8, 7] in B:
    B.remove([8, 7])
if [1, 2] in B and [4, 4] in B:
    B.remove([4, 4])
maxlist = [[0] * 9 for _ in range(N + 1)]
for i in range(N + 1):
    if i != 0 and sum(maxlist[i]) == 0:
        continue
    else:
        for b in B:
            if i + b[1] > N:
                continue
            # ans=maxlist[i]+str(b[0])
            ans = [maxlist[i][k] for k in range(9)]
            ans[b[0] - 1] += 1
            _max = maxlist[i + b[1]]
            if comp(ans, _max) == "left":
                maxlist[i + b[1]] = [ans[k] for k in range(9)]
for i in range(8, -1, -1):
    print(str(i + 1) * maxlist[N][i], end="")
print()

-N, M = list(map(int, input().split())) -A = list((list(map(int, input().split())))) +def comp(a, b): +    if sum(a) < sum(b): +        return "right" +    elif sum(a) > sum(b): +        return "left" +    else: +        for i in range(8, -1, -1): +            if a[i] < b[i]: +                return "right" +            elif a[i] > b[i]: +                return "left" +        else: +            return "equal" + + +N, M = map(int, input().split()) +A = list((map(int, input().split()))) -maxlist = [""] * (N + 1) +maxlist = [[0] * 9 for _ in range(N + 1)] -    if i != 0 and maxlist[i] == "": +    if i != 0 and sum(maxlist[i]) == 0: -            ans = maxlist[i] + str(b[0]) +            # ans=maxlist[i]+str(b[0]) +            ans = [maxlist[i][k] for k in range(9)] +            ans[b[0] - 1] += 1 -            if len(ans) < len(_max): -                continue -            elif len(ans) > len(_max): -                maxlist[i + b[1]] = ans -            else: -                for j in range(len(ans)): -                    if ans[j] < _max[j]: -                        break -                    if ans[j] > _max[j]: -                        maxlist[i + b[1]] = ans -                        break -print((maxlist[N])) +            if comp(ans, _max) == "left": +                maxlist[i + b[1]] = [ans[k] for k in range(9)] +for i in range(8, -1, -1): +    print(str(i + 1) * maxlist[N][i], end="") +print()