Problem p03128 (python)

Fixed submission:
import numpy as np

#initialization
nmatch = np.array([0,2,5,5,4,5,6,3,7,6])
N,M = map(int,input().split())
dp = [-1]*(N+1) # contains the number of digits it can make
dp[0] = 0
ff = np.array(list(map(int,input().split()))).reshape(-1,1)
fg = nmatch[ff].reshape(-1,1)
Atemp = np.concatenate([ff,fg],axis=1)
index0 = np.argsort(Atemp[:,0])
index1 = np.argsort(Atemp[:,1])
A = Atemp[index1] #sorted by the number of matches: ascending
B = Atemp[index0]
B = B[::-1] #sorted by the number: descending
for i in range(1,N+1):
    for k in range(M):
        j = A[k,1]
        i_old = i - j #available matches
        if (i-j) < 0:
            break
        if (i-j) == 0 and dp[i] == -1:
            dp[i] = 1
        if dp[i_old] != -1 and dp[i_old]+1 > dp[i]:
            dp[i] = dp[i_old] + 1
#print(dp)
ndigits = dp[N]
j = N
final_number = ''
for l in range(ndigits,0,-1):
    index = 0
    while True:
        temp_j = j - B[index,1]
        if temp_j < 0:
            index += 1
            continue
        if dp[temp_j] + 1 == dp[j]:
            final_number += str(B[index,0])
            j = temp_j
            break
        else:
            index += 1
        if index > M:
            break
print(int(final_number))

Buggy submission (verdict: Runtime Error):
import numpy as np

#initialization
nmatch = np.array([0,2,5,5,4,5,6,3,7,6])
N,M = map(int,input().split())
dp = [-1]*(N+1) # contains the number of digits it can make
dp[0] = 0
ff = np.array(list(map(int,input().split()))).reshape(-1,1)
fg = nmatch[ff].reshape(-1,1)
Atemp = np.concatenate([ff,fg],axis=1)
index0 = np.argsort(Atemp[:,0])
index1 = np.argsort(Atemp[:,1])
A = Atemp[index1] #sorted by the number of matches: ascending
B = Atemp[index0]
B = B[::-1] #sorted by the number: descending
for i in range(1,N+1):
    for k in range(M):
        j = A[k,1]
        i_old = i - j #available matches
        if (i-j) < 0:
            break
        if (i-j) == 0 and dp[i] == -1:
            dp[i] = 1
        if dp[i_old] != -1 and dp[i_old]+1 > dp[i]:
            dp[i] = dp[i_old] + 1
#print(dp)
ndigits = dp[N]
j = N
final_number = ''
for l in range(ndigits,0,-1):
    index = 0
    while True:
        temp_j = j - B[index,1]
        if dp[temp_j] + 1 == dp[j]:
            final_number += str(B[index,0])
            j = temp_j
            break
        else:
            index += 1
        if index > M:
            break
print(int(final_number))

Buggy submission (verdict: Runtime Error):
import numpy as np

#initialization
nmatch = np.array([0,2,5,5,4,5,6,3,7,6])
N,M = map(int,input().split())
dp = [-1]*(N+1) # contains the number of digits it can make
dp[0] = 0
ff = np.array(list(map(int,input().split()))).reshape(-1,1)
fg = nmatch[ff].reshape(-1,1)
Atemp = np.concatenate([ff,fg],axis=1)
print(Atemp)
index0 = np.argsort(Atemp[:,0])
index1 = np.argsort(Atemp[:,1])
A = Atemp[index1] #sorted by the number of matches: ascending
print(A)
B = Atemp[index0]
B = B[::-1] #sorted by the number: descending
print(B)
for i in range(1,N+1):
    #dp(i)を求める
    for k in range(M):
        j = A[k,1]
        i_old = i - j #available matches
        if (i-j) < 0:
            break
        if (i-j) == 0 and dp[i] == -1:
            dp[i] = 1
        if dp[i_old] != -1 and dp[i_old]+1 > dp[i]:
            dp[i] = dp[i_old] + 1
print(dp)
ndigits = dp[N]
j = N
final_number = ''
for l in range(ndigits,0,-1):
    #桁を求めていく
    index = 0
    while True:
        temp_j = j - B[index,1]
        if dp[temp_j] + 1 == dp[j]:
            final_number += str(B[index,0])
            j = temp_j
            break
        else:
            index += 1
        if index > M:
            print('smth is wrong')
            break
print(int(final_number))

Buggy submission (verdict: Runtime Error):
import numpy as np

#initialization
nmatch = [2,5,5,4,5,6,3,7,6]
f = input()
f = f.split()
N = int(f[0])
M = int(f[1])
dp = [-1]*(N+1) # contains the number of digits it can make
dp[0] = 0
ff = input()
ff = ff.split()
Atemp = [] #contains the number that can be used and the number of matches needed to make the match
for number in ff:
    temp = [int(number),nmatch[int(number)-1]]
    Atemp.append(temp)
Atemp = np.array(Atemp)
index0 = np.argsort(Atemp[:,0])
index1 = np.argsort(Atemp[:,1])
A = Atemp[index1] #sorted by the number of matches: ascending
B = Atemp[index0]
B = B[::-1] #sorted by the number: descending

