#K5176. Largest Rectangle in Histogram

    ID: 29159 Type: Default 1000ms 256MiB

Largest Rectangle in Histogram

Largest Rectangle in Histogram

You are given n buildings arranged in a line, where the height of the i-th building is given by an integer h_i. Your task is to determine the maximum rectangular area that can be formed by a contiguous group of buildings. The area of a rectangle is computed as:

\( \text{Area} = h \times w \)

where \(h\) is the height of the rectangle (determined by the shortest building in the contiguous sequence) and \(w\) is the width (i.e. the number of buildings in the sequence).

This is a classic stack based problem that demands efficient computation in O(n) time.

inputFormat

The input is read from stdin and has the following format:

  • The first line contains an integer n representing the number of buildings.
  • The second line contains n space-separated integers corresponding to the heights \(h_1, h_2, \ldots, h_n\) of the buildings.

outputFormat

Output to stdout a single integer that denotes the maximum rectangular area obtainable using a contiguous subsequence of the buildings.

## sample
6
2 1 5 6 2 3
10