n,m = map(int, input().split())
al = list(map(int, input().split()))

num_to_hon = [0, 2,5,5,4,5,6,3,7,6]
hon_to_num_dic = {}
for a in al:
    hon = num_to_hon[a]
    hon_to_num_dic.setdefault(hon,0)
    hon_to_num_dic[hon] = max(a, hon_to_num_dic[hon])


dp = [{'keta':0, 'num_cnts':[0]*10} for _ in range(n+10)]
for hon, num in hon_to_num_dic.items():
    dp[hon]['keta'] = 1
    dp[hon]['num_cnts'][num] += 1


for i in range(n):
    i_keta = dp[i]['keta']
    if i_keta == 0: continue
    for hon, num in hon_to_num_dic.items():
        i_num_cnts = dp[i]['num_cnts'][:]
        i_keta = dp[i]['keta']
        i_num_cnts[num] += 1
        next_dp = {'keta': i_keta+1, 'num_cnts':i_num_cnts[:]}
        # if i_keta == 0 and dp[i+hon]['keta'] > 0:
        #     continue
        if next_dp['keta'] > dp[i+hon]['keta']:
            dp[i+hon] = next_dp
            # print()
        elif next_dp['keta'] == dp[i+hon]['keta']:
            for comp_num in range(9,0,-1):
                if i_num_cnts[comp_num] > dp[i+hon]['num_cnts'][comp_num]:
                    dp[i+hon] = next_dp
                    break
                elif  i_num_cnts[comp_num] < dp[i+hon]['num_cnts'][comp_num]:
                    break


ans = []
for num in range(9,0,-1):
    ans.extend([str(num)]*dp[n]['num_cnts'][num])

