Treeland is a country in which there are n towns connected by n - 1 two-way road such that it’s possible to get from any town to any other town.
In Treeland there are 2k universities which are located in different towns.
Recently, the president signed the decree to connect universities by high-speed network.The Ministry of Education understood the decree in its own way and decided that it was enough to connect each university with another one by using a cable. Formally, the decree will be done!
To have the maximum sum in the budget, the Ministry decided to divide universities into pairs so that the total length of the required cable will be maximum. In other words, the total distance between universities in k pairs should be as large as possible.
Help the Ministry to find the maximum total distance. Of course, each university should be present in only one pair. Consider that all roads have the same length which is equal to 1.
Input
The first line of the input contains two integers n and k (2 ≤ n ≤ 200 000, 1 ≤ k ≤ n / 2) — the number of towns in Treeland and the number of university pairs. Consider that towns are numbered from 1 to n.
The second line contains 2k distinct integers u1, u2, …, u2k (1 ≤ ui ≤ n) — indices of towns in which universities are located.
The next n - 1 line contains the description of roads. Each line contains the pair of integers xj and yj (1 ≤ xj, yj ≤ n), which means that the j-th road connects towns xj and yj. All of them are two-way roads. You can move from any town to any other using only these roads.
Output
Print the maximum possible sum of distances in the division of universities into k pairs.
Examples
Input
7 2
1 5 6 2
1 3
3 2
4 5
3 7
4 3
4 6
Output
6
Input
9 3
3 2 1 6 5 9
8 9
3 2
2 7
3 4
7 6
4 5
2 1
2 8
Output
9
Note
The figure below shows one of possible division into pairs in the first test. If you connect universities number 1 and 6 (marked in red) and universities number 2 and 5 (marked in blue) by using the cable, the total distance will equal 6 which will be the maximum sum in this example.
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=6005;
const int inf=0x7fffffff;
int dp[maxn],n,r[maxn];//dp[i]i+1长度的结尾最小人口数,n 城市数,r[i] 第i个城市的人口数
int first[maxn],nxt[2*maxn],to[2*maxn],len[maxn];//邻接表相关
int ans;
void dfs(int s,int f)
{
int pos=lower_bound(dp,dp+n,r[s])-dp;//找到一个比s人口值小的dp[]值
int tmp=dp[pos];//保存这个dp值
ans=max(pos+1,ans);
dp[pos]=min(dp[pos],r[s]);//更新dp
for(int i=first[s]; i!=-1; i=nxt[i]) //继续往下延拓
{
int t=to[i];
if(t==f)
continue;
dfs(t,s);
}
dp[pos]=tmp;//恢复
}
void addedge(int f,int t,int ind)
{
nxt[ind*2]=first[f];
to[ind*2]=t;
first[f]=2*ind;
nxt[ind*2+1]=first[t];
to[ind*2+1]=f;
first[t]=2*ind+1;
}
int main()
{
scanf("%d",&n);
memset(first,-1,sizeof(first));
fill(dp,dp+n,inf);//初始化
for(int i=1; i<=n; i++)
scanf("%d",r+i);//输入
for(int i=1; i<n; i++)
{
int f,t;
scanf("%d%d",&f,&t);
addedge(f,t,i-1);
}
for(int i=1; i<=n; i++)
{
dfs(i,-1); //开始分别以i为根遍历
}
printf("%d\n",ans);//输出
return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int maxm = 2e5 + 10;
const int maxn = 2e5 + 10;
typedef long long ll;
struct Edges
{
int u,v;
} edge[maxm];
vector<int> G[maxn];
int cnt[maxn];
int dfs(int cur,int from)
{
for(int i = 0 ; i < G[cur].size() ; i ++)
{
int v = G[cur][i];
if(v == from)
continue;
cnt[cur] += dfs(v,cur);
}
return cnt[cur];
}
int main ()
{
int n,k;
scanf("%d %d", &n,&k);
for(int i = 1 ; i <= k*2 ; i ++)
{
int tmp;
scanf("%d", &tmp);
cnt[tmp] = 1;
}
int cn = 0;
for(int i = 0 ; i < n-1 ; i ++)
{
int u,v;
scanf("%d %d", &u,&v);
edge[i].u = u;
edge[i].v = v;
G[u].push_back(v);
G[v].push_back(u);
}
dfs(1,-1);
ll exp = 0;
for(int i = 0 ; i < n-1 ; i ++)
{
Edges & e = edge[i];
int tt = min(cnt[e.u],cnt[e.v]);
int tmp = min(2*k-tt,tt);
exp += tmp;
}
printf("%I64d\n",exp);
}