#C14414. Container With Most Water

    ID: 44061 Type: Default 1000ms 256MiB

Container With Most Water

Container With Most Water

Given a list of building heights, your task is to determine the maximum amount of water that can be held between any two buildings. The water is held by the shorter of the two buildings and the distance between them. The area of water contained between two buildings at positions i and j is computed as:

\(\text{Area} = \min(h_i, h_j) \times (j-i)\)

where \(h_i\) and \(h_j\) are the heights of the buildings at positions i and j respectively.

Optimize your solution to handle large input efficiently.

inputFormat

The input is read from standard input (stdin) and consists of two lines:

  • The first line contains an integer \(n\) which is the number of buildings.
  • The second line contains \(n\) space-separated positive integers representing the heights of the buildings.

outputFormat

Output to standard output (stdout) a single integer: the maximum area of water that can be held between any two buildings, computed as \(\min(h_i, h_j) \times (j-i)\).

## sample
9
1 8 6 2 5 4 8 3 7
49