-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path274B.cpp
More file actions
47 lines (43 loc) · 841 Bytes
/
Copy path274B.cpp
File metadata and controls
47 lines (43 loc) · 841 Bytes
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
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
ll v[100009];
ll addn[100009], ren[100009];
struct Edge{
int to, nxt;
}e[200009];
int head[100009], tot;
void add(int x, int y){
e[++tot].to = y;
e[tot].nxt = head[x];
head[x] = tot;
}
void dfs(int x, int fa){
for(int i = head[x]; i; i = e[i].nxt){
int y = e[i].to;
if(y == fa) continue;
dfs(y, x);
addn[x] = max(addn[x], addn[y]);
ren[x] = max(ren[x], ren[y]);
}
v[x] += addn[x] - ren[x];
if(v[x] > 0) ren[x] += v[x];
else addn[x] -= v[x];
}
int main(){
scanf("%d", &n);
for(int i = 1; i <= n - 1; i++){
int x, y;
scanf("%d%d", &x, &y);
add(x, y), add(y, x);
}
for(int i = 1; i <= n; i++){
scanf("%lld", &v[i]);
if(v[i] > 0) ren[i] = v[i];
else addn[i] = -v[i];
}
dfs(1, 0);
printf("%lld\n", addn[1] + ren[1]);
return 0;
}