forked from pongsaphol/toi15-teamsu
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSUBREM.cpp
43 lines (43 loc) · 965 Bytes
/
SUBREM.cpp
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
#include <bits/stdc++.h>
using namespace std;
int a[100005];
vector<int> g[100005];
long long sum[100005];
long long val[100005];
int x;
long long gmn;
void dfs(int u, int p = -1){
sum[u] = a[u];
val[u] = 0ll;
for(int v : g[u]){
if(v == p) continue;
dfs(v, u);
sum[u] += sum[v];
val[u] += val[v];
}
val[u] = max(val[u], -sum[u] - (long long)x);
gmn = max(gmn, val[u]);
}
int main(){
int t;
scanf("%d",&t);
while(t--){
gmn = 0ll;
int n;
scanf("%d%d",&n,&x);
long long sum = 0ll;
for(int i = 0; i < 100005; i++) g[i].clear();
for(int i = 1; i <= n; i++){
scanf("%d",a+i);
sum += (long long)a[i];
}
for(int i = 1; i < n; i++){
int u,v;
scanf("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1);
printf("%lld\n",sum + gmn);
}
}