Problem: 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.

Submission attempts (in order):
N,M=map(int,input().split())
x=[0,2,5,5,4,5,6,3,7,6]
a=list(map(int,input().split()))
a.sort()
x=[ x[a[i]] for i in range(M) ]


p=[[-1 for j in range(M)] for i in range(N+10)]
p[0]=[0 for j in range(M)]

for i in range(N+1):
    for j in range(M):
        if p[i][0]>-1 and sum(p[i])+1>sum(p[i+x[j]]):
            p[i+x[j]]=[p[i][k] for k in range(M)]
            p[i+x[j]][j]+=1

        elif p[i][0]>-1 and sum(p[i])+1==sum(p[i+x[j]]):
            tmp=[p[i][k] for k in range(M)]

            tmp[j]+=1
            for k in range(M-1,-1,-1):
                if tmp[k] > p[i+x[j]][k] :
                    p[i+x[j]]=[tmp[k] for k in range(M)]
                    break


for i in range(M-1,-1,-1):
    for j in range(p[N][i]):
        print(a[i],end="")

N,M=map(int,input().split())
x=[0,2,5,5,4,5,6,3,7,6]
a=list(map(int,input().split()))
a.sort()
x=[ x[a[i]] for i in range(M) ]

p=[[-1 for j in range(M)] for i in range(430)]
p[0]=[0 for j in range(M)]
for i in range(10):
    for j in range(M):
        if p[i][0]>-1 and sum(p[i])+1>=sum(p[i+x[j]]):
            p[i+x[j]]=[p[i][k] for k in range(M)]
            p[i+x[j]][j]+=1

for j in range(M):
    if x[j]==min(x): Mi=j

ans=[0 for i in range(M)]
for i in range(420):
    if N-i<0: break
    if (N-i)%x[Mi]==0 and (N-i)//x[Mi]+sum(p[i])>sum(ans):
        ans=p[i]
        ans[Mi]+=(N-i)//x[Mi]

for i in range(M-1,-1,-1):
    for j in range(ans[i]):
        print(a[i],end="")

import sys

def chk(i,j):
    ip=i+x[j]
    tmp=[p[i][k] for k in range(M)]
    tmp[j]+=1
    for k in range(M-1,-1,-1):
        if tmp[k] > p[ip][k] :
            p[ip]=[tmp[l] for l in range(M)]
            break

N,M=map(int,input().split())
x=[0,2,5,5,4,5,6,3,7,6]
a=list(map(int,input().split()))
a.sort()
b=[]
for i in range(M):
    for j in range(M):
        if x[a[j]]==x[a[i]]: tmp=j
    b.append(a[tmp]) 

a=list(set(b))
a.sort()
M=len(a)
x=[ x[a[i]] for i in range(M) ]



p=[[-100 for j in range(M)] for i in range(N+10)]
p[0]=[0 for j in range(M)]

for i in range(N+1):
    if sum(p[i])>-1:
        for j in range(M):
            if sum(p[i])+1>sum(p[i+x[j]]):
                p[i+x[j]] = [ p[i][k] for k in range(M) ]
                p[i+x[j]][j]+=1
            
            elif sum(p[i])+1==sum(p[i+x[j]]) and chk(i,j):
                p[i+x[j]] = [ p[i][k] for k in range(M) ]
                p[i+x[j]][j]+=1
                

for i in range(M-1,-1,-1):
    for j in range(p[N][i]):
        print(a[i],end="")


N,M=map(int,input().split())
x=[0,2,5,5,4,5,6,3,7,6]
a=list(map(int,input().split()))
a.sort()
x=[ x[a[i]] for i in range(M) ]

p=[[-1 for j in range(M)] for i in range(430)]
p[0]=[0 for j in range(M)]

for i in range(421):
    for j in range(M):
        if p[i][0]>-1 and sum(p[i])+1>sum(p[i+x[j]]):
            p[i+x[j]]=[p[i][k] for k in range(M)]
            p[i+x[j]][j]+=1

        elif p[i][0]>-1 and sum(p[i])+1==sum(p[i+x[j]]):
            tmp=[p[i][k] for k in range(M)]

            tmp[j]+=1
            for k in range(M-1,-1,-1):
                if tmp[k] > p[i+x[j]][k] :
                    p[i+x[j]]=[tmp[k] for k in range(M)]
                    break

for j in range(M):
    if x[j]==min(x): Mi=j

ans=[0 for i in range(M)]
for i in range(421):
    if N-i<0: break
    if (N-i)%x[Mi]==0 and (N-i)//x[Mi]+sum(p[i])>=sum(ans):
        ans=p[i]
        ans[Mi]+=(N-i)//x[Mi]

for i in range(M-1,-1,-1):
    for j in range(ans[i]):
        print(a[i],end="")

N,M=map(int,input().split())
x=[0,2,5,5,4,5,6,3,7,6]
a=list(map(int,input().split()))
a.sort()
x=[ x[a[i]] for i in range(M) ]
p=[[-1 for j in range(M)] for i in range(430)]
p[0]=[0 for j in range(M)]

