Fertilizing Pastures
Time limit2sMemory limit1024 MB
Walk a tree from pasture 1 to visit every pasture, where grass grows at rate a_i, and report the minimum time and the least fertilizer for that time.
Statement
There are pastures (), connected by roads so that they form a tree. Each road takes 1 second to cross. Every pasture starts with 0 grass, and the grass in pasture grows at () units per second. Farmer John starts in pasture 1 and must drive around to fertilize the grass in every pasture. If he visits a pasture with units of grass, it needs units of fertilizer. A pasture only needs fertilizer the first time it is visited, and fertilizing takes no time.
The input has an extra parameter .
- If , Farmer John must end at pasture 1.
- If , Farmer John may end at any pasture.
Compute the minimum time to fertilize every pasture, and the minimum amount of fertilizer needed to finish in that time.
Input
The first line contains and . Then for each from to , there is a line containing and , which means a road connects pastures and . It is guaranteed that .
Output
Print the minimum time and the minimum fertilizer, separated by a space.