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: Runtime Error)

using namespace std;

#include <bits/stdc++.h>

using namespace std;

int mod_div(int a, int div, int mod)
{
    while(a%div != 0)
    {
        a += mod;
    }

    return a/div;
}

bool diophantine_bool(int a, int b, int c)
{
    int sol[2] = {0,0};

    if(__gcd(a,b) == 1 && (a != 1 && b!= 1))
    {
        sol[1] = mod_div(c%a,b%a,a);
        sol[0] = (c - b * sol[1])/a;

        bool output = (sol[0] >= 0 && sol[1] >= 0)? true: false; return output;
    }
    else if((a+b)%__gcd(a,b)== c%__gcd(a,b) && c>=0){return true;}
    else if(a == 0 || b == 0){return true;}
    else {return false;}
}

vector <int> solve_diophantine(vector<int> coeff,int res)
{
    int n = (int) coeff.size();
    vector <int> coeff_c;
    vector <int> new_v;
    vector <int> output = {};

    if(n > 2)
    {
        int mod = coeff[0];

        for(int a=res/mod;a>=0;a--)
        {
            coeff_c = coeff;
            if(a == 0)
            {
                coeff.erase(coeff.begin());
                new_v = solve_diophantine(coeff,res - a*mod);
                if(!new_v.empty())
                {
                    output.push_back(a);
                    output.insert(output.end(),new_v.begin(),new_v.end());
                }
                break;
            }

            bool solvable = false;

            for(int i=0;i<n;i++)
            {
                if((res - a * mod)>=coeff[i])
                {
                    solvable = true;
                    int n_coeff = i;
                    break;
                }
            }

            if(solvable == true)
            {
                coeff_c.erase(coeff_c.begin());
                new_v = solve_diophantine(coeff_c,res - a*mod);
                if(!new_v.empty())
                {
                    output.push_back(a);
                    output.insert(output.end(),new_v.begin(),new_v.end());
                    break;
                }
            }
            else if(res - a * mod == 0)
            {
                output.push_back(a);
                for(int i=1;i<n;i++)
                {
                    output.push_back(0);
                }
                return output;
            }
        }
    }
    else
    {
        if(diophantine_bool(coeff[0],coeff[1],res))
        {
            output.push_back(0);
            output.push_back(mod_div(res%coeff[0],coeff[1]%coeff[0],coeff[0]));
            output[0] = (res - coeff[1] * output[1])/coeff[0];
        }
        return output;
    }
}

#include <bits/stdc++.h>

int main()
{
    int N,M; cin>>N>>M;
    int data[9] = {2,5,5,4,5,6,3,7,6};
    int A[M], B[M];

    for(int i=0;i<M;i++)
    {
        cin>>B[i];
        A[i] = data[B[i]-1];
    }

    for(int i=0;i<M;i++)
    {
        for(int j=i;j<M;j++)
        {
            if(A[i] > A[j]){ swap(A[i],A[j]); swap(B[i],B[j]);}
            else if(A[i] == A[j] && B[i] < B[j])
            { swap(A[i],A[j]); swap(B[i],B[j]);}
        }
    }

    vector<int> v;

    for(int i=0;i<M;i++)
    {
        v.push_back(A[i]);
    }

    vector<int> digits = solve_diophantine(v,N);

    for(int i=0;i<M;i++)
    {
        for(int j=i;j<M;j++)
        {
            if(B[i] < B[j])
            { swap(A[i],A[j]); swap(B[i],B[j]); swap(digits[i],digits[j]);}
        }
    }

    for(int i=0;i<M;i++)
    {
        for(int j=0;j<digits[i];j++)
        {
            cout<<B[i];
        }
    }
}