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 C# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Linq;
class Program{
  static void Main(string[] args){
    bool[] c =new bool[9];
    for(int i=0;i<9;i++){c[i]=false;}
    string[] tmp = Console.ReadLine().Split(' ');
    int n = int.Parse(tmp[0]);
    int m = int.Parse(tmp[1]);
    string[] a = Console.ReadLine().Split(' ');
    for(int i=0;i<m;i++){ c[int.Parse(a[i])-1]=true; }
    int e = 0;//最小本数
    bool[] o = new bool[8]; //使える本数
    o[2]=c[0];
    o[3]=c[6];
    o[4]=c[3];
    o[5]=c[1]||c[2]||c[4];
    o[6]=c[5]||c[8];
    o[7]=c[7];
    for(int i=7;i>1;i--){if(o[i]){e=i;}}
    int d = n/e;//桁数
    string[] p = new string[d];//回答
    string fill="";//埋める用
    switch(e){
      case 7:
        for(int i=0;i<d;i++){p[i]="8";}
        break;
      case 6:
        fill=c[8]?"9":"5";
        for(int i=0;i<d-d%6;i++){p[i]=fill;}
        for(int i=d-d%6;i<d;i++){p[i]="8";}
        break;
      case 5:
        fill=c[4]?"5":(c[2]?"3":"2");
        for(int i=d%5;i<d;i++){p[i]=fill;}
        switch(d%5){
          case 1: p[0]=c[8]?"9":"5"; break;
          case 2:
            if(c[8]){p[0]="9";p[1]="9";}
            else if(c[7]){p[0]="8";p[1]=fill;}
            else{p[0]="6";p[1]="6";}
            break;
          case 3:
            if(c[8]){p[0]="9";p[1]="9";p[2]="9";}
            else if(c[7]){p[0]="8";p[1]="6";p[2]=fill;}
            else{p[0]="6";p[1]="6";p[2]="6";}
            break;
          case 4:
            if(c[8]){p[0]="9";p[1]="9";p[2]="9";p[3]="9";}
            else if(c[7]){p[0]="8";p[1]="8";p[2]=fill;p[3]=fill;}
            else{p[0]="6";p[1]="6";p[2]="6";p[3]="6";}
            break;
        }
        break;
      case 4:
        for(int i=d%4;i<d;i++){p[i]="3";}
        switch(d%4){
          case 1: p[0]=c[4]?"5":(c[2]?"3":"2"); break;
          case 2:
            if(c[5]||c[8]){p[0]=c[8]?"9":"6";p[1]="3";}
            else{fill=c[4]?"5":(c[2]?"3":"2");p[0]=fill;p[1]=fill;}
            break;
          case 3:
            fill=c[4]?"5":(c[2]?"3":"2");
            if(c[8]&&o[5]){p[0]="9";p[1]="4";p[2]=fill;}
            else if(o[7]){p[0]="8";p[1]="4";p[2]="4";}
            else if(o[6]){p[0]=c[8]?"9":"6";p[1]=fill;p[2]="4";}
            else{p[0]=fill;p[1]=fill;p[2]=fill;}
            break;
        }
        break;
      case 3:
        fill=c[4]?"5":(c[2]?"3":"2");
        for(int i=d%3;i<d;i++){p[i]="7";}
        if(d%3==1){p[0]="4";}
        else if(d%3==2){
          if(o[4]){p[0]=fill;p[1]="7";}
          else{p[0]="4";p[1]="4";}
        }
        break;
      case 2:
        for(int i=d%2;i<d;i++){p[i]="1";}
        if(d%2==1){p[0]="7";}
        break;
    }
    Array.Sort(p);
    Array.Reverse(p);
    Console.WriteLine(string.Join("",p));
  }
}