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):
def solve(string):
    n, m, *a = map(int, string.split())
    needs = {i + 1: n for i, n in enumerate(map(int, "2 5 5 4 5 6 3 7 6".split()))}
    if 2 in a and 5 in a or 2 in a and 3 in a:
        a.remove(2)
    if 3 in a and 5 in a:
        a.remove(3)
    if 6 in a and 9 in a:
        a.remove(6)
    a = sorted(a, key=lambda x: needs[x])
    index = 0
    base = str(a[0]) * (n // needs[a[0]])
    n %= needs[a[0]]
    while n > 0:
        use = [_b for _b in a if needs[_b] <= needs[a[0]] + n]
        if len(use) == 1:
            base = base[:-1]
            n += needs[a[0]]
            continue
        flag = max(use) == a[0]
        tmp_n = use[-1] if flag else max(use)
        diff = needs[tmp_n] - needs[a[0]]
        tmp_c = n // diff
        if flag:
            base = base[:-tmp_c] + str(tmp_n) * tmp_c
        else:
            base = base[:index] + str(tmp_n) * tmp_c + base[index + tmp_c:]
            index += tmp_c
        n -= diff * tmp_c
    return base


if __name__ == '__main__':
    print(solve('\n'.join([input(), input()])))

Solution (Python):
def solve(string):
    n, m, *a = map(int, string.split())
    needs = {i + 1: n for i, n in enumerate(map(int, "2 5 5 4 5 6 3 7 6".split()))}
    if 2 in a and (3 in a or 5 in a):
        a.remove(2)
    if 3 in a and 5 in a:
        a.remove(3)
    if 6 in a and 9 in a:
        a.remove(6)
    b = sorted(a, key=lambda x: needs[x])
    index = 0
    base = str(b[0]) * (n // needs[b[0]])
    n %= needs[b[0]]
    while n > 0:
        use = [_b for _b in b if needs[_b] <= needs[b[0]] + n]
        if len(use) == 1:
            base = base[:-1]
            n += needs[b[0]]
            continue
        flag = max(use) == b[0]
        tmp_n = use[-1] if flag else max(use)
        tmp_c = n // (needs[tmp_n] - needs[b[0]])
        if flag:
            base = base[:-tmp_c] + str(tmp_n) * tmp_c
        else:
            base = base[:index] + str(tmp_n) * tmp_c + base[index + tmp_c:]
            index += tmp_c
        n -= (needs[tmp_n] - needs[b[0]]) * tmp_c
    return base


if __name__ == '__main__':
    print(solve('\n'.join([input(), input()])))

Solution (Python):
def solve(string):
    n, m, *a = map(int, string.split())
    needs = {i + 1: n for i, n in enumerate(map(int, "2 5 5 4 5 6 3 7 6".split()))}
    if 2 in a and (3 in a or 5 in a):
        a.remove(2)
    if 3 in a and 5 in a:
        a.remove(3)
    if 6 in a and 9 in a:
        a.remove(6)
    b = sorted(a, key=lambda x: needs[x])
    index = 0
    base = str(b[0]) * (n // needs[b[0]])
    n %= needs[b[0]]
    while n > 0:
        use = [_b for _b in b if needs[_b] <= needs[b[0]] + n]
        if len(use) == 1:
            base = base[:-1]
            n += needs[b[0]]
            continue
        flag = max(use) == b[0]
        tmp_n = use[-1] if flag else max(use)
        tmp_c = n // (needs[tmp_n] - needs[b[0]])
        if flag:
            base = base[:-tmp_c] + str(tmp_n) * tmp_c
        else:
            base = base[:index] + str(tmp_n) * tmp_c + base[index + tmp_c:]
            index += tmp_c
        n -= (needs[tmp_n] - needs[b[0]]) * tmp_c
    return base


if __name__ == '__main__':
    print(solve('\n'.join([input(), input()])))

Solution (Python):
def solve(string):
    n, m, *a = map(int, string.split())
    needs = {i + 1: n for i, n in enumerate(map(int, "2 5 5 4 5 6 3 7 6".split()))}
    if 2 in a and 5 in a or 2 in a and 3 in a:
        a.remove(2)
    if 3 in a and 5 in a:
        a.remove(3)
    if 6 in a and 9 in a:
        a.remove(6)
    b = sorted(a, key=lambda x: needs[x])
    index = 0
    base = str(b[0]) * (n // needs[b[0]])
    n %= needs[b[0]]
    while n > 0:
        use = [_b for _b in b if needs[_b] <= needs[b[0]] + n]
        if len(use) == 1:
            base = base[:-1]
            n += needs[b[0]]
            continue
        flag = max(use) == b[0]
        tmp_n = use[-1] if flag else max(use)
        tmp_c = n // (needs[tmp_n] - needs[b[0]])
        if flag:
            base = base[:-tmp_c] + str(tmp_n) * tmp_c
        else:
            base = base[:index] + str(tmp_n) * tmp_c + base[index + tmp_c:]
            index += tmp_c
        n -= (needs[tmp_n] - needs[b[0]]) * tmp_c
    return base


if __name__ == '__main__':
    print(solve('\n'.join([input(), input()])))

Solution (Python):
def solve(string):
    n, m, *a = map(int, string.split())
    needs = {i + 1: n for i, n in enumerate(map(int, "2 5 5 4 5 6 3 7 6".split()))}
    if 2 in a and 5 in a:
        a.remove(2)
    if 3 in a and 5 in a:
        a.remove(3)
    if 2 in a and 3 in a:
        a.remove(2)
    if 6 in a and 9 in a:
        a.remove(6)
    b = sorted(a, key=lambda x: needs[x])
    index = 0
    base = str(b[0]) * (n // needs[b[0]])
    n %= needs[b[0]]
    while n > 0:
        use = [_b for _b in b if needs[_b] <= needs[b[0]] + n]
        if len(use) == 1:
            base = base[:-1]
            n += needs[b[0]]
        elif max(use) == b[0]:
            tmp_n = use[-1]
            tmp_c = n // (needs[tmp_n] - needs[b[0]])
            base = base[:-tmp_c] + str(tmp_n) * tmp_c
            n -= (needs[tmp_n] - needs[b[0]]) * tmp_c
        else:
            tmp_n = max(use)
            tmp_c = n // (needs[tmp_n] - needs[b[0]])
            base = base[:index] + str(tmp_n) * tmp_c + base[index + tmp_c:]
            n -= (needs[tmp_n] - needs[b[0]]) * tmp_c
            index += tmp_c
    return base


if __name__ == '__main__':
    print(solve('\n'.join([input(), input()])))

Solution (Python):
n, _ = map(int, input().split())
numbers = list(map(int, input().split()))
reqs = {
    1: 2,
    2: 5,
    3: 5,
    4: 4,
    5: 5,
    6: 6,
    7: 3,
    8: 7,
    9: 6,
}

memo = {}


def max_counts(a, b):
    """
    a と b 大きい方を返す
    :param a: solve() の返り値
    :param b: solve() の返り値
    :return:
    """
    if a is None:
        return b
    if b is None:
        return a
    
    if sum(a) > sum(b):
        return a
    if sum(a) < sum(b):
        return b
    
    for a_c, b_c in zip(reversed(a), reversed(b)):
        if a_c > b_c:
            return a
        if a_c < b_c:
            return b
    return a




memo[0] = [0 for _ in range(10)]
for i in range(n):
    if i in memo:
        for num in numbers:
            next_n = i + reqs[num]
            next_counts = memo[next_n] if next_n in memo else None
            if next_n <= n:
                memo[next_n] = max_counts(
                    next_counts,
                    memo[i][:num] + [memo[i][num] + 1] + memo[i][num + 1:]
                )

ans = ''
for num in range(9, 0, -1):
    for _ in range(memo[n][num]):
        ans += str(num)
print(ans)

Solution (Python):

N,M=map(int,raw_input().split())
A=map(int,raw_input().split())
dp=[-1 for i in range(10**4 + 1)]
Cost={(i+1):v for i,v in enumerate([2,5,5,4,5,6,3,7,6])}

for a in A:
    dp[Cost[a]] = max(dp[Cost[a]], a)

for n in range(2, N+1):
    for a in A:
        if n-Cost[a]>=0 and dp[n-Cost[a]] != -1:
            dp[n] = max(dp[n], dp[n-Cost[a]] * 10 + a)
print(dp[n])

Solution (Python):

N,M=map(int,raw_input().split())
A=map(int,raw_input().split())
dp=[-1 for i in range(10**4 + 1)]
Cost={(i+1):v for i,v in enumerate([2,5,5,4,5,6,3,7,6])}

for a in A:
    dp[Cost[a]]=1
for n in range(2,N+1):
    for a in A:
        if n-Cost[a]>=0 and dp[n-Cost[a]] != -1:
            dp[n]=max(dp[n], dp[n-Cost[a]]+1)
k=dp[N]
n=N
buf=[]
for i in range(dp[N]-1):
    b=-1
    for a in A:
        if n-Cost[a]>=0 and dp[n-Cost[a]] == k-1:
            b=max(b,a)
    k-=1
    n-=Cost[b]
    buf.append(b)
b=-1
for a in A:
    if Cost[a] == n:
        b=max(b,a)
buf.append(b)   
print("".join(map(str,buf)))

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

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

for i in range(N+1):
    for a in A:
        if i-cost[a-1] < 0 or dp[i-cost[a-1]] < 0:
            continue
        if dp[i-cost[a-1]]*10 + a > dp[i]:
            dp[i] = dp[i-cost[a-1]]*10 + a
print(dp[N])

Solution (Python):

import sys


def solve(N: int, M: int, A: "List[int]"):
    costs = [10**18, 2, 5, 5, 4, 5, 6, 3, 7, 6]
    cand = [(i, costs[i]) for i in A]
    cand.sort(reverse=True)
    digits = [-1 * 10**16 for i in range(N+1)]
    digits[0] = 0
    for i in range(1, N+1):
        r = -1 * 10**16
        for c in cand:
            if i - c[1] >= 0:
                r = max(r, digits[i - c[1]] + 1)
        digits[i] = r
    digit = digits[N]
    lastCost = N
    result = 0
    
    for i in range(digit):
        for c in cand:
            if lastCost - c[1] >= 0 and digits[lastCost - c[1]] == digits[lastCost] - 1:
                result = result*10 + c[0]
                lastCost = lastCost - c[1]
                
                break
    print(result)
    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()

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

dp = [-1 for _ in range(n+1)]
dp[0] = 0
for a in alist:
    match = matches[a]
    for i in range(n - match + 1):
        if dp[i] == -1:
            continue
        dp[i + match] = max(dp[i+match], dp[i] * 10 + a)
        
print(dp[-1])

Solution (Python):
import sys

ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: map(int, sys.stdin.readline().split())
nms = lambda: map(str, sys.stdin.readline().split())
nl = lambda: list(nm())

n, m = nm()
a = nl()
lis = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [-1 for _ in range(n + 1)]
dp[0] = 0
for i in range(1, n + 1):
    for j in a:
        if i - lis[j] >= 0:
            dp[i] = max(dp[i], dp[i - lis[j]] * 10 + j)
print(dp[n])

Solution (Python):
B = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
N, M = map(int, input().split())
A = list(map(int, input().split()))
dic = {i: B[i] for i in A}
dp = [0] * (N + 1)
for i in range(N + 1):
    for j in A:
        if i>0 and dp[i]==0:
            break
        if i + dic[j] <= N:
            dp[i + dic[j]] = max(dp[i] * 10 + j, dp[i + dic[j]])
print(dp[N])

Solution (Python):
import math

n,m = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]


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


if 2 in a:
  if 3 in a or 5 in a:
    a.remove(2)
    m -= 1

if 3 in a:
  if 5 in a:
    a.remove(3)
    m -= 1
  
if 6 in a:
  if 9 in a:
    a.remove(6)
    m -= 1


exist_set = [i for i in set if i[0] in a]


max_honsu = max([i[1] for i in exist_set])


m -= 1


max_keta = 0
ans_kouho = []

for exist_one in exist_set:
  keta_tmp = n//exist_one[1]
  num_tmp = exist_one[0]
  amari_tmp = n % exist_one[1]
  honsu_tmp = exist_one[1]
  
  
  
  
  if amari_tmp != 0 and honsu_tmp == max_honsu:
    continue
  if keta_tmp >= max_keta:
    keta = keta_tmp
    num = num_tmp
    amari = amari_tmp
    honsu = honsu_tmp

    
    if amari == 0:
      ans_kouho.append(int(str(num)*keta))
      max_keta = keta
      continue
      
    

    
    exist_yet = [inc for inc in exist_set if inc[0] != num]

    
    for keta_num in range(1,min(max_honsu,keta+1)):
      
      kouho = []

      
      
      bit = math.ceil(math.log2(m+1))
      for all_bit in range((2**m)*keta_num):
        change = []
        nokori = amari + honsu * keta_num
        for bit_conf in range(keta_num):
          bit_index =  (all_bit >> bit_conf*bit) & (2**m-1)

          
          if bit_index > m:
            break
            
          
          if bit_index == m:
            change.append(0)
            continue

          
          change.append(exist_yet[bit_index][0])
          nokori -= exist_yet[bit_index][1]

          if nokori < 0:
            break
            
        if bit_index > m or nokori < 0:
          continue

        
        if nokori == 0:
          kouho.append(change)
      
      
      if len(kouho) != 0:

        
        del_len = len(kouho[0])

        
        kouho = [[inc_1 for inc_1 in inc_2 if inc_1 != 0] for inc_2 in kouho]

        
        max_len = max([len(inc) for inc in kouho])
        kouho = [inc for inc in kouho if len(inc) == max_len]
        
        max_keta = keta - del_len + max_len

        
        kouho = [sorted(kouho_one,reverse=True) for kouho_one in kouho]

        
        kouho_tmp = []
        for kouho_one in kouho:
          if kouho_one not in kouho_tmp:
            kouho_tmp.append(kouho_one)
        kouho = kouho_tmp

        
        if len(kouho) != 1:
          for kouho_one in range(len(kouho[0])):
            max_i = max([inc[kouho_one] for inc in kouho])
            kouho = [inc for inc in kouho if inc[kouho_one] == max_i]
        kouho = kouho[0]

        ans = [num]*(keta-del_len) + kouho
        ans.sort(reverse=True)
        ans_str = ''
        for ans_one in ans:
          ans_str += str(ans_one)
        ans_kouho.append(int(ans_str))

ans_kouho.sort()

print(ans_kouho[-1])

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

dp = [0] * (N+1)

for i in range(1, N+1):
    now = - 10 ** 9
    for aa in a:
        if i - num[aa] >= 0:
            now = max(now, dp[i-num[aa]]+1)
    dp[i] = now

answer = ''
for i in range(dp[-1]):
    for aa in a:
        if N >= num[aa]:
            if dp[N-num[aa]] == dp[N] - 1:
                answer += str(aa)
                N -= num[aa]
                break
print(answer)

Solution (Python):
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]*10010
dp[0]=0
for i in range(1,n+1):
    for a in A:
        if i - cost[a] >= 0:
            dp[i] = max(dp[i],a+dp[i-cost[a]]*10)
print(dp[n])

Solution (Python):
N, M = map(int, input().split())
A = list(map(int, input().split()))
 
need = [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 + need[a] <= N:
                        dp[i + need[a]] = max(dp[i + need[a]], dp[i] * 10 + a)
 
print(dp[-1])

Solution (Python):


import sys
input = sys.stdin.readline

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

need = [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 + need[a] <= N:
                        dp[i + need[a]] = max(dp[i + need[a]], dp[i] * 10 + a)

print(dp[-1])

Solution (Python):


import sys
input = sys.stdin.readline

N, M = map(int, input().split())
A = sorted(list(map(int, input().split())), reverse=True)

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

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

for num in A:
        required = need[num]

        for i in range(N - required + 1):
                if dp[i] == -1:
                        continue
                
                dp[i + required] = max(dp[i + required], dp[i] * 10 + num)

print(dp[-1])

Solution (Python):

import sys

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

def solve(n: int, m: int, A: "List[int]"):
    dp = [-1] * (n + 1)
    dp[0] = 0
    A.sort(reverse=True)
    for c in A:
        for i in range(n + 1):
            if i - a[c] >= 0 and dp[i - a[c]] >= 0:
                dp[i] = max(dp[i], dp[i - a[c]] * 10 + c)
    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()

Solution (Python):

import sys

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

def sort_str(s):
    ret = list(s)
    ret.sort(reverse=True)
    return ''.join(ret)

def is_greater(a, b):
    if len(a) > len(b):
        return True
    elif len(a) < len(b):
        return False
    for i in range(len(a)):
        if a[i] > b[i]:
            return True
        else:
            return False
    return False

def _solve(n: int, m: int, A: "List[int]"):
    dp = [None] * (n + 1)
    dp[0] = ''
    for c in A:
        for i in range(n + 1):
            if i - a[c] >= 0 and dp[i - a[c]] is not None:
                tmp = sort_str(dp[i - a[c]] + str(c))
                if dp[i] is None or is_greater(tmp, dp[i]):
                    dp[i] = tmp
        
    print(dp[n])
    return

def solve(n: int, m: int, A: "List[int]"):
    dp = [-1] * (n + 1)
    memo = ['' for _ in range(n + 1)]
    dp[0] = 0
    A.sort(reverse=True)
    
    for c in A:
        for i in range(n + 1):
            if i - a[c] >= 0 and dp[i - a[c]] >= 0:
                dp[i] = max(dp[i], dp[i - a[c]] * 10 + c)
                
                
                
        
        
    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()

Solution (Python):
from collections import defaultdict
N,M = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
cost = [0,2,5,5,4,5,6,3,7,6]
dp = defaultdict(lambda:'')
for a in A:
    dp[cost[a]] = str(a)
for i in range(N+1):
    for a in A:
        if dp[i-cost[a]] != '':
            if len(dp[i]) <= len(dp[i-cost[a]]+str(a)):
                dp[i] = dp[i-cost[a]]+str(a)
        ans = dp[N]
ans = list(map(int,list(ans)))
ans.sort(reverse=True)
print(''.join(list(map(str,ans))))

Solution (Python):

from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = SR()
    return l
sys.setrecursionlimit(1000000)
mod = 1000000007


def A():
    a,b = LI()
    if b%a == 0:
        print(a+b)
    else:
        print(b-a)
    return


def B():
    n,m = LI()
    d = defaultdict(int)
    for i in range(n):
        l = LI()
        for j in range(1,len(l)):
            d[l[j]] += 1
    ans = 0
    for i in d.values():
        if i == n:
            ans += 1
    print(ans)
    return


def C():
    def gcd(a,b):
        if a == 0:
            return b
        return gcd(b%a,a)
    n = I()
    a = LI()
    g = a[0]
    for i in range(1,n):
        g = gcd(g,a[i])
    print(g)
    return


def D():
    n,m = LI()
    a = LI()
    f = [0,2,5,5,4,5,6,3,7,6]
    dp = [None for i in range(n+1)]
    dp[0] = []
    for j in a:
        for i in range(n+1):
            if i-f[j] >= 0 and dp[i-f[j]] != None:
                if dp[i] == None:
                    dp[i] = dp[i-f[j]]+[j]
                    dp[i].sort(reverse = True)
                elif len(dp[i]) < len(dp[i-f[j]])+1:
                    dp[i] = dp[i-f[j]]+[j]
                    dp[i].sort(reverse = True)
                elif len(dp[i]) == len(dp[i-f[j]])+1:
                    d = dp[i-f[j]]+[j]
                    d.sort(reverse = True)
                    if dp[i] < d:
                        dp[i] = d
    ans = 0
    k = pow(10,len(dp[n])-1)
    for i in range(len(dp[n])):
        ans += k*dp[n][i]
        k //= 10
    print(ans)
    return


def E():
    return


def F():
    return


def G():
    return


def H():
    return


if __name__ == "__main__":
    D()

Solution (Python):


import sys
INF = float('inf')
MOD = 10 ** 9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


def main():
    n, m = LI()
    A = LI()

    match = [6, 2, 5, 5, 4, 5, 6, 3, 7, 6]
    
    dp = [-INF] * (n + 10)
    dp[0] = 0
    for i in range(n):
        for a in A:
            j = i + match[a]
            dp[j] = max(dp[j], dp[i] * 10 + a)

    return dp[n]


print(main())

Solution (Python):


import sys
import math
INF = float('inf')
MOD = 10 ** 9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


def main():
    n, m = LI()
    A = LI()
    A.sort()
    A.reverse()

    match = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
    
    dp = [-1] * (n + 1)
    dp[0] = 0
    res = [0] * (n + 1)
    for i in range(1, n + 1):
        for j in A:
            
            
            k = i - match[j]
            if k < 0 or dp[k] == -1:
                continue

            if dp[i] < dp[k] + 1:
                dp[i] = dp[k] + 1
                res[i] = j * 10 ** dp[k] + res[k]

    return res[n]


print(main())

Solution (Python):


import sys
import math
INF = float('inf')
MOD = 10 ** 9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


def main():
    n, m = LI()
    A = LI()
    A.sort()
    A.reverse()

    match = [2, 5, 5, 4, 5, 6, 3, 7, 6]
    
    dp = [-1] * (n + 1)
    dp[0] = 0
    res = [0] * (n + 1)
    for i in range(1, n + 1):
        for j in A:
            
            
            k = i - match[j - 1]
            if k < 0 or dp[k] == -1:
                continue

            if dp[i] < dp[k] + 1:
                dp[i] = dp[k] + 1
                res[i] = max(j * 10 ** dp[k] + res[k], res[k] * 10 + j)

    return res[n]


print(main())

Solution (Python):


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

A.sort()
A.reverse()

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

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

print(dp[N])

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()))
A.sort()
A.reverse()
dp = [-1] * (N + 10)
dp[0] = 0
for i in range(N+1):
    for a in A:
        if(i - S[a] >= 0):
            dp[i] = max(dp[i - S[a]] * 10 + a, dp[i])
    
print(dp[N])

Solution (Python):
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
a.reverse()
dp=[0]*(n+1)
ans=[0]*(n+1)
num=[0,2,5,5,4,5,6,3,7,6]
INF=10**18
for i in range(1,n+1):
  stack=[]
  chance=[]
  for j in a:
    if i-num[j]>=0:
      stack.append(dp[i-num[j]])
      chance.append([dp[i-num[j]],ans[i-num[j]]])
    else:
      stack.append(-INF)
      chance.append([-1,-1])
  dp[i]=max(stack)+1
  if dp[i]>0:
    for v in range(len(stack)):
      if stack[v]==max(stack):
        ans[i] += chance[v][1]+a[v]*10**(dp[i]-1)
        break
print(ans[n])

Solution (Python):


def main():
    N, M = map(int, input().split())
    A = sorted(list(map(int, input().split())))[::-1]
    bar_num = [2,5,5,4,5,6,3,7,6]
       
    dp = [-1] * (N+5) 
    dp[0] = 0
    for i in range(N+1):
        for a in A:
            if i - bar_num[a-1] >= 0:
                dp[i] = max(dp[i], dp[i-bar_num[a-1]]*10+a)
    print(dp[N])
    
if __name__ =='__main__':
    main()

Solution (Python):
import sys
stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))

N,M = read_int()

A = read_int()

A.sort(reverse=True)
A.insert(0,-1)

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

dp = [-1 for j in range(N+1)]
def solve():
    dp[0] = 0
    for i in range(M):
        for j in range(N+1):
            if j - W[A[i + 1]] >= 0 and dp[j - W[A[i + 1]]] >= 0:
                dp[j] = max(dp[j], dp[j - W[A[i + 1]]]*10 + A[i + 1])
                
if __name__ == "__main__":
    solve()
    print(dp[N])

Solution (Python):
from math import ceil,log10
import sys
stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))
read_str = lambda : stdin.readline().rstrip()

