I learnt of a "prefix sum/min" based formulation from the solution to question D, codeforces educational round 88 .
The idea is to start with the max prefix sum (for optima) as the difference of right minus left:
Which is then expressed as:
Since is a prefix-sum of , and is a prefix min of , the whole thing is serial, parallel. In haskell, this translates to:
let heights deltas = scanl (+) 0 deltaslet lowest_heights = scanl1 min . sumslet elevations xs = zipWith (-) (sums xs) (lowest_heights xs)-- elevations [1, 2, 3, -2, -1, -4, 4, 6]-- > [0,1,3,6,4,3,0,4,10]let max_elevation deltas = max (elevation deltas)best = max_elevations [1, 2, 3, -2, -1, -4, 4, 6]lowest_heights keeps track of the sea level, while the
elevations computes the elevation from the lowest height.
The maximum sum subarray will correspond to treating the elements of the array
as deltas, where we are trying to find the highest elevation. since elevation
is an integral (sum) of the deltas in height.
