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

Solution in PyPy3 (2.4.0) (judge verdict: Wrong Answer)

n,m=map(int,input().split())
arr=list(map(int,input().split()))
dic={1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6}
cand=[[''] for _ in range(6)]
for val in arr:
  cand[dic[val]-2].append(str(val))
pos=6
for i in range(5,-1,-1):
  cand[i]=sorted(cand[i],reverse=True)
  if len(cand[i])>1:
    pos=i
ans=[]
for c1 in range(n//(pos+2),max(-1,n//(pos+2)-15),-1):
  tmp=[[cand[pos][0],c1]]
  if pos==5 and (pos+2)*c1==n:
    t=''
    tmp=sorted(tmp,reverse=True,key=lambda x:x[0])
    for char,val in tmp:
      t+=char*val
    ans.append(t)
  for c2 in range(11):
    if pos>=5:
      continue
    tmp2=tmp+[[cand[pos+1][0],c2]]
    if pos==4 and (pos+2)*c1+(pos+2+1)*c2==n:
      t=''
      tmp2=sorted(tmp2,reverse=True,key=lambda x:x[0])
      for char,val in tmp2:
        t+=char*val
      ans.append(t)
    for c3 in range(11):
      if pos>=4:
        continue
      tmp3=tmp2+[[cand[pos+2][0],c3]]
      if pos==3 and (pos+2)*c1+(pos+2+1)*c2+(pos+2+2)*c3==n:
        t=''
        tmp3=sorted(tmp3,reverse=True,key=lambda x:x[0])
        for char,val in tmp3:
          t+=char*val
        ans.append(t)
      for c4 in range(11):
        if pos>=3:
          continue
        tmp4=tmp3+[[cand[pos+3][0],c4]]
        if pos==2 and (pos+2)*c1+(pos+2+1)*c2+(pos+2+2)*c3+(pos+2+3)*c4==n:
          t=''
          tmp4=sorted(tmp4,reverse=True,key=lambda x:x[0])
          for char,val in tmp4:
            t+=char*val
          ans.append(t)
        for c5 in range(11):
          if pos>=2:
            continue
          tmp5=tmp4+[[cand[pos+4][0],c5]]
          if pos==1 and (pos+2)*c1+(pos+2+1)*c2+(pos+2+2)*c3+(pos+2+3)*c4+(pos+2+4)*c5==n:
            t=''
            tmp5=sorted(tmp5,reverse=True,key=lambda x:x[0])
            for char,val in tmp5:
              t+=char*val
            ans.append(t)
          for c6 in range(11):
            if pos>=1:
              continue
            tmp6=tmp5+[[cand[pos+5][0],c6]]
            if pos==0 and (pos+2)*c1+(pos+2+1)*c2+(pos+2+2)*c3+(pos+2+3)*c4+(pos+2+4)*c5+(pos+2+5)*c6==n:
              t=''
              tmp6=sorted(tmp6,reverse=True,key=lambda x:x[0])
              for char,val in tmp6:
                t+=char*val
              ans.append(t)
ans=sorted(ans,reverse=True,key=lambda x:len(x))
print(ans[0])