luogu#P3177. [HAOI2015] 树上染色

    ID: 2246 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>动态规划 DP2015河南各省省选树形 DP

[HAOI2015] 树上染色

Problem 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