Problem 670
Patching Array
Problem
You are given a sorted array of positive integers and a number TARGET. Add (patch) as few numbers as possible to the array so that every integer from 1 to TARGET can be formed as the sum of some elements of the array, each element used at most once. Greedy: keep the smallest sum MISS that cannot be formed yet (start with 1). If the next array element is at most MISS, use it to extend the range to MISS + element; otherwise patch the number MISS itself, which doubles the range.
Input
The first line contains N and TARGET. The second line contains the N sorted array elements.
Output
Print the minimum number of patches.
Constraints
1 <= N <= 1000; 1 <= A[i] <= 10000; A is sorted; 1 <= TARGET <= 2^31 - 1
Example input
2 6 1 3
Example output
1
Explanation
With 1 3 and TARGET = 6, the sums 1, 3 and 4 are possible but 2 is not. Patching 2 makes 1 to 6 all reachable (for example 5 = 2 + 3 and 6 = 1 + 2 + 3), so the answer is 1.