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>
#include <stdlib.h>
#include <string.h>
#include <math.h>

void print_table(int table[])
{
    int i, j;
    for (i = 9; i > 0; i--) {
        for (j = 0; j < table[i]; j++) {
            printf("%d", i);
        }
    }
    printf("\n");

    return;
}

int main(int argc, char *argv[])
{
    int n, m;
    int x;
    int a;
    int usable[10];
    int match[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
    int table[10];
    int i, j;
    int ans;

    for (i = 0; i < 10; i++) {
        usable[i] = 0;
        table[i] = 0;
    }

    scanf("%d %d", &n, &m);
    for (i = 0; i < m; i++) {
        scanf("%d", &a);
        usable[a] = 1;
    }

    for (i = 0; i < 10; i++) {
        if (usable[i] == 0) {
            match[i] = 0;
        }
    }

    x = n;
    // 2本: 1
    if (usable[1]) {
        table[1] = x / 2;
        x = x % 2;
    }
    // 3本: 7
    if (usable[7] && x > 0) {
        while(x < 3) {
            table[1]--;
            x += 2;
        }
        table[7] = x / 3;
        x = x % 3;
    }
    // 4本: 4
    if (usable[4] && x > 0) {
        while(x < 4) {
            table[7]--;
            x += 3;
        }
        table[4] = x / 4;
        x = x % 4;
    }
    // 5本: 2, 3, 5
    if (x > 0) {
        if (usable[2] || usable[3] || usable[5]) {
            while(x < 5) {
                if (table[4] > 0 && x == 1) {table[4]--; x += 4;} else
                if (table[7] > 0 && x == 2) {table[7]--; x += 3;} else
                if (table[1] > 0 && x == 3) {table[1]--; x += 2;} else //
                if (table[4] > 0) {table[4]--; x += 4;} else
                if (table[7] > 0) {table[7]--; x += 3;} else
                if (table[1] > 0) {table[1]--; x += 2;}
            }
        }
        while(x >= 5) {
            if (usable[5]) {table[5] = x / 5; x = x % 5;} else
            if (usable[3]) {table[3] = x / 5; x = x % 5;} else
            if (usable[2]) {table[2] = x / 5; x = x % 5;}
        }
    }
    // 6本: 6, 9
    if (x > 0) {
        while(x < 6) {
            if (table[2] > 0 && x == 1) {table[2]--; x += 5;} else
            if (table[3] > 0 && x == 1) {table[3]--; x += 5;} else
            if (table[5] > 0 && x == 1) {table[5]--; x += 5;} else
            if (table[4] > 0 && x == 2) {table[4]--; x += 4;} else
            if (table[7] > 0 && x == 3) {table[7]--; x += 3;} else
            if (table[1] > 0 && x == 4) {table[1]--; x += 2;} else //
            if (table[2] > 0) {table[2]--; x += 5;} else
            if (table[3] > 0) {table[3]--; x += 5;} else
            if (table[5] > 0) {table[5]--; x += 5;} else
            if (table[4] > 0) {table[4]--; x += 4;} else
            if (table[7] > 0) {table[7]--; x += 3;} else
            if (table[1] > 0) {table[1]--; x += 2;}
        }
        while(x >= 6) {
            if (usable[9]) {table[9] = x / 6; x = x % 6;} else
            if (usable[6]) {table[6] = x / 6; x = x % 6;}
        }
    }
    // 7本: 8
    if (x > 0) {
        while(x < 7) {
            if (table[6] > 0 && x == 1) {table[6]--; x += 6;} else
            if (table[9] > 0 && x == 1) {table[9]--; x += 6;} else
            if (table[2] > 0 && x == 2) {table[2]--; x += 5;} else
            if (table[3] > 0 && x == 2) {table[3]--; x += 5;} else
            if (table[5] > 0 && x == 2) {table[5]--; x += 5;} else
            if (table[4] > 0 && x == 3) {table[4]--; x += 4;} else
            if (table[7] > 0 && x == 4) {table[7]--; x += 3;} else
            if (table[1] > 0 && x == 5) {table[1]--; x += 2;} else //
            if (table[6] > 0) {table[6]--; x += 6;} else
            if (table[9] > 0) {table[9]--; x += 6;} else
            if (table[2] > 0) {table[2]--; x += 5;} else
            if (table[3] > 0) {table[3]--; x += 5;} else
            if (table[5] > 0) {table[5]--; x += 5;} else
            if (table[4] > 0) {table[4]--; x += 4;} else
            if (table[7] > 0) {table[7]--; x += 3;} else
            if (table[1] > 0) {table[1]--; x += 2;}
        }
        while (x >= 7) {
            if (usable[8]) {table[8] = x / 7; x = x % 7;}
        }
    }

    print_table(table);

    return 0;
}