#K86332. Maximum Container Area

    ID: 36841 Type: Default 1000ms 256MiB

Maximum Container Area

Maximum Container Area

A common problem in coding competitions is to determine the maximum container area that can be formed using two buildings.

Given a sequence of building heights, your task is to select two distinct buildings such that the container formed between them holds the maximum water. The area of the container is computed using the formula:

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

where \(h_i\) and \(h_j\) denote the heights of the two chosen buildings and \(j-i\) is the horizontal distance between them.

Implement an efficient algorithm to calculate this maximum area.

inputFormat

The input is given via standard input (stdin) and consists of two lines:

  • The first line contains an integer \(n\) (\(1 \le n \le 10^5\)), representing the number of buildings.
  • The second line contains \(n\) space-separated non-negative integers, where the \(i\)-th number represents the height of the \(i\)-th building.

outputFormat

Output a single integer to standard output (stdout) representing the maximum area of the container that can be formed by any two buildings.

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