Ascending Tree

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

문제

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: NN, the number of vertices in the tree, and C_1C\_1, the label assigned to the root. The vertices are numbered 11 through NN, and the root is vertex 11 (1N1051 \le N \le 10^5).

The next N1N - 1 line describe non-root vertices. The ii-th line contains two integers: P_iP\_i, the number of the parent of the vertex ii, and C_iC\_i, the label assigned to the vertex ii (1P_i<i1 \le P\_i < i, 109c_i109-10^9 \le c\_i \le 10^9).

출력

Print the minimum cost on a single line.

힌트

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.