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
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
#include <ext/numeric>
using namespace std ;
using namespace __gnu_cxx ;
typedef long long LL ;
typedef pair<int,int> PII ;
typedef vector<int> VI ;
const int INF = 1e9+100 ;
const LL INFLL = (LL)INF * (LL)INF ;
#define REP(i,n) for(i=0;i<(n);++i)
#define ALL(c) c.begin(),c.end()
#define VAR(v,i) __typeof(i) v=(i)
#define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i)
#define CLEAR(t) memset(t,0,sizeof(t))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second

template<class T1,class T2> ostream& operator<<(ostream &s, const pair<T1,T2> &x) { s<< "(" << x.FI << "," << x.SE << ")"; return s;}
template<class T>           ostream& operator<<(ostream &s, const vector<T>   &t) { FOREACH(it, t) s << *it << " " ; return s ; }
template<class T>           ostream& operator<<(ostream &s, const set<T>      &t) { FOREACH(it, t) s << *it << " " ; return s ; }
template<class T1,class T2> ostream& operator<<(ostream &s, const map<T1, T2> &t) { FOREACH(it, t) s << *it << " " ; return s ; }

///////////////////////////////////////////

const int MAXN = 500100 ;

VI G[MAXN] ;
int n, m ;
int a[MAXN] ;

int parent[MAXN] ;
pair<LL, PII> ret[MAXN] ; // zeby ominac limit na stos spamietujemy wywolania fukcji

VI topol ;
void DFS(int u, int p) {
	parent[u]=p ;
	FOREACH(it, G[u])
		if(*it != p)
			DFS(*it, u) ;
	topol.PB(u) ;
}

pair<LL, PII> doit(int u) {
	if(u<=m) return MP(0, MP(a[u],a[u])) ;
	else {
		vector<PII> segments ;
		LL pay=0 ;
		FOREACH(it, G[u]) {
			if(*it == parent[u]) continue ;
			pair<LL, PII> tmp = ret[*it] ;
			pay += tmp.FI ;
			segments.PB(tmp.SE) ;
		}
		vector<PII> ends ;
		LL sl=0, sr=0 ;
		int l=0, r=segments.size(), N=segments.size() ;
		FOREACH(it, segments) {
			ends.PB(MP(it->FI,0)) ;
			ends.PB(MP(it->SE,1)) ;
			sr += it->FI ;
		}
		sort(ALL(ends)) ;
		VI mySegment ;
		LL myPay=INFLL ;
		for(int i=0 ; i<ends.size() ; i++) {
			LL x = ends[i].FI ;
			if(ends[i].SE == 0)	{	// zaczyna sie
				r-- ;
				sr -= x ;
			}
			
			LL d = (x*l-sl)+(sr-x*r) ;
			assert(d>=0) ;
			if(d<myPay) {
				myPay = d ;
				mySegment = VI(1,x) ;
			}
			else if(d==myPay) {
				if(mySegment.empty() || mySegment.back()!=x)
					mySegment.PB(x) ;
			}
				
			if(ends[i].SE == 1) {	// konczy sie
				l++ ;
				sl += x ;
			}
		}
		assert(r==0 && l==N) ;
		assert(!mySegment.empty()) ;
		
		pair<LL, PII> ret = MP(pay+myPay, MP(mySegment[0], mySegment.back())) ;
		//cout << u << ") " << ret << endl ;
		return ret ;
	}
}

int main()
{
	ios_base::sync_with_stdio(0) ;
	int u, v, i ;
	cin >> n >> m ;
	REP(i, n-1) {
		cin >> u >> v ;
		G[u].PB(v) ;
		G[v].PB(u) ;
	}
	for(i=1 ; i<=m ; i++)
		cin >> a[i] ;	// rozstaw na stacjach zagranicznych
	
	if(n==m && n==2)	// specjalny przypadek, brak stacji w krolestwie
		cout << abs(a[2]-a[1]) << endl ;
	else {
		DFS(n, 0) ;
		FOREACH(it, topol)
			ret[*it] = doit(*it) ;
		
		cout << ret[n].FI << endl ;
	}
}