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



Sample input:
20 4
3 7 8 4


Sample output:
777773

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import sys
import math
import copy

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

match_num=[6,2,5,5,4,5,6,3,7,6]
use_A_dict = {}
for a in A:
  if a in use_A_dict.values():
    use_A_dict[match_num[a]] = max(a,use_A_dict[match_num[a]])
  else:
    use_A_dict[match_num[a]] = a

match = list(use_A_dict.keys())
match.sort()
number = [use_A_dict[m] for m in match]
use_num = [0]*len(number)
current_use_num = [-1]*len(number)
use_num_cand = []
N_left = N
index = -1
use_num_sum = 0
finish_flag = 0
while finish_flag == 0:
  index += 1
  if index < len(number):
    use_num[index] = math.floor(N_left/match[index])
    N_left = N_left % match[index]
    if N_left == 0:
      if use_num_sum <= sum(use_num):
        if use_num != current_use_num:
          use_num_cand.append(copy.deepcopy(use_num))
          use_num_sum = sum(use_num)
          current_use_num = copy.deepcopy(use_num)
      else:
        finish_flag = 1
  else:
    index -= 1
    while use_num[index] == 0:
      if index != 0:
        index -= 1
      else:
        finish_flag = 1
        break
    use_num[index] -= 1
    N_left += match[index]

ans_cand = []
ans_list = []
for j in range(len(use_num_cand)):
  ans_list = []
  for i in range(len(number)):
    ans_list.extend([number[i]]*use_num_cand[j][i])
  ans_list.sort(reverse=True)
  ans = ""
  for i in ans_list:
    ans += str(i)
  ans_cand.append(int(ans))
print(max(ans_cand))

Submission status: Wrong Answer

Solution code:
import sys
import math
import copy

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

match_num=[6,2,5,5,4,5,6,3,7,6]
use_A_dict = {}
for a in A:
  if a in use_A_dict.values():
    use_A_dict[match_num[a]] = max(a,use_A_dict[match_num[a]])
  else:
    use_A_dict[match_num[a]] = a

match = list(use_A_dict.keys())
match.sort()
number = [use_A_dict[m] for m in match]
use_num = [0]*len(number)
current_use_num = [-1]*len(number)
use_num_cand = []
N_left = N
index = -1
use_num_sum = 0
finish_flag = 0
while finish_flag == 0:
  index += 1
  if index < len(number):
    use_num[index] = math.floor(N_left/match[index])
    N_left = N_left % match[index]
    if N_left == 0:
      if use_num_sum <= sum(use_num):
        if use_num != current_use_num:
          use_num_cand.append(copy.deepcopy(use_num))
          use_num_sum = sum(use_num)
          current_use_num = copy.deepcopy(use_num)
      else:
        finish_flag = 1
  else:
    index -= 1
    while use_num[index] == 0:
      if index != 0:
        index -= 1
      else:
        finish_flag = 1
        break
    use_num[index] -= 1
    N_left += match[index]

ans_cand = []
ans_list = []
print(use_num_cand)
for j in range(len(use_num_cand)):
  ans_list = []
  for i in range(len(number)):
    ans_list.extend([number[i]]*use_num_cand[j][i])
  ans_list.sort(reverse=True)
  ans = ""
  for i in ans_list:
    ans += str(i)
  ans_cand.append(int(ans))
print(max(ans_cand))

Submission status: Wrong Answer

Solution code:
import sys
import math

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

match_num=[6,2,5,5,4,5,6,3,7,6]
use_A_dict = {}
for a in A:
  if a in use_A_dict.values():
    use_A_dict[match_num[a]] = max(a,use_A_dict[match_num[a]])
  else:
    use_A_dict[match_num[a]] = a

match = list(use_A_dict.keys())
match.sort()
number = [use_A_dict[m] for m in match]
use_num = [0]*len(number)
N_left = N
index = -1
del_flag = 0
while N_left != 0:
  if del_flag == 1:
    del match[0]
    del number[0]
    del use_num[0]
    N_left = N
    index = -1
    del_flag = 0
  index += 1
  if index < len(number):
    use_num[index] = math.floor(N_left/match[index])
    N_left = N_left % match[index]
  else:
    index -= 1
    while use_num[index] == 0:
      if index != 0:
        index -= 1
      else:
        del_flag = 1
        break
    use_num[index] -= 1
    N_left += match[index]

ans_list = []
for i in range(len(number)):
  ans_list.extend([number[i]]*use_num[i])
ans_list.sort(reverse=True)
ans = ""
for i in ans_list:
  ans += str(i)
print(ans)

Submission status: Wrong Answer

Solution code:
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="")

Submission status: Wrong Answer

Solution code:
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="")

Submission status: Wrong Answer

Solution code:
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="")

Submission status: Wrong Answer

Solution code:
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="")

Submission status: Wrong Answer

Solution code:
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="")

Submission status: Wrong Answer

Solution code:
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="")

Submission status: Wrong Answer

Solution code:
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="")

Submission status: Wrong Answer

Solution code:
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="")

Submission status: Wrong Answer

Solution code:
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)

Submission status: Wrong Answer

Solution code:
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])

Submission status: Wrong Answer

Solution code:
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])

Submission status: Wrong Answer

Solution code:
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="")

Submission status: Wrong Answer

Solution code:
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="")

Submission status: Wrong Answer

Solution code:
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="")

Submission status: Wrong Answer

Solution code:
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="")

Submission status: Wrong Answer

Solution code:
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="")

Submission status: Wrong Answer

Solution code:
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="")

Submission status: Wrong Answer

Solution code:
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="")

Submission status: Wrong Answer

Solution code:
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="")

Submission status: Wrong Answer

Solution code:
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="")

Submission status: Wrong Answer

Solution code:
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="")

Submission status: Wrong Answer

Solution code:
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="")

Submission status: Wrong Answer

Solution code:
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="")

Submission status: Wrong Answer

Solution code:
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="")

Submission status: Wrong Answer

Solution code:
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="")

Submission status: Wrong Answer

Solution code:
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="")

Submission status: Wrong Answer

Solution code:
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="")