Skip to main content
Tuesday (24.11.2026) at 20:00: ALGOR Contest: Round #4 (Special Challenge) starts!

~/algor $ cat problems/stack-gistogramma

Largest rectangle in a histogram

Medium 1000 ms 128 MB 62%
Share

A histogram consists of $n$ columns standing side by side: each column has width $1$, and the height of the $i$-th is $h_{i}$. Find the area of the largest rectangle with sides parallel to the axes that fits inside the histogram (the rectangle must lie entirely within the columns).

Histogram columns; the rectangle of the largest area that fits inside them is highlighted

Input

The first line contains a positive integer $n$. The second line contains $n$ positive integers $h_{i}$ separated by spaces.

Output

One integer — the largest area.

Note

$1 \le n \le 2 * 10^{5}$
$1 \le h_{i} \le 10^{9}$

Examples

Input #1
6
2 1 5 6 2 3
Output #1
10
Input #2
1
7
Output #2
7

Sign in to submit a solution

Signing up is free and takes a minute.