#K86737. Maximum Subarray Sum
Maximum Subarray Sum
Maximum Subarray Sum
You are given an array of integers. Your task is to find the maximum subarray sum. In other words, you need to find a contiguous subsequence whose sum is the largest among all possible subsequences.
If the array contains only non-positive numbers (i.e. every element is less than or equal to 0), then the answer should be 0. This can be formally written as:
\[ \text{if } \forall x \in arr,\; x \le 0, \quad \text{then } answer = 0 \]Otherwise, compute the maximum sum of any contiguous subarray. This is a classic problem that can be solved using Kadane's Algorithm.
inputFormat
The input is given via standard input and consists of two lines:
- The first line contains an integer
n
representing the number of elements in the array. - The second line contains
n
space-separated integers representing the elements of the array.
outputFormat
Output the maximum subarray sum to standard output. If the array only contains non-positive numbers, output 0.
## sample5
-1 -2 -3 -4 -5
0
</p>