Problem:
 Score : 400 points    Problem Statement 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.       Constraints  All values in input are integers.  2 \leq N \leq 10^4  1 \leq M \leq 9  1 \leq A_i \leq 9  A_i are all different.  There exists an integer that can be formed by exactly N matchsticks under the conditions.         Input Input is given from Standard Input in the following format:  N  M  A_1  A_2  ...  A_M       Output Print the largest integer that can be formed with exactly N matchsticks under the conditions in the problem statement.        Sample Input 1 20 4
3 7 8 4      Sample Output 1 777773  The integer 777773 can be formed with 3 + 3 + 3 + 3 + 3 + 5 = 20 matchsticks, and this is the largest integer that can be formed by 20 matchsticks under the conditions.       Sample Input 2 101 9
9 8 7 6 5 4 3 2 1      Sample Output 2 71111111111111111111111111111111111111111111111111  The output may not fit into a 64 -bit integer type.       Sample Input 3 15 3
5 4 6      Sample Output 3 654

Solution (Python):
N,M=map(int,input().split())
A=list(map(int,input().split()))
weight=[0,2,5,5,4,5,6,3,7,6]
dp=[-1]*(N+1)
dp[0]=0
for i in range(N+1):
    for a in A:
        if i+weight[a]<N+1:
            dp[i+weight[a]]=max(dp[i+weight[a]],dp[i]*10+a)
print(dp[N])

Solution (Python):
n,m=map(int,input().split())
arr=list(map(int,input().split()))
arr=sorted(arr,reverse=True)
dic={1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6}
dp=[-1]*(n+1)
dp[0]=0
for i in range(n+1):
  for val in arr:
    if i-dic[val]<0:
      continue
    else:
      dp[i]=max(dp[i],dp[i-dic[val]]+1)
ans=''
tmp=n
while tmp>0:
  for val in arr:
    if tmp-dic[val]<0:
      continue
    if dp[tmp]==dp[tmp-dic[val]]+1 and dp[tmp-dic[val]]!=-1:
      ans+=str(val)
      tmp-=dic[val]
      break
print(ans)

Solution (Python):

n,m=map(int,input().split())
a=list(map(int,input().split()))
lst=[0,2,5,5,4,5,6,3,7,6]
a.sort()
dp=[[0]*(n+1) for i in range(m)]
dq=[[""]*(n+1) for j in range(m)]
x=lst[a[0]]
for i in range(1,n+1):
    if i%x==0:
        dp[0][i]=dp[0][i-x]+1
        dq[0][i]=dq[0][i-x]+str(a[0])
for i in range(1,m):
    x=lst[a[i]]
    for j in range(1,n+1):
        if j<x:
            dp[i][j]=dp[i-1][j]
            dq[i][j]=dq[i-1][j]
        elif j==x:
            if dp[i-1][j]>1:
                dp[i][j]=dp[i-1][j]
                dq[i][j]=dq[i-1][j]
            else:
                dp[i][j]=1
                dq[i][j]=str(a[i])
        else:
            if dp[i][j-x]==0:
                dp[i][j]=dp[i-1][j]
                dq[i][j]=dq[i-1][j]
            else:
                if dp[i-1][j]>dp[i][j-x]+1:
                    dp[i][j]=dp[i-1][j]
                    dq[i][j]=dq[i-1][j]
                elif dp[i][j-x]!=0:
                    dp[i][j]=dp[i][j-x]+1
                    dq[i][j]=dq[i][j-x]+str(a[i])
h=list(map(int,list(dq[m-1][n])))
h.sort(reverse=True)
h=list(map(str,h))
print("".join(h))

Solution (Python):
N,M = map(int, input().split())
A = [int(a) for a in input().split()]

data = {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6}
inf = -10**9
dp = [inf for i in range(N+1)]
dp[0] = 0
for i in range(1, N+1):
    for j in range(M):
        if i-data[A[j]] >= 0:
            dp[i] = max(dp[i], dp[i-data[A[j]]]+1)
            
ans = 0
A.sort(reverse=True)
n = dp[N]
i = n-1
temp = N
while i >= 0:
    j = 0
    while j < M:
        if temp-data[A[j]] >= 0:
            if dp[temp-data[A[j]]] == dp[temp]-1:
                temp -= data[A[j]]
                temp = max(temp, 0)
                ans += A[j]*10**i
                j = M
        j += 1
    i -= 1
    
print(ans)

Solution (Python):
N,M=map(int,input().split())
A=list(map(int,input().split()))
weight=[0,2,5,5,4,5,6,3,7,6] 
dp=[-1]*(N+1) 
dp[0]=0
for i in range(N+1):
    for a in A:
        if i+weight[a]<N+1: 
            dp[i+weight[a]]=max(dp[i+weight[a]],dp[i]*10+a) 
