#K90352. Maximum Subarray Sum

    ID: 37733 Type: Default 1000ms 256MiB

Maximum Subarray Sum

Maximum Subarray Sum

Given an array of integers, your task is to find the contiguous subarray which has the largest sum and then output that sum. This problem can be mathematically formulated as:

max1ijnk=ijak\max_{1 \leq i \leq j \leq n} \sum_{k=i}^{j} a_k

where (a_1, a_2, \dots, a_n) are the elements of the array. An efficient solution to this problem is provided by Kadane's algorithm, which runs in linear time.

inputFormat

The first line of input contains a single integer (n) representing the number of elements in the array. The second line contains (n) space-separated integers, which are the elements of the array.

outputFormat

Output a single integer, the maximum sum of any contiguous subarray.## sample

5
-1 2 3 -2 5
8

</p>