Find the maximum sum of any contiguous subarray. This is the famous Maximum Subarray problem. You could use prefix sums: for . Track the minimum prefix seen so far.
But there's a simpler approach: Kadane's algorithm. At each position, either extend the current subarray or start fresh. . The answer is . This runs in time and space.