Merge the Tree and Sequence
Time limit2sMemory limit1024 MB
Assign the sequence B to the tree vertices one-to-one to find the minimum and maximum of the sum over color regions of (sum of A) times (sum of matched B).
Problem
Jeonghwi has a tree with vertices and a sequence of length . Each vertex of the tree has one integer written on it, and each edge is painted with one of the colors, which are natural numbers from 1 to 200,000.
Jeonghwi finds it tedious to keep the tree and the sequence separate, so he decided to merge them by matching the vertices of the tree one-to-one with the elements of the sequence. Merging them without any twist would be dull, so he defined the score of a merge as follows.
-
Split the edges of the tree into one or more regions that satisfy the conditions below. The way to split the tree in this manner is unique.
- Every edge of the tree belongs to exactly one region.
- Every region contains at least one edge.
- If two edges sharing an endpoint have the same color, the two edges belong to the same region.
-
The score of a region is (the sum of the integers written on the endpoints of the edges in the region) × (the sum of the sequence elements matched to the endpoints of the edges in the region).
-
The score of a merge is the sum of the scores of all regions.
For example, the tree below is split into 4 regions.

If Jeonghwi merges the sequence and the tree as shown below, he gets 102 points.

Jeonghwi wants to know the minimum and maximum scores he can get when merging the tree and the sequence, but the tree and the sequence are too large for him to compute by hand. Help Jeonghwi find the minimum and maximum scores he can get.
Input
The first line contains the integer , the number of vertices in the tree and the length of the sequence. ()
From the second line to the -th line, each of the next lines contains three integers separated by spaces. This means that the edge connecting vertex and vertex has color . (, )
The next line contains the integers written on vertices of the tree, separated by spaces. ()
The next line contains the integers , the elements of the sequence of length , separated by spaces. ()
Output
On the first line, print the minimum score Jeonghwi can get.
On the second line, print the maximum score Jeonghwi can get.
Hint
- The answer can exceed the range of a 32-bit integer.