Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

N, M = map(int, input().split())
A = list(map(int, input().split()))

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

dic = {}
for a in A:
    dic[COUNT[a]] = a

C = sorted(dic.keys(), key=lambda x: -dic[x])
min_c = min(C)
C.remove(min_c)
#print(C)

def dfs(remain, l, now, L):
    #print(remain, l, now)
    if remain == 0:
        L.append(now)
        return L
    if l == len(C):
        return L
    loop = remain//(C[l]-min_c)
    for h in reversed(range(loop+1)):
        L = dfs(remain-(C[l]-min_c)*h, l+1, now+[dic[C[l]]]*h, L)
    return L


k = 0
while True:
    r = N%min_c + k*min_c
    p = N//min_c - k
    if r == 0:
        ans = {dic[min_c]: p}
        break
    L = dfs(r, 0, [], [])
    #print(L)
    ans = []
    for now in L:
        l = len(now)
        if l > p: continue
        P = sorted(now+[dic[min_c]]*(p-l), reverse=True)
        #print(P)
        if not ans:
            ans = P
            continue
        ok = True
        for i in range(p):
            if P[i] < ans[i]:
                ok = False
                break
            elif P[i] > ans[i]:
                ok = True
                break
        if ok:
            ans = P

    if ans: break
    k += 1

ans.sort(reverse=True)
print("".join([str(a) for a in ans]))

Fixed submission:
import sys
input = sys.stdin.readline

NUM = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
INF = 10**14

N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort(reverse=True)

dp = [-INF]*(N+1)
dp[0] = 0
for i in range(N):
    for a in A:
        c = NUM[a]
        if i + c <= N:
            dp[i+c] = max(dp[i+c], dp[i]+1)

D = dp[N]
ind = N
ans = ''
for _ in range(D):
    for a in A:
        c = NUM[a]
        if ind - c < 0: continue
        if dp[ind] == dp[ind-c] + 1:
            ind -= c
            ans += str(a)
            break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

N, M = map(int, input().split()) 
A = list(map(int, input().split()))

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

B = {}
for a in A:
    ind = seg[a]
    if not ind in B.keys():
        B[ind] = a
    else:
        if B[ind] < a:
            B[ind] = a

I = sorted(list(B.keys()))
#print(I)
Ap = []
def search(ind, remain, ret, l, maxl):
    if ind >= len(I):
        return False, ret, l
    #print(I[ind], remain, ret)
    a = I[ind]
    p = remain // a
    r = remain % a 
    if p == 0:
        return False, ret, l
    if r == 0:
        ret.append((a, p))
        l += p
        return True, ret, l
    for q in range(p-1, -1, -1):
        nowl = l
        r += a
        oldr = ret[:]
        ok, newr, newl = search(ind+1, r, oldr, nowl, maxl)
        if ok:
            newl += q
            newr.append((a, q))
            if ind == 0:
                if newl >= maxl:
                    maxl = newl
                    Ap.append(newr)
                continue
            return True, newr, newl
    if ind == 0:
        return False, Ap, l
    return False, ret, l

def main():

    only, Ls, l = search(0, N, [], 0, 0)
    #print(Ls)
    if only:
        Q = []
        for k, v in Ls:
            Q.extend([B[k]]*v)
        Q.sort(reverse=True)
        print(''.join([str(q) for q in Q]))
    else:
        anss = []
        for i, L in enumerate(Ls):
            P = []
            for k, v in L:
                P.append((B[k], v))
            P.sort(reverse=True)
            if i == 0:
                maxp = P[0][1]
            elif P[0][1] >= maxp:
                maxp = P[0][1]
            else:
                continue
            Q = ''
            for p, v in P:
                Q = Q + str(p)*v
            num = int(Q)
            anss.append(num)

        print(max(anss))

if __name__ == "__main__":
    main()

Fixed submission:
import sys
input = sys.stdin.readline

NUM = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
INF = 10**14

N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort(reverse=True)

