from copy import deepcopy
def main():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    l = [2,5,5,4,5,6,3,7,6]
    d = {}
    for i in A:
        if l[i-1] not in d:
            d[l[i-1]] = i
        else:
            if d[l[i-1]] < i:
                d[l[i-1]] = i
    d3 = {}
    for i in d:
        d3[d[i]] = i
    d2 = list(d)
    d2.sort()
    s = [[[-1] * (len(d2)) for _ in range(len(d2))] for _ in range(N+1)]
    dp = [[0] * (len(d2)) for _ in range(N+1)]
    for w in range(N+1):
        if w % d2[0] == 0:
            dp[w][0] = w // d2[0]
            s[w][0] = [0] * (len(d2))
            s[w][0][0] = w // d2[0]
    for i in range(1, len(d2)):
        for w in range(N+1):
            if w < d2[i]:
                continue
            elif max(dp[w-d2[i]]) > 0:
                x = dp[w-d2[i]].index(max(dp[w-d2[i]]))
                if dp[w][i-1] > dp[w-d2[i]][x] + 1:
                    dp[w][i] = dp[w][i-1]
                    s[w][i] = deepcopy(s[w][i-1])
                else:
                    dp[w][i] = dp[w-d2[i]][x] + 1
                    s[w][i] = deepcopy(s[w-d2[i]][x])
                    s[w][i][i] += 1
            elif w == d2[i]:
                dp[w][i] = 1
                s[w][i] = [0] * len(d2)
                s[w][i][i] = 1
            else:
                dp[w][i] = dp[w][i-1]
                s[w][i] = deepcopy(s[w][i-1])
    maxn = 0
    for i in s[-1]:
        if sum(i) <= 0:
            continue
        qq = []
        for j,v in enumerate(i):
            if v > 0:
                qq.append((d[d2[j]],v))
        qq.sort(key=lambda x:x[0], reverse=True)
        r = ''
        for k in qq:
            r += str(k[0]) * k[1]
        if int(r) > maxn:
            maxn = int(r)
    print(maxn)
