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

dp = [-1 for i in range(N+10)]
l = [0,2,5,5,4,5,6,3,7,6]
dp[0] = 0
for i in range(N-1):
    for s in A:
        if dp[i] == -1 and i!=0:
            continue
        if dp[i + l[int(s)]] < int(str(dp[i])+s):
            dp[i + l[int(s)]] = int(str(dp[i])+s)
            

print(dp[N])


                





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

dp = [-1 for i in range(N+10)]
l = [0,2,5,5,4,5,6,3,7,6]
dp[0] = 0
for i in range(N-1):
    for s in A:
        if dp[i] == -1 and i!=0:
            continue
        if dp[i + l[s]] < dp[i]*10+s:
            dp[i + l[s]] = dp[i]*10+s
print(dp[N])

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

def cmp(a,b):
    if a == None:
        return True
    elif b == None:
        return False
    if len(a) > len(b):
        return False
    elif len(a) < len(b):
        return True
    else:
        if int(a) >= int(b):
            return False
        else:
            return True

dp = [-1] * (N+10)
l = [0,2,5,5,4,5,6,3,7,6]
dp[0] = 0
for i in range(N-1):
    for s in A:
        if dp[i] == -1 and i!=0:
            continue
        if dp[i + l[int(s)]] < int(str(dp[i])+s):
            dp[i + l[int(s)]] = int(str(dp[i])+s)
            

print(dp[N])


                





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

dp = [-1 for i in range(N+10)]
l = [0,2,5,5,4,5,6,3,7,6]
dp[0] = 0
for i in range(N-1):
    for s in A:
        if dp[i] == -1 and i!=0:
            continue
        if dp[i + l[s]] < dp[i]*10+s:
            dp[i + l[s]] = dp[i]*10+s
print(dp[N])

Buggy python submission (verdict: Time Limit Exceeded):


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

def cmp(a,b):
    if a == None:
        return True
    elif b == None:
        return False
    if len(a) > len(b):
        return False
    elif len(a) < len(b):
        return True
    else:
        if int(a) >= int(b):
            return False
        else:
            return True

dp = [None] * (N+10)
l = [0,2,5,5,4,5,6,3,7,6]
dp[0] = ""
for i in range(N-1):
    for s in A:
        if dp[i] == None and i!=0:
            continue
        if cmp(dp[i + l[int(s)]] , dp[i]+s):
            dp[i + l[int(s)]] = dp[i]+s
            

print(dp[N])


                





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

dp = [-1 for i in range(N+10)]
l = [0,2,5,5,4,5,6,3,7,6]
dp[0] = 0
for i in range(N-1):
    for s in A:
        if dp[i] == -1 and i!=0:
            continue
        if dp[i + l[s]] < dp[i]*10+s:
            dp[i + l[s]] = dp[i]*10+s
print(dp[N])

Buggy python submission (verdict: Time Limit Exceeded):


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

def cmp(a,b):
    if a == None:
        return True
    elif b == None:
        return False
    if len(a) > len(b):
        return False
    elif len(a) < len(b):
        return True
    else:
        if int(a) >= int(b):
            return False
        else:
            return True

dp = [None] * (N+10)
l = [0,2,5,5,4,5,6,3,7,6]
dp[0] = ""
for i in range(N+1):
    for s in A:
        if dp[i] == None and i!=0:
            continue
        ss = "".join([dp[i],s])
        if cmp(dp[i + l[int(s)]] , ss):
            dp[i + l[int(s)]] = ss
            

print(dp[N])


                





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

dp = [-1 for i in range(N+10)]
l = [0,2,5,5,4,5,6,3,7,6]
dp[0] = 0
for i in range(N-1):
    for s in A:
        if dp[i] == -1 and i!=0:
            continue
        if dp[i + l[s]] < dp[i]*10+s:
            dp[i + l[s]] = dp[i]*10+s
print(dp[N])

Buggy python submission (verdict: Time Limit Exceeded):


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

def cmp(a,b):
    if a == None:
        return True
    elif b == None:
        return False
    if len(a) > len(b):
        return False
    elif len(a) < len(b):
        return True
    else:
        if int(a) >= int(b):
            return False
        else:
            return True

