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()))
a.sort()
a.reverse()
lis = [2,5,5,4,5,6,3,7,6]
mat=[]
for num in a:
    mat.append([num,lis[num-1]])

def new_val(num1,num2): # 123 , 4 -> 4123
    if num1<0:
        return -(10**10)
    if num1==0:
        return num2

    return int(str(num1)+str(num2))

dp = [[-(10**10) for i in range(1+n)] for j in range(1+m)]
for i in range(m):
    dp[i][0]=0

for i in range(m):
    num=mat[i][0]
    honsu=mat[i][1]
    for j in range(1+n):
        if j<honsu:
            dp[i+1][j] = dp[i][j]
        else:
            dp[i+1][j] = max(new_val(dp[i+1][j-honsu] , num),dp[i][j])
print(dp[-1][-1])

Fixed submission:
n,m = map(int, input().split())
a = list(map(int, input().split()))
a.sort(reverse=True)
lis = [2,5,5,4,5,6,3,7,6]
def new_val(num1,num2): # 123 , 4 -> 1234
    if num1<0:
        return -(10**10)
    if num1==0:
        return num2
    ret = (10**len(str(num2)))*num1 + num2
    return ret

dp = [[-(10**10) for i in range(1+n)] for j in range(1+m)]
for i in range(m):
    dp[i][0]=0

for i in range(m):
    num=a[i]
    honsu=lis[a[i]-1]
    for j in range(1+n):
        if j<honsu:
            dp[i+1][j] = dp[i][j]
        else:
            dp[i+1][j] = max(new_val(dp[i+1][j-honsu] , num),dp[i][j])
print(dp[-1][-1])

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()))
a.sort()
a.reverse()
lis = [2,5,5,4,5,6,3,7,6]
mat=[]
for num in a:
    mat.append([num,lis[num-1]])

def new_val(num1,num2): # 123 , 4 -> 4123
    if num1<0:
        return -(10**10)
    if num1==0:
        return num2

    return int(str(num1)+str(num2))

dp = [[-(10**10) for i in range(1+n)] for j in range(1+m)]
for i in range(m):
    dp[i][0]=0

for i in range(m):
    num=mat[i][0]
    honsu=mat[i][1]
    for j in range(1+n):
        if j<honsu:
            dp[i+1][j] = dp[i][j]
        else:
            dp[i+1][j] = max(new_val(dp[i+1][j-honsu] , num),dp[i][j])
print(dp[-1][-1])

Fixed submission:
n,m = map(int, input().split())
a = list(map(int, input().split()))
a.sort(reverse=True)
lis = [2,5,5,4,5,6,3,7,6]
def new_val(num1,num2): # 123 , 4 -> 1234
    if num1<0:
        return -(10**10)
    if num1==0:
        return num2
    ret = (10**len(str(num2)))*num1 + num2
    return ret

dp = [[-(10**10) for i in range(1+n)] for j in range(1+m)]
for i in range(m):
    dp[i][0]=0

for i in range(m):
    num=a[i]
    honsu=lis[a[i]-1]
    for j in range(1+n):
        if j<honsu:
            dp[i+1][j] = dp[i][j]
        else:
            dp[i+1][j] = max(new_val(dp[i+1][j-honsu] , num),dp[i][j])
print(dp[-1][-1])

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
a.reverse()
lis = [2,5,5,4,5,6,3,7,6]
mat=[]
for num in a:
    mat.append([num,lis[num-1]])

def new_val(num1,num2): # 123 , 4 -> 4123
    if num1<0:
        return -(10**10)
    if num1==0:
        return num2
    return int(str(num1)+str(num2))

dp = [[-(10**10) for i in range(1+n)] for j in range(1+m)]
for i in range(m):
    dp[i][0]=0

for i in range(m):
    num=mat[i][0]
    honsu=mat[i][1]
    for j in range(1+n):
        if j<honsu:
            dp[i+1][j] = dp[i][j]
        else:
            dp[i+1][j] = max(new_val(dp[i+1][j-honsu] , num),dp[i][j])
print(dp[-1][-1])