for i in range(10):
    for j in range(M):
        if p[i][0]>-1 and sum(p[i])+1>sum(p[i+x[j]]):
            p[i+x[j]]=[p[i][k] for k in range(M)]
            p[i+x[j]][j]+=1

for j in range(M):
    if x[j]==min(x): Mi=j

ans=[0 for i in range(M)]
for i in range(420):
    if N-i<0: break
    if (N-i)%x[Mi]==0 and (N-i)//x[Mi]+sum(p[i])>sum(ans):
        ans=p[i]
        ans[Mi]+=(N-i)//x[Mi]

for i in range(M-1,-1,-1):
    for j in range(ans[i]):
        print(a[i],end="")

import sys

def chk(i,j):
    tmp=[p[i][k] for k in range(M)]
    tmp[j]+=1
    for k in range(M-1,-1,-1):
        if tmp[k] > p[i+x[j]][k] :
            p[i+x[j]]=[tmp[k] for k in range(M)]
            return

N,M=map(int,input().split())
x=[0,2,5,5,4,5,6,3,7,6]
a=list(map(int,input().split()))
a.sort()
b=[]
for i in range(M):
    for j in range(M):
        if x[a[j]]==x[a[i]]: tmp=j
    b.append(a[tmp]) 

a=list(set(b))
a.sort()
M=len(a)
x=[ x[a[i]] for i in range(M) ]


p=[[-1 for j in range(M)] for i in range(N+10)]
p[0]=[0 for j in range(M)]

for i in range(N+1):
    if sum(p[i])>-1:
        for j in range(M):
            if sum(p[i])+1>sum(p[i+x[j]]):
                p[i+x[j]]=[ p[i][k] for k in range(M) ]
                p[i+x[j]][j]+=1

            elif sum(p[i])+1==sum(p[i+x[j]]):
                chk(i,j)


for i in range(M-1,-1,-1):
    for j in range(p[N][i]):
        print(a[i],end="")

import sys

def chk(i,j):
    tmp=[p[i][k] for k in range(M)]
    tmp[j]+=1
    for k in range(M-1,-1,-1):
        if tmp[k] > p[i+x[j]][k] :
            p[i+x[j]]=[tmp[k] for k in range(M)]
            return

N,M=map(int,input().split())
x=[0,2,5,5,4,5,6,3,7,6]
a=list(map(int,input().split()))
a.sort()
b=[]
for i in range(M):
    for j in range(M):
        if x[a[j]]==x[a[i]]: tmp=j
    b.append(a[tmp]) 

a=list(set(b))
a.sort()
M=len(a)
x=[ x[a[i]] for i in range(M) ]



p=[[-100 for j in range(M)] for i in range(N+10)]
p[0]=[0 for j in range(M)]

for i in range(N+1):
    if sum(p[i])>-1:
        for j in range(M):
            if sum(p[i])+1==sum(p[i+x[j]]):
                chk(i,j)

            if sum(p[i])+1>sum(p[i+x[j]]):
                p[i+x[j]] = [ p[i][k] for k in range(M) ]
                p[i+x[j]][j]+=1




for i in range(M-1,-1,-1):
    for j in range(p[N][i]):
        print(a[i],end="")


N,M=map(int,input().split())
x=[0,2,5,5,4,5,6,3,7,6]
a=list(map(int,input().split()))
a.sort()
x=[ x[a[i]] for i in range(M) ]


p=[[-1 for j in range(M)] for i in range(N+10)]
p[0]=[0 for j in range(M)]

for i in range(N+1):
    for j in range(M-1,-1,-1):
        if p[i][0]>-1:
            if sum(p[i])+1>sum(p[i+x[j]]):
                p[i+x[j]]=[ p[i][k] for k in range(M) ]
                p[i+x[j]][j]+=1

            elif sum(p[i])+1==sum(p[i+x[j]]):
                tmp=[p[i][k] for k in range(M)]
                tmp[j]+=1
                for k in range(M-1,-1,-1):
                    if tmp[k] > p[i+x[j]][k] :
                        p[i+x[j]]=[tmp[k] for k in range(M)]
                        break


for i in range(M-1,-1,-1):
    for j in range(p[N][i]):
        print(a[i],end="")

import sys

def chk(i,j):
    tmp=[p[i][k] for k in range(M)]
    tmp[j]=p[i][j]+1
    for k in range(M-1,-1,-1):
        if tmp[k] > p[i+x[j]][k] :
            p[i+x[j]]=[tmp[k] for k in range(M)]

N,M=map(int,input().split())
x=[0,2,5,5,4,5,6,3,7,6]
a=list(map(int,input().split()))
a.sort()
b=[]
for i in range(M):
    for j in range(M):
        if x[a[j]]==x[a[i]]: tmp=j
    b.append(a[tmp]) 

a=list(set(b))
a.sort()
M=len(a)
x=[ x[a[i]] for i in range(M) ]



p=[[-100 for j in range(M)] for i in range(N+10)]
p[0]=[0 for j in range(M)]

