Under construction

Still being built. Things may change

Remove K Bars from a Histogram

Time limit6sMemory limit1024 MB

Summary
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 NN bars placed in order, with heights H1,H2,⋯ ,HNH_1, H_2, \cdots, H_N. All bars have width 11.

To offer problems of varied difficulty this year, Heo will make a histogram with exactly KK bars removed, for each K=0,1,⋯ ,N−1K=0,1,\cdots,N-1. Heo likes wide rectangles, so for each KK Heo will pick, among all histograms with exactly KK 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 3,1,4,1,5,93,1,4,1,5,9 in order, removing the 2nd and 5th bars leaves bar heights 3,4,1,93,4,1,9.

Let f(K)f(K) be the maximum area of the largest rectangle contained in any histogram obtained by removing exactly KK bars from Heo's histogram. Find all NN values f(K)f(K) for K=0,1,⋯ ,N−1K=0,1,\cdots,N-1.

Input

The first line contains NN, the number of bars in Heo's histogram (1≤N≤40001\leq N\leq 4000).

The second line contains the heights H1,H2,…,HNH_1, H_2, \ldots, H_N of the bars, separated by spaces (1≤Hi≤1091\leq H_i\leq 10^9).

Output

Print NN lines. On line i+1i+1, print f(i)f(i) (0≤i≤N−10\leq i\leq N-1).

Examples2

  1. Example 1

    Input
    6
    3 1 4 1 5 9
    
    Expected output
    10
    12
    12
    12
    10
    9
    
  2. Example 2

    Input
    7
    9 6 8 2 7 5 9
    
    Expected output
    18
    30
    30
    28
    24
    18
    9