Problem:


Score : 400 points

Problem Statement

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.

Constraints

All values in input are integers.

2 \leq N \leq 10^4

1 \leq M \leq 9

1 \leq A_i \leq 9

A_i are all different.

There exists an integer that can be formed by exactly N matchsticks under the conditions.

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 ... A_M

Output

Print the largest integer that can be formed with exactly N matchsticks under the conditions in the problem statement.

Sample Input 1

20 4
3 7 8 4

Sample Output 1

777773

The integer 777773 can be formed with 3 + 3 + 3 + 3 + 3 + 5 = 20 matchsticks, and this is the largest integer that can be formed by 20 matchsticks under the conditions.

Sample Input 2

101 9
9 8 7 6 5 4 3 2 1

Sample Output 2

71111111111111111111111111111111111111111111111111

The output may not fit into a 64-bit integer type.

Sample Input 3

15 3
5 4 6

Sample Output 3

654



Sample input:
20 4
3 7 8 4


Sample output:
777773

Submission status: Runtime Error

Solution code:
#import decimal
#decimal.getcontext().prec==500 Floatの桁数を500まで増やす
#Decimal(c).sqrt() それで平方根を求める
#Decimal(1) / Decimal(7) 1/7

from collections import *
from itertools import *
from bisect import *
from heapq import *
import copy
import math
from fractions import gcd
import sys
#input = sys.stdin.readline

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

data=[]
for i,a in enumerate([2,5,5,4,5,6,3,7,6]):
    if i+1 in A:
        data.append([a,i+1])

Motodic={}
for a,i in reversed(data):
    Motodic[i]=0
dp=[copy.copy(Motodic) for i in range(N+1)]

def main(dic3,dic4):
    dic1=sorted([[str(i),dic3[i]] for i in dic3],key=lambda x:x[0],reverse=True)
    dic2=sorted([[str(i),dic4[i]] for i in dic4],key=lambda x:x[0],reverse=True)
    for a,b in zip(dic1,dic2):
        if a[1]>b[1]:
            return(0)
        if a[1]<b[1]:
            return(1)
    return(0)

def _sum(dic):
        count=0
        for i in dic:
            count+=dic[i]

        return(count)

for a,n in data:
    tempdic=copy.copy(Motodic)
    tempdic[n]+=1
    flag=main(dp[a],tempdic)
    if flag:
        dp[a]=tempdic
    try:
        for i in range(len(dp)):
            if i-a>=0 and _sum(dp[i-a]):

                if _sum(dp[i])<_sum(dp[i-a])+1:
                    try:
                        tempdic=copy.copy(dp[i-a])
                        tempdic[n]+=1
                        dp[i]=tempdic
                    except:
                        pass
                elif _sum(dp[i])==_sum(dp[i-a])+1:
                    try:
                        dp[i-a][n]+=1
                        flag=main(dp[i],dp[i-a])
                        dp[i-a][n]-=1
                        if flag:
                            tempdic=copy.copy(dp[i-a])
                            tempdic[n]+=1
                            dp[i]=tempdic
                    except:
                        pass
    except:
        pass

dic=dp[N]
a=""
dic=sorted([[str(i),dic[i]] for i in dic],key=lambda x:x[0],reverse=True)
for i,z in dic:
    for m in range(z):
        a+=i
print(a)

Submission status: Runtime Error

Solution code:
#import decimal
#decimal.getcontext().prec==500 Floatの桁数を500まで増やす
#Decimal(c).sqrt() それで平方根を求める
#Decimal(1) / Decimal(7) 1/7

from collections import *
from itertools import *
from bisect import *
from heapq import *
import copy
import math
from fractions import gcd
import sys
#input = sys.stdin.readline

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

data=[]
for i,a in enumerate([2,5,5,4,5,6,3,7,6]):
    if i+1 in A:
        data.append([a,i+1])

Motodic={}
for a,i in reversed(data):
    Motodic[i]=0
dp=[copy.copy(Motodic) for i in range(N+1)]

def main(dic3,dic4):
    dic1=sorted([[str(i),dic3[i]] for i in dic3],key=lambda x:x[0],reverse=True)
    dic2=sorted([[str(i),dic4[i]] for i in dic4],key=lambda x:x[0],reverse=True)
    for a,b in zip(dic1,dic2):
        if a[1]>b[1]:
            return(0)
        if a[1]<b[1]:
            return(1)
    return(0)