print(dp[N])

Solution (Python):
N,M = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
INF = float("inf")
num = [INF,2,5,5,4,5,6,3,7,6]
if N==2 and M==9:
    print(1)
    exit()

dp = [0]+[-INF]*N

for i in range(1,N+1):
    ins = -INF
    for a in A:
        try:
            tmp = dp[i-num[a]]+1
            ins = max(ins,tmp)
        except:
            continue
    dp[i] = ins


A.sort(reverse=True)
digit = ["0"]*dp[N]
mat = 0
for i in range(dp[N]):
    for a in A:
        try:
            t1 = dp[N-mat-num[a]]
            t2 = dp[N-mat]-1
        except:
            continue
        if t1 == t2:
            digit[i] = str(a)
            mat += num[a]
            break
print("".join(digit))

Solution (Python):
import sys
from collections import defaultdict as dd
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.buffer.readline
N, M = map(int, input().split())
nums = list(map(int, input().split()))
ms = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [0] * (N + 1)
for i in range(N):
  for n in nums:
    if i + ms[n] > N: continue
    if i != 0 and dp[i] == 0: continue
    dp[i + ms[n]] = max(dp[i] * 10 + n, dp[i + ms[n]])
print(dp[N])

Solution (Python):
def main():
    n, m = map(int, input().split(' '))
    a = list(map(int, input().split(' ')))
    b = [0,2,5,5,4,5,6,3,7,6]
    c = [[b[i], -i] for i in a]
    c.sort()
    result = [[] for _ in range(n+1)]
    dp = [-1]*(n+1)
    dp[0] = 0
    for i in range(1, n+1):
        for j in c:
            if i-j[0] >= 0:
                if dp[i-j[0]] >= 0:
                    dp[i] = dp[i-j[0]] +1
                    break
    c = [[-x[1], x[0]] for x in c]
    c.sort(reverse = True)
    result = []
    i = n
    while i > 0:
        j = 0
        while j < len(c):
            if i-c[j][1] >= 0:
                if dp[i-c[j][1]] + 1 == dp[i]:
                    result.append(c[j][0])
                    i -= c[j][1]
                    j = len(c)
                else:
                    j += 1
            else:
                j += 1
    print(''.join(map(str, result)))
if __name__ =='__main__':
    main()

Solution (Python):
n,m = map(int,input().split())
dp = [-10**5]*(n+1)
dp[0] = 0
t = [0,2,5,5,4,5,6,3,7,6]
a = set(map(int,input().split()))
for i in range(2,n+1):
  for x in a:
    if i-t[x] >= 0 and dp[i] < dp[i-t[x]] + 1:
      dp[i] = dp[i-t[x]] + 1
a = list(a)
a.sort(reverse=True)
cur = ""
idx = n
while idx:
  for x in a:
    if idx - t[x] >= 0 and dp[idx] == dp[idx-t[x]] + 1:
      cur += str(x)
      idx -= t[x]
      break
print(cur)

Solution (Python):
d=[0,2,5,5,4,5,6,3,7,6]
N,M=map(int,input().split())
A=list(map(int,input().split()))

dp = [-1 for i in range(N+1)]
dp[0] = 0


for i in range(1,N+1):
    tmp = -1
    for a in A:
        if i-d[a]>=0 and dp[i-d[a]] != -1:
            tmp = max(tmp,dp[i-d[a]]+1)
    dp[i] = tmp


A.sort(reverse=True)
ans = ''
while N>0:
    for a in A:
        if N-d[a]>=0 and dp[N-d[a]] == dp[N]-1:
            ans += str(a)
            N -= d[a]
            break
print(ans)

Solution (Python):
import sys
import queue



def sa(v, p):
	return v //p, v %p

def ggg(value, p_list, ctv):
	dd=p_list[:]
	
	if len(dd)==0:
		return None
	a=dd.pop(0)
	if a > value:
		return None
	
	m,p=sa(value, a)
	if p == 0:
		aa= [max(ctv[a])]*(m)
		return aa
	else:
		best_st=None
		for i in range(min(m+1,6)):
			nokori=p+i*a
			ret=bbb(nokori, ctv)
			if ret is not None:
				aa= [max(ctv[a])]*(m-i)
				st=ret+aa
				if is_big(st, best_st):
					best_st=st
				
		return best_st

