#K40652. Largest Rectangle in Histogram

    ID: 26690 Type: Default 1000ms 256MiB

Largest Rectangle in Histogram

Largest Rectangle in Histogram

Given a histogram where the width of each bar is 1, find the area of the largest rectangle that can be formed by combining adjacent bars. The problem can be stated mathematically as follows: For a list of integers \( h_1, h_2, \ldots, h_n \) representing the heights of consecutive bars, determine the maximum area \( A \) defined by:

\( A = \max_{1 \leq i \leq j \leq n}( (j-i+1) \times \min_{i \leq k \leq j} h_k ) \)

It is guaranteed that the input list will contain at least one integer.

inputFormat

The first line of input contains a single integer \( n \) (the number of bars). The second line contains \( n \) space-separated positive integers representing the heights of the bars in the histogram.

Example:

6
2 1 5 6 2 3

outputFormat

Output a single integer representing the maximum rectangular area that can be formed in the histogram.

Example:

10
## sample
6
2 1 5 6 2 3
10