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 Java8 (OpenJDK 1.8.0) (judge verdict: Runtime Error)

import java.util.*;
import java.io.*;

public class Main{
    static int[] dp, a;
    static int amin=10;
    static final int num[]={0,2,5,5,4,5,6,3,7,6};
    static ArrayList<Integer> ans=new ArrayList<>();

    static int dp(int i){
        int max=0;
        if(i<amin) return Integer.MIN_VALUE;
        if(dp[i]>0) return dp[i];
        for(int k:a) if(dp(i-num[k])+1>max) max=dp(i-num[k])+1;
        dp[i]=max;
        return dp[i];
    }
    public static void main(String[] args) throws Exception {
        FastScanner fsc=new FastScanner();
        final int n=fsc.nextInt(), m=fsc.nextInt();
        a=new int[m];
        dp=new int[Math.max(n+1, 8)];
        for(int i=0;i<m;i++){
            a[i]=fsc.nextInt();
            dp[num[a[i]]]=1;
            amin=Math.min(amin, a[i]);
        }
        fsc.close();
        if(dp[2]>0){
            dp[4]=2; dp[6]=3;
            if(dp[3]>0){
                dp[5]=2; dp[7]=3;
            }
            else if(dp[5]>0) dp[7]=2;
        }
        else if(dp[3]>0){
            dp[6]=2;
            if(dp[4]>0) dp[7]=2;
        }
        int d=dp(n);
        //for(int i=0;i<=n;i++) System.out.println("dp["+i+"]:"+dp[i]);
        int now=n;
        Arrays.sort(a);
        for(int i=d;i>=1;i--){
            for(int j=a.length-1;j>=0;j--){
                if(now-num[a[j]]<0) continue;
                if(i==1){
                    if(now-num[a[j]]==0){
                        ans.add(a[j]);
                        now=now-num[a[j]];
                        break;
                    }
                }
                else{
                    if(dp[now-num[a[j]]]==i-1){
                        ans.add(a[j]);
                        now-=num[a[j]];
                        break;
                    }
                }
            }
        }
        if(now!=0) throw new Exception();
        if(ans.size()!=d) throw new Exception();
        StringBuilder sb=new StringBuilder("");
        for(int k:ans) sb.append(k);
        System.out.println(sb);
    }
    static class FastScanner {
        private final InputStream in = System.in;
        private final byte[] buffer = new byte[1024];
        private int ptr = 0;
        private int buflen = 0;
        private boolean hasNextByte(){
            if(ptr<buflen) return true;
            else{
                ptr = 0;
                try{
                    buflen = in.read(buffer);
                }
                catch(IOException e){
                    e.printStackTrace();
                }
                if(buflen<=0) return false;
            }
        return true;
        }
        private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
        private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
        public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();}
        public String next() {
            if (!hasNext()) throw new NoSuchElementException();
            StringBuilder sb = new StringBuilder();
            int b = readByte();
            while(isPrintableChar(b)) {
                sb.appendCodePoint(b);
                b = readByte();
            }
            return sb.toString();
        }
        public long nextLong() {
            if (!hasNext()) throw new NoSuchElementException();
            long n = 0;
            boolean minus = false;
            int b = readByte();
            if (b == '-') {
                minus = true;
                b = readByte();
            }
            if (b < '0' || '9' < b) throw new NumberFormatException();
            while(true){
                if ('0' <= b && b <= '9') {
                    n *= 10;
                    n += b - '0';
                }
                else if(b == -1 || !isPrintableChar(b)) return minus ? -n : n;
                else throw new NumberFormatException();
                b = readByte();
            }
        }
        public int nextInt() {
            long nl = nextLong();
            if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
            return (int) nl;
        }
        public double nextDouble() { return Double.parseDouble(next());}
        public void close(){
            try{in.close();}
            catch(IOException e){e.printStackTrace();}
        }
    }
}