Buggy python submission (verdict: Time Limit Exceeded):
#coding:utf-8
N,M  = map(int,input().split())
A = list(map(int,input().split()))
b = 0
c = [0,2,5,5,4,5,6,3,7,6]
bagS = [[] for i in range(N+1)]
def check(A,B):#A:prev,B:temp
    As = list(map(str,sorted(A,reverse=True)))
    p = int("".join(As)) if len(A) else 0

    B.sort(reverse=True)
    Bs = list(map(str,B))
    
    t = int("".join(Bs)) if len(B) else 0
    if t > p:
        return B
    else :
        return A
def check2(A,B):
    p = 0
    t = 0
    for a in A:
        p = p*10+a
    for b in B:
        t = t*10+b
    if t > p:
        return B

    else:
        return A

while b != N:

    for a in A:
        if b+ c[a] <= N:
  
            bagS[b+c[a]] = check2(bagS[b+c[a]],bagS[b]+[a])

    for i in range(b+1,N+1):
        if bagS[i]:
            b = i
            break
print("".join(list(map(str,bagS[N]))))



Fixed submission:
#coding:utf-8
N,M  = map(int,input().split())
A = list(map(int,input().split()))
b = 0
c = [0,2,5,5,4,5,6,3,7,6]
bagS = [[] for i in range(N+1)]

def check2(A,B):
    p = 0
    t = 0
    # print(A)
    # print(B)
    if len(A) > len(B):
        return A
    elif len(A) < len(B):
        return B
    for a,b in zip(A,B):
        if  a- b > 0:
            return A
        elif a-b < 0:
            return B
    return A
    # for a in A:
    #     p = p*10+a
    # for b in B:
    #     t = t*10+b
    # if t > p:
    #     return B

    # else:
    #     return A

while b != N:

    for a in A:
        if b+ c[a] <= N:
  
            bagS[b+c[a]] = check2(bagS[b+c[a]],sorted((bagS[b]+[a]),reverse=True))

    for i in range(b+1,N+1):
        if bagS[i]:
            b = i
            break
print("".join(list(map(str,bagS[N]))))

Buggy python submission (verdict: Time Limit Exceeded):
#coding:utf-8
N,M  = map(int,input().split())
A = list(map(int,input().split()))
b = 0
c = [0,2,5,5,4,5,6,3,7,6]
bagS = [[] for i in range(N+1)]
def check(A,B):#A:prev,B:temp
    As = list(map(str,sorted(A,reverse=True)))
    p = int("".join(As)) if len(A) else 0

    B.sort(reverse=True)
    Bs = list(map(str,B))
    
    t = int("".join(Bs)) if len(B) else 0
    if t > p:
        return B
    else :
        return A
def check2(A,B):
    p = 0
    t = 0
    for a in A:
        p = p*10+a
    for b in B:
        t = t*10+b
    if t > p:
        return B

    else:
        return A

while b != N:

    for a in A:
        if b+ c[a] <= N:
  
            bagS[b+c[a]] = check2(bagS[b+c[a]],bagS[b]+[a])

    for i in range(b+1,N+1):
        if bagS[i]:
            b = i
            break
print("".join(list(map(str,bagS[N]))))



Fixed submission:
#coding:utf-8
N,M  = map(int,input().split())
A = list(map(int,input().split()))
b = 0
c = [0,2,5,5,4,5,6,3,7,6]
bagS = [[] for i in range(N+1)]

def check2(A,B):
    p = 0
    t = 0
    # print(A)
    # print(B)
    if len(A) > len(B):
        return A
    elif len(A) < len(B):
        return B
    for a,b in zip(A,B):
        if  a- b > 0:
            return A
        elif a-b < 0:
            return B
    return A
    # for a in A:
    #     p = p*10+a
    # for b in B:
    #     t = t*10+b
    # if t > p:
    #     return B

    # else:
    #     return A

while b != N:

    for a in A:
        if b+ c[a] <= N:
  
            bagS[b+c[a]] = check2(bagS[b+c[a]],sorted((bagS[b]+[a]),reverse=True))

    for i in range(b+1,N+1):
        if bagS[i]:
            b = i
            break
