#K57502. Maximum Non-Negative Subarray Sum
Maximum Non-Negative Subarray Sum
Maximum Non-Negative Subarray Sum
You are given an array of integers. Your task is to find the maximum sum of any contiguous subarray that consists only of non-negative numbers. If there is no non-negative number in the array, output 0.
Consider an array \( arr[0 \ldots n-1] \) of length \( n \). You are required to compute the sum of the contiguous subarray consisting solely of non-negative integers that has the maximum possible sum. Formally, for any subarray \( arr[i \ldots j] \) where \( arr[k] \ge 0 \) for all \( i \le k \le j \), compute its sum and find the maximum over all such subarrays. If no such subarray exists, output 0.
Note: The input is read from stdin
and the output should be written to stdout
.
inputFormat
The first line contains a single integer \( n \) (the size of the array). The second line contains \( n \) space-separated integers representing the elements of the array.
outputFormat
Output a single integer, which is the maximum sum of any contiguous subarray of non-negative numbers. If all numbers are negative, output 0.
## sample6
-1 2 3 -5 4 6
10