Explanation
Before we start, append a to the end of the array and force that element to always be blocked. This makes talking about the problem easier while not affecting the answer.
We can binary search on the answer; to see how, let's see how we can check if a cost of at most is possible.
Let be the minimum cost only considering elements up to index . Since we're taking the maximum over the blocked segments' sums, we don't have to worry about them as long as each of them is less than ; we only have to care about the sum of the blocked elements.
If is the smallest element we can start blocking from without having the sum of the new segment we create exceed , then
which can be computed using a segment tree.
Implementation
Time Complexity: , where .
C++
#include <cstdint>#include <iostream>#include <limits>#include <vector>using std::cout;using std::endl;using std::vector;Code Snippet: Segment Tree (from the module) (Click to expand)
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!