main() from copy import deepcopy def main():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    l = [2,5,5,4,5,6,3,7,6]
    d = {}
    for i in A:
        if l[i-1] not in d:
            d[l[i-1]] = i
        else:
            if d[l[i-1]] < i:
                d[l[i-1]] = i
    d3 = {}
    for i in d:
        d3[d[i]] = i
    d2 = list(d)
    d2.sort()
    s = [[[-1] * (len(d2)) for _ in range(len(d2))] for _ in range(N+1)]
    dp = [[0] * (len(d2)) for _ in range(N+1)]
    for w in range(N+1):
        if w % d2[0] == 0:
            dp[w][0] = w // d2[0]
            s[w][0] = [0] * (len(d2))
            s[w][0][0] = w // d2[0]
    for i in range(1, len(d2)):
        for w in range(N+1):
            if w < d2[i]:
                continue
            elif max(dp[w-d2[i]]) > 0:
                x = dp[w-d2[i]].index(max(dp[w-d2[i]]))
                if dp[w][i-1] > dp[w-d2[i]][x] + 1:
                    dp[w][i] = dp[w][i-1]
                    s[w][i] = deepcopy(s[w][i-1])
                else:
                    dp[w][i] = dp[w-d2[i]][x] + 1
                    s[w][i] = deepcopy(s[w-d2[i]][x])
                    s[w][i][i] += 1
            elif w == d2[i]:
                dp[w][i] = 1
                s[w][i] = [0] * len(d2)
                s[w][i][i] = 1
            else:
                dp[w][i] = dp[w][i-1]
                s[w][i] = deepcopy(s[w][i-1])
    maxn = 0
    for i in s[-1]:
        if sum(i) <= 0:
            continue
        qq = []
        for j,v in enumerate(i):
            if v > 0:
                qq.append((d[d2[j]],v))
        qq.sort(key=lambda x:x[0], reverse=True)
        r = ''
        for k in qq:
            r += str(k[0]) * k[1]
        if int(r) > maxn:
            maxn = int(r)
    print(maxn) main N, M = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split M A = list(map(int, input().split())) A list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split l = [2,5,5,4,5,6,3,7,6] l [2,5,5,4,5,6,3,7,6] 2 5 5 4 5 6 3 7 6 d = {} d {} for i in A:
        if l[i-1] not in d:
            d[l[i-1]] = i
        else:
            if d[l[i-1]] < i:
                d[l[i-1]] = i
     i A if l[i-1] not in d:
            d[l[i-1]] = i
        else:
            if d[l[i-1]] < i:
                d[l[i-1]] = i
     l[i-1] not in d l[i-1] l i-1 i 1 d d[l[i-1]] = i d[l[i-1]] d l[i-1] l i-1 i 1 i if d[l[i-1]] < i:
                d[l[i-1]] = i
     d[l[i-1]] < i d[l[i-1]] d l[i-1] l i-1 i 1 i d[l[i-1]] = i d[l[i-1]] d l[i-1] l i-1 i 1 i d3 = {} d3 {} for i in d:
        d3[d[i]] = i
     i d d3[d[i]] = i d3[d[i]] d3 d[i] d i i d2 = list(d) d2 list(d) list d d2.sort() d2.sort d2 sort [[-1] * (len(d2)) for _ in range(len(d2))] for _ in range(N+1) for _ in range(N+1) _ range(N+1) range N+1 N 1 for _ in range(N+1) [[-1] * (len(d2)) for _ in range(len(d2))] s = [[[-1] * (len(d2)) for _ in range(len(d2))] for _ in range(N+1)] s [[[-1] * (len(d2)) for _ in range(len(d2))] for _ in range(N+1)] [0] * (len(d2)) for _ in range(N+1) for _ in range(N+1) _ range(N+1) range N+1 N 1 for _ in range(N+1) [0] * (len(d2)) [0] 0 len(d2) len d2 dp = [[0] * (len(d2)) for _ in range(N+1)] dp [[0] * (len(d2)) for _ in range(N+1)] for w in range(N+1):
        if w % d2[0] == 0:
            dp[w][0] = w // d2[0]
            s[w][0] = [0] * (len(d2))
            s[w][0][0] = w // d2[0]
     w range(N+1) range N+1 N 1 if w % d2[0] == 0:
            dp[w][0] = w // d2[0]
            s[w][0] = [0] * (len(d2))
            s[w][0][0] = w // d2[0]
     w % d2[0] == 0 w % d2[0] w d2[0] d2 0 0 dp[w][0] = w // d2[0] dp[w][0] [w] dp w 0 w // d2[0] w d2[0] d2 0 s[w][0] = [0] * (len(d2)) s[w][0] [w] s w 0 [0] * (len(d2)) [0] 0 len(d2) len d2 s[w][0][0] = w // d2[0] s[w][0][0] [0] [w] s w 0 0 w // d2[0] w d2[0] d2 0 for i in range(1, len(d2)):
        for w in range(N+1):
            if w < d2[i]:
                continue
            elif max(dp[w-d2[i]]) > 0:
                x = dp[w-d2[i]].index(max(dp[w-d2[i]]))
                if dp[w][i-1] > dp[w-d2[i]][x] + 1:
                    dp[w][i] = dp[w][i-1]
                    s[w][i] = deepcopy(s[w][i-1])
                else:
                    dp[w][i] = dp[w-d2[i]][x] + 1
                    s[w][i] = deepcopy(s[w-d2[i]][x])
                    s[w][i][i] += 1
            elif w == d2[i]:
                dp[w][i] = 1
                s[w][i] = [0] * len(d2)
                s[w][i][i] = 1
            else:
                dp[w][i] = dp[w][i-1]
                s[w][i] = deepcopy(s[w][i-1])
     i range(1, len(d2)) range 1 len(d2) len d2 for w in range(N+1):
            if w < d2[i]:
                continue
            elif max(dp[w-d2[i]]) > 0:
                x = dp[w-d2[i]].index(max(dp[w-d2[i]]))
                if dp[w][i-1] > dp[w-d2[i]][x] + 1:
                    dp[w][i] = dp[w][i-1]
                    s[w][i] = deepcopy(s[w][i-1])
                else:
                    dp[w][i] = dp[w-d2[i]][x] + 1
                    s[w][i] = deepcopy(s[w-d2[i]][x])
                    s[w][i][i] += 1
            elif w == d2[i]:
                dp[w][i] = 1
                s[w][i] = [0] * len(d2)
                s[w][i][i] = 1
            else:
                dp[w][i] = dp[w][i-1]
                s[w][i] = deepcopy(s[w][i-1])
     w range(N+1) range N+1 N 1 if w < d2[i]:
                continue
            elif max(dp[w-d2[i]]) > 0:
                x = dp[w-d2[i]].index(max(dp[w-d2[i]]))
                if dp[w][i-1] > dp[w-d2[i]][x] + 1:
                    dp[w][i] = dp[w][i-1]
                    s[w][i] = deepcopy(s[w][i-1])
                else:
                    dp[w][i] = dp[w-d2[i]][x] + 1
                    s[w][i] = deepcopy(s[w-d2[i]][x])
                    s[w][i][i] += 1
            elif w == d2[i]:
                dp[w][i] = 1
                s[w][i] = [0] * len(d2)
                s[w][i][i] = 1
            else:
                dp[w][i] = dp[w][i-1]
                s[w][i] = deepcopy(s[w][i-1])
     w < d2[i] w d2[i] d2 i continue elif max(dp[w-d2[i]]) > 0:
                x = dp[w-d2[i]].index(max(dp[w-d2[i]]))
                if dp[w][i-1] > dp[w-d2[i]][x] + 1:
                    dp[w][i] = dp[w][i-1]
                    s[w][i] = deepcopy(s[w][i-1])
                else:
                    dp[w][i] = dp[w-d2[i]][x] + 1
                    s[w][i] = deepcopy(s[w-d2[i]][x])
                    s[w][i][i] += 1
             max(dp[w-d2[i]]) > 0 max(dp[w-d2[i]]) max dp[w-d2[i]] dp w-d2[i] w d2[i] d2 i 0 x = dp[w-d2[i]].index(max(dp[w-d2[i]])) x dp[w-d2[i]].index(max(dp[w-d2[i]])) [w-d2[i]].index [w-d2[i]] dp w-d2[i] w d2[i] d2 i index max(dp[w-d2[i]]) max dp[w-d2[i]] dp w-d2[i] w d2[i] d2 i if dp[w][i-1] > dp[w-d2[i]][x] + 1:
                    dp[w][i] = dp[w][i-1]
                    s[w][i] = deepcopy(s[w][i-1])
                else:
                    dp[w][i] = dp[w-d2[i]][x] + 1
                    s[w][i] = deepcopy(s[w-d2[i]][x])
                    s[w][i][i] += 1
             dp[w][i-1] > dp[w-d2[i]][x] + 1 dp[w][i-1] [w] dp w i-1 i 1 dp[w-d2[i]][x] + 1 dp[w-d2[i]][x] [w-d2[i]] dp w-d2[i] w d2[i] d2 i x 1 dp[w][i] = dp[w][i-1] dp[w][i] [w] dp w i dp[w][i-1] [w] dp w i-1 i 1 s[w][i] = deepcopy(s[w][i-1]) s[w][i] [w] s w i deepcopy(s[w][i-1]) deepcopy s[w][i-1] [w] s w i-1 i 1 dp[w][i] = dp[w-d2[i]][x] + 1 dp[w][i] [w] dp w i dp[w-d2[i]][x] + 1 dp[w-d2[i]][x] [w-d2[i]] dp w-d2[i] w d2[i] d2 i x 1 s[w][i] = deepcopy(s[w-d2[i]][x]) s[w][i] [w] s w i deepcopy(s[w-d2[i]][x]) deepcopy s[w-d2[i]][x] [w-d2[i]] s w-d2[i] w d2[i] d2 i x s[w][i][i] += 1 s[w][i][i] [i] [w] s w i i 1 elif w == d2[i]:
                dp[w][i] = 1
                s[w][i] = [0] * len(d2)
                s[w][i][i] = 1
             w == d2[i] w d2[i] d2 i dp[w][i] = 1 dp[w][i] [w] dp w i 1 s[w][i] = [0] * len(d2) s[w][i] [w] s w i [0] * len(d2) [0] 0 len(d2) len d2 s[w][i][i] = 1 s[w][i][i] [i] [w] s w i i 1 dp[w][i] = dp[w][i-1] dp[w][i] [w] dp w i dp[w][i-1] [w] dp w i-1 i 1 s[w][i] = deepcopy(s[w][i-1]) s[w][i] [w] s w i deepcopy(s[w][i-1]) deepcopy s[w][i-1] [w] s w i-1 i 1 maxn = 0 maxn 0 for i in s[-1]:
        if sum(i) <= 0:
            continue
        qq = []
        for j,v in enumerate(i):
            if v > 0:
                qq.append((d[d2[j]],v))
        qq.sort(key=lambda x:x[0], reverse=True)
        r = ''
        for k in qq:
            r += str(k[0]) * k[1]
        if int(r) > maxn:
            maxn = int(r)
     i s[-1] s -1 if sum(i) <= 0:
            continue
         sum(i) <= 0 sum(i) sum i 0 continue qq = [] qq [] for j,v in enumerate(i):
            if v > 0:
                qq.append((d[d2[j]],v))
         j v enumerate(i) enumerate i if v > 0:
                qq.append((d[d2[j]],v))
         v > 0 v 0 qq.append((d[d2[j]],v)) qq.append qq append (d[d2[j]],v) d[d2[j]] d d2[j] d2 j v qq.sort(key=lambda x:x[0], reverse=True) qq.sort qq sort key=lambda x:x[0] key lambda x:x[0] x[0] x 0 x reverse=True reverse True r = '' r '' for k in qq:
            r += str(k[0]) * k[1]
         k qq r += str(k[0]) * k[1] r str(k[0]) * k[1] str(k[0]) str k[0] k 0 k[1] k 1 if int(r) > maxn:
            maxn = int(r)
     int(r) > maxn int(r) int r maxn maxn = int(r) maxn int(r) int r print(maxn) print maxn main() main def main():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    l = [2,5,5,4,5,6,3,7,6]
    d = {}
    for i in A:
        if l[i-1] not in d:
            d[l[i-1]] = i
        else:
            if d[l[i-1]] < i:
                d[l[i-1]] = i
    d3 = {}
    for i in d:
        d3[d[i]] = i
    d2 = list(d)
    d2.sort()
    s = [[[-1] * (len(d2)) for _ in range(len(d2))] for _ in range(N+1)]
    dp = [[0] * (len(d2)) for _ in range(N+1)]
    for w in range(N+1):
        if w % d2[0] == 0:
            dp[w][0] = w // d2[0]
            s[w][0] = [0] * (len(d2))
            s[w][0][0] = w // d2[0]
    for i in range(1, len(d2)):
        for w in range(N+1):
            if w < d2[i]:
                continue
            elif max(dp[w-d2[i]]) > 0:
                x = dp[w-d2[i]].index(max(dp[w-d2[i]]))
                if dp[w][i-1] > dp[w-d2[i]][x] + 1:
                    dp[w][i] = dp[w][i-1]
                    s[w][i] = deepcopy(s[w][i-1])
                else:
                    dp[w][i] = dp[w-d2[i]][x] + 1
                    s[w][i] = deepcopy(s[w-d2[i]][x])
                    s[w][i][i] += 1
            elif w == d2[i]:
                dp[w][i] = 1
                s[w][i] = [0] * len(d2)
                s[w][i][i] = 1
            else:
                dp[w][i] = dp[w][i-1]
                s[w][i] = deepcopy(s[w][i-1])
    maxn = 0
    for i in s[-1]:
        if sum(i) <= 0:
            continue
        qq = []
        for j,v in enumerate(i):
            if v > 0:
                qq.append((d[d2[j]],v))
        qq.sort(key=lambda x:x[0], reverse=True)
        r = ''
        for k in qq:
            r += str(k[0]) * k[1]
        if int(r) > maxn:
            maxn = int(r)
    print(maxn) def main():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    l = [2,5,5,4,5,6,3,7,6]
    d = {}
    for i in A:
        if l[i-1] not in d:
            d[l[i-1]] = i
        else:
            if d[l[i-1]] < i:
                d[l[i-1]] = i
    d3 = {}
    for i in d:
        d3[d[i]] = i
    d2 = list(d)
    d2.sort()
    s = [[[-1] * (len(d2)) for _ in range(len(d2))] for _ in range(N+1)]
    dp = [[0] * (len(d2)) for _ in range(N+1)]
    for w in range(N+1):
        if w % d2[0] == 0:
            dp[w][0] = w // d2[0]
            s[w][0] = [0] * (len(d2))
            s[w][0][0] = w // d2[0]
    for i in range(1, len(d2)):
        for w in range(N+1):
            if w < d2[i]:
                continue
            elif max(dp[w-d2[i]]) > 0:
                x = dp[w-d2[i]].index(max(dp[w-d2[i]]))
                if dp[w][i-1] > dp[w-d2[i]][x] + 1:
                    dp[w][i] = dp[w][i-1]
                    s[w][i] = deepcopy(s[w][i-1])
                else:
                    dp[w][i] = dp[w-d2[i]][x] + 1
                    s[w][i] = deepcopy(s[w-d2[i]][x])
                    s[w][i][i] += 1
            elif w == d2[i]:
                dp[w][i] = 1
                s[w][i] = [0] * len(d2)
                s[w][i][i] = 1
            else:
                dp[w][i] = dp[w][i-1]
                s[w][i] = deepcopy(s[w][i-1])
    maxn = 0
    for i in s[-1]:
        if sum(i) <= 0:
            continue
        qq = []
        for j,v in enumerate(i):
            if v > 0:
                qq.append((d[d2[j]],v))
        qq.sort(key=lambda x:x[0], reverse=True)
        r = ''
        for k in qq:
            r += str(k[0]) * k[1]
        if int(r) > maxn:
            maxn = int(r)
    print(maxn) main
