#C3480. Maximum Subarray Sum of Non-negative Integers

    ID: 46912 Type: Default 1000ms 256MiB

Maximum Subarray Sum of Non-negative Integers

Maximum Subarray Sum of Non-negative Integers

You are given an integer array. Your task is to find the maximum sum of any contiguous subarray that consists only of non-negative integers. If all numbers are negative, the answer is 0.

Formally, given an array \(A = [a_1, a_2, \ldots, a_n]\), a subarray \(A[i \ldots j]\) (where \(1 \le i \le j \le n\)) is valid if every element in it is non-negative. The objective is to determine the maximum sum among all valid subarrays. If no valid subarray exists, output 0.

Note: The input is read from standard input (stdin) and the output should be written to standard output (stdout).

inputFormat

The first line of input contains a single integer \(n\) representing the number of elements in the array. The second line contains \(n\) space-separated integers representing the array elements.

Example:

5
1 2 3 -2 5

outputFormat

Output a single integer: the maximum sum of any contiguous subarray that consists solely of non-negative integers.

Example:

6
## sample
5
1 2 3 -2 5
6

</p>