#P15059. [UOI 2023 II Stage] Product

    ID: 16990 远端评测题 2000ms 512MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>二分并查集2023ST 表单调栈UOI(乌克兰)

[UOI 2023 II Stage] Product

题目描述

Given an array aa of length nn. The cost of a subarray is defined as the product of the length of the subarray and the sum of its two smallest numbers.

A subarray a[l..r]a[l..r] is a part of the array aa that includes only the elements located at positions from ll to rr, inclusive.

For example, let the array be [5,3,1,5,3][5, 3, 1, 5, 3]. Let us consider the subarray a[2..4]a[2..4], which consists of elements (a[2..4]a[2..4]) [3,1,5][3, 1, 5]. Its length is 33, its first minimum is 11, and its second minimum is 33. Therefore, its cost is 3(1+3)=123 \cdot (1 + 3) = 12. Let us consider another subarray, a[1..2]a[1..2], consisting of elements (a[1..2]a[1..2]) [5,3][5, 3]. Its length is 22, its first minimum is 33, and its second minimum is 55. Therefore, its cost is 2(3+5)=162 \cdot (3 + 5) = 16.

Note that if the minimum number occurs more than once, it is counted several times. For example, if there is a subarray [3,1,1][3, 1, 1], its length is 33, its first minimum is 11, and its second minimum is 11. Therefore, its cost is 3(1+1)=63 \cdot (1 + 1) = 6.

Your task is to find the maximum cost over all subarrays of length at least two elements. That is, you need to find the maximum cost over all subarrays a[l..r]a[l..r], where (1l<rn)(1 \leq l < r \leq n).

输入格式

The first line contains a single integer nn (2n106)(2 \le n \le 10^6).

The second line contains nn integers a1,a2,,ana_1, a_2, \dots, a_n (1ai109)(1 \le a_i \le 10^9).

输出格式

Output a single integer -- the answer to the problem.

5
5 3 1 5 3
20
7
1 1 3 5 10 77 5
174
3
1 2 3
10

提示

In the first example, the maximum cost is achieved for the subarray a[1..5]a[1..5], its length is 55, its minimums are 11 and 33, and the product of (1+3)5=20(1+3) \cdot 5 = 20.

In the second example, the maximum cost is achieved for the subarray a[5..6]a[5..6], its length is 22, its minimums are 1010 and 7777, and the product of (10+77)2=174(10+77) \cdot 2=174.

In the third example, the maximum cost is achieved for the subarray a[2..3]a[2..3], its length is 22, its minimums are 22 and 33, and the product of (2+3)2=10(2+3) \cdot 2=10.

Scoring

  • (66 points): 2n8002 \le n \le 800
  • (77 points): 2n50002 \le n \le 5\,000
  • (1010 points): 2n200002 \le n \le 20\,000
  • (2424 points): All tests are generated randomly in the following way: first, a number nn is determined, which does not happen randomly, and then each aia_i (1in1 \le i \le n ) is assigned a value from 11 to 10910^9 inclusively with equal probability for each value. 2n1052 \le n \le 10^5.
  • (1717 points): 2ain2 \le a_i \le \sqrt{n}
  • (3636 points): No additional constraints.