use std::io::BufRead;

pub struct StdinReader<R: BufRead> {
    pub buf_reader: R,
    pub buf: String,
}

impl<R: BufRead> StdinReader<R> {
    pub fn new(r: R) -> Self {
        StdinReader {
            buf_reader: r,
            buf: String::with_capacity(10000),
        }
    }
}

#[macro_export]
macro_rules! input {
    ($reader : expr, $t : ty) => { // one variable
        match &mut $reader {
            reader => {
                use std::io::BufRead;
                reader.buf_reader.read_line(&mut reader.buf).unwrap();
                let tmp_var: $t = reader.buf.trim().parse().unwrap();
                reader.buf.clear();
                tmp_var
            },
        }
    };
    ($reader : expr, $t0 : ty, $t1 : ty) => { // 2 tuple
        match &mut $reader {
            reader => {
                use std::io::BufRead;
                reader.buf_reader.read_line(&mut reader.buf).unwrap();
                let tmp_var: Vec<$t0> = reader.buf.trim().split_whitespace().map(|a| a.parse().unwrap()).collect();
                reader.buf.clear();
                (tmp_var[0].clone(), tmp_var[1].clone())
            }
        }
    };
    ($reader : expr, $t0 : ty, $t1 : ty, $t2 : ty) => { // 3 tuple
        match &mut $reader {
            reader => {
                use std::io::BufRead;
                reader.buf_reader.read_line(&mut reader.buf).unwrap();
                let tmp_var: Vec<$t0> = reader.buf.trim().split_whitespace().map(|a| a.parse().unwrap()).collect();
                reader.buf.clear();
                (tmp_var[0].clone(), tmp_var[1].clone(), tmp_var[2].clone())
            }
        }
    };
    ($reader : expr, $t : ty; $size : expr) => { // Vector
        match &mut $reader {
            reader => {
                use std::io::BufRead;
                reader.buf_reader.read_line(&mut reader.buf).unwrap();
                let tmp_var: $t = reader.buf.trim().split_whitespace().map(|a| a.parse().unwrap()).collect();
                reader.buf.clear();
                tmp_var
            }
        }
    };
}

const match_set: [usize; 10] = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6];

fn main() {
    let handle = std::io::stdin();
    let mut r = StdinReader::new(handle.lock());
    let (n, m): (usize, usize) = input!(r, usize, usize);
    let nums: Vec<usize> = input!(r, Vec<usize>; m);
    let mut nums_match: Vec<(usize, usize)> = nums.iter().map(|&a| (a, match_set[a])).collect();

    nums_match.sort_by(|a, b| b.0.cmp(&a.0));
    let mut dp: Vec<Option<usize>> = vec![None; n + 1];
    dp[0] = Some(0);
    use std::cmp::max;
    for i in 0..n - 1 {
        if let Some(now) = dp[i] {
            for m in &nums_match {
                if i + m.1 > n {
                    continue;
                }
                dp[i + m.1] = max(Some(1 + now), dp[i + m.1]);
            }
        }
    }
    let digit = dp[n].unwrap();
    let mut left_match = n;
    for i in 0..digit {
        for num in &nums_match {
            if left_match < num.1 { continue; }
            if dp.get(left_match - num.1) == Some(&Some(digit - i - 1)) {
                left_match -= num.1;
                print!("{}", num.0);
                break;
            }
        }
    }
    println!("");
}


Measured memory_bytes: 4352