#K4076. Maximum Area of Container

    ID: 26714 Type: Default 1000ms 256MiB

Maximum Area of Container

Maximum Area of Container

You are given an array of non-negative integers representing the heights of buildings. Each building has a width of 1. Your task is to select two buildings such that, together with the x-axis, they form a container that holds the maximum amount of water.

The amount of water that can be held by the container is calculated by the formula:

$$A = \min(h_i, h_j) \times (j - i)$$

where \(h_i\) and \(h_j\) are the heights of the two selected buildings, and \(j - i\) is the distance between them. Solve the problem using an efficient algorithm, such as the two-pointer approach.

inputFormat

The first line contains a single integer \(n\) (\(2 \le n \le 10^5\)), representing the number of buildings.

The second line contains \(n\) space-separated non-negative integers \(h_1, h_2, \dots, h_n\) representing the heights of the buildings.

outputFormat

Output a single integer representing the maximum area of water that can be contained.

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