def _sum(dic):
        count=0
        for i in dic:
            count+=dic[i]

        return(count)

for a,n in data:
    tempdic=copy.copy(Motodic)
    tempdic[n]+=1
    flag=main(dp[a],tempdic)
    if flag:
        dp[a]=tempdic

    for i in range(len(dp)):
        if i-a>=0 and _sum(dp[i-a]):

            if _sum(dp[i])<_sum(dp[i-a])+1:
                try:
                    tempdic=copy.copy(dp[i-a])
                    tempdic[n]+=1
                    dp[i]=tempdic
                except:
                    pass
            elif _sum(dp[i])==_sum(dp[i-a])+1:
                try:
                    dp[i-a][n]+=1
                    flag=main(dp[i],dp[i-a])
                    dp[i-a][n]-=1
                    if flag:
                        tempdic=copy.copy(dp[i-a])
                        tempdic[n]+=1
                        dp[i]=tempdic
                except:
                    pass
dic=dp[N]
a=""
dic=sorted([[str(i),dic[i]] for i in dic],key=lambda x:x[0],reverse=True)
for i,z in dic:
    for m in range(z):
        a+=i
print(a)

Submission status: Runtime Error

Solution code:
#import decimal
#decimal.getcontext().prec==500 Floatの桁数を500まで増やす
#Decimal(c).sqrt() それで平方根を求める
#Decimal(1) / Decimal(7) 1/7

from collections import *
from itertools import *
from bisect import *
from heapq import *
import copy
import math
from fractions import gcd
import sys
#input = sys.stdin.readline

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

data=[]
for i,a in enumerate([2,5,5,4,5,6,3,7,6]):
    if i+1 in A:
        data.append([a,i+1])

Motodic={}
for a,i in reversed(data):
    Motodic[i]=0
dp=[copy.copy(Motodic) for i in range(N+1)]

def main(dic3,dic4):
    dic1=sorted([[str(i),dic3[i]] for i in dic3],key=lambda x:x[0],reverse=True)
    dic2=sorted([[str(i),dic4[i]] for i in dic4],key=lambda x:x[0],reverse=True)
    for a,b in zip(dic1,dic2):
        if a[1]>b[1]:
            return(0)
        if a[1]<b[1]:
            return(1)
    return(0)

def _sum(dic):
        count=0
        for i in dic:
            count+=dic[i]

        return(count)

for a,n in data:
    tempdic=copy.copy(Motodic)
    tempdic[n]+=1
    flag=main(dp[a],tempdic)
    if flag:
        dp[a]=tempdic

    for i in range(len(dp)):
        if i-a>=0 and _sum(dp[i-a]):

            if _sum(dp[i])<_sum(dp[i-a])+1:
                try:
                    tempdic=copy.copy(dp[i-a])
                    tempdic[n]+=1
                    dp[i]=tempdic
                except:
                    pass
            elif _sum(dp[i])==_sum(dp[i-a])+1:

                dp[i-a][n]+=1
                flag=main(dp[i],dp[i-a])
                dp[i-a][n]-=1
                if flag:
                    tempdic=copy.copy(dp[i-a])
                    tempdic[n]+=1
                    dp[i]=tempdic
dic=dp[N]
a=""
dic=sorted([[str(i),dic[i]] for i in dic],key=lambda x:x[0],reverse=True)
for i,z in dic:
    for m in range(z):
        a+=i
print(a)

Submission status: Runtime Error

Solution code:
#import decimal
#decimal.getcontext().prec==500 Floatの桁数を500まで増やす
#Decimal(c).sqrt() それで平方根を求める
#Decimal(1) / Decimal(7) 1/7

from collections import *
from itertools import *
from bisect import *
from heapq import *
import copy
import math
from fractions import gcd
import sys
#input = sys.stdin.readline

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

data=[]
for i,a in enumerate([2,5,5,4,5,6,3,7,6]):
    if i+1 in A:
        data.append([a,i+1])

Motodic={}
for a,i in reversed(data):
    Motodic[i]=0
dp=[copy.copy(Motodic) for i in range(N+1)]

