Mars Bacteria Lineup
Time limit1sMemory limit128 MB
Given a repulsion matrix, choose left/right swap at every node of a complete binary tree to minimize the total sum of adjacent-pair distances in the resulting leaf permutation.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Tree, Divide and conquer, Combinatorics
- Solved
- No attempts yet
Problem
Scientists discovered unusual bacteria on Mars. The population starts from one bacterium. In each generation, every bacterium splits into two bacteria and the original one disappears. Therefore, generation contains bacteria.
The scientists numbered the bacteria in the last generation from 1 to . The numbering has the property that the descendants of every ancestor form a consecutive interval of numbers. The descendants of the generation bacteria are , , ..., , and the descendants of the generation bacteria are consecutive intervals of size 4. In the same way, the descendants of the two generation 2 bacteria are and .
When the bacteria are rearranged, this property must still hold: for every ancestor bacterium, all of its descendants must occupy one consecutive block in the new order. Equivalently, in the complete binary tree representing the genealogy, each internal node may independently swap the order of its left and right descendant groups.
A repulsion value is given for every pair of bacteria. If two bacteria become adjacent in the new order, the distance between them must be at least their repulsion value. The length of an order is the sum of the distances between every adjacent pair. Find the minimum possible length of an order that satisfies the genealogy condition.
Input
The first line contains a positive integer .
The next lines contain the repulsion matrix. Each line contains integers, and every value is between and , inclusive. The number in row and column is the repulsion between bacterium and bacterium . The matrix is symmetric, and the repulsion of a bacterium with itself is .
Output
Output the minimum possible length of a valid order.