for i in range(N+1):
    if sum(p[i])>-1:
        for j in range(M-1,-1,-1):
            if sum(p[i])+1>sum(p[i+x[j]]):
                p[i+x[j]] = [ p[i][k] for k in range(M) ]
                p[i+x[j]][j]=p[i][j]+1

            elif sum(p[i])+1==sum(p[i+x[j]]):
                chk(i,j)

ans=""
while N>0:
    for j in range(M-1,-1,-1):
        if sum(p[N])==sum(p[N-x[j]])+1:
            ans+=str(a[j])
            N-=x[j]
            break
print(ans[::-1])




import sys

def chk(i,j):
    tmp=[]
    tmp=[p[i][k] for k in range(M)]
    tmp[j]+=1
    for k in range(M-1,-1,-1):
        if tmp[k] > p[i+x[j]][k] :
            p[i+x[j]]=[tmp[k] for k in range(M)]
            return

N,M=map(int,input().split())
x=[0,2,5,5,4,5,6,3,7,6]
a=list(map(int,input().split()))
a.sort()
b=[]
for i in range(M):
    for j in range(M):
        if x[a[j]]==x[a[i]]: tmp=j
    b.append(a[tmp]) 

a=list(set(b))
a.sort()
M=len(a)
x=[ x[a[i]] for i in range(M) ]



p=[[-100 for j in range(M)] for i in range(N+10)]
p[0]=[0 for j in range(M)]

for i in range(N+1):
    if sum(p[i])>-1:
        for j in range(M):
            if sum(p[i])+1==sum(p[i+x[j]]):
                chk(i,j)

            elif sum(p[i])+1>sum(p[i+x[j]]):
                p[i+x[j]] = [ p[i][k] for k in range(M) ]
                p[i+x[j]][j]+=1




for i in range(M-1,-1,-1):
    for j in range(p[N][i]):
        print(a[i],end="")


def chk(i,j):
    tmp=[p[i][k] for k in range(M)]
    tmp[j]+=1
    for k in range(M-1,-1,-1):
        if tmp[k] > p[i+x[j]][k] :
            p[i+x[j]]=[tmp[k] for k in range(M)]
            return

N,M=map(int,input().split())
x=[0,2,5,5,4,5,6,3,7,6]
a=list(map(int,input().split()))
a.sort()
x=[ x[a[i]] for i in range(M) ]

p=[[-1 for j in range(M)] for i in range(N+10)]
p[0]=[0 for j in range(M)]

for i in range(N+1):
    if p[i][0]>-1:
        for j in range(M):
            if sum(p[i])+1>sum(p[i+x[j]]):
                p[i+x[j]]=[ p[i][k] for k in range(M) ]
                p[i+x[j]][j]+=1

            elif sum(p[i])+1==sum(p[i+x[j]]):
                chk(i,j)


for i in range(M-1,-1,-1):
    for j in range(p[N][i]):
        print(a[i],end="")

import sys

def chk(i,j):
    tmp=[p[i][k] for k in range(M)]
    tmp[j]=p[i][j]+1
    for k in range(M-1,-1,-1):
        if tmp[k] > p[i+x[j]][k] :
            p[i+x[j]]=[tmp[k] for k in range(M)]

N,M=map(int,input().split())
x=[0,2,5,5,4,5,6,3,7,6]
a=list(map(int,input().split()))
a.sort()
b=[]
for i in range(M):
    for j in range(M):
        if x[a[j]]==x[a[i]]: tmp=j
    b.append(a[tmp]) 

a=list(set(b))
a.sort()
M=len(a)
x=[ x[a[i]] for i in range(M) ]



p=[[-100 for j in range(M)] for i in range(N+10)]
p[0]=[0 for j in range(M)]

for i in range(N+1):
    if sum(p[i])>-1:
        for j in range(M-1,-1,-1):
            if sum(p[i])+1>sum(p[i+x[j]]):
                p[i+x[j]] = [ p[i][k] for k in range(M) ]
                p[i+x[j]][j]=p[i][j]+1

            elif sum(p[i])+1==sum(p[i+x[j]]):
                chk(i,j)


for i in range(M-1,-1,-1):
    for j in range(p[N][i]):
        print(a[i],end="")


import sys

def chk(i,j):
    tmp=[p[i][k] for k in range(M)]
    tmp[j]=p[i][j]+1
    for k in range(M-1,-1,-1):
        if tmp[k] > p[i+x[j]][k] :
            p[i+x[j]]=[tmp[k] for k in range(M)]

N,M=map(int,input().split())
x=[0,2,5,5,4,5,6,3,7,6]
a=list(map(int,input().split()))
a.sort()
b=[]
for i in range(M):
    for j in range(M):
        if x[a[j]]==x[a[i]]: tmp=j
    b.append(a[tmp]) 

a=list(set(b))
a.sort()
M=len(a)
x=[ x[a[i]] for i in range(M) ]

p=[-100 for i in range(N+10)]
p[0]=0
for i in range(N):
    if p[i]>-1:
        for j in range(M):
            p[i+x[j]]=max(p[i+x[j]],p[i]+1)

