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<bits/stdc++.h>
#define range(i,a,b) for(long long i = (a); i < (b); i++)
#define rep(i,b) for(long long i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x)  cerr << #x << " = " << (x) << endl;
using namespace std;

template <typename X, typename T>
auto vectors(X x, T a) {
	return vector<T>(x, a);
}
template <typename X, typename Y, typename Z, typename... Zs>
auto vectors(X x, Y y, Z z, Zs... zs) {
	auto cont = vectors(y, z, zs...);
	return vector<decltype(cont)>(x, cont);
}
template<typename T>
ostream& operator << (ostream& os, vector<T>& v){
	rep(i,v.size()){ os << v[i] << (i == v.size() - 1 ? "" : " "); } return os;
}
template<typename T>
istream& operator >> (istream& is, vector<T>& v){
	for(T& x: v){ is >> x; } return is;
}

const int order[] = {1, 7, 4, 5, 3, 2, 9, 6, 8};
const int match[] = {0,  2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 };

// 作れる数が多い
// 大きい数が多い

map<int, vector<int>> memo;

bool comp(vector<int>& a, vector<int>& b){
	int as = accumulate(all(a), 0LL);
	int bs = accumulate(all(b), 0LL);
	if(as != bs){
		return as < bs;
	}

	for(int i = 9; i >= 1; i--){
		if(a[i] < b[i]){
			return true;
		}else{
			return false;
		}
	}
	return false;
}

void dfs(const set<int>& s, int n, vector<int> num){
	if(n == 0){
		if(memo.count(0) and comp(memo[n], num)){
			memo[n] = num;
		}

		//show("ans")
		//cout << num << endl;
	}
	if(memo.count(n)) return;
	memo[n] = num;
	//cout << n << ' ' << num << endl;

	for(auto i : order){
		if(not s.count(i)) continue;
		if(n - match[i] < 0) continue;
		num[i]++;
		dfs(s, n - match[i], num);
		num[i]--;
	}
}

int main(){
	int n, m;
	cin >> n >> m;

	set<int> s;
	rep(i,m){
		int a;
		cin >> a;
		s.emplace(a);
	}

	dfs(s, n, vector<int>(10, 0));

	auto ans = memo[0];
	for(int i = 9; i >= 1; i--){
		rep(_,ans[i]){
			cout << i;
		}
	}
}