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 List<string> list;
  //static List<int> primes = new List<int>{2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,
  //                              59,61,67,71,73,79,83,89,97};
	static void Main(string[] args)
	{
		// 入力フェーズ
        string[] input = Console.ReadLine().Split(' ');
        int N = int.Parse(input[0]);
        int M = int.Parse(input[1]);
        string[] input2 = Console.ReadLine().Split(' ');
        // 使えるマッチの数
        // 1 2 3 4 5 6 7 8 9
        // 2 5 5 4 5 6 3 7 6
        // 処理
        // 使える数字の中で一番マッチの本数が少ない数字を探す
        int min = 9, minNum = 0;
        for(int i=0; i<input2.Length; i++)
        {
          int u = int.Parse(input2[i]);
          int match = getUseMatch(u);

          if(min > match)
          {
            min = match;
            if(minNum < u)
            {
              minNum = u;
            }
          }
        }

        // N本以下で表せる最大の桁数は
        int maxDigit = N/min;

        // ちょうどだったら
        if(N%min==0)
        {
          for(int i=0; i<maxDigit; i++)
          {
            Console.Write(minNum + "");
          }
        }else
        {
          // 余ったマッチの本数
          int rest = N%min;
          if(minNum == 1)
          {
            // マッチが１本だけ余る＝1を使えて、2->3本には７しか置き換えでき兄
            int ahead = 0;
            if(isHave(input2, 7))
            {
              ahead = 7;
            }
            else
            {
              // 7がない・・・？
              if(isHave(input2, 5)) ahead = 5;
              else if(isHave(input2, 3)) ahead = 3;
              else if(isHave(input2, 2)) ahead = 2;
              else if(isHave(input2, 8)) ahead = 8;
            }
            Console.Write(ahead + "");
            if(ahead == 7)
              outPut(1, maxDigit-1);
            else if(ahead == 5 || ahead == 3 || ahead == 2)
              outPut(1, maxDigit-2);
            else
              outPut(1, maxDigit-3);
          }
          else if(minNum == 7)
          {
            if(rest == 1)
            {
              int tale = 0;
              if(isHave(input2, 4)) tale = 4;
              else if(isHave(input2, 8)) tale = 8;

              if(tale == 4)
              {
                outPut(7, maxDigit-1);
                Console.Write("4");
              }else if(tale == 8)
              {

                Console.Write("8");
                outPut(7, maxDigit-1);
              }
            }
            else if(rest == 2)
            {
              // １桁を235のいずれかに置き換えor ２桁を44に置き換え
              int tale = 0;
              if(isHave(input2, 5)) tale = 5;
              else if(isHave(input2, 3)) tale = 3;
              else if(isHave(input2, 2)) tale = 2;
              else if(isHave(input2, 4)) tale = 4;

              if(tale == 4)
              {
                outPut(7, maxDigit-2);
                Console.Write("44");
              }
              else
              {
                outPut(7, maxDigit-1);
                Console.Write(tale + "");
              }
            }
          }
          else if(minNum == 4)
          {
            if(rest == 1)
            {
              int tale = 0;
              if(isHave(input2, 5)) tale = 5;
              else if(isHave(input2, 3)) tale = 3;
              else if(isHave(input2, 2)) tale = 2;
              else if(isHave(input2, 8)) tale = 8;

              if(tale == 8)
              {
                Console.Write("888");
                outPut(4, maxDigit-5);
              }
              else if(tale == 5)
              {
                Console.Write(tale + "");
                outPut(4, maxDigit-1);
              }
              else
              {
                outPut(4, maxDigit-1);
                Console.Write(tale + "");
              }
            }
            else if(rest == 2)
            {
              int tale = 0;
              if(isHave(input2, 9)) tale = 9;
              else if(isHave(input2, 6)) tale = 6;
              else if(isHave(input2, 5)) tale = 5;
              else if(isHave(input2, 3)) tale = 3;
              else if(isHave(input2, 2)) tale = 2;
              else if(isHave(input2, 8)) tale = 8;

              if(tale == 8)
              {
                Console.Write("88");
                outPut(4, maxDigit-3);
              }
              else if(tale == 9 || tale == 6)
              {
                Console.Write(tale + "");
                outPut(4, maxDigit-1);
              }
              else if(tale == 5)
              {
                Console.Write("55");
                outPut(4, maxDigit-2);
              }
              else
              {
                outPut(4, maxDigit-2);
                Console.Write(tale + "" + tale);
              }
            }
            else if(rest==3)
            {
              int tale = 0;
              if(isHave(input2, 8)) tale = 8;
              else if(isHave(input2, 9)) tale = 9;
              else if(isHave(input2, 6)) tale = 6;
              else if(isHave(input2, 5)) tale = 5;
              else if(isHave(input2, 3)) tale = 3;
              else if(isHave(input2, 2)) tale = 2;

              if(tale == 8)
              {
                Console.Write("8");
                outPut(4, maxDigit-1);
              }
              else if(tale == 9 || tale == 6)
              {
                Console.Write(tale + "");
                tale = 0;
                if(isHave(input2, 5)) tale = 5;
                else if(isHave(input2, 3)) tale = 3;
                else if(isHave(input2, 2)) tale = 2;

                if(tale == 5)
                {
                Console.Write(tale + "");
                outPut(4, maxDigit-2);

                }
                else
                {
                  outPut(4, maxDigit-2);

                  Console.Write(tale + "");
                }
              }
              else if(tale == 5)
              {
                Console.Write("555");
                outPut(4, maxDigit-3);
              }
              else
              {
                outPut(4, maxDigit-3);
                Console.Write(tale + "" + tale + "" + tale);
              }
            }
          }
          else if(minNum == 5 || minNum == 3 || minNum == 2)
          {
            if(rest == 1)
            {
              int tale = 0;
              if(isHave(input2, 9)) tale = 9;
              else if(isHave(input2, 6)) tale = 6;
              else if(isHave(input2, 8)) tale = 8;

              if(tale == 8)
              {
                Console.Write("888");
                outPut(5, maxDigit-4);
              }
              else
              {
                Console.Write(tale + "");
                outPut(5, maxDigit-1);
              }
            }
            else if(rest == 2)
            {
              int tale = 0;
              if(isHave(input2, 9)) tale = 9;
              else if(isHave(input2, 8)) tale = 8;
              else if(isHave(input2, 6)) tale = 6;

              if(tale == 8)
              {
                Console.Write("8");
                outPut(5, maxDigit-1);
              }
              else
              {
                Console.Write(tale + "" + tale);
                outPut(5, maxDigit-2);
              }
            }
            else if(rest == 3)
            {
              int tale = 0;
              if(isHave(input2, 9)) tale = 9;
              else if(isHave(input2, 8)) tale = 8;
              else if(isHave(input2, 6)) tale = 6;

              if(tale == 8)
              {
                Console.Write("86");
                outPut(5, maxDigit-2);
              }
              else
              {
                Console.Write(tale + "" + tale + "" + tale);
                outPut(5, maxDigit-3);
              }
            }
            else if(rest == 4)
            {
              int tale = 0;
              if(isHave(input2, 9)) tale = 9;
              else if(isHave(input2, 8)) tale = 8;
              else if(isHave(input2, 6)) tale = 6;

              if(tale == 8)
              {
                Console.Write("88");
                outPut(5, maxDigit-2);
              }
              else
              {
                Console.Write(tale + "" + tale + "" + tale + "" + tale);
                outPut(5, maxDigit-4);
              }
            }
          }
          else if(minNum == 6)
          {
            if(rest == 1)
            {
              int tale = 0;
              if(isHave(input2, 8)) tale = 8;

              if(tale == 8)
              {
                Console.Write("8");
                outPut(6, maxDigit-1);
              }
            }else if(rest == 2)
            {
              int tale = 0;
              if(isHave(input2, 8)) tale = 8;

              if(tale == 8)
              {
                Console.Write("88");
                outPut(6, maxDigit-2);
              }
            }else if(rest == 3)
            {
              int tale = 0;
              if(isHave(input2, 8)) tale = 8;

              if(tale == 8)
              {
                Console.Write("888");
                outPut(6, maxDigit-3);
              }
            }else if(rest == 4)
            {
              int tale = 0;
              if(isHave(input2, 8)) tale = 8;

              if(tale == 8)
              {
                Console.Write("8888");
                outPut(6, maxDigit-4);
              }
            }else if(rest == 5)
            {
              int tale = 0;
              if(isHave(input2, 8)) tale = 8;

              if(tale == 8)
              {
                Console.Write("8888");
                outPut(6, maxDigit-5);
              }
            }
          }else if(minNum == 9)
          {
            if(rest == 1)
            {
              int tale = 0;
              if(isHave(input2, 8)) tale = 8;

              if(tale == 8)
              {
              outPut(9, maxDigit-1);
                Console.Write("8");
              }
            }else if(rest == 2)
            {
              int tale = 0;
              if(isHave(input2, 8)) tale = 8;

              if(tale == 8)
              {
              outPut(9, maxDigit-2);
                Console.Write("88");
              }
            }else if(rest == 3)
            {
              int tale = 0;
              if(isHave(input2, 8)) tale = 8;

              if(tale == 8)
              {
              outPut(9, maxDigit-3);
                Console.Write("888");
              }
            }else if(rest == 4)
            {
              int tale = 0;
              if(isHave(input2, 8)) tale = 8;

              if(tale == 8)
              {
              outPut(9, maxDigit-4);
                Console.Write("8888");
              }
            }else if(rest == 5)
            {
              int tale = 0;
              if(isHave(input2, 8)) tale = 8;

              if(tale == 8)
              {
              outPut(9, maxDigit-5);
                Console.Write("8888");
              }
            }
          }else if(minNum == 8)
          {

          }
        }
        Console.WriteLine("");
    }
		class myClass{ //自作クラス
			public long total {set; get;} // 全部の層数
			public long paty {set; get;}  // パティの数
      public long banz {set; get;}  // バンズの数
		}
    static void outPut(int a, int d)
    {
      for(int i=0; i<d; i++)
      {
        Console.Write(a + "");
      }
    }
    static bool isHave(string[] input, int a)
    {
      for(int i=0; i<input.Length; i++)
      {
          int u = int.Parse(input[i]);
          if(u==a) return true;
      }
      return false;
    }
    static int getUseMatch(int u)
    {
      int match = 0;
      switch(u)
      {
        case 1:
          match = 2;
          break;
        case 2:
          match = 5;
          break;
        case 3:
          match = 5;
          break;
        case 4:
          match = 4;
          break;
        case 5:
          match = 5;
          break;
        case 6:
          match = 6;
          break;
        case 7:
          match = 3;
          break;
        case 8:
          match = 7;
          break;
        case 9:
          match = 6;
          break;
      }
      return match;
    }
}