QOJ.ac

QOJ

时间限制: 2 s 内存限制: 128 MB 总分: 100 可 Hack ✓

#17192. Epidemic Control

统计

Country H has $n$ cities connected by $n-1$ bidirectional roads, forming a tree. City $1$ is the capital and the root of the tree.

A highly contagious disease has broken out in the capital. To control the epidemic and prevent it from spreading to the border cities (represented by the leaf nodes), the authorities have decided to deploy troops to establish checkpoints in some cities. This must be done such that every path from the capital to a border city contains at least one checkpoint. Checkpoints can also be established in border cities. However, it is strictly forbidden to establish a checkpoint in the capital.

Currently, troops are already stationed in some cities of Country H, and multiple troops can be stationed in a single city. A troop can move between cities connected by roads and establish a checkpoint in any city except the capital. Each troop can only establish one checkpoint. The time required for a troop to move along a road from one city to another is equal to the length of the road (in hours).

Find the minimum time required to control the epidemic. Note that different troops can move simultaneously.

Input

The first line contains an integer $n$, representing the number of cities.

The next $n-1$ lines each contain three integers $u$, $v$, and $w$, separated by spaces, indicating a road of length $w$ between city $u$ and city $v$. It is guaranteed that the input forms a tree and the root node is $1$.

The next line contains an integer $m$, representing the number of troops.

The next line contains $m$ integers, separated by spaces, representing the city IDs where the $m$ troops are currently stationed.

Output

Output a single integer representing the minimum time required to control the epidemic. If it is impossible to control the epidemic, output $-1$.

Examples

Input 1

4
1 2 1
1 3 2
3 4 3
2
2 2

Output 1

3

Note 1

The first troop establishes a checkpoint at city $2$, and the second troop moves from city $2$ to city $3$ to establish a checkpoint. The total time required is $3$ hours.

Constraints

It is guaranteed that no troops are stationed in the capital.

For $20\%$ of the data, $2 \le n \le 10$;

For $40\%$ of the data, $2 \le n \le 50$, $0 < w < 10^5$;

For $60\%$ of the data, $2 \le n \le 1000$, $0 < w < 10^6$;

For $80\%$ of the data, $2 \le n \le 10{,}000$;

For $100\%$ of the data, $2 \le m \le n \le 50{,}000$, $0 < w < 10^9$.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.