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

~/algor $ cat problems/ikki-korsatkich-eng-katta-idish

Container with the most water

Medium 1000 ms 128 MB 49%
Share

$n$ vertical walls stand in a row, the height of the $i$-th wall is $h_{i}$, the distance between neighbouring walls is $1$. Two walls $i \lt j$ are chosen and water is poured between them: the water level equals the height of the lower wall, and the walls in between do not get in the way. Then $(j - i) \cdot \min(h_{i}, h_{j})$ units of water fit. What is the largest number of units of water that can fit?

Wall columns: the two chosen walls and the rectangle of water between them are highlighted

Input

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

Output

One integer — the largest amount of water.

Note

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

Examples

Input #1
6
1 5 2 4 6 3
Output #1
15
Input #2
2
3 7
Output #2
3

Sign in to submit a solution

Signing up is free and takes a minute.