PivotOJ

Fertilizing Pastures

시간 제한: 2000ms메모리 제한: 1024MB출처: USACO 2023 February GoldBOJ 27844

문제

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 aia_i (1ai1081\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 T{0,1}T\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 pip_i and aia_i, meaning that there is a road connecting pastures pip_i and ii. It is guaranteed that 1pi<i1\le p_i<i.

출력

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

예제

예제 1

입력
5 0
1 1
1 2
3 1
3 4
출력
8 21

예제 2

입력
5 1
1 1
1 2
3 1
3 4
출력
6 29
코드를 제출하려면 로그인하세요.