시간 제한메모리 제한제출정답맞힌 사람정답 비율
5 초 2048 MB72301832.727%

문제

Given an array $A$ of length $N$, a subarray $A[l \ldots r]$ is defined as the part of the array $A$ that includes only the elements located at positions from $l$ to $r$ inclusively. The cost of a subarray is defined as the product of the length of the subarray and the sum of its two smallest elements.

For example, let the array be $A = [5, 1, 3, 5, 3]$. Let us consider the subarray $A[2 \ldots 4] = [1, 3, 5]$. Its length is $3$, its smallest element is $1$, and its second smallest element is $3$. Therefore, its cost is $3 \cdot (1 + 3) = 12$. Let us consider another subarray, $A[1 \ldots 2] = [5, 1]$. Its length is $2$, its smallest element is $1$, and its second smallest element is $5$. Therefore, its cost is $2 \cdot (1 + 5) = 12$.

Note that if the minimal value occurs more than once in a subarray, it is counted several times. For example, the length of the subarray $A[3 \ldots 5] = [3, 5, 3]$ is $3$, its smallest element is $3$, and its second smallest element is also $3$. Therefore, its cost is $3 \cdot (3 + 3) = 18$.

Given an array, find the maximum cost over all subarrays of at least two elements. That is, you need to find the maximum cost over all subarrays $A[l \ldots r]$, where $1 \le l < r \le N$.

입력

The first line contains $N$ ($2 \le N \le 10^6$), the length of the array. The second line contains $N$ integers $A_1, A_2, \dots, A_N$ ($1 \le A_i \le 10^9$).

출력

Output a single integer, the maximum cost over all subarrays of at least two elements.

서브태스크

번호배점제한
16

$N \le 800$.

27

$N \le 5\,000$.

310

$N \le 20\,000$.

424

$N \le 10^5$ and $A_i$ are selected uniformly randomly from $1 \ldots 10^9$.

517

$2 \le A_i \le \sqrt{N}$ for all $1 \le i \le N$.

636

No additional constraints.

예제 입력 1

5
5 1 3 5 3

예제 출력 1

20

The maximum cost is achieved for the subarray $A[1 \ldots 5]$: its length is $5$, the two smallest elements are $1$ and $3$, and the cost is $5 \cdot (1 + 3) = 20$.

예제 입력 2

7
1 1 3 5 10 77 5

예제 출력 2

174

The maximum cost is achieved for the subarray $A[5 \ldots 6]$: its length is $2$, its two smallest elements are $10$ and $77$, and the cost is $2 \cdot (10 + 77) = 174$.

예제 입력 3

3
1 2 3

예제 출력 3

10

The maximum cost is achieved for the subarray $A[2 \ldots 3]$: its length is $2$, its two smallest elements are $2$ and $3$, and the cost is $2 \cdot (2 + 3) = 10$.

채점 및 기타 정보

  • 예제는 채점하지 않는다.