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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <utility>

static const int MAX_N = 500 * 1000;
static const int MAX_M = 500 * 1000;

#define TEST 0

struct vert_t
{
	int firstConn;
};

struct conn_t
{
	int to;
	int next;
};

namespace
{
	vert_t verts[MAX_N + 1];
	conn_t conns[2 * MAX_N];
	int numConns = 0;

	int weights[MAX_M + 1];
	long long int sumWeights = 0;

	int n, m;
}

static inline void addConnDir(int from, int to)
{
	conns[numConns].to = to;
	conns[numConns].next = verts[from].firstConn;
	verts[from].firstConn = numConns;
	numConns++;
}

static inline void addConn(int a, int b)
{
#if TEST
	printf("%d~%d\n", a, b);
#endif

	addConnDir(a, b);
	addConnDir(b, a);
}

void readData()
{
	scanf("%d %d", &n, &m);

	for (int i = 1; i <= n; i++)
	{
		verts[i].firstConn = -1;
	}

	for (int i = 0; i < n - 1; i++)
	{
		int a, b;
		scanf("%d %d", &a, &b);
		addConn(a, b);
	}

	for (int i = 1; i <= m; i++)
		scanf("%d", &weights[i]);
}

std::pair<int, int> dfs(int v, int parent)
{
#if TEST
	printf("[%d]\n", v);
#endif

	if (v <= m)
	{
#if TEST
		printf("END[%d]\n", v);
#endif
		return std::make_pair(weights[v], weights[v] + 1);
	}

	std::vector<std::pair<int, int>> ranges;
	int begin = 1e9, end = 0;

	int v1 = 0, v2 = 0;
	for (int c = verts[v].firstConn; c != -1; c = conns[c].next)
	{
		v1++;
		if (conns[c].to == parent)
			continue;

		v2++;

		ranges.push_back(dfs(conns[c].to, v));
		begin = std::min(begin, ranges.back().first);
		end = std::max(end, ranges.back().second);
	}

#if TEST
	printf("v %d %d %d\n", v1, v2, parent);
	printf("%d ranges\n", ranges.size());

	for (const auto & p : ranges)
		printf("  [%d, %d)\n", p.first, p.second);
#endif

	if (ranges.size() == 1)
	{
#if TEST
		printf("END[%d]\n", v);
#endif
		return ranges[0];
	}

#if TEST
	puts("PEETIS");
#endif

	auto collect = [&](int l) -> long long
	{
		long long acc = 0;
		for (const auto & p : ranges)
		{
			if (l < p.first)
				acc += p.first - l;
			else if (l > p.second - 1)
				acc += l - (p.second - 1);
		}

		return acc;
	};

#if TEST
	puts("POOTIS");
#endif

	// Binary search - left side
	int left = begin, right = end;
	long long lderiv = 0;
	while (left + 1 < right)
	{
#if TEST
		printf("[%d, %d)\n", left, right);
#endif

		int p = (left + right) / 2;
		long long c = collect(p);

		long long lderiv = c - collect(p - 1);

#if TEST
		printf("Value at %d: %lld\n", p, c);
		printf("LDerivative: %lld\n", lderiv);
#endif

		if (lderiv >= 0)
			right = p;
		else
			left = p;
	}

	assert(lderiv == 0);
	int lside = left;

	left = begin;
	right = end;
	lderiv = 0;
	while (left + 1 < right)
	{
#if TEST
		printf("[%d, %d)\n", left, right);
#endif

		int p = (left + right) / 2;
		long long c = collect(p);

		long long lderiv = c - collect(p - 1);

#if TEST
		printf("Value at %d: %lld\n", p, c);
		printf("LDerivative: %lld\n", lderiv);
#endif

		if (lderiv > 0)
			right = p;
		else
			left = p;
	}

	assert(lderiv == 0);
	int rside = right;
	sumWeights += collect(lside);

#if TEST
	printf("Result range: [%d, %d)\n", lside, rside);
	assert(collect(lside) == collect(rside - 1));
	printf("END[%d]\n", v);
#endif

	return std::make_pair(lside, rside);
}

void solve()
{
	// Special case for n == 2, m == 2
	// Thanks, Wiktor Garbacz, you da real MVP!
	if (n == 2 && m == 2)
		printf("%d\n", std::abs(weights[1] - weights[2]));
	else
	{
		auto p = dfs(n, 0);
		printf("%lld\n", sumWeights);
	}
}

int main()
{
	readData();
	solve();

	return 0;
}