ans=""
while N>0:
    for j in range(M-1,-1,-1):
        if p[N]==p[N-x[j]]+1:
            ans+=str(a[j])
            N-=x[j]
            break
print(ans[::-1])




import sys

def chk(i,j):
    ip=i+x[j]
    tmp=[p[i][k] for k in range(M)]
    tmp[j]+=1
    for k in range(M-1,-1,-1):
        if tmp[k] > p[ip][k] :
            p[ip]=[tmp[l] for l in range(M)]
            return
    return

N,M=map(int,input().split())
x=[0,2,5,5,4,5,6,3,7,6]
a=list(map(int,input().split()))
a.sort()
b=[]
for i in range(M):
    for j in range(M):
        if x[a[j]]==x[a[i]]: tmp=j
    b.append(a[tmp]) 

a=list(set(b))
a.sort()
M=len(a)
x=[ x[a[i]] for i in range(M) ]



p=[[-100 for j in range(M)] for i in range(N+10)]
p[0]=[0 for j in range(M)]

for i in range(N+1):
    if sum(p[i])>-1:
        for j in range(M):
            if sum(p[i])+1>sum(p[i+x[j]]):
                p[i+x[j]] = [ p[i][k] for k in range(M) ]
                p[i+x[j]][j]+=1
            
            elif sum(p[i])+1==sum(p[i+x[j]]):
                chk(i,j)
                

for i in range(M-1,-1,-1):
    for j in range(p[N][i]):
        print(a[i],end="")


import sys

def chk(i,j):
    tmp=[p[i][k] for k in range(M)]
    tmp[j]=tmp[j]+1
    for k in range(M-1,-1,-1):
        if tmp[k] > p[i+x[j]][k] :
            p[i+x[j]]=[tmp[k] for k in range(M)]
            return

N,M=map(int,input().split())
x=[0,2,5,5,4,5,6,3,7,6]
a=list(map(int,input().split()))
a.sort()
b=[]
for i in range(M):
    for j in range(M):
        if x[a[j]]==x[a[i]]: tmp=j
    b.append(a[tmp]) 

a=list(set(b))
a.sort()
M=len(a)
x=[ x[a[i]] for i in range(M) ]



p=[[-100 for j in range(M)] for i in range(N+10)]
p[0]=[0 for j in range(M)]

for i in range(N+1):
    if sum(p[i])>-1:
        for j in range(M-1,-1,-1):
            if sum(p[i])+1>sum(p[i+x[j]]):
                p[i+x[j]] = [ p[i][k] for k in range(M) ]
                p[i+x[j]][j]=p[i+x[j]][j]+1

            elif sum(p[i])+1==sum(p[i+x[j]]):
                chk(i,j)


for i in range(M-1,-1,-1):
    for j in range(p[N][i]):
        print(a[i],end="")


import sys
 
def chk(i,j):
    tmp=[p[i][k] for k in range(M)]
    tmp[j]=p[i][j]+1
    for k in range(M-1,-1,-1):
        if tmp[k] > p[i+x[j]][k] :
            p[i+x[j]]=[tmp[k] for k in range(M)]
            return
 
N,M=map(int,input().split())
x=[0,2,5,5,4,5,6,3,7,6]
a=list(map(int,input().split()))
a.sort()
b=[]
for i in range(M):
    for j in range(M):
        if x[a[j]]==x[a[i]]: tmp=j
    b.append(a[tmp]) 
 
a=list(set(b))
a.sort()
M=len(a)
x=[ x[a[i]] for i in range(M) ]
 
 
 
p=[[-100 for j in range(M)] for i in range(N+10)]
p[0]=[0 for j in range(M)]
 
for i in range(N+1):
    if sum(p[i])>-1:
        for j in range(M):
            if sum(p[i])+1>sum(p[i+x[j]]):
                p[i+x[j]] = [ p[i][k] for k in range(M) ]
                p[i+x[j]][j]=p[i][j]+1
 
            elif sum(p[i])+1==sum(p[i+x[j]]):
                chk(i,j)
 
 
for i in range(M-1,-1,-1):
    for j in range(p[N][i]):
        print(a[i],end="")

def chk(i,j):
    tmp=[p[i][k] for k in range(M)]
    tmp[j]=p[i][j]
    for k in range(M-1,-1,-1):
        if tmp[k] > p[i+x[j]][k] :
            p[i+x[j]]=[tmp[k] for k in range(M)]
            return

N,M=map(int,input().split())
x=[0,2,5,5,4,5,6,3,7,6]
a=list(map(int,input().split()))
a.sort()
b=[]
for i in range(M):
    for j in range(M):
        if x[a[j]]==x[a[i]]: tmp=j
    b.append(a[tmp]) 

a=list(set(b))
a.sort()
M=len(a)
x=[ x[a[i]] for i in range(M) ]



p=[[-100 for j in range(M)] for i in range(N+100)]
p[0]=[0 for j in range(M)]

