Problem: Ashish has a tree consisting of nodes numbered to rooted at node . The -th node in the tree has a cost , and binary digit is written in it. He wants to have binary digit written in the -th node in the end.
To achieve this, he can perform the following operation any number of times:
- Select any nodes from the subtree of any node , and shuffle the digits in these nodes as he wishes, incurring a cost of . Here, he can choose ranging from to the size of the subtree of .
He wants to perform the operations in such a way that every node finally has the digit corresponding to its target.
Help him find the minimum total cost he needs to spend so that after all the operations, every node has digit written in it, or determine that it is impossible.
Input Format: First line contains a single integer denoting the number of nodes in the tree.
-th line of the next lines contains 3 space-separated integers , , — the cost of the -th node, its initial digit and its goal digit.
Each of the next lines contain two integers , , meaning that there is an edge between nodes and in the tree.
Output Format: Print the minimum total cost to make every node reach its target digit, and if it is impossible.
Note: The tree corresponding to samples and are:
In sample , we can choose node and for a cost of = and select nodes , shuffle their digits and get the desired digits in every node.
In sample , we can choose node and for a cost of , select nodes and exchange their digits, and similarly, choose node and for a cost of , select nodes and exchange their digits to get the desired digits in every node.
In sample , it is impossible to get the desired digits, because there is no node with digit initially.