print("".join(list(map(str,bagS[N]))))

Buggy python submission (verdict: Time Limit Exceeded):
#coding:utf-8
N,M  = map(int,input().split())
A = list(map(int,input().split()))
b = 0
c = [0,2,5,5,4,5,6,3,7,6]
bagS = [[] for i in range(N+1)]
def check(A,B):#A:prev,B:temp




    As = list(map(str,sorted(A,reverse=True)))
    p = int("".join(As)) if len(A) else 0

    B.sort(reverse=True)
    Bs = list(map(str,B))
    
    t = int("".join(Bs)) if len(B) else 0
    if t > p:
        return B
    else :
        return A

while b != N:

    for a in A:
        if b+ c[a] <= N:
  
            bagS[b+c[a]] = check(bagS[b+c[a]],bagS[b]+[a])

    for i in range(b+1,N+1):
        if bagS[i]:
            b = i
            break
print("".join(list(map(str,bagS[N]))))



Fixed submission:
#coding:utf-8
N,M  = map(int,input().split())
A = list(map(int,input().split()))
b = 0
c = [0,2,5,5,4,5,6,3,7,6]
bagS = [[] for i in range(N+1)]

def check2(A,B):
    p = 0
    t = 0
    # print(A)
    # print(B)
    if len(A) > len(B):
        return A
    elif len(A) < len(B):
        return B
    for a,b in zip(A,B):
        if  a- b > 0:
            return A
        elif a-b < 0:
            return B
    return A
    # for a in A:
    #     p = p*10+a
    # for b in B:
    #     t = t*10+b
    # if t > p:
    #     return B

    # else:
    #     return A

while b != N:

    for a in A:
        if b+ c[a] <= N:
  
            bagS[b+c[a]] = check2(bagS[b+c[a]],sorted((bagS[b]+[a]),reverse=True))

    for i in range(b+1,N+1):
        if bagS[i]:
            b = i
            break
print("".join(list(map(str,bagS[N]))))

Buggy python submission (verdict: Time Limit Exceeded):
#coding:utf-8
N,M  = map(int,input().split())
A = list(map(int,input().split()))
b = 0
c = [0,2,5,5,4,5,6,3,7,6]
bagS = [[] for i in range(N+1)]
def check(A,B):#A:prev,B:temp




    As = list(map(str,sorted(A,reverse=True)))
    p = int("".join(As)) if len(A) else 0

    B.sort(reverse=True)
    Bs = list(map(str,B))
    
    t = int("".join(Bs)) if len(B) else 0
    if t > p:
        return B
    else :
        return A

while b != N:

    for a in A:
        if b+ c[a] <= N:
  
            bagS[b+c[a]] = check(bagS[b+c[a]],bagS[b]+[a])

    for i in range(b+1,N+1):
        if bagS[i]:
            b = i
            break
print("".join(list(map(str,bagS[N]))))



Fixed submission:
#coding:utf-8
N,M  = map(int,input().split())
A = list(map(int,input().split()))
b = 0
c = [0,2,5,5,4,5,6,3,7,6]
bagS = [[] for i in range(N+1)]

def check2(A,B):
    p = 0
    t = 0
    # print(A)
    # print(B)
    if len(A) > len(B):
        return A
    elif len(A) < len(B):
        return B
    for a,b in zip(A,B):
        if  a- b > 0:
            return A
        elif a-b < 0:
            return B
    return A
    # for a in A:
    #     p = p*10+a
    # for b in B:
    #     t = t*10+b
    # if t > p:
    #     return B

    # else:
    #     return A

while b != N:

    for a in A:
        if b+ c[a] <= N:
  
            bagS[b+c[a]] = check2(bagS[b+c[a]],sorted((bagS[b]+[a]),reverse=True))

    for i in range(b+1,N+1):
        if bagS[i]:
            b = i
            break
print("".join(list(map(str,bagS[N]))))