1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include<bits/stdc++.h>
#define PII pair<int,int>
#define f first
#define s second
#define VI vector<int>
#define LL long long
#define MP make_pair
#define LD long double
#define PB push_back
#define ALL(V) V.begin(),V.end()
#define abs(x) max((x),-(x))
#define PDD pair<LD,LD> 
#define VPII vector< PII > 
#define siz(V) ((int)V.size())
#define FOR(x, b, e)  for(int x=b;x<=(e);x++)
#define FORD(x, b, e) for(int x=b;x>=(e);x--)
#define REP(x, n)     for(int x=0;x<(n);x++)
#define mini(a,b) a=min(a,b)
#define maxi(a,b) a=max(a,b)
using namespace std;
int n,a,b,c,d,k,m,q,z;
const int MXN=5e5+5;
VI V[MXN];
int in[MXN];
LL res=0;
PII dp[MXN];
namespace pom
	{
	VPII V;
	LL sskon;
	LL sspoc;
	void addsegment(int a,int b)
		{
		V.PB(MP(a,-1));
		V.PB(MP(b,1));
		sspoc+=a;
		}
	PII calc()
		{
		sort(ALL(V));
		int x=V.size()/4;
		
		int kon=0;
		int poc=0;
		LL skon=0;
		LL spoc=0;
		int i=0;
		LL rez=1e18;
		int mi=1e9;
		int mx=0;
		while(i<V.size())
			{
			int ter=V[i].f;
			while(i<V.size() && V[i].f==ter && V[i].s==-1)
				{
				poc++;
				spoc+=V[i].f;
				i++;
				}
			int l=kon;
			int p=V.size()/2-poc;
			
			LL sl=skon;
			LL sp=sspoc-spoc;
			
//			cerr<<l<<" "<<p<<" "<<sl<<" "<<sp<<" "<<i<<" t: "<<ter<<endl;
			
			if(l<=x && p<=x)
				{
				LL terres=sp-(LL)p*ter+(LL)l*ter-sl;
				if(rez>terres)
					{
					mi=mx=ter;
					rez=terres;
					}
				if(rez==terres)
					{
					mx=ter;
					}
				}
			
			while(i<V.size() && V[i].f==ter)
				{
				kon++;
				skon+=V[i].f;
				i++;
				}
			}

		res+=rez;
//		cerr<<rez<<" oo "<<mi<<" "<<mx<<endl;
		return MP(mi,mx);
		}
	void clear()
		{
		V.clear();
		sspoc=0;
		}
	}
int odl[MXN];
void dfs(int v)
	{
	if(v<=m)
		{
		dp[v]=MP(in[v],in[v]);
		return;
		}
	REP(i,V[v].size())
		{
		if(odl[V[v][i]])continue;
		odl[V[v][i]]=odl[v]+1;
		dfs(V[v][i]);
		}
	
	pom::clear();
	REP(i,V[v].size())
		{
		if(odl[V[v][i]]<odl[v])continue;
		pom::addsegment(dp[V[v][i]].f,dp[V[v][i]].s);
		}
	dp[v]=pom::calc();
//	cerr<<v<<" "<<dp[v].f<<" "<<dp[v].s<<endl;
	}
void readint(int &a)
  {
  char ch=0;
  a=0;
  while(ch<33)ch=getchar_unlocked();
  do
    {
    a=(a<<1)+(a<<3)+ch-'0';
    ch=getchar_unlocked();
    }
  while(ch>33);
  }
main()
{
scanf("%d%d",&n,&m);

FOR(i,1,n-1)
	{
	readint(a);
	readint(b);
	V[a].PB(b);
	V[b].PB(a);
	}
FOR(i,1,m)
	{
	readint(in[i]);
	}
if(n==2&&m==2)
	{
	printf("%d\n",abs(in[1]-in[2]));
	return 0;
	}
odl[n]=1;
dfs(n);
printf("%lld\n",res);
}