atcoder#CF17FINALJ. Tree MST
Tree MST
Score : points
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 .
4
1 3 5 1
1 2 1
2 3 2
3 4 3
22
We connect the following pairs: Vertex and , Vertex and , Vertex and . The costs are , and , respectively, for a total of .
6
44 23 31 29 32 15
1 2 10
1 3 12
1 4 16
4 5 8
4 6 15
359
2
1000000000 1000000000
2 1 1000000000
3000000000