Editorial
For a fixed , define
As increases, every term is nondecreasing, so is also nondecreasing. Therefore, we can binary-search for the smallest such that .
It is enough to use as the right endpoint of the search interval. Since the existence of a valid answer is guaranteed, the first with must satisfy .
For each candidate , we can compute in by scanning the array once. Since , the binary search uses at most about iterations. The total time complexity over all test cases is
and the extra space complexity is per test case.
Also, , so and every related sum fit in a signed 64-bit C++ long long.
Solution written by GPT5.6