Buggy python submission (verdict: Runtime Error):

n, m = map(int, input().split(' '))
useable_numbers = list(sorted(map(int, input().split(' '))))
required_maches = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}

dp = dict()

def evaluate(used_numbers):
    if used_numbers == []:
        return 0
    return int(''.join(map(str, sorted(used_numbers, reverse=True))))

def find(used_matches):
    if used_matches in dp:
        return dp[used_matches]
    if used_matches == 0:
        dp[used_matches] = []
        return dp[used_matches]
    if used_matches < 0:
        dp[used_matches] = None
        return dp[used_matches]
    
    candidates = [ai for ai in useable_numbers 
        if find(used_matches - required_maches[ai]) is not None]
    if candidates == []:
        dp[used_matches] = None
        return dp[used_matches]
    
    max_ai = max(candidates,
        key=lambda ai: evaluate(find(used_matches - required_maches[ai]) + [ai]))
    dp[used_matches] = find(used_matches - required_maches[max_ai]) + [max_ai]
    return dp[used_matches]

print(evaluate(find(n)))

Fixed submission:
n, m = map(int, input().split())
a = [int(a) for a in input().split()]    
required_matches = {
    1: 2,
    2: 5,
    3: 5,
    4: 4,
    5: 5,
    6: 6,
    7: 3,
    8: 7,
    9: 6
}

dp = [0] + [-1] * n
for used_matches in range(1, n+1):
    usable_digits = [ai for ai in a if used_matches - required_matches[ai] >= 0]
    if usable_digits != []:
        dp[used_matches] = max([dp[used_matches-required_matches[ai]]*10 + ai
            for ai in usable_digits])
print(dp[n])

Buggy python submission (verdict: Runtime Error):

n, m = map(int, input().split(' '))
ai = list(sorted(map(int, input().split(' '))))
required_maches = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}

used_numbers = dict()

def evaluate(l):
    if l == None:
        return -9999999999999999999999999999999
    if l == []:
        return 0
    return int(''.join(reversed([str(i) for i in sorted(l)])))

min_maches = min([required_maches[x] for x in ai])
def find(matches_count_to_use):
    if matches_count_to_use not in used_numbers:
        if matches_count_to_use < 0:
            used_numbers[matches_count_to_use] = None
        elif matches_count_to_use == 0:
            used_numbers[matches_count_to_use] = []
        else:
            candidates = list(filter(
                lambda x: find(matches_count_to_use - required_maches[x]) != None, ai))
            if len(candidates) == 0:
                used_numbers[matches_count_to_use] = None
            else:
                number_to_use = max(candidates,
                    key=lambda x: evaluate(find(matches_count_to_use - required_maches[x]) + [x]))
                prev = find(matches_count_to_use - required_maches[number_to_use])
                used_numbers[matches_count_to_use] = prev + [number_to_use]
                
    return used_numbers[matches_count_to_use]
    
print(evaluate(find(n)))

Fixed submission:
n, m = map(int, input().split())
a = [int(a) for a in input().split()]    
required_matches = {
    1: 2,
    2: 5,
    3: 5,
    4: 4,
    5: 5,
    6: 6,
    7: 3,
    8: 7,
    9: 6
}

dp = [0] + [-1] * n
for used_matches in range(1, n+1):
    usable_digits = [ai for ai in a if used_matches - required_matches[ai] >= 0]
    if usable_digits != []:
        dp[used_matches] = max([dp[used_matches-required_matches[ai]]*10 + ai
            for ai in usable_digits])
print(dp[n])

Buggy python submission (verdict: Runtime Error):

n, m = map(int, input().split(' '))
ai = list(sorted(map(int, input().split(' '))))
required_maches = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}

used_numbers = dict()

def evaluate(l):
    if l == None:
        return -9999999999999999999999999999999
    if l == []:
        return 0
    return int(''.join(reversed([str(i) for i in sorted(l)])))

min_maches = min([required_maches[x] for x in ai])
def find(matches_count_to_use):
    if matches_count_to_use not in used_numbers:
        if matches_count_to_use == 0:
            used_numbers[matches_count_to_use] = []
        elif matches_count_to_use < min_maches:
            used_numbers[matches_count_to_use] = None
        else:
            candidates = list(filter(
                lambda x: find(matches_count_to_use - required_maches[x]) != None, ai))
            if len(candidates) == 0:
                used_numbers[matches_count_to_use] = None
            else:
                number_to_use = max(candidates,
                    key=lambda x: evaluate(find(matches_count_to_use - required_maches[x]) + [x]))
                prev = find(matches_count_to_use - required_maches[number_to_use])
                used_numbers[matches_count_to_use] = prev + [number_to_use]
                
    return used_numbers[matches_count_to_use]
    
