Problem p03128 (python)

Fixed submission:
n,m = [int(i) for i in input().split()]
a = list(map(int,input().split()))

import sys
sys.setrecursionlimit(50000)

match = [0,2,5,5,4,5,6,3,7,6]

dig = [0] * (n+1)

inf = 10**9


for i in range(1, n + 1):
    lst = []
    for j in range(len(a)):
        num = a[j]
        mat = match[num]
        bef = i - mat
        if bef >= 0:
            if bef == 0:
                lst.append(dig[bef])
            else:
                if dig[bef] > 0:
                    lst.append(dig[bef])
        if lst:
            dig[i] = max(lst) + 1


ans = ""


def dp(i):
    k = -1
    if i == 0:
        return
    global ans
    maxi = 0
    for j in range(len(a)):
        num = a[j]
        mat = match[num]
        bef = i - mat
        if dig[i] == 1:
            if bef == 0:
                maxi = max(maxi, num)
                k = 0
        else:
            if bef >= 0:
                if dig[bef] == dig[i] - 1:
                    if num > maxi:
                        maxi = num
                        k = bef
    ans += str(maxi)
    if k >= 0:
        dp(k)


dp(n)
print(ans)

Buggy submission (verdict: Runtime Error):
n,m = [int(i) for i in input().split()]
a = list(map(int,input().split()))


match = [0,2,5,5,4,5,6,3,7,6]

dig = [0] * (n+1)

inf = 10**9


for i in range(1, n + 1):
    lst = []
    for j in range(len(a)):
        num = a[j]
        mat = match[num]
        bef = i - mat
        if bef >= 0:
            if bef == 0:
                lst.append(dig[bef])
            else:
                if dig[bef] > 0:
                    lst.append(dig[bef])
        if lst:
            dig[i] = max(lst) + 1


ans = ""


def dp(i):
    k = -1
    if i == 0:
        return
    global ans
    maxi = 0
    for j in range(len(a)):
        num = a[j]
        mat = match[num]
        bef = i - mat
        if dig[i] == 1:
            if bef == 0:
                maxi = max(maxi, num)
                k = 0
        else:
            if bef >= 0:
                if dig[bef] == dig[i] - 1:
                    if num > maxi:
                        maxi = num
                        k = bef
    ans += str(maxi)
    if k >= 0:
        dp(k)


dp(n)
print(ans)

Buggy submission (verdict: Runtime Error):
n,m = [int(i) for i in input().split()]
a = list(map(int,input().split()))

import sys
sys.setrecursionlimit(50000)

match = [0,2,5,5,4,5,6,3,7,6]

dig = [0] * (n+1)

inf = 10**9


for i in range(1,n+1):
    lst = []
    for j in range(len(a)):
        num = a[j]
        mat = match[num]
        bef = i-mat
        if bef >= 0:
            lst.append(dig[bef])
        if lst:
            dig[i] = max(lst) + 1


ans = ""
def dp(i):
    if i == 0:
        return
    global ans
    maxi = 0
    for j in range(len(a)):
        num = a[j]
        mat = match[num]
        bef = i - mat
        if dig[i] == 1:
            if bef == 0:
                maxi = max(maxi,num)
                k = 0
        else:
            if bef >= 0:
                if dig[bef] == dig[i]-1:
                    if num > maxi:
                        maxi = num
                        k = bef
    ans += str(maxi)
    dp(k)

dp(n)
print(ans)

Buggy submission (verdict: Runtime Error):
n,m = [int(i) for i in input().split()]
a = list(map(int,input().split()))

import sys
sys.setrecursionlimit(10000)

match = [0,2,5,5,4,5,6,3,7,6]

dig = [0] * (n+1)

inf = 10**9


for i in range(1,n+1):
    lst = []
    for j in range(len(a)):
        num = a[j]
        mat = match[num]
        bef = i-mat
        if bef >= 0:
            lst.append(dig[bef])
        if lst:
            dig[i] = max(lst) + 1