def main(dic3,dic4):
    dic1=sorted([[str(i),dic3[i]] for i in dic3],key=lambda x:x[0],reverse=True)
    dic2=sorted([[str(i),dic4[i]] for i in dic4],key=lambda x:x[0],reverse=True)
    for a,b in zip(dic1,dic2):
        if a[1]>b[1]:
            return(0)
        if a[1]<b[1]:
            return(1)
    return(0)

def _sum(dic):
    try:
        count=0
        for i in dic:
            count+=dic[i]

        return(count)
    except:
        return(0)


for a,n in data:
    tempdic=copy.copy(Motodic)
    tempdic[n]+=1
    flag=main(dp[a],tempdic)
    if flag:
        dp[a]=tempdic

    for i in range(len(dp)):
        if i-a>=0 and _sum(dp[i-a]):
            if _sum(dp[i])<_sum(dp[i-a])+1:

                tempdic=copy.copy(dp[i-a])
                tempdic[n]+=1
                dp[i]=tempdic
            elif _sum(dp[i])==_sum(dp[i-a])+1:

                dp[i-a][n]+=1
                flag=main(dp[i],dp[i-a])
                dp[i-a][n]-=1
                if flag:
                    tempdic=copy.copy(dp[i-a])
                    tempdic[n]+=1
                    dp[i]=tempdic
dic=dp[N]
a=""
dic=sorted([[str(i),dic[i]] for i in dic],key=lambda x:x[0],reverse=True)
for i,z in dic:
    for m in range(z):
        a+=i
print(a)

Submission status: Runtime Error

Solution code:
#import decimal
#decimal.getcontext().prec==500 Floatの桁数を500まで増やす
#Decimal(c).sqrt() それで平方根を求める
#Decimal(1) / Decimal(7) 1/7

from collections import *
from itertools import *
from bisect import *
from heapq import *
import copy
import math
from fractions import gcd
import sys
#input = sys.stdin.readline

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

data=[]
for i,a in enumerate([2,5,5,4,5,6,3,7,6]):
    if i+1 in A:
        data.append([a,i+1])

Motodic={}
for a,i in reversed(data):
    Motodic[i]=0
dp=[copy.copy(Motodic) for i in range(N+1)]

def main(dic3,dic4):
    dic1=sorted([[str(i),dic3[i]] for i in dic3],key=lambda x:x[0],reverse=True)
    dic2=sorted([[str(i),dic4[i]] for i in dic4],key=lambda x:x[0],reverse=True)
    for a,b in zip(dic1,dic2):
        if a[1]>b[1]:
            return(0)
        if a[1]<b[1]:
            return(1)
    return(0)

def _sum(dic):
    count=0
    for i in dic:
        count+=dic[i]
    return(count)


for a,n in data:
    tempdic=copy.copy(Motodic)
    tempdic[n]+=1
    flag=main(dp[a],tempdic)
    if flag:
        dp[a]=tempdic

    for i in range(N):
        if i-a>=0 and _sum(dp[i-a]):
            if _sum(dp[i])<_sum(dp[i-a])+1:

                tempdic=copy.copy(dp[i-a])
                tempdic[n]+=1
                dp[i]=tempdic
            elif _sum(dp[i])==_sum(dp[i-a])+1:

                dp[i-a][n]+=1
                flag=main(dp[i],dp[i-a])
                dp[i-a][n]-=1
                if flag:
                    tempdic=copy.copy(dp[i-a])
                    tempdic[n]+=1
                    dp[i]=tempdic
dic=dp[N]
a=""
dic=sorted([[str(i),dic[i]] for i in dic],key=lambda x:x[0],reverse=True)
for i,z in dic:
    for m in range(z):
        a+=i
print(a)

Submission status: Runtime Error

Solution code:
#import decimal
#decimal.getcontext().prec==500 Floatの桁数を500まで増やす
#Decimal(c).sqrt() それで平方根を求める
#Decimal(1) / Decimal(7) 1/7

from collections import *
from itertools import *
from bisect import *
from heapq import *
import copy
import math
from fractions import gcd
import sys
#input = sys.stdin.readline

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

data=[]
for i,a in enumerate([2,5,5,4,5,6,3,7,6]):
    if i+1 in A:
        data.append([a,i+1])

Motodic={}
for a,i in reversed(data):
    Motodic[i]=0
dp=[copy.copy(Motodic) for i in range(N+1)]

