#P11958. 「ZHQOI R1」划分

    ID: 11717 远端评测题 1000ms 64MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划 DPO2优化李超线段树洛谷比赛

「ZHQOI R1」划分

Description

You are given array a1,a2,,an a_1, a_2, \dots, a_n . You need to split it into some subsegments (so every element is included in exactly one subsegment).

The weight of a subsegment al,al+1,,ar a_l, a_{l+1}, \dots, a_r is equal to (mini=lrai)×(maxi=lrai)(\min_{i=l}^{r}a_i)\times(\max_{i=l}^{r}a_i) . The weight of a partition is a total weight of all its segments.

Find the partition of minimal weight.

Input Format

The first line contains an integer n n — the length of the array a a .

The second line contains n n integers a1,a2,,an a_1, a_2, \dots, a_n — the array a a .

Output Format

Print a single integer — the minimal weight among all possible partitions.

4
-1 2 -1 2
-4
6
-3 4 -9 1 2 4

-48

Hint

The optimal partition in the first example is next: 1 -1 2 2 \bigg| 1 -1 2 2 .

The optimal partition in the second example is next: 3 -3 4 4 \bigg| 9 -9 1 1 2 2 4 4 .

Constraints

This problem uses subtask scoring.

For 100%100\% of the data, 1n1061 \le n \le 10^6, 106ai106-10^6 \le a_i \le 10^6.

Subtask nn\leq Additional Constraints Score
11 500500 None 55
22 50005000 1010
33 10510^5 All aia_i have the same sign 55
44 ai{1,0,1}a_i \in \{-1, 0, 1\} 1010
55 aia_i are randomly generated
66 None 1515
77 10610^6 Number of negatives in aa is less than 20002000
88 None 3030