dp = [-INF]*(N+1)
dp[0] = 0
for i in range(N):
    for a in A:
        c = NUM[a]
        if i + c <= N:
            dp[i+c] = max(dp[i+c], dp[i]+1)

D = dp[N]
ind = N
ans = ''
for _ in range(D):
    for a in A:
        c = NUM[a]
        if ind - c < 0: continue
        if dp[ind] == dp[ind-c] + 1:
            ind -= c
            ans += str(a)
            break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

N, M = map(int, input().split()) 
A = list(map(int, input().split()))

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

B = {}
for a in A:
    ind = seg[a]
    if not ind in B.keys():
        B[ind] = a
    else:
        if B[ind] < a:
            B[ind] = a

I = sorted(list(B.keys()))
#print(I)
Ap = []
def search(ind, remain, ret, l, maxl):
    if ind >= len(I):
        return False, ret, l
    #print(I[ind], remain, ret)
    a = I[ind]
    p = remain // a
    r = remain % a 
    if p == 0:
        return False, ret, l
    if r == 0:
        ret.append((a, p))
        l += p
        return True, ret, l
    for q in range(p-1, -1, -1):
        nowl = l
        r += a
        oldr = ret[:]
        ok, newr, newl = search(ind+1, r, oldr, nowl, maxl)
        if ok:
            newl += q
            newr.append((a, q))
            if ind == 0:
                if newl >= maxl:
                    maxl = newl
                    Ap.append(newr)
                continue
            return True, newr, newl
    if ind == 0:
        return False, Ap, l
    return False, ret, l

def main():

    only, Ls, l = search(0, N, [], 0, 0)
    #print(Ls)
    if only:
        Q = []
        for k, v in Ls:
            Q.extend([B[k]]*v)
        Q.sort(reverse=True)
        print(''.join([str(q) for q in Q]))
    else:
        anss = []
        for i, L in enumerate(Ls):
            P = []
            for k, v in L:
                P.append((B[k], v))
            P.sort(reverse=True)
            if i == 0:
                maxp = P[0][1]
            elif P[0][1] >= maxp:
                maxp = P[0][1]
            else:
                continue
            Q = []
            for p, v in P:
                Q.extend([p]*v)
            num = int(''.join([str(q) for q in Q]))
            anss.append(num)

        print(max(anss))

if __name__ == "__main__":
    main()

Fixed submission:
import sys
input = sys.stdin.readline

NUM = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
INF = 10**14

N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort(reverse=True)

dp = [-INF]*(N+1)
dp[0] = 0
for i in range(N):
    for a in A:
        c = NUM[a]
        if i + c <= N:
            dp[i+c] = max(dp[i+c], dp[i]+1)

D = dp[N]
ind = N
ans = ''
for _ in range(D):
    for a in A:
        c = NUM[a]
        if ind - c < 0: continue
        if dp[ind] == dp[ind-c] + 1:
            ind -= c
            ans += str(a)
            break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

N, M = map(int, input().split()) 
A = list(map(int, input().split()))

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

B = {}
for a in A:
    ind = seg[a]
    if not ind in B.keys():
        B[ind] = a
    else:
        if B[ind] < a:
            B[ind] = a

I = sorted(list(B.keys()))
#print(I)
Ap = []
def search(ind, remain, ret, l, maxl):
    if ind >= len(I):
        return False, ret, l
    #print(I[ind], remain, ret)
    a = I[ind]
    p = remain // a
    r = remain % a 
    if p == 0:
        return False, ret, l
    if r == 0:
        ret.append((a, p))
        l += p
        return True, ret, l
    for q in range(p-1, -1, -1):
        nowl = l
        r += a
        oldr = ret[:]
        ok, newr, newl = search(ind+1, r, oldr, nowl, maxl)
        if ok:
            newl += q
            newr.append((a, q))
            if ind == 0:
                if newl >= maxl:
                    maxl = newl
                    Ap.append(newr)
                continue
            return True, newr, newl
    if ind == 0:
        return False, Ap, l
    return False, ret, l