Fixed submission:
n,m = map(int, input().split())
a = list(map(int, input().split()))
a.sort(reverse=True)
lis = [2,5,5,4,5,6,3,7,6]
def new_val(num1,num2): # 123 , 4 -> 1234
    if num1<0:
        return -(10**10)
    if num1==0:
        return num2
    ret = (10**len(str(num2)))*num1 + num2
    return ret

dp = [[-(10**10) for i in range(1+n)] for j in range(1+m)]
for i in range(m):
    dp[i][0]=0

for i in range(m):
    num=a[i]
    honsu=lis[a[i]-1]
    for j in range(1+n):
        if j<honsu:
            dp[i+1][j] = dp[i][j]
        else:
            dp[i+1][j] = max(new_val(dp[i+1][j-honsu] , num),dp[i][j])
print(dp[-1][-1])

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
a.reverse()
lis = [2,5,5,4,5,6,3,7,6]
mat=[]
for num in a:
    mat.append([num,lis[num-1]])

def new_val(num1,num2): # 123 , 4 -> 4123
    if num1<0:
        return -(10**10)
    if num1==0:
        return num2
    return int(str(num1)+str(num2))

dp = [[-(10**10) for i in range(1+n)] for j in range(1+m)]
for i in range(m):
    dp[i][0]=0

for i in range(m):
    num=mat[i][0]
    honsu=mat[i][1]
    for j in range(1+n):
        if j<honsu:
            dp[i+1][j] = dp[i][j]
        else:
            dp[i+1][j] = max(new_val(dp[i+1][j-honsu] , num),dp[i][j])
print(dp[-1][-1])

Fixed submission:
n,m = map(int, input().split())
a = list(map(int, input().split()))
a.sort(reverse=True)
lis = [2,5,5,4,5,6,3,7,6]
def new_val(num1,num2): # 123 , 4 -> 1234
    if num1<0:
        return -(10**10)
    if num1==0:
        return num2
    ret = (10**len(str(num2)))*num1 + num2
    return ret

dp = [[-(10**10) for i in range(1+n)] for j in range(1+m)]
for i in range(m):
    dp[i][0]=0

for i in range(m):
    num=a[i]
    honsu=lis[a[i]-1]
    for j in range(1+n):
        if j<honsu:
            dp[i+1][j] = dp[i][j]
        else:
            dp[i+1][j] = max(new_val(dp[i+1][j-honsu] , num),dp[i][j])
print(dp[-1][-1])

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int, input().split())
a = list(map(int, input().split()))
lis = [2,5,5,4,5,6,3,7,6]
mat=[]
for num in a:
    mat.append([num,lis[num-1]])

def new_val(num1,num2): # 123 , 4 -> 4123
    if num1<0:
        return -(10**10)
    if num1==0:
        return num2
    return max(int(str(num2)+str(num1)),int(str(num1)+str(num2)))

dp = [[-(10**10) for i in range(1+n)] for j in range(1+m)]
for i in range(m):
    dp[i][0]=0

for i in range(m):
    num=mat[i][0]
    honsu=mat[i][1]
    for j in range(1+n):
        if j<honsu:
            dp[i+1][j] = dp[i][j]
        else:
            dp[i+1][j] = max(new_val(dp[i+1][j-honsu] , num),dp[i][j])
print(dp[-1][-1])

Fixed submission:
n,m = map(int, input().split())
a = list(map(int, input().split()))
a.sort(reverse=True)
lis = [2,5,5,4,5,6,3,7,6]
def new_val(num1,num2): # 123 , 4 -> 1234
    if num1<0:
        return -(10**10)
    if num1==0:
        return num2
    ret = (10**len(str(num2)))*num1 + num2
    return ret

dp = [[-(10**10) for i in range(1+n)] for j in range(1+m)]
for i in range(m):
    dp[i][0]=0

for i in range(m):
    num=a[i]
    honsu=lis[a[i]-1]
    for j in range(1+n):
        if j<honsu:
            dp[i+1][j] = dp[i][j]
        else:
            dp[i+1][j] = max(new_val(dp[i+1][j-honsu] , num),dp[i][j])
print(dp[-1][-1])