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 OCaml (judge verdict: Accepted)

let dbg = Printf.printf "[debug]%s"

let id = fun x -> x
let tuple2 x y = (x,y)

let (++) n m =
  let rec aux i =
    if i = m then [m]
    else i :: aux (i+1) in
  if n > m then [] else aux n

let (++^) n m = n ++ (m-1)

let scanf fmt f = Scanf.sscanf (read_line ()) fmt f

let scanfs n fmt f =
  List.map (fun _ -> scanf fmt f) (0++^n)

let scan_array n conv =
  let b = Scanf.Scanning.from_string @@ read_line () in
  Array.init n (fun _ -> Scanf.bscanf b " %s" conv)

let scan_matrix n m e conv =
  let arr = Array.make_matrix n m e in
  Array.iteri (fun i line ->
      let s = Scanf.Scanning.from_string @@ read_line () in
      Array.iteri (fun j _ ->
          arr.(i).(j) <- Scanf.bscanf s " %s" conv;
        ) line) arr; arr

let between n x m = n <= x && x < m

let (n,m) = scanf "%d %d" tuple2
let coins = scan_array m int_of_string

let () =
  let memo = Array.make_matrix (m+1) (n+1) max_int in
  memo.(0).(0) <- 0;
  List.iter (fun i ->
      let c = coins.(i-1) in
      List.iter (fun j ->
          memo.(i).(j) <-
            if j -c >= 0 then
              let x = if memo.(i-1).(j-c) <> max_int
                then memo.(i-1).(j-c) + 1 else max_int in
              let y = if memo.(i).(j-c) <> max_int
                then memo.(i).(j-c) + 1 else max_int in
              min memo.(i-1).(j) @@ min x y
            else
              memo.(i-1).(j)
        ) (0++n)
    ) (1++m);
  Printf.printf "%d\n" memo.(m).(n)