This is the Frobenius Problem which is in general NP-Hard.
For small instances, reasonably fast algorithms are known, though.
The paper here: http://www.combinatorics.org/Volume_12/PDF/v12i1r27.pdf seems to describe previous algorithms (which includes an application of Dijkstra's shortest path algorithm!) plus it gives a new algorithm which is apparently faster than the previous ones.
In any case, for the case when there are only 2 numbers, a and b such that gcd(a,b) = 1, finding i, j >= 0 such that a*i + b*j = M is easy to solve.
It is also known that any number greater than (a-1)(b-1) can be represented in the form a*i + b*j, with i >=0 and j>= 0. The Frobenius number is defined to be the largest number that cannot be represented in that form, and exists when n >= 2 and gcd(a,b,c...) = 1.
So in your case, if the numbers involved are small enough, you could sort the array, find the 'smallest' two a and b such that gcd(a,b) = 1 and see if M >(a-1)(b-1), which can be solved just using a and b.
if M <= (a-1)(b-1), and a and b are small enough, you might just be able to brute force it out.