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 Ruby (2.3.3) (judge verdict: Wrong Answer)

DBG = true
n,m = gets.split.map{|z| z.to_i}
a = gets.split.map{|z| z.to_i}

has4 = a.include?(4)
has5 = a.include?(5)||a.include?(3)||a.include?(2)
has6 = a.include?(6)||a.include?(9)
has7 = a.include?(7)
has8 = a.include?(8)
char5 = a.include?(5) ? "5" : a.include?(3) ? "3" : "2"
char6 = a.include?(9) ? "9" : "6"

if a.include?(1)
  if n%2 == 0
    (n/2).times { print "1" }
  elsif a.include?(7)
    print "7"
    (n/2-1).times { print "1" }
  elsif has5
    print char5
    (n/2-2).times { print "1" }
  else
    print "8"
    (n/2-3).times { print "1" }
  end

elsif a.include?(7)
  if n%3 == 0
    (n/3).times { print "7" }
  elsif n%3 == 1
    if a.include?(4)
      (n/3-1).times { print "7" }
      print "4"
    elsif a.include?(8)
      print "8"
      (n/3-2).times { print "7" }
    else
      (n/3-3).times { print "7" }
      2.times { print char5 }
    end
  else # n%3 == 2
    if has5
      (n/3-1).times { print "7" }
      print char5
    elsif a.include?(4)
      (n/3-2).times { print "7" }
      2.times { print "4" }
    else
      2.times { print "8" }
      (n/3-4).times { print "7" }
    end
  end

elsif a.include?(4)
  if n%4 == 0
    (n/4).times { print "4" }
  elsif n%4 == 1
    if has5
      (n/4-1).times { print "4" }
      print char5
    elsif has6 && has8
      if a.include?(9)
        print "9"
        print "8"
        (n/4-3).times { print "4" }
      else
        print "8"
        print "6"
        (n/4-3).times { print "4" }
      end
    elsif !has5 && !has6 && has8
      3.times { print "8" }
      (n/4-5).times { print "4" }
    end
  elsif n%4 == 2
    if has6
      print char6
      (n/4-1).times { print "4" }
    elsif has5
      if a.include?(5)
        2.times { print "5" }
        (n/4-2).times { print "4" }
      else
        (n/4-2).times { print "4" }
        2.times { print char5 }
      end
    elsif a.include?(8)
      2.times { print "8" }
      (n/4-3).times { print "4" }
    end
  else # if n%4 == 3
    if has8
      print "8"
      (n/4-1).times { print "4" }
    elsif has6 && has5
      print char6
      print "5" if a.include?(5)
      (n/4-2).times { print "4" }
      print char5 if !a.include?(5)
    else # has5
      3.times { print "5" } if a.include?(5)
      (n/4-3).times { print "4" }
      3.times { print char5 } if !a.include?(5)
    end
  end

elsif has5
  if n%5 == 0
    (n/5).times { print char5 }
  elsif n%5 == 1
    if has6
      print char6
      (n/5-1).times { print char5 }
    else # !has6&&has8
      3.times { print "8" }
      (n/5-4).times { print char5 }
    end
  elsif n%5 == 2
    if has8 && !a.include?(9)
      print "8"
      (n/5-1).times { print char5 }
    else # has6&&!has8
      2.times { print char6 }
      (n/5-2).times { print char5 }
    end

  elsif n%5 == 3
    if has6 && has8
      print char6 if a.include?(9)
      print "8"
      print char6 if !a.include?(9)
      (n/5-2).times { print char5 }
    elsif has6
      3.times { print char6 }
      (n/5-3).times { print char5 }
    else # if has8
      4.times { print "8" }
      (n/5-5).times { print char5 }
    end

  else # n%5==4
    if has8
      2.times { print "8" }
      (n/5-2).times { print char5 }
    else # has6&&!has8
      4.times { print char6 }
      (n/5-4).times { print char5 }
    end

  end

elsif has6
  md = n%6
  if md == 0
    (n/6).times { print char6 }
  else
    (n/6-md).times { print char6 } if a.include?(9)
    md.times { print "8" }
    (n/6-md).times { print char6 } if !a.include?(9)
  end

else # has8
  (n/7).times { print "8" }

end

puts ""