This page is still under construction.

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

Cities

Interview

Time limit4sMemory limit256 MB

Summary
Given a weighted undirected graph, pick a minimum-cost set of edges so that all k special cities (k at most 10) end up in one connected component.
Level

Medium7 of 10

Topics
Minimum spanning tree, Dynamic programming, Graph, Union-find
Solved
No attempts yet

Problem

Apple Country has nn cities, and kk of them are important cities that King Gusagwa visits often. There are mm roads connecting pairs of cities, and the cities are numbered 11 through nn.

One day a storm hit Apple Country. It was the largest natural disaster since the country was founded, and every road became unusable. The ministers decided to rebuild roads so that traffic between cities can continue, and they surveyed the cost of rebuilding each road.

King Gusagwa had lived a life of luxury and indulgence, so the treasury was empty and not every road could be rebuilt. For now the ministers want to rebuild a set of roads that connects the important cities to each other, meaning that from any important city you can reach every other important city.

Find the minimum cost of rebuilding roads so that the important cities are connected to each other.

Input

The first line contains the number of cities nn, the number of important cities kk, and the number of roads mm, separated by spaces.

The second line contains kk integers separated by spaces. They are distinct integers between 11 and nn, and they give the numbers of the important cities.

Each of the next mm lines describes one road with three integers aa, bb, cc. The road connects city aa and city bb, and rebuilding that road costs cc.

Every city is connected by roads. Several roads may connect the same pair of cities.

Output

Print the minimum cost of rebuilding roads so that the important cities are connected to each other, as a single integer.

Constraints

  • 1≤n≤1001 \le n \le 100
  • 1≤k≤min⁡(n,10)1 \le k \le \min(n, 10)
  • 0≤m≤20000 \le m \le 2000
  • 1≤a,b≤n1 \le a, b \le n, a≠ba \ne b
  • 1≤c≤1091 \le c \le 10^9

Examples2

  1. Example 1

    Input
    4 3 6
    1 3 4
    1 2 4
    1 3 9
    1 4 6
    2 3 2
    2 4 5
    3 4 8
    
    Expected output
    11
    
  2. Example 2

    Input
    2 2 1
    1 2
    1 2 7
    
    Expected output
    7