Baltic Olympiad in Informatics: 2007 Day 2, Problem 3
We are given a sequence . We can manipulate this sequence using the operation reduce(i)
,
which replaces elements and with a single element , resulting in a new shorter
sequence. The cost of this operation is . After reduce
operations, we obtain a
sequence of length . Our task is to compute the cost of the optimal reducing scheme, i.e. the sequence
of reduce operations with minimal cost leading to a sequence of length .
Constraints
Subtask 1 [30%]
Subtask 2 [20%]
Subtask 3 [50%]
No additional constraints.
Input Specification
The first line of input contains an integer , the length of the sequence. The following lines contain one integer , the elements of the sequence.
Output Specification
Output the minimal cost of reducing the sequence to a single element.
Sample Input
3
1
2
3
Sample Output
5
Explanation for Sample Output
The optimal way to reduce the array to one element has a cost of :
- [1, 2, 3] → [2, 3]
- [2, 3] → [3]
Comments