for i in range(N+10):
    if sum(p[i])>-1:
        for j in range(M):
            if sum(p[i])+1>sum(p[i+x[j]]):
                p[i+x[j]] = [ p[i][k] for k in range(M) ]
                p[i+x[j]][j]=p[i][j]+1

            elif sum(p[i])+1==sum(p[i+x[j]]):
                chk(i,j)


for i in range(M-1,-1,-1):
    for j in range(p[N][i]):
        print(a[i],end="")

def chk(i,j):
    tmp=[p[i][k] for k in range(M)]
    tmp[j]+=1
    for k in range(M-1,-1,-1):
        if tmp[k] > p[i+x[j]][k] :
            p[i+x[j]]=[tmp[k] for k in range(M)]
            return

N,M=map(int,input().split())
x=[0,2,5,5,4,5,6,3,7,6]
a=list(map(int,input().split()))
a.sort()
x=[ x[a[i]] for i in range(M) ]


p=[[-1 for j in range(M)] for i in range(N+10)]
p[0]=[0 for j in range(M)]

for i in range(N+1):
    if sum(p[i])>-1:
        for j in range(M):
            if sum(p[i])+1>sum(p[i+x[j]]):
                p[i+x[j]]=[ p[i][k] for k in range(M) ]
                p[i+x[j]][j]+=1

            elif sum(p[i])+1==sum(p[i+x[j]]):
                chk(i,j)


for i in range(M-1,-1,-1):
    for j in range(p[N][i]):
        print(a[i],end="")

import sys

def chk(i,j):
    tmp=[p[i][k] for k in range(M)]
    tmp[j]=p[i][j]
    for k in range(M-1,-1,-1):
        if tmp[k] > p[i+x[j]][k] :
            p[i+x[j]]=[tmp[k] for k in range(M)]
            return

N,M=map(int,input().split())
x=[0,2,5,5,4,5,6,3,7,6]
a=list(map(int,input().split()))
a.sort()
b=[]
for i in range(M):
    for j in range(M):
        if x[a[j]]==x[a[i]]: tmp=j
    b.append(a[tmp]) 

a=list(set(b))
a.sort()
M=len(a)
x=[ x[a[i]] for i in range(M) ]



p=[[-100 for j in range(M)] for i in range(N+10)]
p[0]=[0 for j in range(M)]

for i in range(N+1):
    if sum(p[i])>-1:
        for j in range(M):
            if sum(p[i])+1>sum(p[i+x[j]]):
                p[i+x[j]] = [ p[i][k] for k in range(M) ]
                p[i+x[j]][j]=p[i][j]+1

            elif sum(p[i])+1==sum(p[i+x[j]]):
                chk(i,j)


for i in range(M-1,-1,-1):
    for j in range(p[N][i]):
        print(a[i],end="")

N,M=map(int,input().split())
x=[0,2,5,5,4,5,6,3,7,6]
a=list(map(int,input().split()))
a.sort()
x=[ x[a[i]] for i in range(M) ]

p=[[-1 for j in range(M)] for i in range(430)]
p[0]=[0 for j in range(M)]

for i in range(421):
    for j in range(M):
        if p[i][0]>-1 and sum(p[i])+1>sum(p[i+x[j]]):
            p[i+x[j]]=[p[i][k] for k in range(M)]
            p[i+x[j]][j]+=1

        elif p[i][0]>-1 and sum(p[i])+1==sum(p[i+x[j]]):
            tmp=[p[i][k] for k in range(M)]

            tmp[j]+=1
            for k in range(M-1,-1,-1):
                if tmp[k] > p[i+x[j]][k] :
                    p[i+x[j]]=[tmp[k] for k in range(M)]
                    break

for j in range(M):
    if x[j]==min(x): Mi=j

ans=[0 for i in range(M)]
for i in range(420):
    if N-i<0: break
    if (N-i)%x[Mi]==0 and (N-i)//x[Mi]+sum(p[i])>sum(ans):
        ans=p[i]
        ans[Mi]+=(N-i)//x[Mi]

for i in range(M-1,-1,-1):
    for j in range(ans[i]):
        print(a[i],end="")

import sys

def chk(i,j):
    tmp=[]
    tmp=[p[i][k] for k in range(M)]
    tmp[j]+=1
    for k in range(M-1,-1,-1):
        if tmp[k] > p[i+x[j]][k] : return 1
    
    return 0

N,M=map(int,input().split())
x=[0,2,5,5,4,5,6,3,7,6]
a=list(map(int,input().split()))
a.sort()
b=[]
for i in range(M):
    for j in range(M):
        if x[a[j]]==x[a[i]]: tmp=j
    b.append(a[tmp]) 

a=list(set(b))
a.sort()
M=len(a)
x=[ x[a[i]] for i in range(M) ]



p=[[-100 for j in range(M)] for i in range(N+10)]
p[0]=[0 for j in range(M)]