print(''.join(ans)) n,m = map(int, input().split()) n map(int, input().split()) map int input().split() ().split () input split m al = list(map(int, input().split())) al list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split num_to_hon = [0, 2,5,5,4,5,6,3,7,6] num_to_hon [0, 2,5,5,4,5,6,3,7,6] 0 2 5 5 4 5 6 3 7 6 hon_to_num_dic = {} hon_to_num_dic {} for a in al:
    hon = num_to_hon[a]
    hon_to_num_dic.setdefault(hon,0)
    hon_to_num_dic[hon] = max(a, hon_to_num_dic[hon]) a al hon = num_to_hon[a] hon num_to_hon[a] num_to_hon a hon_to_num_dic.setdefault(hon,0) hon_to_num_dic.setdefault hon_to_num_dic setdefault hon 0 hon_to_num_dic[hon] = max(a, hon_to_num_dic[hon]) hon_to_num_dic[hon] hon_to_num_dic hon max(a, hon_to_num_dic[hon]) max a hon_to_num_dic[hon] hon_to_num_dic hon {'keta':0, 'num_cnts':[0]*10} for _ in range(n+10) for _ in range(n+10) _ range(n+10) range n+10 n 10 for _ in range(n+10) {'keta':0, 'num_cnts':[0]*10} 'keta' 'keta' 0 'num_cnts' 'num_cnts' [0]*10 [0] 0 10 dp = [{'keta':0, 'num_cnts':[0]*10} for _ in range(n+10)] dp [{'keta':0, 'num_cnts':[0]*10} for _ in range(n+10)] for hon, num in hon_to_num_dic.items():
    dp[hon]['keta'] = 1
    dp[hon]['num_cnts'][num] += 1 hon num hon_to_num_dic.items() hon_to_num_dic.items hon_to_num_dic items dp[hon]['keta'] = 1 dp[hon]['keta'] [hon] dp hon 'keta' 1 dp[hon]['num_cnts'][num] += 1 dp[hon]['num_cnts'][num] ['num_cnts'] [hon] dp hon 'num_cnts' num 1 for i in range(n):
    i_keta = dp[i]['keta']
    if i_keta == 0: continue
    for hon, num in hon_to_num_dic.items():
        i_num_cnts = dp[i]['num_cnts'][:]
        i_keta = dp[i]['keta']
        i_num_cnts[num] += 1
        next_dp = {'keta': i_keta+1, 'num_cnts':i_num_cnts[:]}
        # if i_keta == 0 and dp[i+hon]['keta'] > 0:
        #     continue
        if next_dp['keta'] > dp[i+hon]['keta']:
            dp[i+hon] = next_dp
            # print()
        elif next_dp['keta'] == dp[i+hon]['keta']:
            for comp_num in range(9,0,-1):
                if i_num_cnts[comp_num] > dp[i+hon]['num_cnts'][comp_num]:
                    dp[i+hon] = next_dp
                    break
                elif  i_num_cnts[comp_num] < dp[i+hon]['num_cnts'][comp_num]:
                    break i range(n) range n i_keta = dp[i]['keta'] i_keta dp[i]['keta'] [i] dp i 'keta' if i_keta == 0: continue
     i_keta == 0 i_keta 0 for hon, num in hon_to_num_dic.items():
        i_num_cnts = dp[i]['num_cnts'][:]
        i_keta = dp[i]['keta']
        i_num_cnts[num] += 1
        next_dp = {'keta': i_keta+1, 'num_cnts':i_num_cnts[:]}
        # if i_keta == 0 and dp[i+hon]['keta'] > 0:
        #     continue
        if next_dp['keta'] > dp[i+hon]['keta']:
            dp[i+hon] = next_dp
            # print()
        elif next_dp['keta'] == dp[i+hon]['keta']:
            for comp_num in range(9,0,-1):
                if i_num_cnts[comp_num] > dp[i+hon]['num_cnts'][comp_num]:
                    dp[i+hon] = next_dp
                    break
                elif  i_num_cnts[comp_num] < dp[i+hon]['num_cnts'][comp_num]:
                    break hon num hon_to_num_dic.items() hon_to_num_dic.items hon_to_num_dic items i_num_cnts = dp[i]['num_cnts'][:] i_num_cnts dp[i]['num_cnts'][:] ['num_cnts'] [i] dp i 'num_cnts' : i_keta = dp[i]['keta'] i_keta dp[i]['keta'] [i] dp i 'keta' i_num_cnts[num] += 1 i_num_cnts[num] i_num_cnts num 1 next_dp = {'keta': i_keta+1, 'num_cnts':i_num_cnts[:]} next_dp {'keta': i_keta+1, 'num_cnts':i_num_cnts[:]} 'keta' 'keta' i_keta+1 i_keta 1 'num_cnts' 'num_cnts' i_num_cnts[:] i_num_cnts : if next_dp['keta'] > dp[i+hon]['keta']:
            dp[i+hon] = next_dp
            # print()
        elif next_dp['keta'] == dp[i+hon]['keta']:
            for comp_num in range(9,0,-1):
                if i_num_cnts[comp_num] > dp[i+hon]['num_cnts'][comp_num]:
                    dp[i+hon] = next_dp
                    break
                elif  i_num_cnts[comp_num] < dp[i+hon]['num_cnts'][comp_num]:
                    break next_dp['keta'] > dp[i+hon]['keta'] next_dp['keta'] next_dp 'keta' dp[i+hon]['keta'] [i+hon] dp i+hon i hon 'keta' dp[i+hon] = next_dp dp[i+hon] dp i+hon i hon next_dp elif next_dp['keta'] == dp[i+hon]['keta']:
            for comp_num in range(9,0,-1):
                if i_num_cnts[comp_num] > dp[i+hon]['num_cnts'][comp_num]:
                    dp[i+hon] = next_dp
                    break
                elif  i_num_cnts[comp_num] < dp[i+hon]['num_cnts'][comp_num]:
                    break next_dp['keta'] == dp[i+hon]['keta'] next_dp['keta'] next_dp 'keta' dp[i+hon]['keta'] [i+hon] dp i+hon i hon 'keta' for comp_num in range(9,0,-1):
                if i_num_cnts[comp_num] > dp[i+hon]['num_cnts'][comp_num]:
                    dp[i+hon] = next_dp
                    break
                elif  i_num_cnts[comp_num] < dp[i+hon]['num_cnts'][comp_num]:
                    break comp_num range(9,0,-1) range 9 0 -1 if i_num_cnts[comp_num] > dp[i+hon]['num_cnts'][comp_num]:
                    dp[i+hon] = next_dp
                    break
                elif  i_num_cnts[comp_num] < dp[i+hon]['num_cnts'][comp_num]:
                    break i_num_cnts[comp_num] > dp[i+hon]['num_cnts'][comp_num] i_num_cnts[comp_num] i_num_cnts comp_num dp[i+hon]['num_cnts'][comp_num] ['num_cnts'] [i+hon] dp i+hon i hon 'num_cnts' comp_num dp[i+hon] = next_dp dp[i+hon] dp i+hon i hon next_dp break elif  i_num_cnts[comp_num] < dp[i+hon]['num_cnts'][comp_num]:
                    break i_num_cnts[comp_num] < dp[i+hon]['num_cnts'][comp_num] i_num_cnts[comp_num] i_num_cnts comp_num dp[i+hon]['num_cnts'][comp_num] ['num_cnts'] [i+hon] dp i+hon i hon 'num_cnts' comp_num break ans = [] ans [] for num in range(9,0,-1):
    ans.extend([str(num)]*dp[n]['num_cnts'][num]) num range(9,0,-1) range 9 0 -1 ans.extend([str(num)]*dp[n]['num_cnts'][num]) ans.extend ans extend [str(num)]*dp[n]['num_cnts'][num] [str(num)] str(num) str num dp[n]['num_cnts'][num] ['num_cnts'] [n] dp n 'num_cnts' num print(''.join(ans)) print ''.join(ans) ''.join '' join ans m = map(int, input().split()) map(int, input().split()) m al = list(map(int, input().split())) list(map(int, input().split())) al hon_to_num_dic = {} {} hon_to_num_dic i_keta = dp[i]['keta'] dp[i]['keta'] i_keta next_dp = {'keta': i_keta+1, 'num_cnts':i_num_cnts[:]} {'keta': i_keta+1, 'num_cnts':i_num_cnts[:]} next_dp i_keta = dp[i]['keta'] dp[i]['keta'] i_keta n,m = map(int, input().split()) map(int, input().split()) n hon = num_to_hon[a] num_to_hon[a] hon num_to_hon = [0, 2,5,5,4,5,6,3,7,6] [0, 2,5,5,4,5,6,3,7,6] num_to_hon dp = [{'keta':0, 'num_cnts':[0]*10} for _ in range(n+10)] [{'keta':0, 'num_cnts':[0]*10} for _ in range(n+10)] dp i_num_cnts = dp[i]['num_cnts'][:] dp[i]['num_cnts'][:] i_num_cnts ans = [] [] ans
