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: Accepted)

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cstring>
#include <stack>
#include <utility>
#include <map>
#include <cmath>
#include <stack>
#include <queue>
#include <deque>
#include <cstdlib>
#include <set>
#include <chrono> 

#define pii pair<int,int>
#define vi vector<int>
#define ll long long
#define pb push_back
#define mp make_pair
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL)
#define repf(i,st,end,inc) for(int i = st; i < end; i += inc)
#define repb(i,st,end,dec) for(int i = st; i >= end; i -= dec) 

#define MOD 1000000007

#define trace1(a) cout << a << endl;
#define trace2(a,b) cout << a << " | " << b << endl;
#define trace3(a,b,c) cout << a << " | " << b << " | " << c << endl;
#define trace4(a,b,c,d) cout << a << " | " << b << " | " << c << " | "<< d << endl;

using namespace std;
using namespace std::chrono;

int cost[] = {2,5,5,4,5,6,3,7,6};

int main(){
	fast;
	auto start = high_resolution_clock::now(); 

	int n, m;
	cin >> n >> m;

	vector<int> allowed(m);
	vector<int> corr_cost(m);
	for(int i = 0 ; i< m ; i++){
		cin >> allowed[i];
		corr_cost[i] = cost[allowed[i] - 1];
	}

	vector< vector<int> > a(n+1 , vector<int>(m , -10000000));

	for(int i = 0 ; i < m ; i++)
		a[0][i] = 0;

	for(int i = 1; i <= n; i++){
		for(int j = 0; j < m; j++){
			if (i - corr_cost[j] < 0)
				continue;
			for(int k = 0 ; k < m; k++){
				a[i][j] = max(1 + a[i - corr_cost[j]][k], a[i][j]); 
			}
			// trace1(a[i][j])
		}
	}

	// for(int i = 1; i <= n; i++){
	// 	for(int j = 0; j < m; j++){
	// 		cout << a[i][j] << " ";
	// 	}
	// 	cout << endl;
	// }

	for(int i = n; i >= 0 ; i--){
		int max_elem = -1;
		int max_len = -1;
		int max_idx = -1;
		for(int j = 0 ; j < m;j++){
			// trace1(a[i][j])
			if(a[i][j] >= max_len){
				// trace4(a[i][j], j, max_len, max_elem)
				if(a[i][j] == max_len){
					if(allowed[j] > max_elem){
						max_elem = allowed[j];
						max_len = a[i][j];
						max_idx = j;
						// trace3(max_len, max_elem, max_idx)
					}
				}
				else{
					max_elem = allowed[j];
					max_len = a[i][j];
					max_idx = j;
					
				}
			}
		}
		// trace2(max_len, max_elem)
		if(max_len == 0)
			break;
		else{
			cout << max_elem;
			i = i - corr_cost[max_idx] + 1;
		}
			// continue;
	}

	auto stop = high_resolution_clock::now(); 
	auto duration = duration_cast<microseconds>(stop - start);	
	// cout << endl;
	// cout << double(duration.count()) / 1000000 << endl; 

	return 0;
}