Problem: 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.

Submission attempts (in order):
# coding: utf-8
import sys


# import math
# import itertools

# import numpy as np


"""Template"""


class IP:
    

    def __init__(self):
        self.input = sys.stdin.readline

    def I(self):
        
        return int(self.input())

    def S(self):
        
        return self.input()

    def IL(self):
        
        return list(map(int, self.input().split()))

    def SL(self):
        
        return list(map(str, self.input().split()))

    def ILS(self, n):
        
        return [int(self.input()) for _ in range(n)]

    def SLS(self, n):
        
        return [self.input() for _ in range(n)]

    def SILS(self, n):
        
        return [self.IL() for _ in range(n)]

    def SSLS(self, n):
        
        return [self.SL() for _ in range(n)]


class Idea:
    def __init__(self):
        pass

    def HF(self, p):
        
        return sorted(set(p[i] + p[j] for i in range(len(p)) for j in range(i, len(p))))

    def Bfs2(self, a):
        
        
        # https://blog.rossywhite.com/2018/08/06/bit-search/
        
        value = []
        for i in range(1 << len(a)):
            output = []

            for j in range(len(a)):
                if self.bit_o(i, j):
                    
                    # output.append(a[j])
                    output.append(a[j])
            value.append([format(i, 'b').zfill(16), sum(output)])

        value.sort(key=lambda x: x[1])
        bin = [value[k][0] for k in range(len(value))]
        val = [value[k][1] for k in range(len(value))]
        return bin, val

    def S(self, s, r=0, m=-1):
        
        r = bool(r)
        if m == -1:
            s.sort(reverse=r)
        else:
            s.sort(reverse=r, key=lambda x: x[m])

    def bit_n(self, a, b):
        
        return bool((a >> b & 1) > 0)

    def bit_o(self, a, b):
        
        return bool(((a >> b) & 1) == 1)

    def ceil(self, x, y):
        
        return -(-x // y)

    def ave(self, a):
        
        return sum(a) / len(a)

    def gcd(self, x, y):
        if y == 0:
            return x
        else:
            return self.gcd(y, x % y)





def main():
    
    r, e = range, enumerate
    ip = IP()
    id = Idea()

    
    n, m = ip.IL()
    a = ip.IL()
    c = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
    f = [-1] * n
    f[0] = 0

    for i in r(n):
        i += 1
        for x in a:
            if i - c[x] >= 0:
                f[i] = max(f[i], f[i - c[x]] * 10 + x)

    print(f[n])


main()


# coding: utf-8
import sys


# import math
# import itertools
# import numpy as np


"""Template"""


class IP:
    

    def __init__(self):
        self.input = sys.stdin.readline

    def I(self):
        
        return int(self.input())

    def S(self):
        
        return self.input()

    def IL(self):
        
        return list(map(int, self.input().split()))

    def SL(self):
        
        return list(map(str, self.input().split()))

    def ILS(self, n):
        
        return [int(self.input()) for _ in range(n)]

    def SLS(self, n):
        
        return [self.input() for _ in range(n)]

    def SILS(self, n):
        
        return [self.IL() for _ in range(n)]

    def SSLS(self, n):
        
        return [self.SL() for _ in range(n)]


class Idea:
    def __init__(self):
        pass

    def HF(self, p):
        
        return sorted(set(p[i] + p[j] for i in range(len(p)) for j in range(i, len(p))))

    def Bfs2(self, a):
        
        
        # https://blog.rossywhite.com/2018/08/06/bit-search/
        
        value = []
        for i in range(1 << len(a)):
            output = []

            for j in range(len(a)):
                if self.bit_o(i, j):
                    
                    # output.append(a[j])
                    output.append(a[j])
            value.append([format(i, 'b').zfill(16), sum(output)])

        value.sort(key=lambda x: x[1])
        bin = [value[k][0] for k in range(len(value))]
        val = [value[k][1] for k in range(len(value))]
        return bin, val

    def S(self, s, r=0, m=-1):
        
        r = bool(r)
        if m == -1:
            s.sort(reverse=r)
        else:
            s.sort(reverse=r, key=lambda x: x[m])

    def bit_n(self, a, b):
        
        return bool((a >> b & 1) > 0)

    def bit_o(self, a, b):
        
        return bool(((a >> b) & 1) == 1)

    def ceil(self, x, y):
        
        return -(-x // y)

    def ave(self, a):
        
        return sum(a) / len(a)

    def gcd(self, x, y):
        if y == 0:
            return x
        else:
            return self.gcd(y, x % y)





def main():
    
    r, e = range, enumerate
    ip = IP()
    id = Idea()

    
    n, m = ip.IL()
    a = ip.IL()
    c = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]  
    f = [0] + [-1] * 10010  # DP
    
    id.S(a, 1)
    
    
    for i in r(n):
        
        i += 1
        for x in a:
            if i - c[x] >= 0:
                f[i] = max(f[i], f[i - c[x]] * 10 + x)
                break

    print(f[n])
    print(f)


main()


# coding: utf-8
import sys


# import math
# import itertools

# import numpy as np


"""Template"""


class IP:
    

    def __init__(self):
        self.input = sys.stdin.readline

    def I(self):
        
        return int(self.input())

    def S(self):
        
        return self.input()

    def IL(self):
        
        return list(map(int, self.input().split()))

    def SL(self):
        
        return list(map(str, self.input().split()))

    def ILS(self, n):
        
        return [int(self.input()) for _ in range(n)]

    def SLS(self, n):
        
        return [self.input() for _ in range(n)]

    def SILS(self, n):
        
        return [self.IL() for _ in range(n)]

    def SSLS(self, n):
        
        return [self.SL() for _ in range(n)]


class Idea:
    def __init__(self):
        pass

    def HF(self, p):
        
        return sorted(set(p[i] + p[j] for i in range(len(p)) for j in range(i, len(p))))

    def Bfs2(self, a):
        
        
        # https://blog.rossywhite.com/2018/08/06/bit-search/
        
        value = []
        for i in range(1 << len(a)):
            output = []

            for j in range(len(a)):
                if self.bit_o(i, j):
                    
                    # output.append(a[j])
                    output.append(a[j])
            value.append([format(i, 'b').zfill(16), sum(output)])

        value.sort(key=lambda x: x[1])
        bin = [value[k][0] for k in range(len(value))]
        val = [value[k][1] for k in range(len(value))]
        return bin, val

    def S(self, s, r=0, m=-1):
        
        r = bool(r)
        if m == -1:
            s.sort(reverse=r)
        else:
            s.sort(reverse=r, key=lambda x: x[m])

    def bit_n(self, a, b):
        
        return bool((a >> b & 1) > 0)

    def bit_o(self, a, b):
        
        return bool(((a >> b) & 1) == 1)

    def ceil(self, x, y):
        
        return -(-x // y)

    def ave(self, a):
        
        return sum(a) / len(a)

    def gcd(self, x, y):
        if y == 0:
            return x
        else:
            return self.gcd(y, x % y)





def main():
    
    r, e = range, enumerate
    ip = IP()
    id = Idea()

    
    n, m = ip.IL()
    a = ip.IL()
    c = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
    f = [-1] * (n + 10)
    f[0] = 0

    for i in r(n):
        i += 1
        for x in a:
            if i - c[x] >= 0:
                f[i] = max(f[i], f[i - c[x]] * 10 + x)

    print(f[n])
    print(f)


main()


n, m = map(int, input().split())
a = list(map(int, input().split()))
c = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]  
f = [0] + [0] * 10000  # DP


for i in range(n):
    
    i += 1
    for x in a:
        
        

        o = c[x]  

        if i - o >= 0:  
            
            f[i] = max(f[i], f[i - o] * 10 + x)

print(f[n])


n, m = map(int, input().split())
a = list(map(int, input().split()))
c = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]  
f = [0] + [-1] * 10000  # DP


for i in range(n):
    
    i += 1
    for x in a:
        
        

        o = c[x]  

        if i - o >= 0:  
            
            f[i] = max(f[i], f[i - o] * 10 + x)

print(f[n])


Verdicts: Runtime Error

Wrong Answer

Wrong Answer

Wrong Answer

Accepted