Under construction

Still being built. Things may change

Histogram and Blue Rectangles

Time limit2sMemory limit512 MB

Summary
For each prefix of j columns of a histogram with heights a_i, find the largest axis-aligned rectangle whose interior lies entirely within those columns.
Level

Hard9 of 10

Topics
Stack, Dynamic programming, Geometry
Solved
No attempts yet

Problem

Consider an integer array aa of length nn where all aia_i are positive. Such an array may be represented as a histogram. To draw the histogram, for each ii from 0 to n−1n-1, draw a blue rectangle with vertices (i,0)(i,0), (i,ai)(i,a_i), (i+1,ai)(i+1,a_i), (i+1,0)(i+1,0).

A rectangle with vertices at integer points is called covered by the histogram if its sides are parallel to the coordinate axes and all the internal points of the rectangle are blue.

For each jj between 1 and nn, calculate the maximal area of the rectangle covered by the first jj columns of the given histogram.

Input

The first line contains one integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5), the length of the array aa. The second line contains nn integers, where the ii-th integer is aia_i (1≤ai≤1071 \le a_i \le 10^7).

Output

Print nn integers, each on a new line. The ii-th integer is the maximal area of the rectangle covered by the first ii columns of the given histogram.

Examples2

  1. Example 1

    Input
    5
    1 7 2 3 9
    
    Expected output
    1
    7
    7
    7
    9
    
  2. Example 2

    Input
    5
    4 5 3 8 7
    
    Expected output
    4
    8
    9
    12
    15