Problem p03128 (python)

Fixed submission:
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)

Buggy submission (verdict: Runtime Error):
d=[0,2,5,5,4,5,6,3,7,6]
N,M=map(int,input().split())
A=list(map(int,input().split()))
l=[]
for a in A:
    l.append([a,d[a]])
l.sort(key=lambda x:x[0],reverse=True)
l.sort(key=lambda x:x[1])
min_val,min_match=l[0]

ans=[]
if N%min_match==0:
    ans.append(int(str(min_val)*(N//min_match)))
from itertools import combinations_with_replacement
for i in range(1,M):
    f = False
    r = 1
    while not f:
        for p in combinations_with_replacement(l[1:],r):
        v=[]
        m_sum=0
        for val,mat in p:
            v.append(str(val))
            m_sum+=mat
        if (N-m_sum)%min_match==0:
            ans.append(int(''.join(sorted(v+
                [str(min_val)]*((N-m_sum)//min_match),reverse=True))))
        f = (len(ans) == 0)
        r += 1
print(max(ans))

Buggy submission (verdict: Runtime Error):
d=[0,2,5,5,4,5,6,3,7,6]
N,M=map(int,input().split())
A=list(map(int,input().split()))
l=[]
for a in A:
    l.append([a,d[a]])
l.sort(key=lambda x:x[0],reverse=True)
l.sort(key=lambda x:x[1])
min_val,min_match=l[0]
 
ans=[]
if N%min_match==0:
    ans.append(int(str(min_val)*(N//min_match)))
from itertools import combinations
for i in range(1,M):
    for p in combinations(l[1:],i):
        v=[]
        m_sum=0
        for val,mat in p:
            v.append(str(val))
            m_sum+=mat
        if (N-m_sum)%min_match==0:
            ans.append(int(''.join(sorted(v+
                [str(min_val)]*((N-m_sum)//min_match),reverse=True))))
print(max(ans))

Buggy submission (verdict: Runtime Error):
d=[0,2,5,5,4,5,6,3,7,6]
N,M=map(int,input().split())
A=list(map(int,input().split()))
l=[]
for a in A:
    l.append([a,d[a]])
l.sort(key=lambda x:x[0],reverse=True)
l.sort(key=lambda x:x[1])
min_val,min_match=l[0]

ans=[]
if N%min_match==0:
    ans.append(int(str(min_val)*(N//min_match)))
from itertools import combinations
for i in range(1,M):
    for p in combinations(l[1:],i):
        v=[]
        m_sum=0
        for val,mat in p:
            v.append(str(val))
            m_sum+=mat
        if (N-m_sum)%min_match==0:
            ans.append(int(''.join(sorted(v+
                [str(min_val)]*((N-m_sum)//min_match),reverse=True))))
print(max(ans))

Buggy submission (verdict: Runtime Error):
d=[0,2,5,5,4,5,6,3,7,6]
N,M=map(int,input().split())
A=list(map(int,input().split()))
l=[]
for a in A:
    l.append([a,d[a]])
l.sort(key=lambda x:x[0],reverse=True)
l.sort(key=lambda x:x[1])
min_val,min_match=l[0]

ans=[]
if N%min_match==0:
    ans.append(str(min_val)*(N//min_match))
from itertools import combinations
for i in range(1,M):
    for p in combinations(l[1:],i):
        v=[]
        m_sum=0
        for val,mat in p:
            v.append(str(val))
            m_sum+=mat
        if (N-m_sum)%min_match==0:
            ans.append(int(''.join(sorted(v+
                [str(min_val)]*((N-m_sum)//min_match),reverse=True))))
print(max(ans))