for i in range(N+1):
    if sum(p[i])>-1:
        for j in range(M):
            if sum(p[i])+1>sum(p[i+x[j]]):
                p[i+x[j]] = [ p[i][k] for k in range(M) ]
                p[i+x[j]][j]+=1
            
            elif sum(p[i])+1==sum(p[i+x[j]]) and chk(i,j):
                p[i+x[j]] = [ p[i][k] for k in range(M) ]
                p[i+x[j]][j]+=1
                

for i in range(M-1,-1,-1):
    for j in range(p[N][i]):
        print(a[i],end="")


import sys

def chk(i,j):
    ip=i+x[j]
    tmp=[p[i][k] for k in range(M)]
    tmp[j]+=1
    for k in range(M-1,-1,-1):
        if tmp[k] > p[ip][k] :
            p[ip]=[tmp[l] for l in range(M)]
            return
    return

N,M=map(int,input().split())
x=[0,2,5,5,4,5,6,3,7,6]
a=list(map(int,input().split()))
a.sort()
b=[]
for i in range(M):
    for j in range(M):
        if x[a[j]]==x[a[i]]: tmp=j
    b.append(a[tmp]) 

a=list(set(b))
a.sort()
M=len(a)
x=[ x[a[i]] for i in range(M) ]



p=[[-100 for j in range(M)] for i in range(N+10)]
p[0]=[0 for j in range(M)]

for i in range(N+1):
    if sum(p[i])>-1:
        for j in range(M):
            if sum(p[i])+1>sum(p[i+x[j]]):
                p[i+x[j]] = [ p[i][k] for k in range(M) ]
                p[i+x[j]][j]+=1
            
            elif sum(p[i])+1==sum(p[i+x[j]]) and chk(i,j):
                p[i+x[j]] = [ p[i][k] for k in range(M) ]
                p[i+x[j]][j]+=1
                

for i in range(M-1,-1,-1):
    for j in range(p[N][i]):
        print(a[i],end="")


N,M=map(int,input().split())
x=[0,2,5,5,4,5,6,3,7,6]
a=list(map(int,input().split()))
a.sort()
x=[ x[a[i]] for i in range(M) ]

p=[[-1 for j in range(M)] for i in range(N+10)]
p[0]=[0 for j in range(M)]

for i in range(N+1):
    for j in range(M):
        if p[i][0]>-1 and sum(p[i])+1>sum(p[i+x[j]]):
            p[i+x[j]]=[p[i][k] for k in range(M)]
            p[i+x[j]][j]+=1

        elif p[i][0]>-1 and sum(p[i])+1==sum(p[i+x[j]]):
            tmp=[p[i][k] for k in range(M)]
            tmp[j]+=1
            for k in range(M-1,-1,-1) and tmp!=p[i+x[j]]:
                if tmp[k] > p[i+x[j]][k] :
                    p[i+x[j]]=[tmp[k] for k in range(M)]
                    break


for i in range(M-1,-1,-1):
    for j in range(p[N][i]):
        print(a[i],end="")

N,M=map(int,input().split())
x=[0,2,5,5,4,5,6,3,7,6]
a=list(map(int,input().split()))
a.sort()
x=[ x[a[i]] for i in range(M) ]

p=[[-1 for j in range(M)] for i in range(N+10)]
p[0]=[0 for j in range(M)]

for i in range(N+1):
    for j in range(M):
        if p[i][0]>-1 and sum(p[i])+1>sum(p[i+x[j]]):
            p[i+x[j]]=[p[i][k] for k in range(M)]
            p[i+x[j]][j]+=1

        elif p[i][0]>-1 and sum(p[i])+1==sum(p[i+x[j]]):
            tmp=[p[i][k] for k in range(M)]
            tmp[j]+=1
            if tmp!=p[i+x[j]]:
                for k in range(M-1,-1,-1):
                    if tmp[k] > p[i+x[j]][k] :
                        p[i+x[j]]=[tmp[k] for k in range(M)]
                        break


for i in range(M-1,-1,-1):
    for j in range(p[N][i]):
        print(a[i],end="")

import sys

def chk(i,j):
    tmp=[p[i][k] for k in range(M)]
    tmp[j]=p[i][j]
    for k in range(M-1,-1,-1):
        if tmp[k] > p[i+x[j]][k] :
            p[i+x[j]]=[tmp[k] for k in range(M)]
            return

N,M=map(int,input().split())
x=[0,2,5,5,4,5,6,3,7,6]
a=list(map(int,input().split()))
a.sort()
b=[]
for i in range(M):
    for j in range(M):
        if x[a[j]]==x[a[i]]: tmp=j
    b.append(a[tmp]) 

a=list(set(b))
a.sort()
M=len(a)
x=[ x[a[i]] for i in range(M) ]



p=[[-100 for j in range(M)] for i in range(N+10)]
p[0]=[0 for j in range(M)]

for i in range(N+1):
    if sum(p[i])>-1:
        for j in range(M-1,-1,-1):
            if sum(p[i])+1>sum(p[i+x[j]]):
                p[i+x[j]] = [ p[i][k] for k in range(M) ]
                p[i+x[j]][j]=p[i][j]+1

            elif sum(p[i])+1==sum(p[i+x[j]]):
                chk(i,j)


