//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define vec vector<long long>
#define pb push_back
#define sz(a) a.size()
#define fi first
#define se second
#define ret(a) return cout<<(a)<<"\n",void()
#define endl "\n"
#define el cout<<"\n"
#define f(a) for(int i=0;i<(a);i++)
#define f1(a) for(int i=1;i<(a);i++)
#define all(v) v.begin(),v.end()
template<class T> void PRINTARR(T a[], int n){for(int i=0;i<n;i++){std::cout<<a[i]<<" ";}std::cout<<'\n';}
template<class T> void PRINTVEC(std::vector<T> arr){for(int i=0;i<arr.size();i++){std::cout<<arr[i]<<" ";}std::cout << '\n';}
int tc=1;
void input(){
}
const ll oo = 1e18;
const int maxn = 2e5+5;
const int mod = 1e9+7;
const int S = 360;
//REMEMBER:
//USE LONG LONG WHEN NECESSARY
//ALWAYS USE "\n" OR el;
//PRAGMA?
int n,k;
vector<int> p;
vector<int> g[maxn];
int dp[2][maxn];
int tin[maxn]={},tout[maxn]={};
int vis[maxn]={};
int up[maxn][20]={};
int h[maxn]={};
//dp[i][j]=argmin{dp[i-1][k-1]+C(k,j)}
int L,R;
int cur=0,tm=1;
void dfs(int u){
tin[u]=tm;tm++;
for(auto v:g[u]){
if(!vis[v]){
vis[v]=1;
h[v]=h[u]+1;
up[v][0]=u;
for(int i=1;i<20;i++){
up[v][i]=up[up[v][i-1]][i-1];
}
dfs(v);
}
}
tout[u]=tm;tm++;
}
//2 way to imple:
//easy: set.
//hard: trie. (time+space efficient)
int rtin[2*maxn],rtout[2*maxn];
set<int> s;
void init(){
for(int i=1;i<=n;i++){
s.insert(tin[i]);
rtin[tin[i]]=i;
rtout[tout[i]]=i;
}
//sus
rtin[tout[1]]=1;
//
s.insert(tout[1]);
cur=(n-1)*2;
}
int lca(int u, int v){
if(h[u]>h[v]) swap(u,v);
//h[u]<=h[v]
int diff=h[v]-h[u];
for(int i=0;i<30;i++){
if(diff&(1<<i)){
v=up[v][i];
}
}
if(u==v) return u;
for(int i=19;i>=0;i--){
int u1=up[u][i],v1=up[v][i];
if(u1!=v1){
u=u1;v=v1;
}
}
return up[u][0];
}
int dis(int u, int v){
if(h[u]>h[v]) swap(u,v);
//h[u]<=h[v];
int x=lca(u,v);
if(x==u) return h[v]-h[u];
return (h[u]-h[x])+(h[v]-h[x]);
}
void update(int l, int r){
//test dnc dp xem tm tinh chat ko:
//cur=mn(l,r);
//Optimize:
while(l<L){
L--;
if(p[L]!=1){
s.insert(tin[p[L]]);
auto it1=s.lower_bound(p[L]);it1--;
auto it2=s.lower_bound(p[L]);it2++;
int prv=rtin[*it1],aft=rtin[*it2];
cur+=(dis(prv,p[L])+dis(p[L],aft)-dis(prv,aft));
}
}
while(l>L){
if(p[L]!=1){
auto it1=s.lower_bound(p[L]);it1--;
auto it2=s.lower_bound(p[L]);it2++;
int prv=rtin[*it1],aft=rtin[*it2];
s.erase(tin[p[L]]);
cur-=(dis(prv,p[L])+dis(p[L],aft)-dis(prv,aft));
}
L++;
}
while(r<R){
if(p[R]!=1){
auto it1=s.lower_bound(p[R]);it1--;
auto it2=s.lower_bound(p[R]);it2++;
int prv=rtin[*it1],aft=rtin[*it2];
s.erase(tin[p[R]]);
cur-=(dis(prv,p[R])+dis(p[R],aft)-dis(prv,aft));
}
R--;
}
while(r>R){
R++;
if(p[R]!=1){
s.insert(tin[p[R]]);
auto it1=s.lower_bound(p[R]);it1--;
auto it2=s.lower_bound(p[R]);it2++;
int prv=rtin[*it1],aft=rtin[*it2];
cur+=(dis(prv,p[R])+dis(p[R],aft)-dis(prv,aft));
}
}
}
void cal(int l, int r, int otpl, int otpr){
if(l>r) return;
int mid=(l+r)/2;
int otp;
for(int k=otpl;k<=min(mid,otpr);k++){
update(k,mid);
if(dp[1][mid]<=dp[0][k-1]+cur){
dp[1][mid]=dp[0][k-1]+cur;
otp=k;
}
}
cal(l,mid-1,otp,otpr);
cal(mid+1,r,otpl,otp);
}
void solve()
{
cin>>n>>k;
L=1,R=n;
p.resize(n+5);
f1(n+1) cin>>p[i];
f(n-1){
int u,v;
cin>>u>>v;
g[u].pb(v);
g[v].pb(u);
}
vis[1]=1;
h[1]=1;
dfs(1);
init();
memset(dp,0,sizeof(dp));
for(int i=1;i<=k;i++){
cal(1,n,1,n);
for(int j=0;j<=n;j++){
dp[0][j]=dp[1][j];
dp[1][j]=0;
}
}
cout<<dp[0][n];
}
int32_t main()
{
// ios_base::sync_with_stdio(0);
// cin.tie(0);
// cout.tie(0);
input();
while (tc--)
solve();
cerr << "\nTime elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms.\n";
return 0;
}