Problem p03128 (python)

Fixed submission:
# cook your dish here
def f(x):
    if x == 1:
        return 2
    if x == 2:
        return 5
    if x == 3:
        return 5
    if x == 4:
        return 4
    if x == 5:
        return 5
    if x == 6:
        return 6
    if x == 7:
        return 3
    if x == 8:
        return 7
    if x == 9:
        return 6
n,m = map(int,input().split())
a = list(map(int,input().split()))
if 2 in a and 3 in a:
    a.remove(2)
    m -=1
if 2 in a and 5 in a:
    a.remove(2)
    m -=1
if 3 in a and 5 in a:
    a.remove(3)
    m -=1
if 6 in a and 9 in a:
    a.remove(6)
    m -=1
for w in range(1,10):
    if n < f(w) and w in a:
        a.remove(w)
        m -=1
a.sort()
m = len(a)

dp = [[0]*(m+1) for i in range(n+2)]


def g(k):
    cand = [0]*(m+1)
    for i in range(m):
        if k - f(a[i])>=0:
            if cand[0] <= dp[k-f(a[i])][0]+1 and dp[k-f(a[i])][0] != 0:
                for r in range(m+1):
                    cand[r] = dp[k-f(a[i])][r]
                cand[0] += 1
                cand[i+1] += 1
    if sum(cand) != 0:
        dp[k] = cand
for i in range(m):
    if f(a[i]) <= n:
        dp[f(a[i])][0] = 1
        dp[f(a[i])][i+1] = 1

for j in range(1,n+1):
    g(j)

ass = ''

for i in range(m):
    for k in range(dp[n][m-i]):
        ass += str(a[m-i-1])
print(ass)

Buggy submission (verdict: Runtime Error):
def f(x):
    if x == 1:
        return 2
    if x == 2:
        return 5
    if x == 3:
        return 5
    if x == 4:
        return 4
    if x == 5:
        return 5
    if x == 6:
        return 6
    if x == 7:
        return 3
    if x == 8:
        return 7
    if x == 9:
        return 6
n,m = map(int,input().split())
a = list(map(int,input().split()))
if 2 in a and 3 in a:
    a.remove(2)
    m -=1
if 2 in a and 5 in a:
    a.remove(2)
    m -=1
if 3 in a and 5 in a:
    a.remove(3)
    m -=1
if 6 in a and 9 in a:
    a.remove(6)
    m -=1
for w in range(1,10):
    if n < f(w):
        a.remove(w)
        m -=1
a.sort()
m = len(a)

dp = [[0]*(m+1) for i in range(n+2)]


def g(k):
    cand = [0]*(m+1)
    for i in range(m):
        if k - f(a[i])>=0:
            if cand[0] <= dp[k-f(a[i])][0]+1 and dp[k-f(a[i])][0] != 0:
                for r in range(m+1):
                    cand[r] = dp[k-f(a[i])][r]
                cand[0] += 1
                cand[i+1] += 1
    if sum(cand) != 0:
        dp[k] = cand
for i in range(m):
    if f(a[i]) <= n:
        dp[f(a[i])][0] = 1
        dp[f(a[i])][i+1] = 1
print(1)

Buggy submission (verdict: Runtime Error):
# cook your dish here
def f(x):
    if x == 1:
        return 2
    if x == 2:
        return 5
    if x == 3:
        return 5
    if x == 4:
        return 4
    if x == 5:
        return 5
    if x == 6:
        return 6
    if x == 7:
        return 3
    if x == 8:
        return 7
    if x == 9:
        return 6
n,m = map(int,input().split())
a = list(map(int,input().split()))
if 2 in a and 3 in a:
    a.remove(2)
    m -=1
if 2 in a and 5 in a:
    a.remove(2)
    m -=1
if 3 in a and 5 in a:
    a.remove(3)
    m -=1
if 6 in a and 9 in a:
    a.remove(6)
    m -=1
for w in range(1,10):
    if n < f(w):
        a.remove(w)
        m -=1
a.sort()

dp = [[0]*(m+1) for i in range(n+1)]


def g(k):
    cand = [0]*(m+1)
    for i in range(m):
        if k - f(a[i])>=0:
            if cand[0] <= dp[k-f(a[i])][0]+1 and dp[k-f(a[i])][0] != 0:
                for r in range(m+1):
                    cand[r] = dp[k-f(a[i])][r]
                cand[0] += 1
                cand[i+1] += 1
    if sum(cand) != 0:
        dp[k] = cand
for i in range(m):
    if f(a[i]) <= n:
        dp[f(a[i])][0] = 1
        dp[f(a[i])][i+1] = 1

for j in range(1,n+1):
    g(j)

ass = ''

for i in range(m):
    for k in range(dp[n][m-i]):
        ass += str(a[m-i-1])
print(ass)

Buggy submission (verdict: Runtime Error):
# cook your dish here
n,m = map(int,input().split())
a = list(map(int,input().split()))
if 2 in a and 3 in a:
    a.remove(2)
    m -=1
if 2 in a and 5 in a:
    a.remove(2)
    m -=1
if 3 in a and 5 in a:
    a.remove(3)
    m -=1
if 6 in a and 9 in a:
    a.remove(6)
    m -=1
a.sort()

dp = [[0]*(m+1) for i in range(n+2)]
def f(x):
    if x == 1:
        return 2
    if x == 2:
        return 5
    if x == 3:
        return 5
    if x == 4:
        return 4
    if x == 5:
        return 5
    if x == 6:
        return 6
    if x == 7:
        return 3
    if x == 8:
        return 7
    if x == 9:
        return 6

def g(k):
    cand = [0]*(m+1)
    for i in range(m):
        if k - f(a[i])>=0:
            if cand[0] <= dp[k-f(a[i])][0]+1 and dp[k-f(a[i])][0] != 0:
                for r in range(m+1):
                    cand[r] = dp[k-f(a[i])][r]
                cand[0] += 1
                cand[i+1] += 1
    if sum(cand) != 0:
        dp[k] = cand
for i in range(m):
    dp[f(a[i])][0] = 1
    dp[f(a[i])][i+1] = 1

for j in range(1,n+1):
    g(j)

ass = ''

for i in range(m):
    for k in range(dp[n][m-i]):
        ass += str(a[m-i-1])
print(ass)