ans = ""
def dp(i):
    if i == 0:
        return
    global ans
    maxi = 0
    for j in range(len(a)):
        num = a[j]
        mat = match[num]
        bef = i - mat
        if dig[i] == 1:
            if bef == 0:
                maxi = max(maxi,num)
                k = 0
        else:
            if bef >= 0:
                if dig[bef] == dig[i]-1:
                    if num > maxi:
                        maxi = num
                        k = bef
    ans += str(maxi)
    dp(k)

dp(n)
print(ans)

Buggy submission (verdict: Runtime Error):
n,m = [int(i) for i in input().split()]
a = list(map(int,input().split()))

match = [0,2,5,5,4,5,6,3,7,6]

dig = [0] * (n+1)

inf = 10**9


for i in range(1,n+1):
    lst = []
    for j in range(len(a)):
        num = a[j]
        mat = match[num]
        bef = i-mat
        if bef >= 0:
            lst.append(dig[bef])
        if lst:
            dig[i] = max(lst) + 1

ans = ""
def dp(i):
    if i == 0:
        return
    global ans
    maxi = 0
    for j in range(len(a)):
        num = a[j]
        mat = match[num]
        bef = i - mat
        if bef >= 0:
            if dig[i] == 1:
                if bef == 0:
                    maxi = max(maxi,num)
                    k = 0
            else:
                if dig[bef] == dig[i]-1:
                    if num > maxi:
                        maxi = num
                        k = bef
    ans += str(maxi)
    dp(k)

dp(n)
print(ans)

Buggy submission (verdict: Runtime Error):
n,m = [int(i) for i in input().split()]
a = list(map(int,input().split()))

match = [0,2,5,5,4,5,6,3,7,6]

dig = [0] * (n+1)

inf = 10**9


for i in range(1,n+1):
    lst = []
    for j in range(len(a)):
        num = a[j]
        mat = match[num]
        bef = i-mat
        if bef >= 0:
            lst.append(dig[bef])
        if lst:
            dig[i] = max(lst) + 1


ans = ""
def dp(i):
    if i == 0:
        return
    global ans
    maxi = 0
    for j in range(len(a)):
        num = a[j]
        mat = match[num]
        bef = i - mat
        if bef >= 0:
            if dig[bef] == dig[i]-1 and (dig[bef] > 0 or bef == 0):
                if num > maxi:
                    maxi = num
                    k = bef
    dp(k)

dp(n)
print(ans)

Buggy submission (verdict: Runtime Error):
n,m = [int(i) for i in input().split()]
a = list(map(int,input().split()))

match = [0,2,5,5,4,5,6,3,7,6]

dig = [0] * (n+1)

inf = 10**9


for i in range(1,n+1):
    lst = []
    for j in range(len(a)):
        num = a[j]
        mat = match[num]
        bef = i-mat
        if bef >= 0:
            lst.append(dig[bef])
        if lst:
            dig[i] = max(lst) + 1


ans = ""
def dp(i):
    if i == 0:
        return
    global ans
    maxi = 0
    k = -1
    for j in range(len(a)):
        num = a[j]
        mat = match[num]
        bef = i - mat
        if bef >= 0:
            if dig[bef] == dig[i]-1 and (dig[bef] > 0 or bef == 0):
                if num > maxi:
                    maxi = num
                    k = bef
    ans += str(maxi)
    if k >=0:
        dp(k)

dp(n)
print(ans)

Buggy submission (verdict: Runtime Error):
n,m = [int(i) for i in input().split()]
a = list(map(int,input().split()))

match = [0,2,5,5,4,5,6,3,7,6]

dig = [0] * (n+1)

inf = 10**9


for i in range(1,n+1):
    lst = []
    for j in range(len(a)):
        num = a[j]
        mat = match[num]
        bef = i-mat
        if bef >= 0:
            lst.append(dig[bef])
        if lst:
            dig[i] = max(lst) + 1


ans = ""
def dp(i):
    if i == 0:
        return
    global ans
    maxi = 0
    for j in range(len(a)):
        num = a[j]
        mat = match[num]
        bef = i - mat
        if dig[bef] == dig[i]-1 and (dig[bef] > 0 or bef == 0):
            if num > maxi:
                maxi = num
                k = bef
    ans += str(maxi)
    dp(k)

dp(n)
print(ans)