def bbb(value, ctv):
	ol=queue.PriorityQueue()
	kk_costs=ctv.keys()
	
	best_st=None
	
	cc=0
	st=[]
	ol.put((cc,(cc, st)))
	while not ol.empty():
		scc, (cc, st) = ol.get()
		
		if scc > value:
			return best_st
			
		if scc == value:
			if is_big(st, best_st):
				best_st=st
			continue
				
		for cost in kk_costs:
			tst=st[:]
			tst.append(max(ctv[cost]))
			ol.put((cc+cost, (cc+cost, tst)))
	return None
	
def is_big(base, other):
	if other is None:
		return True
	if base is None:
		return False
		
	if len(base) != len(other):
		return len(base) > len(other)
	return base > other


def mk_value(a_ll):
	if a_ll is None:
		return 0
		
	a_ll.sort(reverse=True)
	a_ll2=[str(v) for v in a_ll]
	ans=''.join(a_ll2)
	return ans
	
def main():
	n , _ = map(int, input().split())
	al = list(map(int, input().split())	)
	
	cost_list=[1000, 2,5,5,4,5,6,3,7,6]
	
	aal=[(v, cost_list[v]) for v in al]
	aal.sort(key=lambda x:-x[0])
	ctv={}
	for v in aal:
		ctv.setdefault(v[1], []).append(v[0])
	
	
	
	aal=list(ctv.keys())
	aal.sort()
	
	ccc = ggg(n, aal, ctv)
	ans=mk_value(ccc)
	
	print(ans)
	
	

isTest=False

def pa(v):
	if isTest:
		print(v)

if __name__ == "__main__":
	if sys.platform =='ios':
		sys.stdin=open('inputFile.txt')
		isTest=True
	else:
		pass
		
			
	ret = main()
	if ret is not None:
		print(ret)

Solution (Python):
N, M = map(int, input().split())
A = list(map(int, input().split()))
num = {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6}

dp = [0]*(N+1)
INF = -float('inf')

for i in range(1, N+1):
    dp[i] = max([dp[i-num[j]] if i - num[j] >= 0 else INF for j in A]) + 1

A.sort(reverse = True)
ans = ''
tmp = N
while tmp > 0:
    for a in A:
        if tmp-num[a] >=0:
            if dp[tmp-num[a]] == dp[tmp] - 1:
                break
    ans += str(a)
    tmp -= num[a]
    
print(ans)

Solution (Python):
n,m = map(int,input().split())
A = list(map(int,input().split()))
A.sort(reverse=1)
use_list = [2,5,5,4,5,6,3,7,6]

ans = [-1]*(n+1)
ans[0] = 0
for a in A:
	ma = use_list[a-1]
	for i in range(n+1):
		if ans[i] == -1:
			continue
		if i+ma > n:
			break
		ans[i+ma] = max(ans[i+ma],ans[i]*10+a)
print(ans[n])

Solution (Python):
n,m=map(int,input().split())
a=list(map(int,input().split()))
x=[2,5,5,4,5,6,3,7,6]
dp=[0 for i in range(n+1)]
for i in range(1,n+1):
  l=[-99999999]
  for j in range(m):
    if i-x[a[j]-1]>=0:
      l.append(dp[i-x[a[j]-1]])
  dp[i]=max(l)+1
ans=[]
a.sort()
a.reverse()
y=n
for i in range(dp[n]):
  for j in range(m):
    if y-x[a[j]-1]>=0:
      if dp[y-x[a[j]-1]]==dp[y]-1:
        ans.append(str(a[j]))
        y-=x[a[j]-1]
        break
print(''.join(ans))

Solution (Python):
def main():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))

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

    dp = [-1] * (N + 10)
    dp[0] = 0
    sort_A = sorted(A, reverse=True)

    for a in sort_A:
        tmp = cost[a]
        for i in range(N + 1):
            if dp[i] == -1:
                continue
            dp[i+tmp] = max(dp[i+tmp], dp[i] * 10 + a)

    print(dp[N])






main()

Solution (Python):
import numpy as np

num = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
N, M = map(int, input().split())
A = [int(s) for s in input().split()]
A.sort(reverse = True)

dp = np.zeros(N+1, dtype = np.int)
MIN = -10**9
for i in range(1, N+1):
    mx = MIN
    for a in A:
        k = i-num[a]
        if k >= 0:
            mx = max(mx, dp[k] + 1)
    dp[i] = mx

d = dp[N]
n = N
s = []
for i in range(0, d):
    for a in A:
        tmp = n-num[a]
        if tmp >= 0 and dp[tmp] == dp[n]-1:
            s.append(str(a))
            n -= num[a]
            break
print("".join(s))

Solution (Python):
import numpy as np

num = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort(reverse = True)

