Shortest Subarray with Sum at Least K
You are given an array of n integers a_1, a_2, …, a_n (they may be negative or zero) and a number K.
Find the length of the shortest non-empty contiguous subarray a_l, a_{l+1}, …, a_r whose sum is at least K. If no such subarray exists, report that.
Input
The first line contains two integers n and K (1 ≤ n ≤ 10^6, 1 ≤ K ≤ 10^11).
The second line contains n integers a_1, a_2, …, a_n (−10^5 ≤ a_i ≤ 10^5).
Output
Print a single integer: the minimum value of r − l + 1 over all pairs 1 ≤ l ≤ r ≤ n with a_l + a_{l+1} + … + a_r ≥ K, or -1 if there is no such pair.
Notes
In the first example, the subarray 4 -1 5 has sum 8 ≥ 6, and no subarray of length 1 or 2 reaches 6.
In the second example, the largest possible subarray sum is 4, so the answer is -1.
Sums of subarrays can exceed the range of a 32-bit integer.