NOI Contest Archive

Archive / 2024 / Final Round — Day 1

Pebbles 3

Medium · National Olympiad in Informatics Sri Lanka 2024 - Day 1

Alice has a thread with \(n\) pebbles and wants to separate them. As her good friend, you offered to help her, But you need to do it in the minimum possible effort.

You can break the thread in multiple steps. At each step, you need to select one of the existing threads and break it. Initially, there is only one thread with pebbles from \(0\) to \(n-1\). Then you can choose where to break that thread. The cost of breaking depends only on the start and end of the thread, not on the breaking point.

If you have a pebble thread from \(i^{th}\) pebble to the \(j^{th}\) pebble, breaking it requires effort \(C(i,j\))

\[C(i,j) = (k_i + k_j)\]

\(k\) is an integer array of length \(n\).

After you break the initial thread, you will have two threads. If you break the initial thread at point \(l\), you will get two threads: from \(i\) to \(l\) and \(l+1\) to \(j\). Then you can pick any of these to perform the next operation.

You task is to repeatedly do break operations until you get \(n\) threads each containing only a single pebble.

Find the minimum effort to separate all pebbles in the thread.

Input Format

First line contains single integer \(n\)

Second line contains \(n\) space seperated integers \(k_i\) (\(0 \leq i \leq n-1 \text{ (0 - indexed)}\))

Output Format

Constraints

Subtasks

Limits

Sample Input 0

4
5 1 3 2

Sample Output 0

14