#P10641. BZOJ3252 攻略

    ID: 12116 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>贪心线段树O2优化可并堆树链剖分

BZOJ3252 攻略

Background

As everyone knows, Katsuragi Keima is the god of “walkthroughs”. After turning on god mode, he can play through kk games at the same time.

Today he got a new game called XX Peninsula. This game has nn scenes. From some scenes, you can reach other scenes through different choices. All scenes and choice branches form a tree structure: when the game starts, you are at the root node (common route), and the leaf nodes are endings. Each scene has a value. Now Keima turns on god mode and plays through this game kk times at the same time. What is the maximum possible total value of the scenes he gets to watch? (Watching the same scene multiple times does not give the value repeatedly.)

“Why do you already know the value of each scene before playing?”
“I have already seen the ending.”

Problem Description

Given a tree with nn nodes. Each node has a positive integer weight. Select kk simple paths that start from the root node and end at leaf nodes. Find the maximum possible sum of the weights of all nodes in the union of these paths.

Input Format

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

The second line contains nn positive integers wiw_i, representing the weight of each node.

Then follow n1n - 1 lines. Each line contains two positive integers u,vu, v, meaning node uu is the parent of node vv.

Output Format

Output one positive integer, the answer.

5 2
4 3 2 1 1
1 2
1 5
2 3
2 4
10

Hint

For all testdata, it is guaranteed that 1n2×1051 \leq n \leq 2 \times 10^5, and 1wi23111 \leq w_i \leq 2^{31} - 1.

Translated by ChatGPT 5