This page is still under construction.

Parts of this page are still being built. What you see may change.

Intrepid climber

Interview

Time limit3sMemory limit256 MB

Summary
Starting from the root of a weighted tree, visit all marked nodes with free descents and costly climbs at minimum total energy.
Level

Medium5 of 10

Topics
Tree, DFS, Greedy
Solved
No attempts yet

Problem

You climbed the highest mountain of your city. You want to tell all your friends about it, and you decided to start with the ones who are trying to reach the exact spot where you are standing right now.

The mountain has NN landmarks, and one of them is the top, where you are now. Each friend climbing the mountain is at a different landmark other than the top, and you want to visit all of them. Tracks connect pairs of landmarks so that exactly one route, meaning one sequence of consecutive tracks, goes down from the top to every other landmark. Visiting two friends at two different landmarks may force you to go down some tracks, climb others, and go down again. Going down the mountain is easy, so it costs no energy, but each time you climb a track you spend a fixed amount of energy. After visiting all your friends, you can just sit and rest.

For example, take a mountain with 6 landmarks where tracks join 1 and 2, 1 and 3, 2 and 4, 3 and 5, and 3 and 6. If your friends are at landmarks 5 and 2, you can visit both by following the order 1 ↓ 2 ↑ 1 ↓ 3 ↓ 5, where a ↓ b means that you go down a track from landmark a to landmark b, and a ↑ b means that you climb a track from landmark a to landmark b. The order 1 ↓ 3 ↓ 5 ↑ 3 ↑ 1 ↓ 2 works as well.

You are given the tracks between the landmarks, the energy required to climb each of them, and the landmarks where your friends are. Compute the minimum total amount of energy required to visit all your friends starting from the top.

Input

The first line contains two integers NN and FF, the number of landmarks and the number of friends climbing the mountain (1≤F<N≤1051 \le F < N \le 10^5). Landmarks are identified by distinct integers from 1 to NN, and landmark 1 is the top of the mountain, where you start.

Each of the next N−1N - 1 lines describes one track with three integers AA, BB and CC, meaning that a track goes down from landmark AA to landmark BB and that climbing it requires CC energy (1≤A≤N1 \le A \le N, 2≤B≤N2 \le B \le N, A≠BA \ne B, 1≤C≤1001 \le C \le 100).

The last line contains FF distinct integers L1,L2,…,LFL_1, L_2, \dots, L_F, the landmarks where your friends are (2≤Li≤N2 \le L_i \le N). You may assume that the tracks are such that exactly one route goes down from the top of the mountain to each other landmark.

Output

Print one line with an integer, the minimum total amount of energy required to visit all your friends starting from the top of the mountain.

Examples3

  1. Example 1

    Input
    6 2
    1 2 2
    2 4 2
    1 3 3
    3 6 3
    3 5 1
    5 2
    
    Expected output
    2
    
  2. Example 2

    Input
    4 2
    1 2 2
    1 3 1
    3 4 2
    2 4
    
    Expected output
    2
    
  3. Example 3

    Input
    4 2
    1 4 1
    1 3 1
    4 2 2
    2 4
    
    Expected output
    0