QOJ.ac

QOJ

Time Limit: 7 s Memory Limit: 512 MB Total points: 100 Hackable ✓

#5363. ZYB's Tour Plan

Statistics

The city where ZYB lives can be abstracted as a tree with $N$ nodes, where each node is a tourist attraction. Node 1 is ZYB's home (which is also a tourist attraction).

One day, ZYB decides to revisit all the tourist attractions (including his own home). To make a plan, ZYB ranks all attractions into a permutation $P$ based on his preference, where an attraction appearing earlier in the permutation is more preferred by ZYB.

Now, ZYB needs to create his tour plan. This plan spans $K$ days, and each day ZYB will choose some attractions to visit. The plan must satisfy the following conditions:

  1. To ensure he doesn't get bored, ZYB must visit at least one attraction each day that has not been visited before.
  2. By the end of day $K$, all attractions must have been visited exactly once.
  3. After any day's tour, there cannot be an unvisited attraction such that there exists another attraction with a lower preference (appearing later in $P$) that has already been visited.

Each day, ZYB starts from his home, travels the shortest path to visit all the chosen attractions (Note: the set of attractions chosen each day is unordered, meaning ZYB can determine the order of visitation to minimize the fatigue, and he will always choose the path with the minimum fatigue), and then returns home. The fatigue for each day is defined as the number of edges he traverses.

As a fitness enthusiast, ZYB wants to maximize the total fatigue over these $K$ days. Please help him create a plan and find this maximum total fatigue.

For example, in the sample, you could choose $\{2, 4\}$ and $\{3, 5, 1\}$, or $\{2, 4, 3\}$ and $\{5, 1\}$. In the first case, on the first day he traverses $1-2-3-4-3-2-1$ (ZYB can pass through an attraction without visiting it), and on the second day he traverses $1-2-3-4-5-4-3-2-1$, resulting in a total fatigue of $6+8=14$. If he chooses $\{2\}$ and $\{4, 3, 5, 1\}$, the fatigue is $2+8=10$.

Input

The first line contains two integers $N$ and $K$, representing the number of nodes and the number of days.

The second line contains $N$ integers, describing the permutation $P$.

The next $N-1$ lines each contain two integers $x$ and $y$, describing the tree.

Output

Output the answer on a single line.

Examples

Input 1

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

Output 1

14

Subtasks

Subtask 1 (5 pts): $K=2$

Subtask 2 (10 pts): $N \leq 1000$

Subtask 3 (15 pts): $N \leq 10000$

Subtask 4 (10 pts): The degree of node 1 is at most 5, and the degree of other nodes is at most 2.

Subtask 5 (60 pts): No special restrictions.

For all data, $2 \leq K \leq N \leq 200\,000$.

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.