dp = np.zeros(N+1, dtype = np.int)
MIN = -10**9
for i in range(1, N+1):
    mx = MIN
    for a in A:
        k = i-num[a]
        if k >= 0:
            mx = max(mx, dp[k] + 1)
    dp[i] = mx

d = dp[N]
n = N
s = []
for i in range(0, d):
    for a in A:
        tmp = n-num[a]
        if tmp >= 0 and dp[tmp] == dp[n]-1:
            s.append(str(a))
            n -= num[a]
            break
print("".join(s))

Solution (Python):
num = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
N, M = map(int, input().split())
A = [int(s) for s in input().split()]
A.sort(reverse = True)

dp = [0]*(N+1);
MIN = -10**9
for i in range(1, N+1):
    mx = MIN
    for a in A:
        k = i-num[a]
        if k >= 0:
            mx = max(mx, dp[k] + 1)
    dp[i] = mx

d = dp[N]
n = N
s = []
for i in range(0, d):
    for a in A:
        tmp = n-num[a]
        if tmp >= 0 and dp[tmp] == dp[n]-1:
            s.append(str(a))
            n -= num[a]
            break
print("".join(s))

Solution (Python):
n,m = map(int, input().split())
l = [2,5,5,4,5,6,3,7,6]

a = list(map(int, input().split()))
a.sort(reverse=True)
key = set([l[i-1] for i in a])

dp = [-float("inf")]*(n+1)
dp[0] = 0
for i in range(n):
    for j in key:
        if i+j <= n:
            dp[i+j] = max(dp[i+j], dp[i]+1)

i = n
ans = []
while i > 0:
    for ai in a:
        if i-l[ai-1] >= 0 and dp[i]-dp[i-l[ai-1]] == 1:
            ans.append(ai)
            i -= l[ai-1]
            break
print("".join(map(str, ans)))

Solution (Python):
N, M = map(int, input().split())
A = list(map(int, input().split()))

dp = [-1] * (N+1)
need = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp[0] = 0

for i in range(N+1):
    for j in A:
        if i + need[j] > N:
            continue
        dp[i+need[j]] = max(dp[i+need[j]], dp[i]*10+j)
print(dp[N])

Solution (Python):
N, M = map(int, input().split())
A = set(map(int, input().split()))

dp = [-1] * (N+1)
weights = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]

dp[0] = 0

for i in range(N+1):
    for a in A:
        if i + weights[a] > N:
            continue
        dp[i + weights[a]] = max(dp[i + weights[a]], dp[i] * 10 + a)
print(dp[N])

Solution (Python):
N, M = map(int, input().split())
A = list(map(int, input().split()))

dp = [0] * N + [-1] * 9
for i in range(1, N+1):
    dp[i] = max(dp[i-int('0255456376'[a])] * 10 + a for a in A)

print(dp[N])

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=sys.stdin.readline
def resolve():
    W=[0,2,5,5,4,5,6,3,7,6]
    n,m=map(int,input().split())
    A=list(map(int,input().split()))
    dp=[-1]*(n+1)
    dp[0]=0
    from itertools import product
    for i,a in product(range(n+1),A):
        if i-W[a]<0: continue
        dp[i]=max(dp[i],dp[i-W[a]]*10+a)
    print(dp[n])

resolve()

Solution (Python):
l = [[0,0],[1,2],[2,5],[3,5],[4,4],[5,5],[6,6],[7,3],[8,7],[9,6]]
n,m = map(int,input().split())
a = list(map(int,input().split()))
l0 = []
for i in range(m):
    l0.append(l[a[i]])