def main(dic3,dic4):
    dic1=sorted([[str(i),dic3[i]] for i in dic3],key=lambda x:x[0],reverse=True)
    dic2=sorted([[str(i),dic4[i]] for i in dic4],key=lambda x:x[0],reverse=True)
    for a,b in zip(dic1,dic2):
        if a[1]>b[1]:
            return(0)
        if a[1]<b[1]:
            return(1)
    return(0)

def _sum(dic):
    count=0
    for i in dic:
        count+=dic[i]
    return(count)


for a,n in data:
    tempdic=copy.copy(Motodic)
    tempdic[n]+=1
    flag=main(dp[a],tempdic)
    if flag:
        dp[a]=tempdic

    for i in range(len(dp)):
        if i-a>=0 and _sum(dp[i-a]):
            if _sum(dp[i])<_sum(dp[i-a])+1:

                tempdic=copy.copy(dp[i-a])
                tempdic[n]+=1
                dp[i]=tempdic
            elif _sum(dp[i])==_sum(dp[i-a])+1:

                dp[i-a][n]+=1
                flag=main(dp[i],dp[i-a])
                dp[i-a][n]-=1
                if flag:
                    tempdic=copy.copy(dp[i-a])
                    tempdic[n]+=1
                    dp[i]=tempdic
dic=dp[N]
a=""
dic=sorted([[str(i),dic[i]] for i in dic],key=lambda x:x[0],reverse=True)
for i,z in dic:
    for m in range(z):
        a+=i
print(a)

Submission status: Runtime Error

Solution code:
#import decimal
#decimal.getcontext().prec==500 Floatの桁数を500まで増やす
#Decimal(c).sqrt() それで平方根を求める
#Decimal(1) / Decimal(7) 1/7

from collections import *
from itertools import *
from bisect import *
from heapq import *
import copy
import math
from fractions import gcd
import sys
#input = sys.stdin.readline

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

data=[]
for i,a in enumerate([2,5,5,4,5,6,3,7,6]):
    if i+1 in A:
        data.append([a,i+1])

Motodic={}
for a,i in reversed(data):
    Motodic[i]=0
dp=[copy.copy(Motodic) for i in range(N+1)]

def main(dic3,dic4):
    dic1=sorted([[str(i),dic3[i]] for i in dic3],key=lambda x:x[0],reverse=True)
    dic2=sorted([[str(i),dic4[i]] for i in dic4],key=lambda x:x[0],reverse=True)
    for a,b in zip(dic1,dic2):
        if a[1]>b[1]:
            return(0)
        if a[1]<b[1]:
            return(1)
    return(0)

def _sum(dic):
    count=0
    for i in dic:
        count+=dic[i]
    return(count)


for a,n in data:
    tempdic=copy.copy(Motodic)
    tempdic[n]+=1
    flag=main(dp[a],tempdic)
    if flag:
        dp[a]=tempdic

    for i in range(len(dp)):
        if i>=a and _sum(dp[i-a]):
            if _sum(dp[i])<_sum(dp[i-a])+1:

                tempdic=copy.copy(dp[i-a])
                tempdic[n]+=1
                dp[i]=tempdic
            elif _sum(dp[i])==_sum(dp[i-a])+1:
                exit()
                dp[i-a][n]+=1
                flag=main(dp[i],dp[i-a])
                dp[i-a][n]-=1
                if flag:
                    tempdic=copy.copy(dp[i-a])
                    tempdic[n]+=1
                    dp[i]=tempdic
dic=dp[N]
a=""
dic=sorted([[str(i),dic[i]] for i in dic],key=lambda x:x[0],reverse=True)
for i,z in dic:
    for m in range(z):
        a+=i
print(a)

Submission status: Runtime Error

Solution code:
#import decimal
#decimal.getcontext().prec==500 Floatの桁数を500まで増やす
#Decimal(c).sqrt() それで平方根を求める
#Decimal(1) / Decimal(7) 1/7

from collections import *
from itertools import *
from bisect import *
from heapq import *
import copy
import math
from fractions import gcd
import sys
#input = sys.stdin.readline

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

data=[]
for i,a in enumerate([2,5,5,4,5,6,3,7,6]):
    if i+1 in A:
        data.append([a,i+1])

Motodic={}
for a,i in reversed(data):
    Motodic[i]=0
dp=[copy.copy(Motodic) for i in range(N+1)]

