This page is still under construction.

Parts of this page are still being built. What you see may change.

Merge the Tree and Sequence

Time limit2sMemory limit1024 MB

Summary
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).
Level

Hard8 of 10

Topics
Tree, Sorting, Greedy
Solved
No attempts yet

Problem

Jeonghwi has a tree with NN vertices and a sequence of length NN. 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 NN, the number of vertices in the tree and the length of the sequence. (2≤N≤200 0002 \leq N \leq 200\,000)

From the second line to the NN-th line, each of the next N−1N-1 lines contains three integers vi,wi,civ_i, w_i, c_i separated by spaces. This means that the edge connecting vertex viv_i and vertex wiw_i has color cic_i. (1≤vi,wi≤N1 \leq v_i, w_i \leq N, 1≤ci≤200 0001 \leq c_i \leq 200\,000)

The next line contains the integers A1,A2,⋯ ,ANA_1, A_2, \cdots, A_N written on vertices 1,2,⋯ ,N1, 2, \cdots, N of the tree, separated by spaces. (−1 000≤Ai≤1 000-1\,000 \leq A_i \leq 1\,000)

The next line contains the integers B1,B2,⋯ ,BNB_1, B_2, \cdots, B_N, the elements of the sequence of length NN, separated by spaces. (−1 000≤Bi≤1 000-1\,000 \leq B_i \leq 1\,000)

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.

Examples1

  1. Example 1

    Input
    8
    1 2 1
    2 5 1
    2 4 3
    5 6 2
    6 3 2
    6 8 2
    8 7 1
    5 -3 4 -1 1 0 -1 2
    2 -2 0 -1 0 4 6 1
    
    Expected output
    -51
    122