#K64052. Maximum Water Level Calculation

    ID: 31889 Type: Default 1000ms 256MiB

Maximum Water Level Calculation

Maximum Water Level Calculation

You are provided with an array of integers which represent the daily net change of the water level in a reservoir. The water level of the reservoir is not allowed to drop below zero. Your task is to determine the maximum cumulative water level that can be reached over any contiguous period.

Formally, given an array of integers \(a_1, a_2, \ldots, a_n\), define the sum over an interval as \(S(i,j) = \sum_{k=i}^{j} a_k\). However, if the running sum ever becomes negative, it is reset to zero. You need to compute the maximum value of the running sum during the process. If no positive accumulation exists, the answer is 0.

inputFormat

The first line of input contains a single integer \(n\) representing the number of days.

The second line contains \(n\) space-separated integers representing the daily net changes in the water level.

outputFormat

Output a single integer which is the maximum water level attained over any contiguous period with the condition that the running total is reset to zero if it ever becomes negative.

## sample
5
1 -2 3 1 -1
4

</p>