N,M = read_int()

A = read_int()

A.sort(reverse=True)
A.insert(0,-1)

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

dp = [-1 for j in range(N+1)]
def solve():
    dp[0] = 0
    for i in range(M):
        digit = [0 for _ in range(N+1)]
        for j in range(N+1):
            if j - W[A[i + 1]] >= 0 and dp[j - W[A[i + 1]]] >= 0:
                dp[j] = max(dp[j], dp[j - W[A[i + 1]]]*10 + A[i + 1])
                
if __name__ == "__main__":
    solve()
    print(dp[N])

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

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

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

dp = [-1] * (N+1)
dp[0] = 0
for i in range(1, N+1):
    max_scale = -1
    for num in useful_num_list:
        if need_matches[num] <= i:
            scale = dp[i-need_matches[num]] + 1
            if max_scale < scale:
                max_scale = scale
    dp[i] = max_scale

useful_num_list.sort(reverse=True)
ans = 0
n = N
while n > 0:
    for num in useful_num_list:
        if n < need_matches[num]:
            continue
        if dp[n] - 1 == dp[n - need_matches[num]]:
            ans += num * (10**(dp[n]-1))
            n -= need_matches[num]
            break
print(ans)

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

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

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

print(dp[-1])

Solution (Python):
n, m=map(int, input().split())
a=list(map(int, input().split()))
d={1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6}
memo=[0]*(n+1)
memo[0]=0
memo[1]=-10**10
for i in range(2, n+1):
    res=-10**10
    for ai in a:
        if d[ai]>i:
            continue
        res=max(res, ai+memo[i-d[ai]]*10)
    memo[i]=res
