Maximum Sum

3.5

19 votes
Algorithms, Data Structures, Easy, Hash Maps, Hash Tables, Sets
Problem

Given an array A of N integers. Now, you have to output the sum of unique values of the maximum subarray sum of all the possible subarrays of the given array A.
Note: Subarray means contiguous elements with atleast one element in it.

Input Format

The first line of the input contains a single integer N, the total number of elements in array A.
The next line of the input contains N space-separated integers representing the elements of the array.

Output Format

The only single line of the output should contain a single integral value representing the answer to the problem.

Constraints

1N2000
0|Ai|109

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Following are the possible number of subarrays and their respective maximum subarray sums:

[5]=[5]=5
[5,2]=[5]=5
[5,2,7]=[5,2,7]=10
[5,2,7,3]=[5,2,7]=10
[2]=[2]=2
[2,7]=[7]=7
[2,7,3]=[7]=7
[7]=[7]=7
[7,3]=[7]=7
[3]=[3]=3

5+10+(2)+7+(3)=17

Editor Image

?