n,m = map(int, input().split())
al = list(map(int, input().split()))

num_to_hon = [0, 2,5,5,4,5,6,3,7,6]
hon_to_num_dic = {}
for a in al:
    hon = num_to_hon[a]
    hon_to_num_dic.setdefault(hon,0)
    hon_to_num_dic[hon] = max(a, hon_to_num_dic[hon])


dp = [{'keta':0, 'num_cnts':[0]*10} for _ in range(n+10)]
for hon, num in hon_to_num_dic.items():
    dp[hon]['keta'] = 1
    dp[hon]['num_cnts'][num] += 1


for i in range(n):
    i_keta = dp[i]['keta']
    if i_keta == 0: continue
    for hon, num in hon_to_num_dic.items():
        i_num_cnts = dp[i]['num_cnts'][:]
        i_keta = dp[i]['keta']
        i_num_cnts[num] += 1
        next_dp = {'keta': i_keta+1, 'num_cnts':i_num_cnts[:]}
        # if i_keta == 0 and dp[i+hon]['keta'] > 0:
        #     continue
        if next_dp['keta'] > dp[i+hon]['keta']:
            dp[i+hon] = next_dp
            # print()
        elif next_dp['keta'] == dp[i+hon]['keta']:
            for comp_num in range(9,0,-1):
                if i_num_cnts[comp_num] > dp[i+hon]['num_cnts'][comp_num]:
                    dp[i+hon] = next_dp
                    break
                elif  i_num_cnts[comp_num] < dp[i+hon]['num_cnts'][comp_num]:
                    break


ans = []
for num in range(9,0,-1):
    ans.extend([str(num)]*dp[n]['num_cnts'][num])

print(''.join(ans))