def main(dic3,dic4):
    dic1=sorted([[str(i),dic3[i]] for i in dic3],key=lambda x:x[0],reverse=True)
    dic2=sorted([[str(i),dic4[i]] for i in dic4],key=lambda x:x[0],reverse=True)
    for a,b in zip(dic1,dic2):
        if a[1]>b[1]:
            return(0)
        if a[1]<b[1]:
            return(1)
    return(0)

def _sum(dic):
    count=0
    for i in dic:
        count+=dic[i]
    return(count)


for a,n in data:
    tempdic=copy.copy(Motodic)
    tempdic[n]+=1
    flag=main(dp[a],tempdic)
    if flag:
        dp[a]=tempdic

    for i in range(len(dp)):
        if i>=a and _sum(dp[i-a]):
            if _sum(dp[i])<_sum(dp[i-a])+1:

                tempdic=copy.copy(dp[i-a])
                tempdic[n]+=1
                dp[i]=tempdic
            elif _sum(dp[i])==_sum(dp[i-a])+1:
                dp[i-a][n]+=1
                flag=main(dp[i],dp[i-a])
                dp[i-a][n]-=1
                if flag:
                    tempdic=copy.copy(dp[i-a])
                    tempdic[n]+=1
                    dp[i]=tempdic
exit()
dic=dp[N]
a=""
dic=sorted([[str(i),dic[i]] for i in dic],key=lambda x:x[0],reverse=True)
for i,z in dic:
    for m in range(z):
        a+=i
print(a)

Submission status: Runtime Error

Solution code:
#import decimal
#decimal.getcontext().prec==500 Floatの桁数を500まで増やす
#Decimal(c).sqrt() それで平方根を求める
#Decimal(1) / Decimal(7) 1/7

from collections import *
from itertools import *
from bisect import *
from heapq import *
import copy
import math
from fractions import gcd
import sys
#input = sys.stdin.readline

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

data=[]
for i,a in enumerate([2,5,5,4,5,6,3,7,6]):
    if i+1 in A:
        data.append([a,i+1])

Motodic={}
for a,i in reversed(data):
    Motodic[i]=0
dp=[copy.copy(Motodic) for i in range(N+1)]

def main(dic3,dic4):
    dic1=sorted([[str(i),dic3[i]] for i in dic3],key=lambda x:x[0],reverse=True)
    dic2=sorted([[str(i),dic4[i]] for i in dic4],key=lambda x:x[0],reverse=True)
    for a,b in zip(dic1,dic2):
        if a[1]>b[1]:
            return(0)
        if a[1]<b[1]:
            return(1)
    return(0)

def _sum(dic):
    count=0
    for i in dic:
        count+=dic[i]
    return(count)


for a,n in data:
    tempdic=copy.copy(Motodic)
    tempdic[n]+=1
    flag=main(dp[a],tempdic)
    if flag:
        dp[a]=tempdic
    for i in range(len(dp)):
        if i>=a and _sum(dp[i-a]):
            if _sum(dp[i])<_sum(dp[i-a])+1:

                tempdic=copy.copy(dp[i-a])
                tempdic[n]+=1
                dp[i]=tempdic
            elif _sum(dp[i])==_sum(dp[i-a])+1:
                dp[i-a][n]+=1
                flag=main(dp[i],dp[i-a])
                dp[i-a][n]-=1
                if flag:
                    tempdic=copy.copy(dp[i-a])
                    tempdic[n]+=1
                    dp[i]=tempdic
dic=dp[N]
a=""
dic=sorted([[str(i),dic[i]] for i in dic],key=lambda x:x[0],reverse=True)
for i,z in dic:
    for m in range(z):
        a+=i
print(a)

Submission status: Runtime Error

Solution code:
#import decimal
#decimal.getcontext().prec==500 Floatの桁数を500まで増やす
#Decimal(c).sqrt() それで平方根を求める
#Decimal(1) / Decimal(7) 1/7

from collections import *
from itertools import *
from bisect import *
from heapq import *
import copy
import math
from fractions import gcd
import sys
#input = sys.stdin.readline

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

data=[]
for i,a in enumerate([2,5,5,4,5,6,3,7,6]):
    if i+1 in A:
        data.append([a,i+1])

dic={}
for a,i in reversed(data):
    dic[i]=0
