Remove K Bars from a Histogram
Time limit6sMemory limit1024 MB
Given N histogram bars, find for each K from 0 to N-1 the largest rectangle area left after removing exactly K bars.
- Level
Medium7 of 10
- Topics
- Array, Dynamic programming, Sorting
- Solved
- No attempts yet
Statement
Heo Kuang-wang, who took over the class after Lef, plans to set this year's largest-rectangle-in-a-histogram problem again. Heo's histogram is made of bars placed in order, with heights . All bars have width .
To offer problems of varied difficulty this year, Heo will make a histogram with exactly bars removed, for each . Heo likes wide rectangles, so for each Heo will pick, among all histograms with exactly bars removed, the one that contains the widest rectangle.
- The largest rectangle contained in a histogram is the rectangle with the greatest area that lies entirely inside the histogram and has sides parallel to the coordinate axes.
- Removing bars means that the remaining bars are joined together in their original order with no gaps.
For example, in a histogram with bar heights in order, removing the 2nd and 5th bars leaves bar heights .
Let be the maximum area of the largest rectangle contained in any histogram obtained by removing exactly bars from Heo's histogram. Find all values for .
Input
The first line contains , the number of bars in Heo's histogram ().
The second line contains the heights of the bars, separated by spaces ().
Output
Print lines. On line , print ().