Reducing a Sequence
Time limit1sMemory limit128 MB
Given a sequence, find the minimum total cost of repeatedly merging adjacent elements where cost equals the max of the pair, until one element remains.
Problem
You are given a sequence . The operation merges the two adjacent elements and into a single element and places in their position. Each time this operation is performed, the length of the sequence decreases by .
The cost of one operation is the maximum of the two merged elements, . Performing the operation times on a sequence of length reduces its length to .
Compute the minimum possible sum of the costs of all operations needed to reduce the sequence to length .
Input
The first line contains the length of the sequence . Each of the next lines contains one element of the sequence in order .
Output
Print the minimum sum of costs required to reduce the sequence to length .