There is a network which is a completely binary tree. In the network, there are 2N users numbered from 1, 2, 3, 4, ... 2N, which are the leaves in the network-tree. For example, Figure 1 is a network-tree. In the tree, there are 8 users (leaf nodes) and 7 transmitters (gray nodes).
Using network-tree means that you have to pay a cost for it. The charging way is so interesting, named "pair charging". The cost you must pay is the sum of cost from every pair of user i and user j ( 1 ≤ i < j ≤ 2N). Every user can choose mode A or mode B to pay for the charge.
For every two users i, j (1 ≤ i < j ≤ 2N), first find their LCA (The Least Common Ancestors): Transmitter P. Then count the number of mode A users in the tree of ROOT transmitter P, so do mode B.
Next, let's name the number nA and nB and at last charge the cost.
f[i][j] is the flow between user i and user j.
The charging way is followed:
At first, every user has an initial mode, A or B. However, they want to reduce the cost, so a part of them would change the mode from A to B, or from B to A. Every change will have an extra cost. If user i changes his mode, he will pay Ci cost.
Now, you should find the minimum cost, that is, the sum of the cost having to pay for and the extra cost of changing mode.