Splitting DNA
InterviewTime limit1sMemory limit512 MB
Given the lengths of N fragments in order, find the minimum total energy to split the original chain, where each cut costs the current chain length.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Intervals, Prefix sum, Greedy
- Solved
- No attempts yet
Problem
You are studying a protein that splits long chains of DNA into smaller chains. The protein works as follows. To split a long chain of DNA, the protein first "reads" the whole chain and cuts it into two parts (not necessarily equal), then recursively splits the two smaller chains.
Splitting a chain into the two parts and takes an amount of energy proportional to the length of (the sum of the lengths of and ). More generally, splitting a chain () takes an energy proportional to the length of to cut it into two, plus the energy required to recursively split the two smaller chains.
You know the original DNA chain and the fragments obtained after the split. Since nature is usually very energy-efficient, you wonder what the minimal energy required to split the DNA chain is.
You noticed that the computation of this minimal energy only depends on , where is the length of . Given these integers , you want to compute the minimal energy required by the protein to split the long chain into these chunks.
Input
The input consists of two lines:
- the first line contains the number of strings, an integer;
- the second line contains space-separated integers representing .
Output
Print a single line with a single integer: the minimal total energy required to split the original chain.
Constraints
- ;
- for all .