National Olympiad in Informatics, China, 2006
Networking has become an essential part of the modern world. Each day, there are hundreds of millions of users that use the web for activities such as education, research, entertainment, and more. However, a point that cannot be ignored is the huge operating costs of networks. So, appropriately charging users of the net is both necessary, and reasonable.
City MY's NS high school has an educational network like such. The
number of users in the network is
The MY networking company's charging scheme is rather special, and is
known as "pairwise charging". A fee is charged for all pairs of users
To make explanations easier, let's first define some terms regarding this network tree:
- Ancestor: The root node has no ancestors. The ancestors of a non-root node include its parent node and the ancestors of its parent node.
- Containment of leaf nodes: Leaf nodes do not contain any leaf nodes. A non-leaf node contains the number of leaf nodes its left child contains, plus the number of leaf nodes its right child contains.
- Distance: Between two nodes in the tree, the distance is the number of edges on the path with the fewest number of edges connecting them.
Regarding any two users
Relationship between |
Payment Factor |
Actual Charges (Dollars) | ||
---|---|---|---|---|
Since the final charges is very much related to the payment methods
involved, NS high school's users wish to change their own payment plans
to minimize the total charge to the school. However, the networking
company has already recorded each user's payment plan when they
registered. If user
Now the question is, given the initially registered payment plans of
every user, as well as
Input Specification
The 1st line of input contains one positive integer
The 2nd line contains
The 3rd line contains
The next
Output Specification
Your program must output a single integer, representing the minimum amount that NS high school has to pay to the network company in dollars.
Sample Input
2
1 0 1 0
2 2 10 9
10 1 2
2 1
3
Sample Output
8
Explanation
When user number
Constraints
In test cases worth
In test cases worth
In test cases worth
Problem translated to English by .
Comments