Two Buildings
Time limit1sMemory limit512 MB
Given n building heights, find the maximum of (h_i + h_j) times (j - i) over all pairs i < j, with n up to 1,000,000.
- Level
Medium5 of 10
- Topics
- Two pointers, Greedy
- Solved
- No attempts yet
Problem
There are buildings along a horizontal street. The buildings stand next to each other, and the -th building from left to right has width 1 and height . Among the buildings, choose two buildings, the -th and the -th with , so that is maximized.
For example, the figure shows 5 buildings with heights 1, 3, 2, 5, 4 from left to right. If we choose the first 2 buildings, we get . If we choose the first and fifth buildings, we get . The maximum is achieved by the second and fifth buildings, with heights 3 and 4: .
Write a program that prints .
Input
The first line contains an integer (), the number of buildings. The second line contains the heights of the buildings separated by spaces, from left to right, so that the -th number is the height of the -th building ().
Output
Print exactly one line containing .