#cf17finalj. [cf17_final_j]Tree MST
[cf17_final_j]Tree MST
Problem Statement
Ringo has a tree with vertices. The -th of the edges in this tree connects Vertex and Vertex and has a weight of . Additionally, Vertex has a weight of .
Here, we define as the distance between Vertex and Vertex , plus .
We will consider a complete graph with vertices. The cost of its edge that connects Vertex and Vertex is . Find the minimum spanning tree of .
Constraints
- The given graph is a tree.
- All input values are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the cost of the minimum spanning tree of .
Sample Input 1
4
1 3 5 1
1 2 1
2 3 2
3 4 3
Sample Output 1
22
We connect the following pairs: Vertex and , Vertex and , Vertex and . The costs are , and , respectively, for a total of .
Sample Input 2
6
44 23 31 29 32 15
1 2 10
1 3 12
1 4 16
4 5 8
4 6 15
Sample Output 2
359
Sample Input 3
2
1000000000 1000000000
2 1 1000000000
Sample Output 3
3000000000