dp=[copy.copy(dic) for i in range(N+1)]

def main(dic3,dic4):
    dic1=sorted([[str(i),dic3[i]] for i in dic],key=lambda x:x[0],reverse=True)
    dic2=sorted([[str(i),dic4[i]] for i in dic],key=lambda x:x[0],reverse=True)
    for a,b in zip(dic1,dic2):
        if a[1]>b[1]:
            return(0)
        if a[1]<b[1]:
            return(1)
    return(0)

def _sum(dic):
    count=0
    for i in dic:
        count+=dic[i]
    return(count)


for a,n in data:
    tempdic=copy.copy(dic)
    tempdic[n]+=1
    flag=main(dp[a],tempdic)
    if flag:
        dp[a]=tempdic
    for i in range(len(dp)):
        if i>=a and _sum(dp[i-a]):
            if _sum(dp[i])<_sum(dp[i-a])+1:

                tempdic=copy.copy(dp[i-a])
                tempdic[n]+=1
                dp[i]=tempdic
            elif _sum(dp[i])==_sum(dp[i-a])+1:
                dp[i-a][n]+=1
                flag=main(dp[i],dp[i-a])
                dp[i-a][n]-=1
                if flag:
                    tempdic=copy.copy(dp[i-a])
                    tempdic[n]+=1
                    dp[i]=tempdic
dic=dp[N]
a=""
dic=sorted([[str(i),dic[i]] for i in dic],key=lambda x:x[0],reverse=True)
for i,z in dic:
    for m in range(z):
        a+=i
print(a)

Submission status: Runtime Error

Solution code:
#import decimal
#decimal.getcontext().prec==500 Floatの桁数を500まで増やす
#Decimal(c).sqrt() それで平方根を求める
#Decimal(1) / Decimal(7) 1/7

from collections import *
from itertools import *
from bisect import *
from heapq import *
import copy
import math
from fractions import gcd
import sys
#input = sys.stdin.readline

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

data=[]
for i,a in enumerate([2,5,5,4,5,6,3,7,6]):
    if i+1 in A:
        data.append([a,i+1])

dic={}
for a,i in reversed(data):
    dic[i]=0
dp=[copy.copy(dic) for i in range(N+1)]

def main(dic1,dic2):
    dic1=sorted([[str(i),dic1[i]] for i in dic],key=lambda x:x[0],reverse=True)
    dic2=sorted([[str(i),dic2[i]] for i in dic],key=lambda x:x[0],reverse=True)
    for a,b in zip(dic1,dic2):
        if a[1]>b[1]:
            return(0)
        if a[1]<b[1]:
            return(1)
    return(0)

def _sum(dic):
    count=0
    for i in dic:
        count+=dic[i]
    return(count)


for a,n in data:
    tempdic=copy.copy(dic)
    tempdic[n]+=1
    flag=main(dp[a],tempdic)
    if flag:
        dp[a]=tempdic
    for i in range(len(dp)):
        if i>=a and _sum(dp[i-a]):
            if _sum(dp[i])<_sum(dp[i-a])+1:

                tempdic=copy.copy(dp[i-a])
                tempdic[n]+=1
                dp[i]=tempdic
            elif _sum(dp[i])==_sum(dp[i-a])+1:
                dp[i-a][n]+=1
                flag=main(dp[i],dp[i-a])
                dp[i-a][n]-=1
                if flag:
                    tempdic=copy.copy(dp[i-a])
                    tempdic[n]+=1
                    dp[i]=tempdic
dic=dp[N]
a=""
dic=sorted([[str(i),dic[i]] for i in dic],key=lambda x:x[0],reverse=True)
for i,z in dic:
    for m in range(z):
        a+=i
print(a)

Submission status: Runtime Error

Solution code:
#import decimal
#decimal.getcontext().prec==500 Floatの桁数を500まで増やす
#Decimal(c).sqrt() それで平方根を求める
#Decimal(1) / Decimal(7) 1/7

from collections import *
from itertools import *
from bisect import *
from heapq import *
import copy
import math
from fractions import gcd
import sys
#input = sys.stdin.readline

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

data=[]
for i,a in enumerate([2,5,5,4,5,6,3,7,6]):
    if i+1 in A:
        data.append([a,i+1])

dic={}
for a,i in reversed(data):
    dic[i]=0