for i in range(M-1,-1,-1):
    for j in range(p[N][i]):
        print(a[i],end="")


import sys

def chk(i,j):
    tmp=[]
    tmp=[p[i][k] for k in range(M)]
    tmp[j]+=1
    for k in range(M-1,-1,-1):
        if tmp[k] > p[i+x[j]][k] :
            p[i+x[j]]=[tmp[l] for l in range(M)]
            return

N,M=map(int,input().split())
x=[0,2,5,5,4,5,6,3,7,6]
a=list(map(int,input().split()))
a.sort()
b=[]
for i in range(M):
    for j in range(M):
        if x[a[j]]==x[a[i]]: tmp=j
    b.append(a[tmp]) 

a=list(set(b))
a.sort()
M=len(a)
x=[ x[a[i]] for i in range(M) ]



p=[[-100 for j in range(M)] for i in range(N+10)]
p[0]=[0 for j in range(M)]

for i in range(N+1):
    if sum(p[i])>-1:
        for j in range(M):
            if sum(p[i])+1>sum(p[i+x[j]]):
                p[i+x[j]] = [ p[i][k] for k in range(M) ]
                p[i+x[j]][j]+=1
            
            elif sum(p[i])+1==sum(p[i+x[j]]):
                chk(i,j)

for i in range(M-1,-1,-1):
    for j in range(p[N][i]):
        print(a[i],end="")


N,M=map(int,input().split())
x=[0,2,5,5,4,5,6,3,7,6]
a=list(map(int,input().split()))
a.sort()
x=[ x[a[i]] for i in range(M) ]
 
p=[[-1 for j in range(M)] for i in range(430)]
p[0]=[0 for j in range(M)]
for i in range(420):
    for j in range(M):
        if p[i][0]>-1 and sum(p[i])+1>=sum(p[i+x[j]]):
            p[i+x[j]]=[p[i][k] for k in range(M)]
            p[i+x[j]][j]+=1
 
for j in range(M):
    if x[j]==min(x): Mi=j
 
ans=[0 for i in range(M)]
for i in range(420):
    if N-i<0: break
    if (N-i)%x[Mi]==0 and (N-i)//x[Mi]+sum(p[i])>sum(ans):
        ans=p[i]
        ans[Mi]+=(N-i)//x[Mi]
 
for i in range(M-1,-1,-1):
    for j in range(ans[i]):
        print(a[i],end="")

N,M=map(int,input().split())
x=[0,2,5,5,4,5,6,3,7,6]
a=list(map(int,input().split()))
a.sort()
x=[ x[a[i]] for i in range(M) ]

p=[[-1 for j in range(M)] for i in range(430)]
p[0]=[0 for j in range(M)]

for i in range(421):
    for j in range(M):
        if p[i][0]>-1 and sum(p[i])+1>sum(p[i+x[j]]):
            p[i+x[j]]=[p[i][k] for k in range(M)]
            p[i+x[j]][j]+=1

        elif p[i][0]>-1 and sum(p[i])+1==sum(p[i+x[j]]):
            tmp=[p[i][k] for k in range(M)]

            tmp[j]+=1
            for k in range(M-1,-1,-1):
                if tmp[k] > p[i+x[j]][k] :
                    p[i+x[j]]=[tmp[k] for k in range(M)]
                    break

for j in range(M):
    if x[j]==min(x): Mi=j

ans=[0 for i in range(M)]

for i in range(421):
    if N-i<0: break

    if (N-i)%x[Mi]==0 and (N-i)//x[Mi]+sum(p[i])>sum(ans):
        ans=[p[i][k] for k in range(M)]
        ans[Mi]+=(N-i)//x[Mi]

    elif (N-i)%x[Mi]==0 and (N-i)//x[Mi]+sum(p[i])>sum(ans):
        tmp=[p[i][k] for k in range(M)]
        tmp[Mi]+=(N-i)//x[Mi]
        for k in range(M-1,-1,-1):
            if tmp[k] > p[i][k] :
                ans=[tmp[k] for k in range(M)]
                break


for i in range(M-1,-1,-1):
    for j in range(ans[i]):
        print(a[i],end="")

N,M=map(int,input().split())
x=[0,2,5,5,4,5,6,3,7,6]
a=list(map(int,input().split()))
a.sort()
x=[ x[a[i]] for i in range(M) ]

p=[[-1 for j in range(M)] for i in range(N+10)]
p[0]=[0 for j in range(M)]

for i in range(N+1):
    for j in range(M):
        if p[i][0]>-1:
            if sum(p[i])+1>sum(p[i+x[j]]):
                p[i+x[j]]=[ p[i][k] for k in range(M) ]
                p[i+x[j]][j]+=1

            elif sum(p[i])+1==sum(p[i+x[j]]):
                tmp=[p[i][k] for k in range(M)]
                tmp[j]+=1
                for k in range(M-1,-1,-1):
                    if tmp[k] > p[i+x[j]][k] :
                        p[i+x[j]]=[tmp[k] for k in range(M)]
                        break


