#K60077. Maximum Energy Sum
Maximum Energy Sum
Maximum Energy Sum
In a magical forest, there are (N) trees arranged in a straight line. Each tree has a unique magical energy value (E_i) which can be positive, negative, or zero. A skilled magician wishes to select a contiguous segment of trees such that the sum of their energy values is maximized. However, if all possible contiguous segments yield a negative sum, the magician opts to select none, resulting in a sum of (0).
Your task is to compute the maximum sum of the energy values from any contiguous subarray of trees. Note that the contiguous subarray may be empty if no positive-sum segment exists, in which case the answer should be (0).
inputFormat
The input is given via standard input (stdin) and consists of two lines. The first line contains an integer (N) representing the number of trees. The second line contains (N) space-separated integers (E_1, E_2, \dots, E_N) denoting the energy values of the trees.
outputFormat
Output a single integer to standard output (stdout) representing the maximum contiguous subarray sum. If every contiguous subarray has a negative sum, output (0).## sample
4
1 2 3 4
10