for i in range(1,N+1):
    #dp(i)を求める
    for k in range(M):
        j = A[k,1]
        i_old = i - j #available matches
        if (i-j) < 0:
            break
        if (i-j) == 0 and dp[i] == -1:
            dp[i] = 1
        if dp[i_old] != -1 and dp[i_old]+1 > dp[i]:
            dp[i] = dp[i_old] + 1
ndigits = dp[N]
j = N
final_number = ''
for l in range(ndigits,0,-1):
    #桁を求めていく
    index = 0
    while True:
        temp_j = j - B[index,1]
        if dp[temp_j] + 1 == dp[j]:
            final_number  += str(B[index,0])
            j = temp_j
            break
        else:
            index += 1
        if index > M:
            print('smth is wrong')
            break
print(int(final_number))

Buggy submission (verdict: Runtime Error):
import numpy as np

#initialization
nmatch = [2,5,5,4,5,6,3,7,6]
f = input()
f = f.split()
N = int(f[0])
M = int(f[1])
dp = [-1]*(N+1) # contains the number of digits it can make
dp[0] = 0
ff = input()
ff = ff.split()
Atemp = [] #contains the number that can be used and the number of matches needed to make the match
for number in ff:
    temp = [int(number),nmatch[int(number)-1]]
    Atemp.append(temp)
Atemp = np.array(Atemp)
index0 = np.argsort(Atemp[:,0])
index1 = np.argsort(Atemp[:,1])
A = Atemp[index1] #sorted by the number of matches: ascending
B = Atemp[index0]
B = B[::-1] #sorted by the number: descending

for i in range(1,N+1):
    #dp(i)を求める
    for k in range(M):
        j = A[k,1]
        i_old = i - j #available matches
        if (i-j) < 0:
            break
        if (i-j) == 0 and dp[i] == -1:
            dp[i] = 1
        if dp[i_old] != -1 and dp[i_old]+1 > dp[i]:
            dp[i] = dp[i_old] + 1
ndigits = dp[N]
j = N
final_number = ''
for l in range(ndigits,0,-1):
    #桁を求めていく
    index = 0
    while True:
        temp_j = j - B[index,1]
        if dp[temp_j] + 1 == dp[j]:
            final_number  += str(B[index,0])
            j = temp_j
            break
        else:
            index += 1
        if index > M:
            print('smth is wrong')
            break
print(int(final_number))

Buggy submission (verdict: Runtime Error):
import numpy as np

#initialization
nmatch = [2,5,5,4,5,6,3,7,6]
f = input()
f = f.split()
N = int(f[0])
M = int(f[1])
dp = [-1]*(N+1) # contains the number of digits it can make
dp[0] = 0
ff = input()
ff = ff.split()
Atemp = [] #contains the number that can be used and the number of matches needed to make the match
for number in ff:
    temp = [int(number),nmatch[int(number)-1]]
    Atemp.append(temp)
Atemp = np.array(Atemp)
index0 = np.argsort(Atemp[:,0])
index1 = np.argsort(Atemp[:,1])
A = Atemp[index1] #sorted by the number of matches: ascending
B = Atemp[index0]
B = B[::-1] #sorted by the number: descending

for i in range(1,N+1):
    #dp(i)を求める
    for k in range(M):
        j = A[k,1]
        i_old = i - j #available matches
        if (i-j) < 0:
            break
        if (i-j) == 0 and dp[i] == -1:
            dp[i] = 1
        if dp[i_old] != -1 and dp[i_old]+1 > dp[i]:
            dp[i] = dp[i_old] + 1
ndigits = dp[N]
j = N
final_number = ''
for l in range(ndigits,0,-1):
    #桁を求めていく
    index = 0
    while True:
        temp_j = j - B[index,1]
        if dp[temp_j] + 1 == dp[j]:
            final_number  += str(B[index,0])
            j = temp_j
            break
        else:
            index += 1
        if index > 4:
            print('smth is wrong')
            break
print(int(final_number))

Buggy submission (verdict: Runtime Error):
import numpy as np
def judge(N,n,list_new,A,min):
    # judge if we can make the perfect match
    if n == 1:
        if N in list_new:
            s = np.where(N==list_new)
            s = s[0].tolist()
            l = []
            for item in A[s]:
                l.append([item])
            return l
        else:
            return []
    listy = []
    list_here = np.sort(list_new)
    list_h = list_here[::-1]
    for i in range(len(list_h)):
        item = list_new[i]
        M = N-item
        m = n-1
        if M<min*m or item*m < M:
            pass
        else:
            s = judge(M,m,list_new,A,min)
            if s != []:
                for sin in s:
                    listy.append(np.append(sin,A[i]))
    if listy == []:
        return []
    else:
        return listy


list = [2,5,5,4,5,6,3,7,6]
s = input()
s = s.split()
N = int(s[0])
M = int(s[1])
t = input()
A = t.split()
list_new = []
for i in range(M):
    A[i] = int(A[i])
A = np.array(A)
for item in A:
    list_new.append(list[item-1])
list_new = np.array(list_new)
min = np.min(list_new)
q, mod = divmod(N,min)
a = np.where(list_new == min)
a = a[0].tolist()
num = A[a]
digit = max(num)
number = str(digit)*q

while True:
    a = judge(N,q,list_new,A,min)
    if a != []:
        listofnumbers = []
        for item in a:
            item.sort()
            string = ''
            for  i in item:
                string = str(i) + string
            listofnumbers.append(int(string))
        print(np.max(listofnumbers))
        break
    else:
        q = q-1