The key to this problem
It is natural to consider using a sliding window and prefix sum for this problem. The main challenge lies in understanding that the edge of the optimal window w
It is natural to consider using a sliding window and prefix sum for this problem. The main challenge lies in understanding that the edge of the optimal window will always align with either the left or right edge of a segment.

While it is possible for the optimal window to start in the middle of one segment and end in the middle of another, there will always be an equivalent optimal window where one of its edges aligns with a segment boundary.

Everything else is straightforward.
['Python3']