#P1213G. Path Queries
Path Queries
No submission language available for this problem.
Description
You are given a weighted tree consisting of vertices. Recall that a tree is a connected graph without cycles. Vertices and are connected by an edge with weight .
You are given queries. The -th query is given as an integer . In this query you need to calculate the number of pairs of vertices () such that the maximum weight of an edge on a simple path between and doesn't exceed .
The first line of the input contains two integers and () — the number of vertices in the tree and the number of queries.
Each of the next lines describes an edge of the tree. Edge is denoted by three integers , and — the labels of vertices it connects (, ) and the weight of the edge (). It is guaranteed that the given edges form a tree.
The last line of the input contains integers (), where is the maximum weight of an edge in the -th query.
Print integers — the answers to the queries. The -th value should be equal to the number of pairs of vertices () such that the maximum weight of an edge on a simple path between and doesn't exceed .
Queries are numbered from to in the order of the input.
Input
The first line of the input contains two integers and () — the number of vertices in the tree and the number of queries.
Each of the next lines describes an edge of the tree. Edge is denoted by three integers , and — the labels of vertices it connects (, ) and the weight of the edge (). It is guaranteed that the given edges form a tree.
The last line of the input contains integers (), where is the maximum weight of an edge in the -th query.
Output
Print integers — the answers to the queries. The -th value should be equal to the number of pairs of vertices () such that the maximum weight of an edge on a simple path between and doesn't exceed .
Queries are numbered from to in the order of the input.
Samples
Note
The picture shows the tree from the first example: