Problems > Largest Rectangle in a Histogram

Largest Rectangle in a Histogram

Difficulty: 1900 3500 ms 128 MB

stack

Bars of width 1 and given heights stand in a row. Print the area of the largest axis-aligned rectangle fitting inside the histogram.

Input: the first line has N; the second line has the N heights.

Output: the maximum rectangle area.

Constraints:

  • 1 ≤ N ≤ 105
  • 0 ≤ hi ≤ 109

Sample Tests

Input 1
6
2 1 5 6 2 3
Output 1
10
Input 2
2
2 2
Output 2
4

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.