#P4393. [BalticOI 2007] Sequence 序列问题

[BalticOI 2007] Sequence 序列问题

Description

Given a sequence a1,,ana _ 1, \cdots, a _ n, we perform an operation reduce(i)\text{reduce}(i) that replaces the two elements aia _ i and ai+1a _ {i+1} with a single element max(ai,ai+1)\max(a _ i,a _ {i+1}), producing a new sequence shorter than the original. The cost of this operation is max(ai,ai+1)\max(a _ i,a _ {i+1}). After performing this operation n1n-1 times, we obtain a sequence of length 11.

Our task is to compute the sequence of reduce\text{reduce} operations with the minimum total cost to turn the given sequence into a sequence of length 11.

Input Format

The first line contains an integer nn (1n1061 \leq n \leq 10 ^6), the length of the given sequence.

The next nn lines each contain an integer aia _ i (0ai1090 \leq a _ i \leq 10 ^ 9), the elements of the sequence.

Output Format

Output a single line with one integer: the minimum total cost to reduce the sequence to a single element.

3
1
2
3
5

Hint

  • For 30%30\% of the testdata, n500n\le 500.
  • For 50%50\% of the testdata, n20000n \le 20000.
  • For 100%100\% of the testdata, 1n1061 \le n \le 10^6, 0ai1090 \le a_i \le 10^9.

Translated by ChatGPT 5