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 Python (3.4.3) (judge verdict: Runtime Error)

import math

n,m = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]

#値と本数のセット（本数が少ない順かつ値が大きい順）
set = [[1,2],[7,3],[4,4],[5,5],[3,5],[2,5],[9,6],[6,6],[8,7]]

#同じ本数で値が小さい数字を使うことはない
if 2 in a:
  if 3 in a or 5 in a:
    a.remove(2)
    m -= 1

if 3 in a:
  if 5 in a:
    a.remove(3)
    m -= 1
  
if 6 in a:
  if 9 in a:
    a.remove(6)
    m -= 1

#aの最大値。あとで使用
max_a = max(a)

#上記の処理を施した上で与えられたaの数と本数の組み合わせを抽出
exist_set = [i for i in set if i[0] in a]

#最大桁数のうち値が大きいもの
keta = 0
num = 0
for i in exist_set:
  keta_tmp = n//i[1]
  num_tmp = i[0]
  amari_tmp = n % i[1]
  #余りが0出ない場合、aの最大値は不採用（余りを調整できない）
  if amari_tmp != 0 and num_tmp == max_a:
    continue
  if keta_tmp >= keta and num_tmp > num:
    keta = keta_tmp
    num = num_tmp
    amari = amari_tmp
    honsu = i[1]

    #余り0ならちょうどなので終了
    if amari == 0:
      print(str(num)*keta)
      exit()
      
    #以下余りを調整

    #変更する値は必ずnum以外だからexist_setからnumを削除
    exist_yet = [i for i in exist_set if i[0] != num]
    m -= 1

    #1桁ずつ値を変更し、あまりを0にできるか調査
    for i in range(1,num):

      #bit全探索
      bit = math.ceil(math.log2(m))
      kouho = []
      for j in range((2**m)*i):
        change = []
        nokori = amari + honsu * i
        for k in range(i):
          bit_index =  (j >> k*bit) & (2**m-1)

          #bit_indexは0~m-1だからm以上は終了
          if bit_index >= m:
            break

          #bit_indexに従い数字を記録、残りの本数を減らす
          change.append(exist_yet[bit_index][0])
          nokori -= exist_yet[bit_index][1]

          if nokori < 0:
            break

        #残りが0ならちょうど使い切る。数の組み合わせを候補に追加
        if nokori == 0:
          kouho.append(change)
      
      #候補があればループを抜ける
      if len(kouho) != 0:
        break

    if len(kouho) == 0:
      continue
 
    #候補から値の大きいものを抜き出す

    #候補を降順に並び替え
    kouho = [sorted(i,reverse=True) for i in kouho]

    #同じパターンを削除
    kouho_tmp = []
    for i in kouho:
      if i not in kouho_tmp:
        kouho_tmp.append(i)
    kouho = kouho_tmp

    #最大候補の抽出
    for i in range(len(kouho[0])):
      if len(kouho) == 1:
        break
      max_i = max([j[i] for j in kouho])
      kouho = [j for j in kouho if j[i] == max_i]
    kouho = kouho[0]

    ans = [num]*(keta-len(kouho)) + kouho
    ans.sort(reverse=True)
    ans_str = ''
    for i in ans:
      ans_str += str(i)
    print(ans_str)
    exit()