for i in range(M-1,-1,-1):
    for j in range(p[N][i]):
        print(a[i],end="")

import sys

def chk(i,j):
    tmp=[p[i][k] for k in range(M)]
    tmp[j]=p[i][j]+1
    for k in range(M-1,-1,-1):
        if tmp[k] > p[i+x[j]][k] :
            p[i+x[j]]=[tmp[k] for k in range(M)]

N,M=map(int,input().split())
x=[0,2,5,5,4,5,6,3,7,6]
a=list(map(int,input().split()))
a.sort()
b=[]
for i in range(M):
    for j in range(M):
        if x[a[j]]==x[a[i]]: tmp=j
    b.append(a[tmp]) 

a=list(set(b))
a.sort()
M=len(a)
x=[ x[a[i]] for i in range(M) ]
print("a:",a)
print("x:",x)


p=[-100 for i in range(N+10)]
p[0]=0
for i in range(N):
    if p[i]>-1:
        for j in range(M):
            p[i+x[j]]=max(p[i+x[j]],p[i]+1)

ans=""
while N>0:
    for j in range(M-1,-1,-1):
        if p[N]==p[N-x[j]]+1:
            ans+=str(a[j])
            N-=x[j]
            break
print(ans)




import sys

def func():
    for i in range(N//x[a[0]]):
        print(a[0],end="")
    sys.exit()

def chk(i,j):
    tmp=[p[i][k] for k in range(M)]
    tmp[j]=p[i][j]
    for k in range(M-1,-1,-1):
        if tmp[k] > p[i+x[j]][k] :
            p[i+x[j]]=[tmp[k] for k in range(M)]
            return

N,M=map(int,input().split())
x=[0,2,5,5,4,5,6,3,7,6]
a=list(map(int,input().split()))

if M==1: func()

a.sort()
b=[]
for i in range(M):
    for j in range(M):
        if x[a[j]]==x[a[i]]: tmp=j
    b.append(a[tmp]) 

a=list(set(b))
a.sort()
M=len(a)
x=[ x[a[i]] for i in range(M) ]



p=[[-100]*M for i in range(N+100)]
p[0]=[0]*M

for i in range(N+10):
    if sum(p[i])>-1:
        for j in range(M):
            if sum(p[i])+1>sum(p[i+x[j]]):
                p[i+x[j]] = [ p[i][k] for k in range(M) ]
                p[i+x[j]][j]=p[i][j]+1

            elif sum(p[i])+1==sum(p[i+x[j]]):
                chk(i,j)


for i in range(M-1,-1,-1):
    for j in range(p[N][i]):
        print(a[i],end="")

import sys

def chk(i,j):
    tmp=[p[i][k] for k in range(M)]
    tmp[j]=p[i][j]+1
    for k in range(M-1,-1,-1):
        if tmp[k] > p[i+x[j]][k] :
            p[i+x[j]]=[tmp[k] for k in range(M)]
            return

N,M=map(int,input().split())
x=[0,2,5,5,4,5,6,3,7,6]
a=list(map(int,input().split()))
a.sort()
b=[]
for i in range(M):
    for j in range(M):
        if x[a[j]]==x[a[i]]: tmp=j
    b.append(a[tmp]) 

a=list(set(b))
a.sort()
M=len(a)
x=[ x[a[i]] for i in range(M) ]



p=[[-100 for j in range(M)] for i in range(N+10)]
p[0]=[0 for j in range(M)]

for i in range(N+1):
    if sum(p[i])>-1:
        for j in range(M-1,-1,-1):
            if sum(p[i])+1>sum(p[i+x[j]]):
                p[i+x[j]] = [ p[i][k] for k in range(M) ]
                p[i+x[j]][j]=p[i][j]+1

            elif sum(p[i])+1==sum(p[i+x[j]]):
                chk(i,j)


for i in range(M-1,-1,-1):
    for j in range(p[N][i]):
        print(a[i],end="")


import sys

def chk(i,j):
    tmp=[p[i][k] for k in range(M)]
    tmp[j]=p[i][j]+1
    for k in range(M-1,-1,-1):
        if tmp[k] > p[i+x[j]][k] :
            p[i+x[j]]=[tmp[k] for k in range(M)]

N,M=map(int,input().split())
x=[0,2,5,5,4,5,6,3,7,6]
a=list(map(int,input().split()))
a.sort()
b=[]
for i in range(M):
    for j in range(M):
        if x[a[j]]==x[a[i]]: tmp=j
    b.append(a[tmp]) 

a=list(set(b))
a.sort()
M=len(a)
x=[ x[a[i]] for i in range(M) ]

p=[-100 for i in range(N+10)]
p[0]=0
for i in range(N):
    if p[i]>-1:
        for j in range(M):
            p[i+x[j]]=max(p[i+x[j]],p[i]+1)

ans=""
while N>0:
    for j in range(M-1,-1,-1):
        if p[N]==p[N-x[j]]+1:
            ans+=str(a[j])
            N-=x[j]
            break
print(ans)




Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Runtime Error

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Accepted