Buggy python submission (verdict: Runtime Error):
import sys
from bisect import bisect_left
INF = 1 << 30

def main():
    N,M = map(int,input().split())
    A = set(map(str,input().split()))
    number = '174532968'
    necesary_match = (2,3,4,5,5,5,6,6,7)
    match = {i:j for i,j in zip(number,necesary_match)}

    if '1' in A:
        if N%2 == 0:
            ans = ['1'] * (N//2)
        else:
            for num in '75328':
                if num in A:
                    ans = ['1'] * ((N - match[num])//2) + [num]
                    break
    elif '7' in A:
        if N%3 == 0:
            ans = ['7'] * (N//3)
        elif N%3 == 1:
            flag = False
            for num in '48':
                if num in A:
                    flag = True
                    ans = ['7'] * ((N - match[num])//3) + [num]
                    break
            if not flag:
                for num in '532':
                    if num in A:
                        ans = ['7'] * ((N - 2*match[num])//3) + [num] * 2
                        break
        else:
            flag = False
            for num in '532':
                if num in A:
                    flag = True
                    ans = ['7'] * ((N - match[num])//3) + [num]
                    break
            if not flag:
                for num in '48':
                    if num in A:
                        ans = ['7'] * ((N - 2*match[num])//3) + [num] * 2
                        break
    elif '4' in A:
        if N%4 == 0:
            ans = ['4'] * (N//4)
        elif N%4 == 1:
            flag = False
            for num in '532':
                if num in A:
                    flag = True
                    ans = ['4'] * ((N - match[num])//4) + [num]
                    break
            if not flag:
                for num in '96':
                    if num in A:
                        flag = True
                        ans = ['4'] * ((N - match[num] - match['8'])//4) + [num,'8']
                        break
            if not flag:
                ans = ['4'] * ((N - match['8']*3)//4) + ['8'] * 3
        elif N%4 == 2:
            flag = False
            for num in '96':
                if num in A:
                    ans = ['4'] * ((N - match[num])//4) + [num]
                    flag = True
                    break
            if not flag:
                for num in '532':
                    if num in A:
                        ans = ['4'] * ((N - 2*match[num])//4) + [num] * 2
                        flag = True
                        break
            if not flag:
                ans = ['4'] * ((N - 2*match['8'])//4) + ['8'] * 2
        else:
            flag = False
            if '8' in A:
                flag = True
                ans = ['4'] * ((N - match['8'])//4) + ['8']
            if not flag:
                x = ''
                for num in '96':
                    if num in A:
                        x = num
                        break
                y = ''
                for num in '532':
                    if num in A:
                        y = num
                        break
                if x:
                    ans = ['4'] * ((N - match[x] - match[y])//4) + [x,y]
                else:
                    ans = ['4'] * ((N - 3*match[y])//4) + [y] *3
    else:
        x = ''
        for num in '532':
            if num in A:
                x = num
                break
        if x:
            if N%5 == 0:
                ans = [x] * (N//5)
            elif N&5 == 1:
                flag = False
                for num in '96':
                    if num in A:
                        flag = True
                        ans = [x] * ((N - match[num])//5) + [num]
                        break
                if not flag:
                    ans = [x] * ((N - 3*match['8'])//5) + ['8'] * 3
            elif N%5 == 2:
                if '8' in A:
                    ans = [x] * ((N - match['8'])//5) + ['8']
                else:
                    for num in '96':
                        if num in A:
                            ans = [x] * ((N - 2*match[num])//5) + [num] * 2
                            break
            elif N%5 == 3:
                if '8' in A:
                    flag = False
                    for num in '96':
                        if num in A:
                            flag = True
                            ans = [x] * ((N - match[num] - match['8'])//5) + ['8',num]
                            break
                    if not flag:
                        ans = [x] * ((N - 4*match['8'])//5) + ['8'] * 4
                else:
                    for num in '96':
                        if num in A:
                            ans = [x] * ((N - 3*match[num])//5) + [num] * 3
                            break
            elif N%5 == 4:
                if '8' in A:
                    ans = [x] * ((N - 2*match['8'])//5) + ['8'] * 2
                else:
                    for num in '96':
                        if num in A:
                            ans = [x] * ((N - 4*match[num])//5) + [num] * 4
                            break

        else:
            y = ''
            for num in '96':
                if num in A:
                    y = num
                    break
            if y:
                if N%6 == 0:
                    ans = [y] * (N//6)
                else:
                    k = N%6
                    ans = [y] * ((N - k*match['8'])//6) + ['8'] * k
            else:
                ans = ['8'] * (N//7)
    ans.sort(reverse = True)
    print(''.join(ans))
if __name__ == '__main__':
    main()

Fixed submission:
import sys
sys.setrecursionlimit(10000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15

from copy import deepcopy
def compare(A,B):
    tot_a = sum(A)
    tot_b = sum(B)
    if tot_a > tot_b:
        return A
    if tot_b > tot_a:
        return B
    for i in range(9,0,-1):
        if A[i] > B[i]:
            return A
        elif A[i] < B[i]:
            return B

def main():
    N,M = map(int,input().split())
    A = list(map(int,input().split()))
    match = (-1,2,5,5,4,5,6,3,7,6)

    dp = [[0]*10 for _ in range(1 + N)]
    changed = [0] * (N + 1)
    changed[0] = 1
    for i in range(N):
        for j in A:
            if i + 1 - match[j] >= 0 and changed[i + 1 - match[j]]:
                B = deepcopy(dp[i + 1 - match[j]])
                B[j] += 1
                if compare(dp[i + 1],B) == B:
                    dp[i + 1] = B
                    changed[i + 1] = 1
    ans = ''
    for i in range(9,0,-1):
        ans += str(i) * dp[-1][i] 
    print(ans)    
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Runtime Error):
import sys
from bisect import bisect_left
INF = 1 << 30

def main():
    N,M = map(int,input().split())
    A = set(map(str,input().split()))
    number = '174532968'
    necesary_match = (2,3,4,5,5,5,6,6,7)
    match = {i:j for i,j in zip(number,necesary_match)}

    if '1' in A:
        if N%2 == 0:
            ans = ['1'] * (N//2)
        else:
            for num in '75328':
                if num in A:
                    ans = ['1'] * ((N - match[num])//2) + [num]
                    break
    elif '7' in A:
        if N%3 == 0:
            ans = ['7'] * (N//3)
        elif N%3 == 1:
            flag = False
            for num in '48':
                if num in A:
                    flag = True
                    ans = ['7'] * ((N - match[num])//3) + [num]
                    break
            if not flag:
                for num in '532':
                    if num in A:
                        ans = ['7'] * ((N - 2*match[num])//3) + [num] * 2
                        break
        else:
            flag = False
            for num in '532':
                if num in A:
                    flag = True
                    ans = ['7'] * ((N - match[num])//3) + [num]
                    break
            if not flag:
                for num in '48':
                    if num in A:
                        ans = ['7'] * ((N - 2*match[num])//3) + [num] * 2
                        break
    elif '4' in A:
        if N%4 == 0:
            ans = ['4'] * (N//4)
        elif N%4 == 1:
            flag = False
            for num in '532':
                if num in A:
                    flag = True
                    ans = ['4'] * ((N - match[num])//4) + [num]
                    break
            if not flag:
                for num in '96':
                    if num in A:
                        flag = True
                        ans = ['4'] * ((N - match[num] - match['8'])//4) + [num,'8']
                        break
            if not flag:
                ans = ['4'] * ((N - match['8']*3)//4) + ['8'] * 3
        elif N%4 == 2:
            flag = False
            for num in '96':
                if num in A:
                    ans = ['4'] * ((N - match[num])//4) + [num]
                    flag = True
                    break
            if not flag:
                for num in '532':
                    if num in A:
                        ans = ['4'] * ((N - 2*match[num])//4) + [num] * 2
                        flag = True
                        break
            if not flag:
                ans = ['4'] * ((N - 2*match['8'])//4) + ['8'] * 2
        else:
            flag = False
            if '8' in A:
                flag = True
                ans = ['4'] * ((N - match['8'])//4) + ['8']
            if not flag:
                x = ''
                for num in '96':
                    if num in A:
                        x = num
                        break
                y = ''
                for num in '532':
                    if num in A:
                        y = num
                        break
                if x:
                    ans = ['4'] * ((N - match[x] - match[y])//4) + [x,y]
                else:
                    ans = ['4'] * ((N - 3*match[y])//4) + [y] *3
    else:
        x = ''
        for num in '532':
            if num in A:
                x = num
                break
        if x:
            if N%5 == 0:
                ans = [x] * (N//5)
            elif N&5 == 1:
                flag = False
                for num in '96':
                    if num in A:
                        flag = True
                        ans = [x] * ((N - match[num])//5) + [num]
                        break
                if not flag:
                    ans = [x] * ((N - 3*match['8'])//5) + ['8'] * 3
            elif N%5 == 2:
                if '8' in A:
                    ans = [x] * ((N - match['8'])//5) + ['8']
                else:
                    for num in '96':
                        if num in A:
                            ans = [x] * ((N - 2*match[num])//5) + [num] * 2
                            break
            elif N%5 == 3:
                if '8' in A:
                    flag = False
                    for num in '96':
                        if num in A:
                            flag = True
                            ans = [x] * ((N - match[num] - match['8'])//5) + ['8',num]
                            break
                    if not flag:
                        ans = [x] * ((N - 4*match['8'])//5) + ['8'] * 4
            elif N%5 == 4:
                if '8' in A:
                    ans = [x] * ((N - 2*match['8'])//5) + ['8'] * 2
                else:
                    for num in '96':
                        if num in A:
                            ans = [x] * ((N - 4*match[num])//5) + [num] * 4
                            break

        else:
            y = ''
            for num in '96':
                if num in A:
                    y = num
                    break
            if y:
                if N%6 == 0:
                    ans = [y] * (N//6)
                else:
                    k = N%6
                    ans = [y] * ((N - k*match['8'])//6) + ['8'] * k
            else:
                ans = ['8'] * (N//7)
    ans.sort(reverse = True)
    print(''.join(ans))
if __name__ == '__main__':
    main()

Fixed submission:
import sys
sys.setrecursionlimit(10000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15

from copy import deepcopy
def compare(A,B):
    tot_a = sum(A)
    tot_b = sum(B)
    if tot_a > tot_b:
        return A
    if tot_b > tot_a:
        return B
    for i in range(9,0,-1):
        if A[i] > B[i]:
            return A
        elif A[i] < B[i]:
            return B

def main():
    N,M = map(int,input().split())
    A = list(map(int,input().split()))
    match = (-1,2,5,5,4,5,6,3,7,6)

    dp = [[0]*10 for _ in range(1 + N)]
    changed = [0] * (N + 1)
    changed[0] = 1
    for i in range(N):
        for j in A:
            if i + 1 - match[j] >= 0 and changed[i + 1 - match[j]]:
                B = deepcopy(dp[i + 1 - match[j]])
                B[j] += 1
                if compare(dp[i + 1],B) == B:
                    dp[i + 1] = B
                    changed[i + 1] = 1
    ans = ''
    for i in range(9,0,-1):
        ans += str(i) * dp[-1][i] 
    print(ans)    
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Runtime Error):
import sys
from bisect import bisect_left
INF = 1 << 30

def main():
    N,M = map(int,input().split())
    A = set(map(str,input().split()))
    number = '174532968'
    necesary_match = (1,3,4,5,5,5,6,6,7)
    match = {i:j for i,j in zip(number,necesary_match)}

    if '1' in A:
        if N%2 == 0:
            ans = ['1'] * (N//2)
        else:
            for num in '7532':
                if num in A:
                    ans = ['1'] * ((N - match[num])//2) + [num]
                    break
    elif '7' in A:
        if N%3 == 0:
            ans = ['7'] * (N//3)
        elif N%3 == 1:
            flag = False
            for num in '496':
                if num in A:
                    flag = True
                    ans = ['7'] * ((N - match[num])//3) + [num]
                    break
            if not flag:
                for num in '5328':
                    if num in A:
                        ans = ['7'] * ((N - 2*match[num])//3) + [num] * 2
        else:
            flag = False
            for num in '5328':
                if num in A:
                    flag = True
                    ans = ['7'] * ((N - match[num])//3) + [num]
                    break
            if not flag:
                for num in '496':
                    if num in A:
                        ans = ['7'] * ((N - 2*match[num])//3) + [num] * 2
    elif '4' in A:
        if N%4 == 0:
            ans = ['4'] * (N//4)
        elif N%4 == 1:
            flag = False
            for num in '532':
                if num in A:
                    flag = True
                    ans = ['4'] * ((N - match[num])//4) + [num]
                    break
            if not flag:
                for num in '96':
                    if num in A:
                        flag = True
                        ans = ['4'] * ((N - match[num] - match['8'])//4) + [num,'8']
                        break
            if not flag:
                ans = ['4'] * ((N - match['8']*3)//4) + ['8'] * 3
        elif N%4 == 2:
            flag = False
            for num in '96':
                if num in A:
                    ans = ['4'] * ((N - match[num])//4) + [num]
                    flag = True
                    break
            if not flag:
                for num in '532':
                    if num in A:
                        ans = ['4'] * ((N - 2*match[num])//4) + [num] * 2
                        flag = True
                        break
            if not flag:
                ans = ['4'] * ((N - 2*match['8'])//4) + ['8'] * 2
        else:
            flag = False
            if '8' in A:
                flag = True
                ans = ['4'] * ((N - match['8'])//4) + ['8']
            if not flag:
                x = ''
                for num in '96':
                    if num in A:
                        x = num
                        break
                y = ''
                for num in '532':
                    if num in A:
                        y = num
                        break
                if x:
                    ans = ['4'] * ((N - match[x] - match[y])//4) + [x,y]
                else:
                    ans = ['4'] * ((N - 3*match[y])//4) + [y] *3
    else:
        x = ''
        for num in '532':
            if num in A:
                x = num
                break
        if x:
            if N%5 == 0:
                ans = [x] * (N//5)
            elif N&5 == 1:
                flag = False
                for num in '96':
                    if num in A:
                        flag = True
                        ans = [x] * ((N - match[num])//5) + [num]
                        break
                if not flag:
                    ans = [x] * ((N - 3*match['8'])//5) + ['8'] * 3
            elif N%5 == 2:
                if '8' in A:
                    ans = [x] * ((N - match['8'])//5) + ['8']
                else:
                    for num in '96':
                        if num in A:
                            ans = [x] * ((N - 2*match[num])//5) + [num] * 2
                            break
            elif N%5 == 3:
                if '8' in A:
                    flag = False
                    for num in '96':
                        if num in A:
                            flag = True
                            ans = [x] * ((N - match[num] - match['8'])//5) + ['8',num]
                            break
                    if not flag:
                        ans = [x] * ((N - 4*match['8'])//5) + ['8'] * 4
            elif N%5 == 4:
                if '8' in A:
                    ans = [x] * ((N - 2*match['8'])//5) + ['8'] * 2
                else:
                    for num in '96':
                        if num in A:
                            ans = [x] * ((N - 4*match[num])//5) + [num] * 4
                            break

        else:
            y = ''
            for num in '96':
                if num in A:
                    y = num
                    break
            if y:
                if N%6 == 0:
                    ans = [y] * (N//6)
                else:
                    k = N%6
                    ans = [y] * ((N - k*match['8'])//6) + ['8'] * k
            else:
                ans = ['8'] * (N//7)
    ans.sort(reverse = True)
    print(''.join(ans))
if __name__ == '__main__':
    main()

Fixed submission:
import sys
sys.setrecursionlimit(10000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15

from copy import deepcopy
def compare(A,B):
    tot_a = sum(A)
    tot_b = sum(B)
    if tot_a > tot_b:
        return A
    if tot_b > tot_a:
        return B
    for i in range(9,0,-1):
        if A[i] > B[i]:
            return A
        elif A[i] < B[i]:
            return B

def main():
    N,M = map(int,input().split())
    A = list(map(int,input().split()))
    match = (-1,2,5,5,4,5,6,3,7,6)

    dp = [[0]*10 for _ in range(1 + N)]
    changed = [0] * (N + 1)
    changed[0] = 1
    for i in range(N):
        for j in A:
            if i + 1 - match[j] >= 0 and changed[i + 1 - match[j]]:
                B = deepcopy(dp[i + 1 - match[j]])
                B[j] += 1
                if compare(dp[i + 1],B) == B:
                    dp[i + 1] = B
                    changed[i + 1] = 1
    ans = ''
    for i in range(9,0,-1):
        ans += str(i) * dp[-1][i] 
    print(ans)    
if __name__ == '__main__':
    main()