print(evaluate(find(n)))

Fixed submission:
n, m = map(int, input().split())
a = [int(a) for a in input().split()]    
required_matches = {
    1: 2,
    2: 5,
    3: 5,
    4: 4,
    5: 5,
    6: 6,
    7: 3,
    8: 7,
    9: 6
}

dp = [0] + [-1] * n
for used_matches in range(1, n+1):
    usable_digits = [ai for ai in a if used_matches - required_matches[ai] >= 0]
    if usable_digits != []:
        dp[used_matches] = max([dp[used_matches-required_matches[ai]]*10 + ai
            for ai in usable_digits])
print(dp[n])

Buggy python submission (verdict: Runtime Error):

n, m = map(int, input().split(' '))
ai = list(sorted(map(int, input().split(' '))))
required_maches = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}

used_numbers = dict()

def evaluate(l):
    if l == None:
        return -9999999999999999999999999999999
    if l == []:
        return 0
    return int(''.join(reversed([str(i) for i in sorted(l)])))

min_maches = min([required_maches[x] for x in ai])
def find(matches_count_to_use):
    if matches_count_to_use not in used_numbers:
        if matches_count_to_use == 0:
            used_numbers[matches_count_to_use] = []
        elif matches_count_to_use < min_maches:
            used_numbers[matches_count_to_use] = None
        else:
            number_to_use = max(ai,
                key=lambda x: evaluate(find(matches_count_to_use - required_maches[x])))
            prev = find(matches_count_to_use - required_maches[number_to_use])
            if prev == None:
                used_numbers[matches_count_to_use] = None
            else:
                used_numbers[matches_count_to_use] = prev + [number_to_use]
    return used_numbers[matches_count_to_use]
    
print(evaluate(find(n)))

Fixed submission:
n, m = map(int, input().split())
a = [int(a) for a in input().split()]    
required_matches = {
    1: 2,
    2: 5,
    3: 5,
    4: 4,
    5: 5,
    6: 6,
    7: 3,
    8: 7,
    9: 6
}

dp = [0] + [-1] * n
for used_matches in range(1, n+1):
    usable_digits = [ai for ai in a if used_matches - required_matches[ai] >= 0]
    if usable_digits != []:
        dp[used_matches] = max([dp[used_matches-required_matches[ai]]*10 + ai
            for ai in usable_digits])
print(dp[n])

Buggy python submission (verdict: Runtime Error):

n, m = map(int, input().split(' '))
ai = list(sorted(map(int, input().split(' '))))
required_maches = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}

used_numbers = dict()

def evaluate(l):
    if l == None:
        return -999999999
    if l == []:
        return 0
    return int(''.join(reversed([str(i) for i in sorted(l)])))

min_maches = min([required_maches[x] for x in ai])
def find(matches_count_to_use):
    if matches_count_to_use not in used_numbers:
        if matches_count_to_use == 0:
            used_numbers[matches_count_to_use] = []
        elif matches_count_to_use < min_maches:
            used_numbers[matches_count_to_use] = None
        else:
            number_to_use = max(ai,
                key=lambda x: evaluate(find(matches_count_to_use - required_maches[x])))
            prev = find(matches_count_to_use - required_maches[number_to_use])
            if prev == None:
                used_numbers[matches_count_to_use] = None
            else:
                used_numbers[matches_count_to_use] = prev + [number_to_use]
    return used_numbers[matches_count_to_use]
    
print(evaluate(find(n)))

Fixed submission:
n, m = map(int, input().split())
a = [int(a) for a in input().split()]    
required_matches = {
    1: 2,
    2: 5,
    3: 5,
    4: 4,
    5: 5,
    6: 6,
    7: 3,
    8: 7,
    9: 6
}

dp = [0] + [-1] * n
for used_matches in range(1, n+1):
    usable_digits = [ai for ai in a if used_matches - required_matches[ai] >= 0]
    if usable_digits != []:
        dp[used_matches] = max([dp[used_matches-required_matches[ai]]*10 + ai
            for ai in usable_digits])
print(dp[n])