You are given a tree...
题号:NC241125
时间限制:C/C++/Rust/Pascal 2秒,其他语言4秒
空间限制:C/C++/Rust/Pascal 1024 M,其他语言2048 M
64bit IO Format: %lld

题目描述

You are given an edge-weighted tree with vertices labeled through , where each vertex i has a value a_i.

You need to select a subset of at most k vertices with distinct values. You want to maximize the sum of weights of "spanned" edges, where an edge e is "spanned" if and only if there exists such that e lies on the only path between u and v.

输入描述:

The first line contains two integers , denoting the size of the tree and the maximum size of the subset you need to choose, respectively.

The second line contains n integers , denoting the value of each vertex.

Then n-1 lines follow, where each line contains 3 integers , denoting there is an edge (u,v) with weight w in the tree.

输出描述:

Output an integer in a line, denoting the maximized sum of weights of "spanned" edges.
示例1

输入

复制
4 3
1 2 3 4
1 2 1
1 3 2
1 4 3

输出

复制
6
示例2

输入

复制
4 2
1 2 3 3
1 2 1
1 3 2
2 4 3

输出

复制
4

备注:

For the first sample test, you can choose  so that every edge of the tree is "spanned".

For the second sample test, note that since , you cannot choose even though that would span every edge in the tree. A valid subset that maximizes the sum of weights of ``spanned'' edges is .