#P1364. 医院设置
医院设置
Description
Given a binary tree as shown:

The number inside each circle is the population at that node. The number beside each circle is the node index. Choose one node to build a hospital so that the total distance traveled by all residents is minimized. The distance between two adjacent nodes is .
For example, if the hospital is built at node , the total distance is . If it is built at node , the total distance is .
Input Format
The first line contains an integer , the number of nodes in the tree.
Each of the next lines describes one node with three integers , where is the population at the node, is the left child (with meaning no child), and is the right child (with meaning no child).
Output Format
Output a single integer, the minimum total distance.
5
13 2 3
4 0 0
12 4 5
20 0 0
40 0 0
81
Hint
Constraints
For 100% of the testdata, it is guaranteed that , , .
Translated by ChatGPT 5
京公网安备 11011102002149号