#K93092. Contiguous Flowers Collection
Contiguous Flowers Collection
Contiguous Flowers Collection
You are given a sequence of integers, where a positive integer represents a flowerbed and a negative integer represents the presence of weeds. Your task is to find the maximum number of flowers that can be collected from a contiguous subarray that contains no weeds. In other words, whenever a negative number is encountered, the sequence must be restarted.
The problem can be formulated as follows:
Given an array \(a\) of length \(n\), calculate the maximum sum \(S\) of any contiguous subarray that does not include any negative number. Formally, let \(current\_sum\) be computed as follows: \[ current\_sum = \begin{cases} 0, & \text{if } a_i < 0, \\ current\_sum + a_i, & \text{if } a_i \ge 0, \end{cases} \] and update \(S = \max(S, current\_sum)\) at each step. If all elements are negative, print 0.
It is guaranteed that the input list has at least one element.
inputFormat
The first line of the input contains a single integer \(n\) representing the number of elements in the array. The second line contains \(n\) space-separated integers, each representing a flowerbed (if positive) or weeds (if negative).
For example:
5 3 -1 7 4 2
outputFormat
Output a single integer representing the maximum number of flowers that can be collected from any contiguous subarray that contains no weeds. Output the answer to the standard output.
## sample5
3 -1 7 4 2
13