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
#include<cstdio>
#include<algorithm>
#include<utility>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
#include<iostream>

#define FORE(c, a, b) for(int c=a; c<= int(b); ++c)
#define FORD(c, a, b) for(int c=a; c>= int(b); --c)
#define FORIT(it, cont, cont_t) for(cont_t::iterator it = cont.begin(); it != cont.end(); ++it)
#define REP(i, n) for(int i = 0; i < (n); ++i)
#define ALL(a) a.begin(), a.end() 
#define PR(n) printf("%d ", (int) (n)) 
#define PRL(n) printf("%lld ", (ll) (n)) 
#define SC(n) scanf("%d ", &n) 

#define xx first
#define yy second
#define pb push_back
#define mp make_pair
#define itr iterator

#define dbg if(0)
#define prd dbg printf

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned int uint;

typedef vector<int> vi;
typedef set<int> si;
typedef map<int, int> mi;
typedef pair<int, int> pi;
typedef pair<int, ll> pil;
typedef vector<pi> vpi;
typedef vector<pil> vpil;

const int NMAX = 500 * 1000 + 9;

int n, m, x, y, k;
int roz[NMAX], a[NMAX], b[NMAX], done[NMAX];
ll f[NMAX];
vi edges[NMAX], sta;

void update(int v, vpi &events) {
	if(events.empty()) prd("SHIET %d\n", v);
	int out = 0, bef = 0;
	ll score = 0;
	vpil sc;
	
	sort(ALL(events));
	int y = events[0].xx;
	FORIT(it, events, vpi)
		if(it->yy == 1) {
			bef++;
			score += (it->xx - y);
		}			
	ll minScore = score;
			
	FORIT(it, events, vpi) {
		score += (ll) (it->xx - y) * (out - bef);
		minScore = min(score, minScore);
		sc.pb(mp(it->xx, score));
				
		if(it->yy == 1)
			bef--;
		else
			out++;
		y = it->xx;
	}
	
	f[v] += minScore;
	FORE(i, 0, sc.size() - 1)
		if(sc[i].yy == minScore) {
			a[v] = sc[i].xx;
			break;
		}
	FORD(i, sc.size() - 1, 0)
		if(sc[i].yy == minScore) {
			b[v] = sc[i].xx;
			break;
		}
	prd("updated %d: [%d, %d] f_min = %lld events.size = %d\n", v, a[v], b[v], f[v], (int) events.size());
}

void dfs(int v) {
	prd("dfs %d\n", v);
	if(!done[v]) {
		done[v] = k++;
		if(v < m)
			a[v] = b[v] = roz[v];
		else {
		FORIT(it, edges[v], vi)
			if(!done[*it])
				sta.pb(*it);
		}
	} else {
		sta.pop_back();
		if(v >= m) {
			vpi events;
			events.reserve(2 * edges[v].size());
			FORIT(it, edges[v], vi)
				if(done[*it] > done[v]) {
					f[v] += f[*it];
					events.pb(mp(a[*it], 1));
					events.pb(mp(b[*it], -1));
				}
			update(v, events);
		}
	}
}

int main() {
	scanf("%d%d", &n, &m);
	REP(i, n-1) {
		scanf("%d%d", &x, &y);
		x--; y--;
		edges[x].pb(y);
		edges[y].pb(x);
	}
	REP(i, m)
		scanf("%d", &roz[i]);
	if(n == 2)
		printf("%d", abs(roz[0] - roz[1]));
	else {
		k = 1;
		sta.reserve(NMAX);
		sta.pb(m);
		while(!sta.empty())
			dfs(sta.back());
		printf("%lld", f[m]);
	}
	return 0;
}