Under construction

Still being built. Things may change

Two Buildings

Time limit1sMemory limit512 MB

Summary
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 nn buildings along a horizontal street. The buildings stand next to each other, and the ii-th building from left to right has width 1 and height hih_i. Among the nn buildings, choose two buildings, the ii-th and the jj-th with i<ji < j, so that (hi+hj)×(j−i)(h_i + h_j) \times (j - i) 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 (1+3)×(2−1)=4(1 + 3) \times (2 - 1) = 4. If we choose the first and fifth buildings, we get (1+4)×(5−1)=20(1 + 4) \times (5 - 1) = 20. The maximum is achieved by the second and fifth buildings, with heights 3 and 4: (3+4)×(5−2)=21(3 + 4) \times (5 - 2) = 21.

Write a program that prints max⁡1≤i<j≤n(hi+hj)×(j−i)\max_{1 \le i < j \le n} (h_i + h_j) \times (j - i).

Input

The first line contains an integer nn (2≤n≤10000002 \le n \le 1000000), the number of buildings. The second line contains the heights of the nn buildings separated by spaces, from left to right, so that the ii-th number is the height hih_i of the ii-th building (1≤hi≤10000001 \le h_i \le 1000000).

Output

Print exactly one line containing max⁡1≤i<j≤n(hi+hj)×(j−i)\max_{1 \le i < j \le n} (h_i + h_j) \times (j - i).

Examples2

  1. Example 1

    Input
    5
    1 3 2 5 4
    
    Expected output
    21
    
  2. Example 2

    Input
    5
    8 3 6 3 1
    
    Expected output
    36