Upload code_segments/segment_286.txt with huggingface_hub
Browse files
code_segments/segment_286.txt
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"Why, master," quoth Little John, taking the bags and weighing them in his hand, "here is the chink of gold."
|
2 |
+
|
3 |
+
The folk hero Robin Hood has been troubling Sheriff of Nottingham greatly. Sheriff knows that Robin Hood is about to attack his camps and he wants to be prepared.
|
4 |
+
|
5 |
+
Sheriff of Nottingham built the camps with strategy in mind and thus there are exactly $n$ camps numbered from $1$ to $n$ and $n-1$ trails, each connecting two camps. Any camp can be reached from any other camp. Each camp $i$ has initially $a_i$ gold.
|
6 |
+
|
7 |
+
As it is now, all camps would be destroyed by Robin. Sheriff can strengthen a camp by subtracting exactly $c$ gold from each of its neighboring camps and use it to build better defenses for that camp. Strengthening a camp doesn't change its gold, only its neighbors' gold. A camp can have negative gold.
|
8 |
+
|
9 |
+
After Robin Hood's attack, all camps that have been strengthened survive the attack, all others are destroyed.
|
10 |
+
|
11 |
+
What's the maximum gold Sheriff can keep in his surviving camps after Robin Hood's attack if he strengthens his camps optimally?
|
12 |
+
|
13 |
+
Camp $a$ is neighboring camp $b$ if and only if there exists a trail connecting $a$ and $b$. Only strengthened camps count towards the answer, as others are destroyed.
|
14 |
+
|
15 |
+
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
|
16 |
+
|
17 |
+
Each test case begins with two integers $n$, $c$ ($1 \le n \le 2\cdot10^5, 1 \le c \le 10^9$) — the number of camps and the gold taken from each neighboring camp for strengthening.
|
18 |
+
|
19 |
+
The second line of each test case contains $n$ integers $a_1,a_2,\dots,a_n$ ($-10^9 \le a_i \le 10^9$) — the initial gold of each camp.
|
20 |
+
|
21 |
+
Then follow $n-1$ lines, each with integers $u$, $v$ ($1 \le u, v \le n$, $u \ne v$) — meaning that there is a trail between $u$ and $v$.
|
22 |
+
|
23 |
+
The sum of $n$ over all test cases doesn't exceed $2\cdot10^5$.
|
24 |
+
|
25 |
+
It is guaranteed that any camp is reachable from any other camp.
|
26 |
+
|
27 |
+
Output a single integer, the maximum gold Sheriff of Nottingham can keep in his surviving camps after Robin Hood
|