Problem 01A. Maximum Subarray Sum ≡

Author:Pavel Mesenev   Time limit:2 sec
Input file:Standard input   Memory limit:256 Mb
Output file:Standard output  

Statement

You are given an array of n integers a0, a1, …, an − 1, some of which may be negative. Among all non-empty contiguous blocks al, al + 1, …, ar (with 0 ≤ l ≤ r ≤ n − 1), find the largest possible value of the sum al + al + 1 + …  + ar.

If every element is negative, the answer is the single largest element, because the block must be non-empty.

Input format

The first line contains one integer n — the length of the array. The second line contains n space-separated integers a0… an − 1.

Output format

Print one integer — the maximum sum of a non-empty contiguous block.

Constraints

1 ≤ n ≤ 2 ⋅ 105

 − 109 ≤ ai ≤ 109

The answer fits into a signed 64-bit integer.

Sample tests

No. Standard input Standard output
1
9
-2 1 -3 4 -1 2 1 -5 4
6
2
1
-5
-5
3
5
-1 -2 -3 -4 -5
-1

0.064s 0.020s 16