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 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <stdio.h>
int main(void){
  int N,M,f_A[10]={},i,j,dp[420]={},cost[10]={0,2,5,5,4,5,6,3,7,6};

  scanf("%d %d\n%d",&N,&M,&i);
  f_A[i]=1;
  for(j=2;j<=M;j++){scanf(" %d",&i);f_A[i]=1;}

  dp[1]=1000;
  if(!f_A[1])dp[2]=1000;
  else{dp[2]=1;}
  if(!f_A[7])dp[3]=1000;
  if(!f_A[4])dp[4]=1000;
  if(!(f_A[2]||f_A[3]||f_A[5]))dp[5]=1000;
  if(!(f_A[6]||f_A[9]))dp[6]=1000;
  if(!f_A[8])dp[7]=1000;

  for(i=2;i<=N%420;i++)
  {
    if(f_A[1])dp[i]=dp[i-2]+1;
    if(i-3>=0&&f_A[7])if(dp[i]<dp[i-3]+1)dp[i]=dp[i-3]+1;
    if(i-4>=0&&f_A[4])if(dp[i]<dp[i-4]+1)dp[i]=dp[i-4]+1;
    if(i-5>=0&&(f_A[2]||f_A[3]||f_A[5]))if(dp[i]<dp[i-5]+1)dp[i]=dp[i-5]+1;
    if(i-6>=0&&(f_A[6]||f_A[9]))if(dp[i]<dp[i-6]+1)dp[i]=dp[i-6]+1;
    if(i-7>=0&&f_A[8])if(dp[i]<dp[i-7]+1)dp[i]=dp[i-7]+1;
  }

  for(i=N%420;i>0;)
  {
    if((f_A[9]>0)&&(dp[i]==dp[i-6]+1)){f_A[9]++;i-=6;}
    else if((f_A[8]>0)&&(dp[i]==dp[i-7]+1)){f_A[8]++;i-=7;}
    else if((f_A[7]>0)&&(dp[i]==dp[i-3]+1)){f_A[7]++;i-=3;}
    else if((f_A[6]>0)&&(dp[i]==dp[i-6]+1)){f_A[6]++;i-=6;}
    else if((f_A[5]>0)&&(dp[i]==dp[i-5]+1)){f_A[5]++;i-=5;}
    else if((f_A[4]>0)&&(dp[i]==dp[i-4]+1)){f_A[4]++;i-=4;}
    else if((f_A[3]>0)&&(dp[i]==dp[i-5]+1)){f_A[3]++;i-=5;}
    else if((f_A[2]>0)&&(dp[i]==dp[i-5]+1)){f_A[2]++;i-=5;}
    else if((f_A[1]>0)&&(dp[i]==dp[i-2]+1)){f_A[1]++;i-=2;}
  }

  if(f_A[1])f_A[1]+=(N/420)*210;
  else if(f_A[7])f_A[7]+=(N/420)*140;
  else if(f_A[4])f_A[4]+=(N/420)*105;
  else if(f_A[5])f_A[5]+=(N/420)*84;
  else if(f_A[3])f_A[3]+=(N/420)*84;
  else if(f_A[2])f_A[2]+=(N/420)*84;
  else if(f_A[9])f_A[9]+=(N/420)*70;
  else if(f_A[6])f_A[6]+=(N/420)*70;
  else if(f_A[8])f_A[8]+=(N/420)*60;
  for(i=9;i>=1;i--)for(j=0;j<f_A[i]-1;j++)printf("%d",i);
  return 0;
}