print(memo[n])

Solution (Python):
import sys
from functools import lru_cache


sys.setrecursionlimit(200000)
fin = sys.stdin.readline
num_matches_required = {n: nmatches for n, nmatches 
    in zip(range(1, 10), (2, 5, 5, 4, 5, 6, 3, 7, 6))}

N, M = map(int, fin().split())
nums = sorted(map(int, fin().split()), reverse=True)

@lru_cache(None)
def rec(i):
    if i < 0: 
        return float('-inf')
    elif i == 0: 
        return 0
    else:
        return max(
            rec(i - num_matches_required[num]) for num in nums
        ) + 1


num_digits = rec(N)
cur_num_matches = N
result = []
while cur_num_matches:
    for num in nums:
        if rec(cur_num_matches - num_matches_required[num]) == num_digits - len(result) - 1:
            result.append(str(num))
            cur_num_matches -= num_matches_required[num]
            break

print("".join(result))

Solution (Python):
import sys
from functools import lru_cache


sys.setrecursionlimit(200000)
fin = sys.stdin.readline
num_matches_required = {n: nmatches for n, nmatches 
    in zip(range(1, 10), (2, 5, 5, 4, 5, 6, 3, 7, 6))}

N, M = map(int, fin().split())
nums = sorted(map(int, fin().split()), reverse=True)

