Tree of rabbits
by Behruzbek
English
Language
Contribute a translation- Time limit
- 1000 ms
- Memory limit
- 512 MB
- Submissions
- 2
- Correct
- 2
- Solved by
- 1
- AC rate
- 100.000%
Statement
We have a tree with $N$ vertices numbered $1, 2, \dots, N$, where a rabbit resides at each vertex. The $i$-th edge ($1 \le i \le N-1$) connects vertex $U_i$ and vertex $V_i$ with an initial weight $W_i$.
Let the overall tiredness of the rabbits be defined as:
$$\sum_{i=1}^{N} \sum_{j=i+1}^{N} f(i,j)$$
where $f(i,j)$ represents the distance (the sum of edge weights) on the unique shortest path between vertex $i$ and vertex $j$. You have the Wizard Rabbit friend who can set the weight of any edge to $0$. However, because this process consumes a significant amount of energy, you can ask the Wizard Rabbit to modify at most $K$ edges.
By choosing the edges to modify optimally, find the minimum possible overall tiredness of the rabbits.
Input
The first line contains two integers $N$ and $K$ ($2 \le N \le 2 \cdot 10^5$, $0 \le K \le N-1$) — the number of vertices in the tree and the maximum number of edges the Wizard Rabbit can set to zero.
The following $N-1$ lines describe the tree. The $i$-th of these lines contains three integers $U_i$, $V_i$, and $W_i$ ($1 \le U_i, V_i \le N$, $U_i \neq V_i$, $1 \le W_i \le 10^6$) — the vertices connected by the $i$-th edge and its initial weight.
Output
Print the minimum possible overall tiredness of the rabbits.
Examples
5 3 1 2 1 2 3 1 3 4 1 4 5 1
4
Tags
No tags yet