#P5984. [PA 2019] Podatki drogowe

[PA 2019] Podatki drogowe

Description

Given an unrooted tree with nn nodes, numbered from 11 to nn. The weight of every edge is a positive integer power of nn.

Define the distance d(u,v)\operatorname{d(u,v)} from uu to vv as the sum of edge weights along the unique simple path between uu and vv in the tree.

Given kk, among the n×(n1)2\dfrac{n\times (n-1)}{2} values d(u,v)(1u<vn)\operatorname{d(u,v)}(1\le u<v\le n), find the kk-th smallest value.

Input Format

The first line contains two positive integers n,kn, k.

The next n1n-1 lines each contain three positive integers x,y,z(1x,y,zn)x, y, z(1\le x, y, z\le n), indicating an edge connecting xx and yy with weight nzn^z.

Output Format

Output one integer: the kk-th smallest value modulo 109+710^9+7.

5 8
1 2 1
3 1 3
3 4 1
5 3 2
135

Hint

For 100%100\% of the testdata, 2n2.5×1042\le n\le 2.5\times 10^4, and 1kn(n1)21\le k\le \dfrac{n*(n-1)}{2}.


Sample Explanation

After sorting all dd, we get, in order: 5,5,25,30,125,130,130,135,150,1555, 5, 25, 30, 125, 130, 130, 135, 150, 155.

Translated by ChatGPT 5