Allergic Aron
InterviewTime limit1sMemory limit512 MB
Given a weighted tree, choose a connected set of edges maximizing (number of edges) times (minimum edge weight in the set).
- Level
Medium7 of 10
- Topics
- Tree, Union-find, Sorting, Greedy
- Solved
- No attempts yet
Problem
Aron, Mr. Malnar's former best friend, left his homeland and sought a better future on a lonely, distant island. You are surely wondering why he chose such a location rather than some big city where, under the wing of a bulky corporation, he would build a great career. He is looking for a better future where there are no little tomatoes (the so-called cherry tomato) and no ragweed, to which he is extremely allergic. To spite him, Mr. Malnar grew a ragweed plant in his office.
Although ragweed is not a tree, it is interesting that Mr. Malnar's plant can be represented as a tree with n nodes connected by (n − 1) branches. Recall that a tree is an undirected, connected graph in which a unique path exists between every two nodes. It is known that the allergens are concentrated on the branches, but not all branches are equally potent. Mr. Malnar knows that the branch connecting nodes ui and vi has allergenicity wi. Accordingly, from the plant he will cut out a connected subset of branches of the greatest allergenicity. The allergenicity of a subset is defined as the product of the number of branches inside it and the allergenicity of the least allergenic branch inside that subset, that is, the branch with the minimum value of wi. Mr. Malnar is infallible and immediately found the subset with the greatest allergenicity.
Can you also determine the allergenicity of that subset?
Input
The first line contains the natural number n. (2 ≤ n ≤ 105)
The following n − 1 lines contain the numbers ui, vi and wi. (1 ≤ ui, vi ≤ n, ui ≠ vi, 1 ≤ wi ≤ 109) They represent the branches of the tree as described in the problem.
Output
In a single line, print the allergenicity of the most allergenic connected subset of the ragweed's branches.