from copy import deepcopy
def main():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    l = [2,5,5,4,5,6,3,7,6]
    d = {}
    for i in A:
        if l[i-1] not in d:
            d[l[i-1]] = i
        else:
            if d[l[i-1]] < i:
                d[l[i-1]] = i
    d3 = {}
    for i in d:
        d3[d[i]] = i
    d2 = list(d)
    d2.sort()
    s = [[[-1] * (len(d2)) for _ in range(len(d2))] for _ in range(N+1)]
    dp = [[0] * (len(d2)) for _ in range(N+1)]
    for w in range(N+1):
        if w % d2[0] == 0:
            dp[w][0] = w // d2[0]
            s[w][0] = [0] * (len(d2))
            s[w][0][0] = w // d2[0]
    for i in range(1, len(d2)):
        for w in range(N+1):
            if w < d2[i]:
                continue
            elif max(dp[w-d2[i]]) > 0:
                x = dp[w-d2[i]].index(max(dp[w-d2[i]]))
                if dp[w][i-1] > dp[w-d2[i]][x] + 1:
                    dp[w][i] = dp[w][i-1]
                    s[w][i] = deepcopy(s[w][i-1])
                else:
                    dp[w][i] = dp[w-d2[i]][x] + 1
                    s[w][i] = deepcopy(s[w-d2[i]][x])
                    s[w][i][i] += 1
            elif w == d2[i]:
                dp[w][i] = 1
                s[w][i] = [0] * len(d2)
                s[w][i][i] = 1
            else:
                dp[w][i] = dp[w][i-1]
                s[w][i] = deepcopy(s[w][i-1])
    maxn = 0
    for i in s[-1]:
        if sum(i) <= 0:
            continue
        qq = []
        for j,v in enumerate(i):
            if v > 0:
                qq.append((d[d2[j]],v))
        qq.sort(key=lambda x:x[0], reverse=True)
        r = ''
        for k in qq:
            r += str(k[0]) * k[1]
        if int(r) > maxn:
            maxn = int(r)
    print(maxn)
main()
