#P3177. [HAOI2015] 树上染色

[HAOI2015] 树上染色

Description

There is a tree with nn nodes, and each edge has a weight. You are given a positive integer kk in 0n0 \sim n. You need to choose kk nodes and color them black, and color the remaining nkn - k nodes white. After coloring all nodes, you gain a profit equal to the sum of pairwise distances among black nodes plus the sum of pairwise distances among white nodes. What is the maximum possible profit?

Input Format

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

Lines 22 to nn each contain three positive integers u,v,wu, v, w, indicating that there is an edge (u,v)(u, v) of length ww in the tree. The input guarantees that all nodes are connected.

Output Format

Output a single positive integer, the maximum profit.

3 1
1 2 1
1 3 2
3

Hint

For 100%100\% of the testdata, 0n,k20000 \leq n, k \leq 2000.

Translated by ChatGPT 5