n, m = (int(x) for x in input().split())
A = sorted([int(x) for x in input().split()])[::-1]
cost = (0, 2, 5, 5, 4, 5, 6, 3, 7, 6)
dp = [[-1] * (n+1) for _ in range(m+1)]
for i in range(m+1):
  dp[i][0] = 0
for i in range(m):
  for j in range(1, n+1):
    dp[i+1][j] = max(dp[i+1][j], dp[i][j])
    if j >= cost[A[i]]:
      if dp[i+1][j-cost[A[i]]] == -1:
        dp[i+1][j] = max(dp[i+1][j], dp[i][j])
      else:
        dp[i+1][j] = max(dp[i+1][j], 10 * dp[i+1][j-cost[A[i]]] + A[i])
print(dp[m][n]) int(x) for x in input().split() for x in input().split() x input().split() ().split () input split for x in input().split() int(x) int x n, m = (int(x) for x in input().split()) n (int(x) for x in input().split()) m int(x) for x in input().split() for x in input().split() x input().split() ().split () input split for x in input().split() int(x) int x A = sorted([int(x) for x in input().split()])[::-1] A sorted([int(x) for x in input().split()])[::-1] ([int(x) for x in input().split()]) sorted [int(x) for x in input().split()] ::-1 -1 cost = (0, 2, 5, 5, 4, 5, 6, 3, 7, 6) cost (0, 2, 5, 5, 4, 5, 6, 3, 7, 6) 0 2 5 5 4 5 6 3 7 6 [-1] * (n+1) for _ in range(m+1) for _ in range(m+1) _ range(m+1) range m+1 m 1 for _ in range(m+1) [-1] * (n+1) [-1] -1 n+1 n 1 dp = [[-1] * (n+1) for _ in range(m+1)] dp [[-1] * (n+1) for _ in range(m+1)] for i in range(m+1):
  dp[i][0] = 0 i range(m+1) range m+1 m 1 dp[i][0] = 0 dp[i][0] [i] dp i 0 0 for i in range(m):
  for j in range(1, n+1):
    dp[i+1][j] = max(dp[i+1][j], dp[i][j])
    if j >= cost[A[i]]:
      if dp[i+1][j-cost[A[i]]] == -1:
        dp[i+1][j] = max(dp[i+1][j], dp[i][j])
      else:
        dp[i+1][j] = max(dp[i+1][j], 10 * dp[i+1][j-cost[A[i]]] + A[i]) i range(m) range m for j in range(1, n+1):
    dp[i+1][j] = max(dp[i+1][j], dp[i][j])
    if j >= cost[A[i]]:
      if dp[i+1][j-cost[A[i]]] == -1:
        dp[i+1][j] = max(dp[i+1][j], dp[i][j])
      else:
        dp[i+1][j] = max(dp[i+1][j], 10 * dp[i+1][j-cost[A[i]]] + A[i]) j range(1, n+1) range 1 n+1 n 1 dp[i+1][j] = max(dp[i+1][j], dp[i][j]) dp[i+1][j] [i+1] dp i+1 i 1 j max(dp[i+1][j], dp[i][j]) max dp[i+1][j] [i+1] dp i+1 i 1 j dp[i][j] [i] dp i j if j >= cost[A[i]]:
      if dp[i+1][j-cost[A[i]]] == -1:
        dp[i+1][j] = max(dp[i+1][j], dp[i][j])
      else:
        dp[i+1][j] = max(dp[i+1][j], 10 * dp[i+1][j-cost[A[i]]] + A[i]) j >= cost[A[i]] j cost[A[i]] cost A[i] A i if dp[i+1][j-cost[A[i]]] == -1:
        dp[i+1][j] = max(dp[i+1][j], dp[i][j])
      else:
        dp[i+1][j] = max(dp[i+1][j], 10 * dp[i+1][j-cost[A[i]]] + A[i]) dp[i+1][j-cost[A[i]]] == -1 dp[i+1][j-cost[A[i]]] [i+1] dp i+1 i 1 j-cost[A[i]] j cost[A[i]] cost A[i] A i -1 dp[i+1][j] = max(dp[i+1][j], dp[i][j]) dp[i+1][j] [i+1] dp i+1 i 1 j max(dp[i+1][j], dp[i][j]) max dp[i+1][j] [i+1] dp i+1 i 1 j dp[i][j] [i] dp i j dp[i+1][j] = max(dp[i+1][j], 10 * dp[i+1][j-cost[A[i]]] + A[i]) dp[i+1][j] [i+1] dp i+1 i 1 j max(dp[i+1][j], 10 * dp[i+1][j-cost[A[i]]] + A[i]) max dp[i+1][j] [i+1] dp i+1 i 1 j 10 * dp[i+1][j-cost[A[i]]] + A[i] 10 * dp[i+1][j-cost[A[i]]] 10 dp[i+1][j-cost[A[i]]] [i+1] dp i+1 i 1 j-cost[A[i]] j cost[A[i]] cost A[i] A i A[i] A i print(dp[m][n]) print dp[m][n] [m] dp m n A = sorted([int(x) for x in input().split()])[::-1] sorted([int(x) for x in input().split()])[::-1] A m = (int(x) for x in input().split()) (int(x) for x in input().split()) m n, m = (int(x) for x in input().split()) (int(x) for x in input().split()) n cost = (0, 2, 5, 5, 4, 5, 6, 3, 7, 6) (0, 2, 5, 5, 4, 5, 6, 3, 7, 6) cost dp = [[-1] * (n+1) for _ in range(m+1)] [[-1] * (n+1) for _ in range(m+1)] dp
n, m = (int(x) for x in input().split())
A = sorted([int(x) for x in input().split()])[::-1]
cost = (0, 2, 5, 5, 4, 5, 6, 3, 7, 6)
dp = [[-1] * (n+1) for _ in range(m+1)]
for i in range(m+1):
  dp[i][0] = 0
for i in range(m):
  for j in range(1, n+1):
    dp[i+1][j] = max(dp[i+1][j], dp[i][j])
    if j >= cost[A[i]]:
      if dp[i+1][j-cost[A[i]]] == -1:
        dp[i+1][j] = max(dp[i+1][j], dp[i][j])
      else:
        dp[i+1][j] = max(dp[i+1][j], 10 * dp[i+1][j-cost[A[i]]] + A[i])
print(dp[m][n])