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

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#define MOD 1000000007
typedef long long ll;
using namespace std;

int num[]={0,2,5,5,4,5,6,3,7,6};
int a[10];
vector<int> v;
int n,m;
int ma;
vector<string> res;

void dfs(int rest,string s){
    //cout<<rest<<" "<<s<<endl;
    for(int i=0;i<m;i++){
        if(num[a[i]]==num[ma]) continue;
        if(rest-(num[a[i]]-num[ma])<0) continue;
        if(a[i]>ma){
            for(int j=0;j<s.size();j++){
                if(s[j]-'0'==ma){
                    if(rest-(num[a[i]]-num[ma])==0){
                        string t=s;
                        t[j]=char(a[i]+'0');
                        res.push_back(t);
                        break;
                    }else{
                        string t=s;
                        t[j]=char(a[i]+'0');
                        dfs(rest-(num[a[i]]-num[ma]),t);
                        break;
                    }
                }
            }
        }else{
            for(int j=s.size()-1;j>=0;j--){
                if(s[j]-'0'==ma){
                    if(rest-(num[a[i]]-num[ma])==0){
                        string t=s;
                        t[j]=char(a[i]+'0');
                        res.push_back(t);
                        break;
                    }else{
                        string t=s;
                        t[j]=char(a[i]+'0');
                        dfs(rest-(num[a[i]]-num[ma]),t);
                        break;
                    }
                }
            }
        }
    }
    return;
}

int main(){
  cin>>n>>m;
  int mit=10;
  for(int i=0;i<m;i++){
      cin>>a[i];
      v.push_back(a[i]);
      if(num[a[i]]<mit){
          mit=num[a[i]];
          ma=a[i];
      }else if(num[a[i]]==mit){
          ma=max(ma,a[i]);
      }
  }
  sort(v.begin(),v.end());

  string ans;
  int x=n;
  for(;;){
      if(x-mit>=0){
          ans+=char(ma+'0');
          x-=mit;
      }else break;
  }
  //cout<<x<<endl;
  //cout<<ans<<endl;
  if(x==0) cout<<ans<<endl;
  else{
      int tmp=0;
      for(int k=0;k<=ans.size();k++){
          bool flag=false;
          for(int j=0;j<m;j++){
              if(num[a[j]]==num[ma]) continue;
              if(x%(num[a[j]]-num[ma])==0) flag=true;
          }
          if(flag) break;
          else{
              ans.pop_back();
              x+=num[ma];
          }
      }
      //cout<<ans<<" "<<tmp<<endl;
      /*if(ans.size()==0){
          char sa='0';
          for(int i=0;i<m;i++){
              if(num[a[i]]==n){
                  sa=max(sa,char(a[i]+'0'));
              }
          }
          cout<<sa<<endl;
          return 0;
      }*/
      dfs(x,ans);
      string so=res[0];
      for(int i=0;i<res.size();i++){
          so=max(so,res[i]);
          //cout<<res[i]<<endl;
      }
      //sort(res.begin(),res.end());
      //reverse(res.begin(),res.end());
      cout<<so<<endl;
  }
  

  return 0;
}