An automatic machine has N coins inside, each one with a value. You should compute the smallest amount of money that the machine wouldn't be able to give.
In other words, given a set S of N positive integers, find the smallest integer wich cannot be expressed as a sum of one or more elements of S.
Example:
S: 10 2 14 1 13 2 3
Answer: 9
S: 1 16 2 1 23 18 1 4 3
Answer: 13
For N coins with maximum value MAX it's easy to find the answer in O(N2), for each new coin, I set its value as "avaible", then for all the older coins I set avaible the value coin[i] + new_coin
This would require an array of MAX booleans.
Now, I have 10 seconds for computing this answer for P machines.
Here's the hard part:
- 1 ≤ P ≤ 1000
- 1 ≤ Ni ≤ 10 000 (amount of coins per machine)
- 1 ≤ Mj < 220 (value of a single coin)
- For each machine, the sum of all coins is always smaller than 231
Then we consider two cases.
1) Ai is a consecutive sequence of values from 1 to some value X, where X is the sum of coins from C1 to Ci. Then we know that the answer is not in the range [1,X].
2) Ai is not a consecutive sequence. Suppose there exist some value Y in A such that Y+1 is not in the sequence and Y is minimal. Then Y+1 is your answer.
So when you loop through the sorted sequence of coins, all you need to do is to keep track of the value X. The moment the sequence becomes disjoint, you have your answer.