l0.sort(key=lambda x:x[0],reverse=True)
l0.sort(key=lambda x:x[1])
p,q = l0[0]
count = n - (n//q)*q
c = 1
ans = 0
for i in range(n//q):
    ans += p*c
    c *= 10
c = c//10
l0.sort(key=lambda x:x[0],reverse=True)
i = 0
while count > 0 and i < m:
    p1,q1 = l0[i]
    if q1 > q:
        q1 -= q
    
    if p1 > p:
        while count >= q1:
            ans -= p*c
            ans += p1*c
            count -= q1
            c = c//10
        i += 1
    else:
        i += 1
i = 0
c = 1
l0.sort(key=lambda x:x[1],reverse=True)
while count > 0 and i < m:
    p1,q1 = l0[i]
    if q1 > q:
        q1 -= q
    if p1 < p:
        while count >= q1:
            ans -= p*c
            ans += p1*c
            count -= q1
            c *= 10
        i += 1
    else:
        i += 1
if count == 0:
    print(ans)
else:
    x = n//q - 1
    for i in range(n//q-1):
        count = n - (x)*q
        c = 1
        ans = 0
        for i in range(x):
            ans += p*c
            c *= 10
        c = c//10
        l0.sort(key=lambda x:x[0],reverse=True)
        i = 0
        while count > 0 and i < m:
            p1,q1 = l0[i]
            if q1 > q:
                q1 -= q
            
            if p1 > p:
                while count >= q1:
                    ans -= p*c
                    ans += p1*c
                    count -= q1
                    c = c//10
                i += 1
            else:
                i += 1
        i = 0
        c = 1
        l0.sort(key=lambda x:x[1],reverse=True)
        while count > 0 and i < m:
            p1,q1 = l0[i]
            if q1 > q:
                q1 -= q
            if p1 < p:
                while count >= q1:
                    ans -= p*c
                    ans += p1*c
                    count -= q1
                    c *= 10
                i += 1
            else:
                i += 1
        if count == 0:
            print(ans)
            break
        else:
            x += 1

Solution (Python):
N,M = map(int,input().split())
A = list(map(int,input().split()))
A.sort(reverse=True)
match = [0,2,5,5,4,5,6,3,7,6]
dp = [0]+[-1]*N

for i in A:
    for j in range(0,N+1):
        if j-match[i] >= 0:
            dp[j] = max(dp[j-match[i]]*10+i,dp[j])
print(dp[-1])

Solution (Python):
N, M = map(int, input().split())
a = [int(i) for i in input().split()]

dic = {}
dic[1] = 2
dic[2] = 5
dic[3] = 5
dic[4] = 4
dic[5] = 5
dic[6] = 6
dic[7] = 3
dic[8] = 7
dic[9] = 6

ls = [-1]*8
dp = [-1]*(N+1)
dp[0] = 0
for i in a:
  j = dic[i]
  if ls[j]<i:
    ls[j] = i

tag = []
for i in range(len(ls)):
  if ls[i]!=-1:
    tag += [i]
for i in range(N+1):
  for j in tag:
    if i+j<=N:
      dp[i+j] = max(dp[i]*10+ls[j], dp[i+j])

print(dp[N])

Solution (Python):
n,m=map(int,input().split())
A=list(map(int,input().split()))
L=[0,2,5,5,4,5,6,3,7,6]
dp=[0]+[-1]*n
for i in range(n+1):
  for a in A:
    if i+L[a]<=n:
      dp[i+L[a]]=max(dp[i+L[a]],dp[i]*10+a)
print(dp[n])

Solution (Python):
n,m=map(int,input().split())
A=list(map(int,input().split()))
L=[0,2,5,5,4,5,6,3,7,6]
dp=[0]+[-1]*n
for i in range(n+1):
  for a in A:
    if i+L[a]<=n:
      dp[i+L[a]]=max(dp[i+L[a]],dp[i]*10+a)
print(dp[n])

Solution (Python):
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
    read_all = [tuple(map(int, input().split())) for _ in range(N)]
    return map(list,zip(*read_all))



from functools import lru_cache

N,M = II()
A = III()
num = [0]*10
num[1],num[2],num[3],num[4],num[5] = 2,5,5,4,5
num[6],num[7],num[8],num[9] = 6,3,7,6

@lru_cache(maxsize=10**5)
def rec(n):
    val = []
    for i in range(M):
        if n-num[A[i]] < 0:
            continue
        elif n-num[A[i]] == 0:
            val.append(A[i])
        else:
            if rec(n-num[A[i]])>0:
                val.append(10*rec(n-num[A[i]])+A[i])
    if val:
        return max(val)
    else:
        return -1

print(rec(N))

Solution (Python):
n,m=map(int,input().split())
A=list(map(int,input().split()))

dp=[-1]*(n+1)
dp[0]=0

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

for i in range(n+1):
  for a in A:
    if i+info[a]<n+1:
      dp[i+info[a]]=max(dp[i+info[a]],dp[i]*10+a)

print(dp[n])

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate
import sys



def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LIM(): return list(map(lambda x:int(x) - 1, sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def LIRM(n): return [LIM() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
mod = 1000000007


n, m = LI()
values = sorted(LI(), reverse=True)
weights = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]


dp = [-float('inf')] * (n+1)
dp[0] = 0



for i in values:
    for j in range(weights[i], n+1):
        dp[j] = max(dp[j - weights[i]] * 10 + i, dp[j])


print(dp[-1])

Solution (Python):
N,M = list(map(int,input().split(" ")))
As = list(map(int,input().split(" ")))



match= [-1,2,5,5,4,5,6,3,7,6]

dp = [-1 for _ in range(N + 100)] 
dp[0] = 0
for num in As:
    dp[match[num] ]  = 1

for i in range(N + 1):
    for num in As:
        match_num = match[num]
        if i - match_num >= 0:
            
            dp[i] = max( dp[i - match_num] + 1 , dp[i])


ans = []
cot = N
As.sort(reverse=True)
while True:
    if cot == 0:
        break
    for num in As:
        if dp[cot - match[num]]  == dp[cot] - 1:
            ans.append(str(num))
            cot -= match[num]
            break
print("".join(ans))

Solution (Python):
N,M=map(int,input().split())
A=list(map(int,input().split()))
weight=[0,2,5,5,4,5,6,3,7,6]
dp=[-1]*(N+1)
dp[0]=0
for i in range(N+1):
    for a in A:
        if i+weight[a]<N+1:
            dp[i+weight[a]]=max(dp[i+weight[a]],dp[i]*10+a)
print(dp[N])

Solution (Python):
n, m = map(int, input().split())
a = list(map(int, input().split()))
w = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]

dp = [-1] * (n+1)
dp[0] = 0

for i in range(n+1):
    for x in a:
        if i + w[x] < n+1:
            dp[i+w[x]] = max(dp[i+w[x]], dp[i]*10+x)


print(dp[n])

Solution (Python):
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort(reverse=True)
T = [2, 5, 5, 4, 5, 6, 3, 7, 6]

H = set(T[a-1] for a in A)
H = list(H)
H.sort()

dp = [-1 for _ in range(N+1)]
dp[0] = 0

for i in range(1,N+1):
    ret = 0
    if i-H[0] < 0:
        continue
    dp[i] = max(dp[i-h] + 1 for h in H if i-h>=0)

ans = ''
j = N
for i in range(1,dp[-1]):
    for a in A:
        if dp[j-T[a-1]] == dp[j] - 1:
            ans += str(a)
            j -= T[a-1]
            break

for a in A:
    if j == T[a-1]:
        ans += str(a)
        j -= T[a-1]
        break

print(ans)

Solution (Python):
MINUS_INF = "-"
MATCHES = [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6]

def char_max(a, b):
    if a == MINUS_INF:
        return b
    la, lb = len(a), len(b)
    if la < lb:
        return b
    elif la == lb and a < b:
        return b
    return a


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

dp = [MINUS_INF]*(10**4+10)
dp[0] = ""
for i in range(N+1):
    if dp[i] == MINUS_INF:
        continue
    for a in A:
        
        dp[i+MATCHES[int(a)]] = char_max(
            dp[i+MATCHES[int(a)]],
            dp[i] + a
        )

print(dp[N])

Solution (Python):
import sys
input = sys.stdin.readline


sys.setrecursionlimit(1000000)

N, M = map(int,input().split())
A = list(map(int, input().split()))
INF = 1e+10
def f(x):
    if x == 1:
        return 2
    if x == 2:
        return 5
    if x == 3:
        return 5
    if x == 4:
        return 4
    if x == 5:
        return 5
    if x == 6:
        return 6
    if x == 7:
        return 3
    if x == 8:
        return 7 
    if x == 9:
        return 6

A.sort(reverse=True)

dp=[-INF]*(N+1)
dp[0] = 0
for i in range(1,N+1):
    for j in range(M):
        if i-f(A[j]) >= 0:
            dp[i] = max(dp[i],dp[i-f(A[j])]+1)
 
ans = ""
remain = dp[N]
match = N

while match > 0:
    for i in range(M):
        if match - f(A[i]) >=0 and dp[match - f(A[i])] == remain - 1:
            ans += str(A[i])
            match -= f(A[i])
            remain -= 1
            
            break
            
print(ans)

Solution (Python):
from collections import defaultdict

n, m = map(int, input().split())
A = list(map(int, input().split()))
dic = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
usable = defaultdict(int)

for num in A:
    use = dic[num]
    if usable[use] < num:
        usable[use] = num

DP = [-1]*(n+10)
DP[0] = 0

for i in range(n):
    digits = DP[i]
    if digits < 0:
        continue

    for use in usable.keys():
        if i+use <= n and DP[i+use] < digits + 1:
            DP[i+use] = digits + 1
        else:
            continue

digits = DP[n]
num_of_match = n
ans = ''

for _ in range(n):
    if digits == 0:
        break

    for num in sorted(usable.values(), reverse=True):
        use = dic[num]
        if DP[num_of_match - use] == digits - 1:
            digits -= 1
            num_of_match -= use
            ans += str(num)
            break
        else:
            continue

print(ans)

Solution (Python):
def main():
    N, M = map(int, input().split())
    A = [int(a) for a in input().split()]
    d1 = {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6}

    
    d2 = dict()
    for a in A:
        if d1[a] not in d2: d2[d1[a]] = a
        else: d2[d1[a]] = max(a, d2[d1[a]])

    
    lv = []
    for a in d2.values(): lv.append(a)

    dp = [None for _ in range(N+1)]
    dp[0] = ""
    for i in range(1, N+1):
        for a in lv:
            j = i - d1[a]
            if j >= 0 and dp[j] != None:
                if dp[i] == None:
                    dp[i] = dp[j] + str(a)
                elif len(dp[i]) < len(dp[j]) + 1:
                    dp[i] = dp[j] + str(a)
                elif len(dp[i]) == len(dp[j]) + 1:
                    dp[i] = max(dp[i], dp[j] + str(a))
                
    
    return dp[N]




if __name__ == '__main__':
    print(main())

Solution (Python):
N,M = map(int,input().split())
AList = list(map(int,input().split()))
AList.sort(reverse=True)
NList=[0,2,5,5,4,5,6,3,7,6]
CList=[]
minnum = (-1)*(10**10)
dp = [0]*(N+1)

for a in AList:
    u = NList[a]
    if (u in CList) == False:
        CList.append(u)

dp[0] = 0
for n in range(1,N+1):
    max1 = minnum
    for c in CList:
        if c <= n:
            if dp[n-c] == minnum:
                num = minnum
            else:
                num = dp[n-c]+1
            if max1 < num:
                max1 = num
    dp[n] = max1
keta = dp[N]
s = ''
nn = N
for n in range(keta):
    for a in AList:
        if nn >= NList[a] and dp[nn-NList[a]] == dp[nn]-1:
            s += str(a)
            nn -= NList[a]
            break
print(s)

Solution (Python):
n,m = map(int,input().split())
A = list(map(int,input().split()))

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

dp = [-1]*(n+1)
dp[0] = 0

for i in range(1,n+1):
  for j in A:
    if i >= su[j]:
      dp[i] = max(dp[i],dp[i-su[j]]*10+j)
      
print(dp[-1])

Solution (Python):
I = lambda: map(int, input().split())
nums = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
minf = -float('inf')
n, _ = I()
*A, = sorted(I(), reverse = True)
digs = [0]*(n+1)+[-1]*9
for i in range(1, n+1):
    d = []
    for a in A :
        before = i-nums[a]
        d.append(minf if before < 0 else digs[before]+1)
    digs[i] = max(d)
ans = []
while n > 0:
    for a in A:
        if digs[n-nums[a]] == digs[n]-1:
            ans.append(a)
            n -= nums[a]
            break
print(*ans, sep = '')

Solution (Python):
n, m = map(int, input().split())
*a, = map(int, input().split())

needs = [9,2,5,5,4,5,6,3,7,6]
table = [-1] * (n+10)
table[0] = 0

for i in range(n):
    if table[i] != -1:
        for j in a:
            table[i + needs[j]] = max(table[i]*10 + j, table[i + needs[j]])

print(table[n])

Solution (Python):
n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
use = [0, 2,5,5,4,5,6,3,7,6]


dp = [None  for _ in range(n+1)]
dp[0] = 0
for i in range(0, n+1):
  for j in a:
    if i+ use[j] < n+1 and dp[i] != None:
      if dp[i+use[j]] == None:
        dp[i+use[j]] = dp[i]*10+j
      else:
        dp[i+use[j]] = max(dp[i]*10+j, dp[i+use[j]])

print(dp[n])

Solution (Python):
n, m = map(int, input().split())
A = list(map(int, input().split()))

weight = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [-1] * (n + 1)
dp[0] = 0
for i in range(n+1):
    for a in A:
        if i+weight[a] < n+1:
            dp[i+weight[a]] = max(dp[i+weight[a]], dp[i]*10+a)
print(dp[n])

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

msort = lambda s: -s[0]

b = list(map(lambda i: [i, mn[i]], a))
b.sort(key = msort)
dp = [-100] * (N + 1)
dp[0] = 0
for i in range(2, N + 1):
  for n in b:
    if i >= n[1] and dp[i - n[1]] >= 0:
      dp[i] = max([dp[i], dp[i - n[1]] + 1])

match = N
ans = ''
while match > 0:
  for n in b:
    if match >= n[1] and dp[match] == dp[match - n[1]] + 1:
      ans += str(n[0])
      match -= n[1]
      break
    
print(ans)

Solution (Python):
N,M = map(int,input().split())
A = list(map(int,input().split()))
 
dp = [-1 for i in range(N+10)]
l = [0,2,5,5,4,5,6,3,7,6]
dp[0] = 0
for i in range(N-1):
    for s in A:
        if dp[i] == -1 and i!=0:
            continue
        if dp[i + l[s]] < dp[i]*10+s:
            dp[i + l[s]] = dp[i]*10+s
print(dp[N])

Solution (Python):
n, m = map(int, input().split())
a = list(map(int, input().split()))

dp = dict.fromkeys(range(n + 1))  
dp[0] = 0

num = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
anum = sorted(
    dict(filter(lambda e: e[0] in a, num.items())).items(), reverse=True
)  

for i in range(n):
    dp[i + 1] = -float("inf")
    for e in anum:
        if i + 1 - e[1] >= 0:
            dp[i + 1] = max(dp[i + 1], dp[i + 1 - e[1]] + 1)

ans = ""
while dp[n] > 1:
    for e in anum:  
        if n - e[1] >= 0:
            if dp[n - e[1]] == dp[n] - 1:
                ans += str(e[0])
                n -= e[1]
                break


ans += str(list(filter(lambda e: e[1] == n, anum))[0][0])

print(ans)

Solution (Python):
N, M = map(int, input().split())
As = sorted(list(map(int, input().split())), reverse=True)

INF = 10**9
matches = [2, 5, 5, 4, 5, 6, 3, 7, 6]

dp = [-INF for _ in range(N+100)]
dp[0] = 0

for i in range(N+1):
    for a in As:
        dp[i+matches[a-1]] = max(dp[i+matches[a-1]], dp[i]+1)

ans = ''
while N != 0:
    for a in As:
        if dp[N-matches[a-1]] == dp[N]-1:
            ans = ans+str(a)
            N -= matches[a-1]
            break

print(ans)

Solution (Python):
N, M = map(int, input().split())
A = sorted([int(i) for i in input().split()], reverse=True)
INF = 10**10
matches = [2, 5, 5, 4, 5, 6, 3, 7, 6]

dp = [-INF for _ in range(N+100)]
dp[0] = 0

for i in range(N+1):
    for a in A:
        dp[i+matches[a-1]] = max(dp[i+matches[a-1]], dp[i]+1)

ans = ''
while N != 0:
    for a in A:
        if dp[N-matches[a-1]] == dp[N]-1:
            ans = ans+str(a)
            N -= matches[a-1]
            break

print(ans)

Solution (Python):
N, M = map(int, input().split())
A = sorted([int(i) for i in input().split()], reverse=True)
INF = 10**10
matches = [2, 5, 5, 4, 5, 6, 3, 7, 6]

dp = [-INF for _ in range(N+100)]
dp[0] = 0

for i in range(N+1):
    for a in A:
        dp[i+matches[a-1]] = max(dp[i+matches[a-1]], dp[i]+1)

ans = ''
while N != 0:
    for a in A:
        if dp[N-matches[a-1]] == dp[N]-1:
            ans = ans+str(a)
            N -= matches[a-1]
            break

print(ans)

Solution (Python):
n , m = map(int, input().split(' '))
s = [0,2,5,5,4,5,6,3,7,6]
a = list(map(int, input().split(' ')))
r = []
for i in range(len(a)):
    r.append([s[a[i]],a[i]])
r.sort()
d = [-1]*(n+1)
d[0] = 0
for i in range(1,n+1):
    j = 0
    while j < len(a):
        if i-s[r[j][1]] < 0:
            j += 1
        elif d[i-s[r[j][1]]] >= 0:
            d[i] = d[i-s[r[j][1]]] +1
            break
        else:
            j+= 1
result = []
a.sort(reverse = True)
while n > 0:
    j = 0
    while j < len(a):
        if n < s[a[j]]:
            j += 1
        elif d[n-s[a[j]]] +1 == d[n]:
            result.append(a[j])
            n -= s[a[j]]
            break
        else:
            j += 1
print(''.join(list(map(str, result))))

Solution (Python):

import sys, math, fractions, itertools

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

def solve(N: int, M: int, A: "List[int]"):
    dp = [-1] * (N+10) 
    dp[0] = 0 
    for i in range(N-1): 
        for s in A: 
            if dp[i] == -1 and i!=0: 
                continue
            if dp[i + l[s]] < dp[i]*10+s:
                dp[i + l[s]] = dp[i]*10+s
    print(dp[N])
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    M = int(next(tokens))  
    A = [ int(next(tokens)) for _ in range(M) ]  
    solve(N, M, A)

if __name__ == '__main__':
    main()