Problem:
Coin Changing Problem

Find the minimum number of coins to make change for n cents using coins of denominations d1, d2,.., dm. The coins can be used any number of times.

Input

n m
d1 d2 ... dm

Two integers n and m are given in the first line. The available denominations are given in the second line.

Output

Print the minimum number of coins in a line.

Constraints

1 ≤ n ≤ 50000

1 ≤ m ≤ 20

1 ≤ denomination ≤ 10000

The denominations are all different and contain 1.

Sample Input 1

55 4
1 5 10 50

Sample Output 1

2

Sample Input 2

15 6
1 2 7 8 12 50

Sample Output 2

2

Sample Input 3

65 6
1 2 7 8 12 50

Sample Output 3

3

Solution in Haskell (judge verdict: Time Limit Exceeded)

import Data.Array

data Int_inf = Fin Int | Inf | Ninf  deriving (Show,Eq)

instance Ord Int_inf where
  compare Inf (Fin _) = GT
  compare (Fin _) Inf = LT
  compare Ninf (Fin _)= LT
  compare (Fin _) Ninf= GT
  compare (Fin x) (Fin y) = compare x y
  compare Inf Ninf = LT
  compare Ninf Inf = GT
  compare Inf Inf = EQ
  compare Ninf Ninf = EQ

instance Num Int_inf where
  (+) Inf (Fin _) =Inf
  (+) (Fin _) Inf =Inf
  (+) (Fin x) (Fin y) = Fin (x+y)
  (+) Inf Inf = Inf
  (+) Ninf Ninf = Ninf
  (+) Inf Ninf = undefined
  (+) Ninf Inf = undefined
  (*) Inf (Fin x) = if x>0 then Inf else if x<0 then Inf else undefined
  abs Inf = Inf
  abs Ninf = Inf
  abs (Fin x) = Fin (abs x)
  fromInteger x = (Fin (fromInteger x))
  negate (Fin x) = Fin (negate x)
  negate Inf = Ninf
  negate Ninf = Inf
  signum (Fin x) = (Fin (signum x))
  signum Inf  = Fin 1
  signum Ninf = Fin (-1)

instance Real Int_inf where
  toRational (Fin x) =toRational x
  toRational Inf     = undefined
  toRational Ninf    = undefined
--ナイーブな再帰：面額リスト、使う番号、金額、その時の枚数（不能な時は無限）
naive_rec::Array Int Int -> Int -> Int -> Int_inf
naive_rec _     0 k = if k==0 then Fin 0 else Inf
naive_rec item  i k = minimum [ (naive_rec item (i-1) (k - (j*(item!i))))+(Fin j) | j <- [0..div k (item!i)]  ]

--再帰アレイリストによるメモ。
list_memo::Array Int Int -> Int -> Int -> [Array Int Int_inf]
list_memo item i k = (listArray (0,k) ((Fin 0):(replicate k Inf))):
                                    [listArray (0,k)
                                        [minimum
                                            [prev!(j-l*(item!i))+(Fin l)
                                            |l<-[0.. div j (item!i)]]
                                        |j<-[0..k]]
                                    | (prev,i) <-zip (list_memo item i k) [1..]]

--アイテムリスト：アイテムリストサイズ：目標金額：答え
solver::[Int]->Int->Int->Int
solver item_ls m k = let item = listArray (1,m) item_ls in
                     let Fin x = (last (list_memo item m k))!k in
                     x


main::IO()
main=do
  kmc<-getLine
  let k:m:[]=map read (words kmc)
  datc<-getLine
  let item_ls = map read (words datc)
  print (solver item_ls m k)