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 Kotlin (1.0.0) (judge verdict: Accepted)

import java.lang.Math.max
import java.lang.Math.sqrt

const val MOD = 1000000007L
const val INF = Long.MAX_VALUE - 1

fun main(args: Array<String>) {
    val (n, m) = readIntList()

    val a = readIntList().map {
        match(it)
    }.sortedByDescending {
        it.num
    }

    val dp = Array<Long>(n + 1) {
        if (it == 0) return@Array 0L
        return@Array -1L
    }


    fun dfs(n: Int): Long {
        if (dp[n] != -1L) return dp[n]

        var max = -1L
        a.map { it.cost }.toSet().forEach {
            if (n - it >= 0) {
                max = max(max, dfs(n - it) + 1)
            }
        }
        dp[n] = max
        return max
    }

    val maxLen = dfs(n)
    var N = n
    maxLen.rep {
        val max = maxLen - it
        a.forEach {
            if (N - it.cost >= 0 && dp[N - it.cost] == dp[N] - 1) {
                N -= it.cost
                print(it.num)
                return@rep
            }
        }
    }
    println()

}

data class match(
    val num: Int
) {
    val cost = when (num) {
        1 -> 2
        7 -> 3
        4 -> 4
        2, 3, 5 -> 5
        6, 9 -> 6
        8 -> 7
        else -> -1
    }
}

fun readList() = readLine()!!.split(' ')
fun readInt() = readIntList().first()
fun readIntList() = readList().map(String::toInt)
fun readLong() = readLongList().first()
fun readLongList() = readList().map(String::toLong)
fun readDoubleList() = readList().map(String::toDouble)

fun <T> readClass(n: Int, body: (List<String>) -> T): List<T> {
    val ret = mutableListOf<T>()
    for (i in 0 until n) {
        ret.add(body(readList()))
    }
    return ret
}

fun readPair(): Pair<String, String> {
    val splite = readLine()!!.split(' ')
    return Pair(splite[0], splite[1])
}

fun <T> List<T>.copy() = this.map { it }

fun Long.toBinaryString(): String {
    var t = this
    var res = ""
    while (t > 0) {
        res += (t % 2).toString()
        t /= 2
    }
    return res
}

inline fun <reified T> dualArray(h: Int, w: Int, init: (Int, Int) -> T) {
    Array(h) { i ->
        return@Array Array(w) { j ->
            return@Array init(i, j)
        }
    }
}

// 速度のためforEachは避ける
inline fun Int.rep(start: Int = 0, shiftLast: Boolean = false, body: (Int) -> Unit) {
    for (i in start until if (shiftLast) this + start else this) {
        body(i)
    }
}

inline fun Long.rep(start: Long = 0, shiftLast: Boolean = false, body: (Long) -> Unit) {
    for (i in start until if (shiftLast) this + start else this) {
        body(i)
    }
}

// 約数のList
fun divisor(value: Long): List<Long> {
    val max = sqrt(value.toDouble()).toLong()
    val former = mutableListOf<Long>()
    val latter = mutableListOf<Long>()
    (1..max).filter { value % it == 0L }
        .forEach {
            former.add(it)
            latter.add(value / it)
        }
    former.addAll(latter.reversed())
    return former
}

// 範囲内の素数を取得
// fromだけ指定すると戻り値の個数で素数判定ができる
fun prime(from: Long, to: Long = from): List<Long> {
    return (from..to).filter { i ->
        val max = Math.sqrt(i.toDouble()).toLong()
        (2..max).all { j -> i % j != 0L }
    }
}

// 素因数分解
fun decom(value: Long): List<Long> {
    if (value == 1L) return listOf(1)
    val max = Math.sqrt(value.toDouble()).toLong()
    return prime(2, max).filter { value % it == 0L }
}

// 最大公約数
fun gcd(a: Long, b: Long): Long {
    return if (a % b == 0L) b else gcd(b, a % b)
}

// 文字列を入れ替え
fun swap(base: String, a: String, b: String): String {
    return base.map {
        when (it) {
            a.toCharArray()[0] -> b
            b.toCharArray()[0] -> a
            else -> it.toString()
        }
    }.joinToString()
}

// bit全探索
fun bitAllSearch(n: Int, body: (Array<Boolean>) -> Unit) {
    for (bit in 0 until (1 shl n)) {
        val s = Array(n) {
            return@Array false
        }
        for (i in 0 until n) {
            if ((bit and (1 shl i)) shr i == 1) { // i が bit に入るかどうか
                s[i] = true
            }
        }
        body(s)
    }
}

internal class UnionFindTree(size: Int) {

    private val parent = Array(size) {
        return@Array it
    }
    private val rank = Array(size) {
        return@Array 0
    }

    fun union(x: Int, y: Int): Int {
        val xRoot = find(x)
        val yRoot = find(y)

        if (xRoot == yRoot) return xRoot

        when {
            rank[xRoot] > rank[yRoot] -> parent[yRoot] = xRoot
            rank[xRoot] < rank[yRoot] -> parent[xRoot] = yRoot
            xRoot != yRoot -> {
                parent[yRoot] = xRoot
                rank[xRoot]++
            }
        }
        return parent[xRoot]
    }

    fun find(i: Int): Int {
        if (i != parent[i]) {
            parent[i] = find(parent[i])
        }
        return parent[i]
    }

    fun same(x: Int, y: Int): Boolean {
        return find(x) == find(y)
    }
}