Logo
(追記) (追記ここまで)

19381번 - Ascending Tree 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 256 MB90473748.052%

문제

You are given a rooted tree. Each vertex of the tree is labeled with an integer. If you pay one dollar, you can change (increment or decrement) the label of a vertex by one.

You want to change the labels such that, for each vertex, its label is strictly greater than any of the labels assigned to its children. Compute the minimum cost required to satisfy this condition.

입력

The first line contains two integers: $N,ドル the number of vertices in the tree, and $C_1,ドル the label assigned to the root. The vertices are numbered 1ドル$ through $N,ドル and the root is vertex 1ドル$ (1ドル \le N \le 10^5$).

The next $N - 1$ line describe non-root vertices. The $i$-th line contains two integers: $P_i,ドル the number of the parent of the vertex $i,ドル and $C_i,ドル the label assigned to the vertex $i$ (1ドル \le P_i < i,ドル $-10^9 \le c_i \le 10^9$).

출력

Print the minimum cost on a single line.

제한

예제 입력 1

8 6
1 1
2 1
2 3
1 9
5 6
6 6
6 2

예제 출력 1

8

예제 입력 2

4 5
1 5
2 5
3 5

예제 출력 2

4

힌트

The figure on the left is the input configuration for the first sample. The figure on the right is a possible final configuration: the label of each parent is strictly greater than that of its child.

출처

Camp > Petrozavodsk Programming Camp > Winter 2017 > Day 3: Japanese Contest, Head of Republic of Karelia Cup, Round I L번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /