Problem: Find the largest integer that can be formed with exactly N matchsticks, under the following conditions:    * Every digit in the integer must be one of the digits A_1, A_2, ..., A_M (1 \leq A_i \leq 9).   * The number of matchsticks used to form digits 1, 2, 3, 4, 5, 6, 7, 8, 9 should be 2, 5, 5, 4, 5, 6, 3, 7, 6, respectively.

Submission attempts (in order):
import math

N,M=map(int,input().split())
A=list(map(int,input().split()))
A.sort(reverse=True)
num=[0,2,5,5,4,5,6,3,7,6]
dp=[0]
keta=[0]
tmp=0
tmp1=0
tmp_keta=-1
tmp1_keta=-1
for i in range(1,N+1):
    tmp=0
    for j in A:
        tmp1=-100000000000000000000
        if(i-num[j]>=0):
            if(dp[i-num[j]]!=0):
                tmp1=dp[i-num[j]]+j*(10**keta[i-num[j]])
                tmp1_keta=keta[i-num[j]]+1
            else:
                tmp1=j
                tmp1_keta=1
            if(tmp1>tmp):
                tmp=tmp1
                tmp_keta=tmp1_keta
    dp.append(tmp)
    keta.append(tmp_keta)
ans=dp[N]
print(ans)

ort math

N,M=map(int,input().split())
A=list(map(int,input().split()))
A.sort(reverse=True)
num=[0,2,5,5,4,5,6,3,7,6]
dp=[0]
keta=[0]
tmp=0
tmp1=0
tmp_keta=-1
tmp1_keta=-1
for i in range(1,N+1):
    tmp=-100000000000000000000
    tmp_keta=-1
    for j in A:
        tmp1=-100000000000000000000
        tmp1_keta=-1
        if(i-num[j]>=0):
            if(dp[i-num[j]]!=0):
                tmp1=dp[i-num[j]]+j*(10**keta[i-num[j]])
                tmp1_keta=keta[i-num[j]]+1
            else:
                tmp1=j
                tmp1_keta=1
            if(tmp1>tmp):
                tmp=tmp1
                tmp_keta=tmp1_keta
    dp.append(tmp)
    keta.append(tmp_keta)
ans=dp[N]
print(ans)

N,M=map(int,input().split())
A=list(map(int,input().split()))
A.sort(reverse=True)
num=[0,2,5,5,4,5,6,3,7,6]
dp=[0]
tmp=0
tmp1=0
for i in range(1,N+1):
    tmp=0
    for j in A:
        tmp1=-100000000000000000000
        if(i-num[j]>=0):
            if(dp[i-num[j]]!=0):
                tmp1=dp[i-num[j]]+j*(10**len(str(dp[i-num[j]])))
            else:
                tmp1=j
            if(tmp1>tmp):
                tmp=tmp1
    dp.append(tmp)
ans=dp[N]
print(ans)

import math

N,M=map(int,input().split())
A=list(map(int,input().split()))
A.sort(reverse=True)
num=[0,2,5,5,4,5,6,3,7,6]
dp=[0]
tmp=0
tmp1=0
for i in range(1,N+1):
    tmp=0
    for j in A:
        tmp1=-100000000000000000000
        if(i-num[j]>=0):
            if(dp[i-num[j]]!=0):
                tmp1=dp[i-num[j]]+j*(10**int(math.log10(dp[i-num[j]]) + 1))
            else:
                tmp1=j
            if(tmp1>tmp):
                tmp=tmp1
    dp.append(tmp)
ans=dp[N]
print(ans)
    


N,M=map(int,input().split())
A=list(map(int,input().split()))
if(1 in A):
    if(4 in A):
        A.remove(4)
    if(9 in A):
        A.remove(9)
    if(6 in A):
        A.remove(6)
if(5 in A):
    if(3 in A):
        A.remove(3)
    if(2 in A):
        A.remove(2)
if(3 in A):
    if(2 in A):
        A.remove(2)
if(9 in A):
    if(6 in A):
        A.remove(6)
if(7 in A):
    if(9 in A):
        A.remove(9)
    if(6 in A):
        A.remove(6)
num=[0,2,5,5,4,5,6,3,7,6]
dp=[0]
keta=[0]
tmp=0
tmp1=0
tmp_keta=-1
tmp1_keta=-1
for i in range(1,N+1):
    tmp=-1
    tmp_keta=-1
    for j in A:
        tmp1=-1
        tmp1_keta=-1
        if(i-num[j]>=0):
            if(dp[i-num[j]]>0):
                tmp1=dp[i-num[j]]+j*(10**keta[i-num[j]])
                tmp1_keta=keta[i-num[j]]+1
            elif(dp[i-num[j]]==0):
                tmp1=j
                tmp1_keta=1

            if(tmp1>tmp):
                tmp=tmp1
                tmp_keta=tmp1_keta
                
    dp.append(tmp)
    keta.append(tmp_keta)
print(dp[N])

Verdicts: Wrong Answer

Runtime Error

Wrong Answer

Wrong Answer

Accepted