#K76097. Optimal Festival Stalls

    ID: 34566 Type: Default 1000ms 256MiB

Optimal Festival Stalls

Optimal Festival Stalls

You are given a tree with n intersections (numbered from 1 to n) and exactly n - 1 bidirectional roads connecting them, which guarantees that the graph is a tree. In addition, you are given an integer k along with a list of k integers denoting product types. Although the product types are provided, they are not used in the calculation. The first k intersections (i.e. vertices 1 through k) are selected to set up festival stalls.

Your task is to compute the sum of distances between every pair of stalls. Here, the distance between two intersections is defined as the number of roads in the shortest path connecting them.

Note: All calculations should be based on the tree structure provided.

inputFormat

The input is read from standard input (stdin) and consists of the following:

  • The first line contains two integers n and k, where n is the number of intersections and k is the number of stalls.
  • The second line contains k integers representing the product types for each stall (this information is not used in the computation).
  • The next n - 1 lines each contain two integers u and v, indicating a road connecting intersections u and v.

outputFormat

Output a single integer to standard output (stdout) representing the sum of distances between every pair of stalls located at intersections 1 through k.

## sample
6 3
1 1 2
1 2
1 3
2 4
2 5
3 6
4