Circus 2
Long before he became a goat, Andy was pursuing a career in academia. After writing that 17 is a composite number on an exam, Andy decided that if he was going to be a clown anyway, he may as well join the circus. Andy is once again in charge of setting up the tightropes.
There are n posts in a line, where the ith post has height ai. Andy must choose exactly two posts. The height of the tightrope is the minimum of the two chosen post heights, and the distance between posts i and j is ∣i−j∣.
If Andy chooses posts i and j, the audience's excitement factor is ∣i−j∣⋅min(ai,aj). Find the maximum possible excitement factor.
Input
The first line contains an integer n, the number of posts.
The second line contains n integers a1,a2,…,an, where ai is the height of the ith post.
Output
Print a single integer: the maximum excitement factor Andy can achieve.
Constraints
- 2≤n≤2⋅105
- 1≤ai≤109
Example 1
5
1 2 3 4 5
6
Explanation
Choosing the 3rd and 5th posts gives excitement factor ∣5−3∣⋅min(3,5)=2⋅3=6.
Comments0
No comments yet
Be the first to comment.
New comment
Log in to join the discussion.