Under construction

Still being built. Things may change

Travelling Caterpillar

Interview

Time limit1sMemory limit1024 MB

Summary
Find the shortest closed walk from root 0 that visits every marked node in a weighted tree, and print its total length.
Level

Easy3 of 10

Topics
Tree, DFS
Solved
No attempts yet

Problem

Lilith is a hungry caterpillar! From her vantage point at the root of a tree, she has identified some leaves she wishes to munch before returning to the root. She wants to finish munching all of them as quickly as possible so that she will grow into a plump, buttery butterfly.

The tree Lilith occupies is a bit unusual. We can view it as a collection of nodes, where some of the nodes contain leaves that Lilith wishes to munch. Each branch connects exactly two nodes together. Between every pair of nodes, there is exactly one way to travel from one to the other.

Figure 1: Lilith's route through the tree.

Input

The first line contains two integers NN (1≤N≤10001 \le N \le 1000), the number of nodes in the tree, and KK (1≤K≤N1 \le K \le N), the number of leaves Lilith wants to munch.

The next N−1N-1 lines describe the branches. The iith line contains three integers sis_i, tit_i (0≤si,ti<N0 \le s_i, t_i < N, si≠tis_i \ne t_i), and did_i (0≤di≤1060 \le d_i \le 10^6). This means there is a branch between nodes sis_i and tit_i that takes did_i time to cross. If the tree is rooted at node 00, then sis_i is the parent of tit_i, so sis_i lies on the unique path from 00 to tit_i. Lilith always starts at the root node 00.

The last line contains KK distinct integers a1,…,aKa_1, \dots, a_K (0≤ai<N0 \le a_i < N), the nodes that contain leaves Lilith wants to munch.

Output

Print the length of the shortest path that starts and ends at the root along the branches and lets Lilith munch all the leaves.

Examples3

  1. Example 1

    Input
    7 3
    0 1 5
    0 4 2
    1 2 1
    1 3 4
    4 5 3
    4 6 3
    2 3 6
    
    Expected output
    30
    
  2. Example 2

    Input
    6 3
    0 1 5
    1 2 5
    2 3 42
    2 4 347
    2 5 612
    3 4 5
    
    Expected output
    2022
    
  3. Example 3

    Input
    3 2
    0 1 0
    0 2 21
    1 2
    
    Expected output
    42