def main():

    only, Ls, l = search(0, N, [], 0, 0)
    #print(Ls)
    if only:
        Q = []
        for k, v in Ls:
            Q.extend([B[k]]*v)
        Q.sort(reverse=True)
        print(''.join([str(q) for q in Q]))
    else:
        anss = []
        for i, L in enumerate(Ls):
            P = []
            for k, v in L:
                P.append((B[k], v))
            P.sort(reverse=True)
            if i == 0:
                maxp = P[0][0]
            elif P[0][0] >= maxp:
                maxp = P[0][0]
            else:
                continue
            Q = []
            for p, v in P:
                Q.extend([p]*v)
            num = int(''.join([str(q) for q in Q]))
            anss.append(num)

        print(max(anss))

if __name__ == "__main__":
    main()

Fixed submission:
import sys
input = sys.stdin.readline

NUM = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
INF = 10**14

N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort(reverse=True)

dp = [-INF]*(N+1)
dp[0] = 0
for i in range(N):
    for a in A:
        c = NUM[a]
        if i + c <= N:
            dp[i+c] = max(dp[i+c], dp[i]+1)

D = dp[N]
ind = N
ans = ''
for _ in range(D):
    for a in A:
        c = NUM[a]
        if ind - c < 0: continue
        if dp[ind] == dp[ind-c] + 1:
            ind -= c
            ans += str(a)
            break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

N, M = map(int, input().split()) 
A = list(map(int, input().split()))

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

B = {}
for a in A:
    ind = seg[a]
    if not ind in B.keys():
        B[ind] = a
    else:
        if B[ind] < a:
            B[ind] = a

I = sorted(list(B.keys()))
#print(I)
Ap = []
def search(ind, remain, ret, l, maxl):
    if ind >= len(I):
        return False, ret, l
    #print(I[ind], remain, ret)
    a = I[ind]
    p = remain // a
    r = remain % a 
    if p == 0:
        return False, ret, l
    if r == 0:
        ret.append((a, p))
        l += p
        return True, ret, l
    for q in range(p-1, -1, -1):
        nowl = l
        r += a
        oldr = ret[:]
        ok, newr, newl = search(ind+1, r, oldr, nowl, maxl)
        if ok:
            newl += q
            newr.append((a, q))
            if ind == 0:
                if newl >= maxl:
                    maxl = newl
                    Ap.append(newr)
                continue
            return True, newr, newl
    if ind == 0:
        return False, Ap, l
    return False, ret, l

def main():

    only, Ls, l = search(0, N, [], 0, 0)
    #print(Ls)
    if only:
        Q = []
        for k, v in Ls:
            Q.extend([B[k]]*v)
        Q.sort(reverse=True)
        print(''.join([str(q) for q in Q]))
    else:
        anss = []
        for L in Ls:
            P = []
            for k, v in L:
                P.append((B[k], v))
            P.sort(reverse=True)
            Q = []
            for p, v in P:
                Q.extend([p]*v)
            num = int(''.join([str(q) for q in Q]))
            anss.append(num)

        print(max(anss))

if __name__ == "__main__":
    main()

Fixed submission:
import sys
input = sys.stdin.readline

NUM = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
INF = 10**14

N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort(reverse=True)

dp = [-INF]*(N+1)
dp[0] = 0
for i in range(N):
    for a in A:
        c = NUM[a]
        if i + c <= N:
            dp[i+c] = max(dp[i+c], dp[i]+1)

D = dp[N]
ind = N
ans = ''
for _ in range(D):
    for a in A:
        c = NUM[a]
        if ind - c < 0: continue
        if dp[ind] == dp[ind-c] + 1:
            ind -= c
            ans += str(a)
            break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

N, M = map(int, input().split()) 
A = list(map(int, input().split()))

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

B = {}
for a in A:
    ind = seg[a]
    if not ind in B.keys():
        B[ind] = a
    else:
        if B[ind] < a:
            B[ind] = a

I = sorted(list(B.keys()))
#print(I)
Ap = []
def search(ind, remain, ret, l, maxl):
    if ind >= len(I):
        return False, ret, l
    #print(I[ind], remain, ret)
    a = I[ind]
    p = remain // a
    r = remain % a 
    if p == 0:
        return False, ret, l
    if r == 0:
        ret.append((a, p))
        l += p
        return True, ret, l
    for q in range(p-1, -1, -1):
        nowl = l
        r += a
        oldr = ret[:]
        ok, newr, newl = search(ind+1, r, oldr, nowl, maxl)
        if ok:
            newl += q
            newr.append((a, q))
            if ind == 0:
                if newl >= maxl:
                    maxl = newl
                    Ap.append(newr)
                continue
            return True, newr, newl
    if ind == 0:
        return False, Ap, l
    return False, ret, l

def main():

    only, Ls, l = search(0, N, [], 0, 0)
    #print(Ls)
    if only:
        Q = []
        for k, v in Ls:
            Q.extend([B[k]]*v)
        Q.sort(reverse=True)
        print(''.join([str(q) for q in Q]))
    else:
        anss = []
        for L in Ls:
            Q = []
            for k, v in L:
                Q.extend([B[k]]*v)
            Q.sort(reverse=True)
            num = int(''.join([str(q) for q in Q]))
            anss.append(num)

        print(max(anss))

if __name__ == "__main__":
    main()

Fixed submission:
import sys
input = sys.stdin.readline

NUM = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
INF = 10**14

N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort(reverse=True)

dp = [-INF]*(N+1)
dp[0] = 0
for i in range(N):
    for a in A:
        c = NUM[a]
        if i + c <= N:
            dp[i+c] = max(dp[i+c], dp[i]+1)

D = dp[N]
ind = N
ans = ''
for _ in range(D):
    for a in A:
        c = NUM[a]
        if ind - c < 0: continue
        if dp[ind] == dp[ind-c] + 1:
            ind -= c
            ans += str(a)
            break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

N, M = map(int, input().split()) 
A = list(map(int, input().split()))

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

B = {}
for a in A:
    ind = seg[a]
    if not ind in B.keys():
        B[ind] = a
    else:
        if B[ind] < a:
            B[ind] = a

I = sorted(list(B.keys()))
#print(I)
Ap = []
def search(ind, remain, ret, l, maxl):
    if ind >= len(I):
        return False, ret, l
    #print(I[ind], remain, ret)
    a = I[ind]
    p = remain // a
    r = remain % a 
    if p == 0:
        return False, ret, l
    if r == 0:
        ret.append((a, p))
        l += p
        return True, ret, l
    for q in range(p-1, -1, -1):
        nowl = l
        r += a
        oldr = ret[:]
        ok, newr, newl = search(ind+1, r, oldr, nowl, maxl)
        if ok:
            newl += q
            newr.append((a, q))
            if ind == 0:
                if newl >= maxl:
                    maxl = newl
                    Ap.append(newr)
                continue
            return True, newr, newl
    if ind == 0:
        return False, Ap, l
    return False, ret, l

def main():

    only, Ls, l = search(0, N, [], 0, 0)
    #print(Ls)
    if only:
        Q = []
        for k, v in Ls:
            Q.extend([B[k]]*v)
        Q.sort(reverse=True)
        print(''.join([str(q) for q in Q]))
    else:
        anss = []
        for L in Ls:
            Q = []
            for k, v in L:
                Q.extend([B[k]]*v)
            Q.sort(reverse=True)
            num = int(''.join([str(q) for q in Q]))
            anss.append(num)

        print(max(anss))

if __name__ == "__main__":
    main()

Fixed submission:
import sys
input = sys.stdin.readline

NUM = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
INF = 10**14

N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort(reverse=True)

dp = [-INF]*(N+1)
dp[0] = 0
for i in range(N):
    for a in A:
        c = NUM[a]
        if i + c <= N:
            dp[i+c] = max(dp[i+c], dp[i]+1)

D = dp[N]
ind = N
ans = ''
for _ in range(D):
    for a in A:
        c = NUM[a]
        if ind - c < 0: continue
        if dp[ind] == dp[ind-c] + 1:
            ind -= c
            ans += str(a)
            break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split()) 
A = list(map(int, input().split()))

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

B = {}
for a in A:
    ind = seg[a]
    if not ind in B.keys():
        B[ind] = a
    else:
        if B[ind] < a:
            B[ind] = a

I = sorted(list(B.keys()))
#print(I)
def search(ind, remain, ret):
    if ind >= len(I):
        return False, ret
    #print(I[ind], remain, ret)
    a = I[ind]
    p = remain // a
    r = remain % a 
    if p == 0:
        return False, ret
    if r == 0:
        ret.append((a, p))
        return True, ret
    for q in range(p-1, -1, -1):
        r += a
        oldr = ret[:]
        ok, newr = search(ind+1, r, oldr)
        if ok:
            newr.append((a, q))
            if ind == 0:
                Ap.append(newr)
                continue
            return True, newr
    if ind == 0:
        return False, Ap
    return False, ret

Ap = []
only, Ls = search(0, N, [])
#print(Ls)
if only:
    Q = []
    for k, v in Ls:
        Q.extend([B[k]]*v)
    Q.sort(reverse=True)
    print(''.join([str(q) for q in Q]))
else:
    anss = []
    for L in Ls:
        Q = []
        for k, v in L:
            Q.extend([B[k]]*v)
        Q.sort(reverse=True)
        num = int(''.join([str(q) for q in Q]))
        anss.append(num)

    print(max(anss))

Fixed submission:
import sys
input = sys.stdin.readline

NUM = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
INF = 10**14

N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort(reverse=True)

dp = [-INF]*(N+1)
dp[0] = 0
for i in range(N):
    for a in A:
        c = NUM[a]
        if i + c <= N:
            dp[i+c] = max(dp[i+c], dp[i]+1)

D = dp[N]
ind = N
ans = ''
for _ in range(D):
    for a in A:
        c = NUM[a]
        if ind - c < 0: continue
        if dp[ind] == dp[ind-c] + 1:
            ind -= c
            ans += str(a)
            break
print(ans)

Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split()) 
A = list(map(int, input().split()))

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

B = {}
for a in A:
    ind = seg[a]
    if not ind in B.keys():
        B[ind] = a
    else:
        if B[ind] < a:
            B[ind] = a

I = sorted(list(B.keys()))
#print(I)
def search(ind, remain, ret):
    if ind >= len(I):
        return False, ret
    #print(I[ind], remain, ret)
    a = I[ind]
    p = remain // a
    r = remain % a 
    if p == 0:
        return False, ret
    if r == 0:
        ret.append((a, p))
        return True, ret
    for q in range(p-1, -1, -1):
        r += a
        oldr = ret[:]
        ok, newr = search(ind+1, r, oldr)
        if ok:
            newr.append((a, q))
            if ind == 0:
                Ap.append(newr)
                continue
            return True, newr
    if ind == 0:
        return Ap
    return False, ret

Ap = []
Ls = search(0, N, [])
#print(Ls)
anss = []
for L in Ls:
    Q = []
    for k, v in L:
        Q.extend([B[k]]*v)
    Q.sort(reverse=True)
    num = int(''.join([str(q) for q in Q]))
    anss.append(num)

print(max(anss))

Fixed submission:
import sys
input = sys.stdin.readline

NUM = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
INF = 10**14

N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort(reverse=True)

dp = [-INF]*(N+1)
dp[0] = 0
for i in range(N):
    for a in A:
        c = NUM[a]
        if i + c <= N:
            dp[i+c] = max(dp[i+c], dp[i]+1)

D = dp[N]
ind = N
ans = ''
for _ in range(D):
    for a in A:
        c = NUM[a]
        if ind - c < 0: continue
        if dp[ind] == dp[ind-c] + 1:
            ind -= c
            ans += str(a)
            break
print(ans)