Fertilizing Pastures

아직 제출이 없습니다시간 제한2초메모리 제한1024 MB

문제

There are NN pastures (2N21052 \le N \le 2\cdot 10^5), connected by N1N-1 roads, such that they form a tree. Every road takes 1 second to cross. Each pasture starts out with 0 grass, and the iith pasture's grass grows at a rate of a_ia\_i (1a_i1081\le a\_i\le 10^8) units per second. Farmer John is in pasture 1 at the start, and needs to drive around and fertilize the grass in every pasture. If he visits a pasture with xx units of grass, it will need xx amount of fertilizer. A pasture only needs to be fertilized the first time it is visited, and fertilizing a pasture takes 0 time.

The input contains an additional parameter T0,1T\in \\{0,1\\}.

  • If T=0T=0, Farmer John must end at pasture 1.
  • If T=1T=1, Farmer John may end at any pasture.

Compute the minimum amount of time it will take to fertilize every pasture and the minimum amount of fertilizer needed to finish in that amount of time.

입력

The first line contains NN and TT.

Then for each ii from 22 to NN, there is a line containing p_ip\_i and a_ia\_i, meaning that there is a road connecting pastures p_ip\_i and ii. It is guaranteed that 1p_i\<i1\le p\_i\<i.

출력

The minimum amount of time and the minimum amount of fertilizer, separated by spaces.