#P9808. [POI 2022/2023 R1] zbo

[POI 2022/2023 R1] zbo

Background

This problem is translated from POI2022~2023R1 zbo

Problem Description

In ancient times, there was a king who ruled nn villages. These villages were connected by n1n - 1 roads, and the king’s original castle was in village 11.

The king’s sons will soon become adults. As adult princes, they need their own castles, so new castles will be built in some villages.

Each castle needs to communicate. However, the distances are too large, so each day every castle sends some carrier pigeons to deliver messages to every other castle. A carrier pigeon needs to eat one gram of grain for every kilometer it travels.

Please write a program to compute, after each castle is built (in the given input order), the minimum total amount of grain needed so that all castles can communicate.

More specifically, let dis(x,y)dis(x,y) be the amount of grain needed to travel from xx to yy. After building castle ii, compute x=1iy=1idis(x,y)\sum ^{i} _{x=1} \sum ^{i}_{y=1} dis(x,y).

Note that in the above formula, the cost for two identical locations is taken to be 00.

Input Format

The first line contains two integers nn and k (1k<n105)k \ (1 \leq k < n \leq 10^5), representing the number of villages and the number of new castles to be built.

The next n1n - 1 lines each contain three integers $a,b,c \ (1 \leq a,b \leq n, a \neq b, 1 \leq c \leq 1000)$, indicating that there is an undirected edge of length cc between aa and bb.

The next kk lines each contain one integer did_{i}, meaning that the ii-th castle is built at village did_{i}. Note that no two castles are built at the same village.

Output Format

For each built castle, output the minimum total cost.

5 3
1 4 3
3 1 6
1 2 5
4 5 1
5
3
2
8
40
90

Hint

The subtasks are as follows:

Subtask ID Special property Score
11 nk105n \cdot k \leq 10^5 1515
22 The villages form a chain from 11 to nn 3535
33 No special property 5050

In this problem, subtask 00 is the sample.

Translated by ChatGPT 5