dp = [None] * (N+10)
l = [0,2,5,5,4,5,6,3,7,6]
dp[0] = ""
for i in range(N+1):
    for s in A:
        if dp[i] == None and i!=0:
            continue
        ss = dp[i] + s
        if cmp(dp[i + l[int(s)]] , ss):
            dp[i + l[int(s)]] = ss
            

print(dp[N])


                





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

dp = [-1 for i in range(N+10)]
l = [0,2,5,5,4,5,6,3,7,6]
dp[0] = 0
for i in range(N-1):
    for s in A:
        if dp[i] == -1 and i!=0:
            continue
        if dp[i + l[s]] < dp[i]*10+s:
            dp[i + l[s]] = dp[i]*10+s
print(dp[N])

Buggy python submission (verdict: Time Limit Exceeded):


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

def cmp(a,b):
    if a == "" or b == "":
        if len(a) > len(b):
            return False
        elif len(a) < len(b):
            return True
    else:
        if int(a) >= int(b):
            return False
        else:
            return True

dp = [""] * (N+10)
l = [0,2,5,5,4,5,6,3,7,6]

for i in range(N+1):
    for s in A:
        if dp[i] == "" and i!=0:
            continue
        ss = dp[i] + s
        if cmp(dp[i + l[int(s)]] , ss):
            dp[i + l[int(s)]] = ss
            

print(dp[N])


                





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

dp = [-1 for i in range(N+10)]
l = [0,2,5,5,4,5,6,3,7,6]
dp[0] = 0
for i in range(N-1):
    for s in A:
        if dp[i] == -1 and i!=0:
            continue
        if dp[i + l[s]] < dp[i]*10+s:
            dp[i + l[s]] = dp[i]*10+s
print(dp[N])

Buggy python submission (verdict: Time Limit Exceeded):


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

def cmp(a,b):
    if len(a) > len(b):
        return False
    elif len(a) < len(b):
        return True
    else:
        if int(a) >= int(b):
            return False
        else:
            return True

dp = [""] * (N+100)
l = [0,2,5,5,4,5,6,3,7,6]

for i in range(N+1):
    for s in A:
        if dp[i] == "" and i!=0:
            continue
        ss = dp[i] + s
        if cmp(dp[i + l[int(s)]] , ss):
            dp[i + l[int(s)]] = ss
            

print(dp[N])


                





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

dp = [-1 for i in range(N+10)]
l = [0,2,5,5,4,5,6,3,7,6]
dp[0] = 0
for i in range(N-1):
    for s in A:
        if dp[i] == -1 and i!=0:
            continue
        if dp[i + l[s]] < dp[i]*10+s:
            dp[i + l[s]] = dp[i]*10+s
print(dp[N])

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

def cmp(a,b):
    if len(a) > len(b):
        return False
    elif len(a) < len(b):
        return True
    else:
        if int(a) >= int(b):
            return False
        else:
            return True

dp = [""] * (N+100)
l = [0,2,5,5,4,5,6,3,7,6]

for i in range(N+1):
    for s in A:
        if dp[i] == "" and i!=0:
            continue
        if cmp(dp[i + l[int(s)]] , dp[i] + s):
            dp[i + l[int(s)]] = dp[i] + s
            
print(dp[N])


                





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

dp = [-1 for i in range(N+10)]
l = [0,2,5,5,4,5,6,3,7,6]
dp[0] = 0
for i in range(N-1):
    for s in A:
        if dp[i] == -1 and i!=0:
            continue
        if dp[i + l[s]] < dp[i]*10+s:
            dp[i + l[s]] = dp[i]*10+s
print(dp[N])

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

def cmp(a,b):
    if len(a) > len(b):
        return False
    elif len(a) < len(b):
        return True
    else:
        if int(a) >= int(b):
            return False
        else:
            return True

dp = [""] * (N+100)
l = [0,2,5,5,4,5,6,3,7,6]

for i in range(N+1):
    for s in A:
        if dp[i] == "" and i!=0:
            continue
        if cmp(dp[i + l[int(s)]] , dp[i] + s):
            dp[i + l[int(s)]] = dp[i] + s
            
print(dp[N])


                





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

dp = [-1 for i in range(N+10)]
l = [0,2,5,5,4,5,6,3,7,6]
dp[0] = 0
for i in range(N-1):
    for s in A:
        if dp[i] == -1 and i!=0:
            continue
        if dp[i + l[s]] < dp[i]*10+s:
            dp[i + l[s]] = dp[i]*10+s
print(dp[N])