Concept
Suppose we had a bool array representing which numbers have not yet been found (by summing).

For each number n we meet in an ordered (increasing value) subset of S , we do the following:
- For each existing
True value in position i in numbers we set numbers[i + n] to True - Set
numbers[n] to True
With the help of such a sieve, we mark all the numbers found as True and iterate over the array when the algorithm completes, we find the minimum unattainable amount.
Completion
Obviously, we cannot have such a solution, because the array must be infinite in order to work for all sets of numbers.
The concept can be improved by making a few comments. When you enter 1, 1, 3 array becomes (sequentially):
(numbers represent True values) 

An important point to make:
- (3) For each next number, if previous numbers have already been found, it will be added to all these numbers. This means that if there were no spaces before the number, there will be no spaces after this number .
For the next input 7 it can be argued that:
- (4) Since the input order is ordered, the number will not be less than
7 - (5) If the numbers are not less than
7 , then 6 cannot be obtained

We can conclude that:
- (6) the first gap is the minimum unattainable number .
Algorithm
Due to (3) and (6), we actually do not need the numbers array, we only need one value, max - is the maximum number found so far .
Thus, if the next number n greater than max + 1 , then a space will be made, and max + 1 is the minimum unattainable number.
Otherwise, max becomes max + n . If we skipped all S , the result would be max + 1 .
Actual code (C # easily converted to C):
static int Calculate(int[] S) { int max = 0; for (int i = 0; i < S.Length; i++) { if (S[i] <= max + 1) max = max + S[i]; else return max + 1; } return max + 1; }
It should run pretty fast, since this is obviously linear time (O (n)). Since the input to the function must be sorted, with quick sort it will be O (nlogn). I managed to get the results M = N = 100000 on 8 cores in just 5 minutes.
With the upper limit of the number 10 ^ 9, you can use the radix sort to approximate the O (n) time for sorting, however this will still be more than 2 seconds due to the large number of required types.
But we can use the statistical probability of 1 random to eliminate the subsets before sorting. First check if 1 exists in S , if not, then each query result is 1, because it cannot be received.
Statistically, if we randomly out of 10 ^ 9 numbers 10 ^ 5 times, we have a 99.9% chance of not getting a single one.
Before each sort, check to see if it contains a subset of 1; if not, then its result is one.
With this modification, the code runs on 2 milliseconds on my machine. Here is the code at http://pastebin.com/rF6VddTx