Product#11375

Product

2 seconds
256 megabytes
Very hard

Given an array of length . The cost of a subarray is the product of the subarray's length and the sum of its two smallest numbers.

A subarray is a part of array , considering only the numbers at positions from to inclusive.

For example, let the array be [5, 3, 1, 5, 3]. Consider the subarray , with elements () — [3, 1, 5]. Its length is , the first minimum is , the second minimum is . Therefore, its cost is 3 \cdot (1 + 3) = 12. Consider another subarray , with elements () — . Its length is , the first minimum is , the second minimum is . Therefore, its cost is 2 \cdot (3 + 5) = 16.

Note that if the minimum number occurs more than once, it is still counted multiple times. For example, if there is a subarray [3, 1, 1], then its length is , the first minimum is , the second minimum is . So its cost is 3 \cdot (1 + 1) = 6.

Your task is to find the maximum cost among all subarrays of length at least two elements. That is, you need to find the maximum cost among all subarrays , where 1 \le l < r \le n.

Input

The first line contains one integer (2 \le n \le 10^6).

The second line contains integers a_1, a_2, \dots, a_n (1 \le a_i \le 10^9).

Output

Print one integer — the answer to the problem.

Examples

Example 1

Loading
Loading

Example 2

Loading
Loading

Example 3

Loading
Loading

Note

In the first example, the maximum is achieved on the subarray , its length is , the minimums are and , the product is (1+3) \cdot 5 = 20.

In the second example, the maximum is achieved on the subarray , its length is , the minimums are and , the product is (10+77) \cdot 2=174.

In the third example, the maximum is achieved on the subarray , its length is , the minimums are and , the product is (2+3) \cdot 2=10.

Scoring

  1. points: 2 \le n \le 800

  2. points: 2 \le n \le 5\,000

  3. points: 2 \le n \le 20\,000

  4. points: All tests are generated randomly as follows: first the number is determined (not randomly), then for each a_i where 1 \le i \le n, a value from to 10^9 inclusive is assigned with equal probability for each value. 2 \le n \le 10^5.

  5. points: 2 \le a_i \le \sqrt{n}

  6. points: No additional constraints