Linova and Kingdom
Contest: Codeforces Round 635 (Div. 1)
Difficulty rating: 1600
Tags: dfs and similar, dp, greedy, sortings, trees

Writing light novels is the most important thing in Linova's life. Last night, Linova dreamed about a fantastic kingdom. She began to write a light novel for the kingdom as soon as she woke up, and of course, she is the queen of it.

There are $$$n$$$ cities and $$$n-1$$$ two-way roads connecting pairs of cities in the kingdom. From any city, you can reach any other city by walking through some roads. The cities are numbered from $$$1$$$ to $$$n$$$, and the city $$$1$$$ is the capital of the kingdom. So, the kingdom has a tree structure.

As the queen, Linova plans to choose exactly $$$k$$$ cities developing industry, while the other cities will develop tourism. The capital also can be either industrial or tourism city.

A meeting is held in the capital once a year. To attend the meeting, each industry city sends an envoy. All envoys will follow the shortest path from the departure city to the capital (which is unique).

Traveling in tourism cities is pleasant. For each envoy, his happiness is equal to the number of tourism cities on his path.

In order to be a queen loved by people, Linova wants to choose $$$k$$$ cities which can maximize the sum of happinesses of all envoys. Can you calculate the maximum sum for her?

Input format:
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$2\le n\le 2 \cdot 10^5$$$, $$$1\le k< n$$$)  — the number of cities and industry cities respectively.

Each of the next $$$n-1$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1\le u,v\le n$$$), denoting there is a road connecting city $$$u$$$ and city $$$v$$$.

It is guaranteed that from any city, you can reach any other city by the roads.

Output format:
Print the only line containing a single integer  — the maximum possible sum of happinesses of all envoys.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
In the first example, Linova can choose cities $$$2$$$, $$$5$$$, $$$6$$$, $$$7$$$ to develop industry, then the happiness of the envoy from city $$$2$$$ is $$$1$$$, the happiness of envoys from cities $$$5$$$, $$$6$$$, $$$7$$$ is $$$2$$$. The sum of happinesses is $$$7$$$, and it can be proved to be the maximum one.

In the second example, choosing cities $$$3$$$, $$$4$$$ developing industry can reach a sum of $$$3$$$, but remember that Linova plans to choose exactly $$$k$$$ cities developing industry, then the maximum sum is $$$2$$$.

Example input:
7 4
1 2
1 3
1 4
3 5
3 6
4 7

Example output:
7

Example input:
4 1
1 2
1 3
2 4

Example output:
2

Editorial:
Lemma: In the optimum solution, if a node (except the root) is chosen to develop tourism, its parent must be chosen to develop tourism, too.
Proof: Otherwise, if we choose its parent to develop tourism instead of itself, the sum of happiness will be greater.
Then we can calculate how much happiness we will get if we choose a certain node to develop tourism. Suppose the depth of node $$$u$$$ is $$$dep_u$$$ (i.e. there are $$$dep_u$$$ nodes on the path $$$(1,u)$$$), the size of the subtree rooted on $$$u$$$ is $$$siz_u$$$ (i.e. there are $$$siz_u$$$ nodes $$$v$$$ that $$$u$$$ is on the path $$$(1,v)$$$). Then, if we choose $$$u$$$ to develop tourism, compared to choose it to develop industry, the total happiness will be increased by $$$siz_u-dep_u$$$: the envoy of $$$u$$$ won't be sent, we will lose $$$dep_u-1$$$ happiness; a total of $$$siz_u-1$$$ envoys of all nodes in the subtree rooted on $$$u$$$ except $$$u$$$ itself will get one more happiness.
So, just use DFS to calculate all $$$siz_u-dep_u$$$, then sort them from big to small, calculate the sum of the first $$$n-k$$$ elements. You can also use std::nth_element in STL to get an $$$O(n)$$$ solution.