dp=[copy.copy(dic) for i in range(N+1)]

def main(dic1,dic2):
    for a,b in zip(dic1,dic2):
        if dic1[a]>dic2[b]:
            return(0)
        if dic1[a]<dic2[b]:
            return(1)
    return(0)

def _sum(dic):
    count=0
    for i in dic:
        count+=dic[i]
    return(count)


for a,n in data:
    tempdic=copy.copy(dic)
    tempdic[n]+=1
    flag=main(dp[a],tempdic)
    if flag:
        dp[a]=tempdic
    for i in range(len(dp)):
        if i>=a and _sum(dp[i-a]):
            if _sum(dp[i])<_sum(dp[i-a])+1:

                tempdic=copy.copy(dp[i-a])
                tempdic[n]+=1
                dp[i]=tempdic
            elif _sum(dp[i])==_sum(dp[i-a])+1:
                dp[i-a][n]+=1
                flag=main(dp[i],dp[i-a])
                dp[i-a][n]-=1
                if flag:
                    tempdic=copy.copy(dp[i-a])
                    tempdic[n]+=1
                    dp[i]=tempdic
dic=dp[N]
a=""
dic=sorted([[str(i),dic[i]] for i in dic],key=lambda x:x[0],reverse=True)
for i,z in dic:
    for m in range(z):
        a+=i
print(a)

Submission status: Runtime Error

Solution code:
#import decimal
#decimal.getcontext().prec==500 Floatの桁数を500まで増やす
#Decimal(c).sqrt() それで平方根を求める
#Decimal(1) / Decimal(7) 1/7

from collections import *
from itertools import *
from bisect import *
from heapq import *
import copy
import math
from fractions import gcd
import sys
#input = sys.stdin.readline

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

data=[]
for i,a in enumerate([2,5,5,4,5,6,3,7,6]):
    if i+1 in A:
        data.append([a,i+1])

dic={}
for a,i in reversed(data):
    dic[i]=0
dp=[copy.copy(dic) for i in range(N+1)]

def main(dic1,dic2):
    for a,b in zip(dic1,dic2):
        if dic1[a]>dic2[b]:
            return(0)
        if dic1[a]<dic2[b]:
            return(1)
    return(0)

def _sum(dic):
    count=0
    for i in dic:
        count+=dic[i]
    return(count)


for a,n in data:
    tempdic=copy.copy(dic)
    tempdic[n]+=1
    flag=main(dp[a],tempdic)
    if flag:
        dp[a]=tempdic
    for i in range(len(dp)):
        if i>=a and _sum(dp[i-a]):
            if _sum(dp[i])<_sum(dp[i-a])+1:

                tempdic=copy.copy(dp[i-a])
                tempdic[n]+=1
                dp[i]=tempdic
            elif _sum(dp[i])==_sum(dp[i-a])+1:
                dp[i-a][n]+=1
                flag=main(dp[i],dp[i-a])
                dp[i-a][n]-=1
                if flag:
                    tempdic=copy.copy(dp[i-a])
                    tempdic[n]+=1
                    dp[i]=tempdic
dic=dp[N]
a=""
for i in dic:
    s=str(i)
    for m in range(dic[i]):
        a+=s
print(a)

Submission status: Runtime Error

Solution code:
#import decimal
#decimal.getcontext().prec==500 Floatの桁数を500まで増やす
#Decimal(c).sqrt() それで平方根を求める
#Decimal(1) / Decimal(7) 1/7

from collections import *
from itertools import *
from bisect import *
from heapq import *
import copy
import math
from fractions import gcd
import sys
#input = sys.stdin.readline

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

data=[]
for i,a in enumerate([2,5,5,4,5,6,3,7,6]):
    if i+1 in A:
        data.append([a,i+1])

data=sorted(data,key=lambda x:x[0])
data.append([0,0])
tempcount=M//data[0][0]
M%=data[0][0]
itelst=list(combinations_with_replacement(range(1,len(data)),int((len(data)-1)*1.5)))
lst2=[]
if M==0:
    lst2.append([data[0][1]]*tempcount)
for i in range(30):
    if tempcount<0:
        break
    for lst in itelst:
        K=M
        for i in lst:
            K-=data[i][0]
        if K==0:
            lst2.append([data[i][1] for i in lst if data[i][0]]+[data[0][1]]*tempcount)
    M+=data[0][0]
    tempcount-=1
