#P6196. [EER1] 代价

[EER1] 代价

Description

You are given a sequence aia_i of length n+2n+2, where the 11st number and the (n+2)(n+2)nd number are fixed as 11. Each time, you may choose a number in the middle of the sequence and delete it (it cannot be the first or the last). The cost of deleting the number at position pp is ap1×ap×ap+1a_{p-1} \times a_p \times a_{p+1}. You need to perform this operation until no more operations are possible. Find the minimum total cost.

Input Format

The first line contains a positive integer nn.

The second line contains nn positive integers, where the ii-th number represents ai+1a_{i+1}.

Output Format

Output one positive integer in one line, which is the minimum total cost.

3
1 2 3
9

4
19 26 8 17
846
6
1 1 1 1 1 1
6

Hint

Explanation for Sample 1:

First delete 33, the cost is 66, then delete 22, the cost is 22, then delete 11, the cost is 11.

The total cost is 6+2+1=96+2+1=9.


This problem uses bundled testdata.

Constraints: For 100%100\% of the test points, 1n1061 \leq n \leq 10^6, 1ai1041 \leq a_i \leq 10^4.

This problem has 66 subtasks. The score and constraints for each subtask are as follows:

Subtask 11 (11 point): ai=1a_i = 1.

Subtask 22 (1414 points): 1n101 \leq n \leq 10.

Subtask 33 (55 points): 1ai21 \leq a_i \leq 2.

Subtask 44 (1414 points): 1n401 \leq n \leq 40.

Subtask 55 (2626 points): 1n5001 \leq n \leq 500.

Subtask 66 (4040 points): no special constraints.

Special Thanks

idea: smrsky

solu: CYJian

data: iostream

Translated by ChatGPT 5