@lru_cache(None)
def rec(i):
    if i < 0: 
        return float('-inf')
    elif i == 0: 
        return 0
    else:
        return max(
            rec(i - num_matches_required[num]) for num in nums
        ) + 1


num_digits = rec(N)
cur_num_matches = N
result = []
while cur_num_matches:
    for num in nums:
        if rec(cur_num_matches - num_matches_required[num]) == num_digits - len(result) - 1:
            result.append(str(num))
            cur_num_matches -= num_matches_required[num]
            break

print("".join(result))

Solution (Python):


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

def solve(n, m, a):

    a.sort(reverse = True)

    tmp = a.copy()
    for i in range(len(a) - 1, 0, -1):
        for j in range(i - 1, -1, -1):
            if W[a[i]] == W[a[j]]:
                a.pop(i)
                break

    by_value = [(v, W[v]) for v in a]
    by_weight = sorted(by_value, key = lambda x: x[1])
    v_to_w = {}
    w_to_v = {}
    for i, x in enumerate(by_value):
        for j, y in enumerate(by_weight):
            if x == y:
                v_to_w[i] = j
                w_to_v[j] = i
                break


    a_len = len(a)
    if a_len == 1:
        v, w = by_weight[0]
        return str(v) * (n // w)


    cnd = []
    w0 = by_weight[0][1]
    p = [0] * (a_len + 1)
    while p[-1] == 0:
        x = 0
        for i in range(1, a_len):
            x += p[i] * by_weight[i][1]
        if x <= n and (n - x) % w0 == 0:
            p[0] = (n - x) // w0
            cnd.append((sum(p), tuple([p[v_to_w[j]] for j in range(a_len)])))
        p[1] += 1
        for i in range(1, a_len):
            if p[i] == w0:
                p[i] = 0
                p[i + 1] += 1

    cnd.sort(key = lambda x: x[0], reverse = True)
    digits, _ = cnd[0]
    for i in range(1, len(cnd)):
        d, _ = cnd[i]
        if d < digits:
            cnd = cnd[:i]
            break

    cnd.sort(key = lambda x: x[1], reverse = True)

    ans = ''
    _, t = cnd[0]
    for i, x in enumerate(by_value):
        v, _ = x
        cnt = t[i]
        ans += str(v) * cnt

    return ans


def main():
    n, m = input().split()
    n = int(n)
    m = int(m)
    a = list(map(int, input().split()))


    print(solve(n, m, a))


if __name__ == '__main__':
    main()