SDE Path

Largest Rectangle in Histogram

Hard

Largest Rectangle in Histogram

Given a histogram, find the area of the largest rectangle that fits within it.

Input format

The first line contains N. The second line contains N non-negative bar heights (width 1 each).

Output format

Print the area of the largest rectangle that fits inside the histogram.

Constraints

  • Values fit in a 64-bit signed integer
  • Trailing whitespace and a trailing newline are ignored by the judge

Read from stdin, write to stdout. Sample cases below show the exact format.

Sample cases

Example 1
Input
6
2 1 5 6 2 3
Expected output
10
Example 2
Input
2
2 4
Expected output
4