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 C# (judge verdict: Runtime Error)

using System;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int n,m;
            int[] c = new int[20];
            int[,] dp = new int[50001,20];

            string[] nm = Console.ReadLine().Split(' ');
            n = int.Parse(nm[0]);
            m = int.Parse(nm[1]);

            string[] C = Console.ReadLine().Split(' ');

            for (int a = 0; a < m;a++)
            {
                c[a] = int.Parse(C[a]);
            }
            
            for(int a = 0;a<n+1;a++)
            {
                dp[a,0] = a;
            }

            for(int a = 0;a<m;a++)
            {
                dp[0,m] = 0;
            }

            for (int a = 1; a < m;a++ )
            {
                for (int b = 1; b < n+1;b++)
                {
                    if (c[a] > b)
                    {
                        dp[b, a] = dp[b, a-1];
                    }
                    else
                    {
                        dp[b, a] = Math.Min(dp[b, a-1], Math.Min(dp[b-c[a],a],dp[b-1,a])+1);
                    }
                }
            }

            Console.WriteLine(dp[n, m - 1]);
        }
    }
}