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

if !ENV['RUBY_THREAD_VM_STACK_SIZE']
    #Rubyパスを取得するには、rbconfigかrubygemsを使う。AtCoderでは--disable-gemsされているので、require 'rubygems'は必須である。
    #require 'rbconfig';RUBY=File.join(RbConfig::CONFIG['bindir'],RbConfig::CONFIG['ruby_install_name'])
    require 'rubygems';RUBY=Gem.ruby
    exec({'RUBY_THREAD_VM_STACK_SIZE'=>'100000000'},RUBY,$0) #100MB
end

count = 0
$n = 0
$m = 0
coin_str = ""
STDIN.each_line {|line|
  if count == 0
    count = 1
    n_m = line.split " "
    $n = n_m[0].to_i
    $m = n_m[1].to_i
  end

  coin_str = line
}

$coin_list = coin_str.split " "
$coin_list.map! {|item| item.to_i }
# $coin_list.sort! {|a, b| b <=> a }

$coin_count_map = {}
for index in 1..$n
  $coin_count_map[index] = $n + 1
end

$coin_count_map[0] = 0
def solve i
    if i == $m
      return $coin_count_map[$n]
    end

    # if n == 0
    #   return $coin_count_map[0]
    # end

    now_coin = $coin_list[i]
    for price in now_coin..$n
      $coin_count_map[price] = [$coin_count_map[price], $coin_count_map[price - now_coin] + 1].min
    end
    # tmp = n - coin
    # solve i + 1

  return solve i + 1
end

puts solve 0