Subarray Cost | 프로그래밍의 벗 PivotOJ
PivotOJ

Subarray Cost

시간 제한: 5000ms메모리 제한: 2048MB출처: EIO 2023-24 sel2BOJ 33132

문제

Given an array AA of length NN, a subarray A[lr]A[l \ldots r] is defined as the part of the array AA that includes only the elements located at positions from ll to rr 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]A = [5, 1, 3, 5, 3]. Let us consider the subarray A[24]=[1,3,5]A[2 \ldots 4] = [1, 3, 5]. Its length is 33, its smallest element is 11, and its second smallest element is 33. Therefore, its cost is 3(1+3)=123 \cdot (1 + 3) = 12. Let us consider another subarray, A[12]=[5,1]A[1 \ldots 2] = [5, 1]. Its length is 22, its smallest element is 11, and its second smallest element is 55. Therefore, its cost is 2(1+5)=122 \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[35]=[3,5,3]A[3 \ldots 5] = [3, 5, 3] is 33, its smallest element is 33, and its second smallest element is also 33. Therefore, its cost is 3(3+3)=183 \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[lr]A[l \ldots r], where 1l<rN1 \le l < r \le N.

입력

The first line contains NN (2N1062 \le N \le 10^6), the length of the array. The second line contains NN integers A1,A2,,ANA_1, A_2, \dots, A_N (1Ai1091 \le A_i \le 10^9).

출력

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

예제

예제 1

입력
5
5 1 3 5 3
출력
20

예제 2

입력
7
1 1 3 5 10 77 5
출력
174

예제 3

입력
3
1 2 3
출력
10
코드를 제출하려면 로그인하세요.