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()
{
    int n, m;
    scanf("%d %d", &n, &m);
    int i;
    int a[10];
    for (i = 0; i < m; i++)
        scanf("%d", &a[i]);
    int b[10];
    for (i = 0; i < m; i++)
    {
        if (a[i] == 1)b[i] = 2;
        if (a[i] == 2)b[i] = 5;
        if (a[i] == 3)b[i] = 5;
        if (a[i] == 4)b[i] = 4;
        if (a[i] == 5)b[i] = 5;
        if (a[i] == 6)b[i] = 6;
        if (a[i] == 7)b[i] = 3;
        if (a[i] == 8)b[i] = 7;
        if (a[i] == 9)b[i] = 6;
    }
    int min = 0;
    for (i = 0; i < m; i++)
    {
        if (b[min] > b[i])
            min = i;
        else if (b[i] == b[min] && a[i] > a[min])
            min = i;
    }
    if (n%b[min] == 0)
    {
        for (i = 0; i < n / b[min]; i++)
            printf("%d", a[min]);
        printf("\n");
        return 0;
    }
    int dp[100][1000] = {};
    dp[0][0] = 1;
    int j, k;
    int x, y;
    for (i = 0; i < 100; i++)
    {
        for (j = 0; j < 1000; j++)
        {
            if (dp[i][j] == 1)
            {
                for (k = 0; k < m; k++)
                {
                    if (i + b[k] < 100)
                    {
                        x = 10 * j + a[k];
                        if (x < 1000)
                            dp[i + b[k]][x] = 1;
                        x = j;
                        y = a[k];
                        while (x > 0)
                        {
                            x /= 10;
                            y *= 10;
                        }
                        x = y + j;
                        if (x < 1000)
                            dp[i + b[k]][x] = 1;
                    }
                }
            }
        }
    }
    int max[100] = {};
    for (i = 0; i < 100; i++)
    {
        for (j = 0; j < 1000; j++)
            if (dp[i][j] == 1)
                max[i] = j;
    }
    x = n % b[min];
    while (max[x] == 0)
        x += b[min];
    y = max[x];
    while (y / 10 > 0)
        y /= 10;
    if (y > a[min])
    {
        printf("%d", max[x]);
        n -= x;
        for (i = 0; i < n / b[min]; i++)
            printf("%d", a[min]);
        printf("\n");
    }
    else
    {
        n -= x;
        for (i = 0; i < n / b[min]; i++)
            printf("%d", a[min]);
        printf("%d", max[x]);
        printf("\n");
    }
    return 0;
}