lst2=sorted(lst2,key=lambda x:len(x))
lst2=[sorted(i,reverse=True) for i in lst2 if len(i)==len(lst2[-1])][0]
print("".join([str(i) for i in lst2]))

Submission status: Runtime Error

Solution code:
#import decimal
#decimal.getcontext().prec==500 Floatの桁数を500まで増やす
#Decimal(c).sqrt() それで平方根を求める
#Decimal(1) / Decimal(7) 1/7

from collections import *
from itertools import *
from bisect import *
from heapq import *
import copy
import math
from fractions import gcd
import sys
#input = sys.stdin.readline

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

data=[]
for i,a in enumerate([2,5,5,4,5,6,3,7,6]):
    if i+1 in A:
        data.append([a,i+1])

data=sorted(data,key=lambda x:x[0])
data.append([0,0])
tempcount=M//data[0][0]
M%=data[0][0]
itelst=list(combinations_with_replacement(range(1,len(data)),int((len(data)-1)*1.5)))
lst2=[]
if M==0:
    lst2.append([data[0][1]]*tempcount)
for i in range(20):
    if tempcount<0:
        break
    for lst in itelst:
        K=M
        for i in lst:
            K-=data[i][0]
        if K==0:
            lst2.append([data[i][1] for i in lst if data[i][0]]+[data[0][1]]*tempcount)
    M+=data[0][0]
    tempcount-=1
lst2=sorted(lst2,key=lambda x:len(x))
lst2=[sorted(i,reverse=True) for i in lst2 if len(i)==len(lst2[-1])][0]
print("".join([str(i) for i in lst2]))

Submission status: Runtime Error

Solution code:
#import decimal
#decimal.getcontext().prec==500 Floatの桁数を500まで増やす
#Decimal(c).sqrt() それで平方根を求める
#Decimal(1) / Decimal(7) 1/7

from collections import *
from itertools import *
from bisect import *
from heapq import *
import copy
import math
from fractions import gcd
import sys
#input = sys.stdin.readline

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

data=[]
for i,a in enumerate([2,5,5,4,5,6,3,7,6]):
    if i+1 in A:
        data.append([a,i+1])

data=sorted(data,key=lambda x:x[0])
data.append([0,0])
tempcount=M//data[0][0]
M%=data[0][0]
itelst=list(combinations_with_replacement(range(1,len(data)),len(data)-1))
lst2=[]
if M==0:
    lst2.append([data[0][1]]*tempcount)
for i in range(20):
    if tempcount<0:
        break
    for lst in itelst:
        K=M
        for i in lst:
            K-=data[i][0]
        if K==0:
            lst2.append([data[i][1] for i in lst if data[i][0]]+[data[0][1]]*tempcount)
    M+=data[0][0]
    tempcount-=1
lst2=sorted(lst2,key=lambda x:len(x))
lst2=[sorted(i,reverse=True) for i in lst2 if len(i)==len(lst2[-1])][0]
print("".join([str(i) for i in lst2]))

Submission status: Runtime Error

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

A.sort(reverse=True)

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

dp = [0 for _ in range(N + 1)] #dp[i] i本のマッチを使って作れる最大の桁数

for i in range(1, N + 1):
    for a in A:
        if i - cost[a] >= 0:
            dp[i] = max(dp[i], dp[i - cost[a]] + 1)

ans = ''

i = N
for k in range(dp[N]):
    for a in A:
        if dp[i - cost[a]] == dp[i] - 1:
            if k == dp[N] - 1 and i != cost[a]:
                continue
            ans += str(a)
            i -= cost[a]
            break

print(ans)

Submission status: Runtime Error

Solution code:
def main():
    import sys
    input = sys.stdin.buffer.readline
    N, M = (int(i) for i in input().split())
    A = [int(i) for i in input().split()]
    dp = [""]*(N+1)
    match = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
    for i in range(2, N+1):
        cur = (int(dp[i]) if dp[i] else 0)
        for a in A:
            if match[a] == i or dp[i - match[a]]:
                ne = int(dp[i - match[a]] + str(a))
                if cur < ne:
                    dp[i] = str(ne)
                    cur = ne
    # print(dp)
    print(dp[N])


if __name__ == '__main__':
    main()