Applying Kadane's algorithm to your motivation...