import sys
input=sys.stdin.readline
sys.setrecursionlimit(0x0FFFFFFF)
import threading
threading.stack_size(0x04000000)
def main():
n=int(input())
graph=[[] for _ in range(n)]
for _ in range(n-1):
u,v=map(int,input().split())
graph[u-1].append(v-1)
graph[v-1].append(u-1)
vals=list(map(int,input().split()))
i,d=[0]*n,[0]*n
def dfs(cur,pre):
for nxt in graph[cur]:
if nxt!=pre:
inc,dec=dfs(nxt,cur)
i[cur]=max(i[cur],inc)
d[cur]=max(d[cur],dec)
shift=vals[cur]+i[cur]-d[cur]
if shift>0:d[cur]+=shift
else:i[cur]-=shift
return i[cur],d[cur]
dfs(0,0)
print(i[0]+d[0])
threading.Thread(target=main).start()
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define ld long double
ll mod = 1000000007;
#define fr(i, n) for (int i = 0; i < n; i++)
#define frr(i, a, b) for (int i = a; i < b; i++)
#define fi(i, arr) for (auto i : arr)
#define vl vector<ll>
#define vb vector<bool>
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
void dfs(vl &parent, vl &dfs_order, ll node, vector<vector<ll>> &adj_list)
{
dfs_order.push_back(node);
fi(i, adj_list[node])
{
if (parent[i] == -1)
{
parent[i] = node;
dfs(parent, dfs_order, i, adj_list);
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n;
cin >> n;
vector<vector<ll>> adj_list(n);
fr(i, n - 1)
{
ll a, b;
cin >> a >> b;
adj_list[a - 1].push_back(b - 1);
adj_list[b - 1].push_back(a - 1);
}
vl vals(n);
fr(i, n) { cin >> vals[i]; }
vl parent(n, -1), dfs_order;
parent[0] = 0;
dfs(parent, dfs_order, 0, adj_list);
vl plus(n), minus(n);
for (ll j = n - 1; j >= 0; j--)
{
ll i = dfs_order[j];
vals[i] = plus[i] - minus[i] + vals[i];
if (vals[i] > 0)
{
minus[i] += vals[i];
}
else
{
plus[i] -= vals[i];
}
if (i != 0)
{
plus[parent[i]] = max(plus[parent[i]], plus[i]);
minus[parent[i]] = max(minus[parent[i]], minus[i]);
}
}
cout << minus[0] + plus[0];
}
1633C - Kill the Monster | 1611A - Make Even |
1030B - Vasya and Cornfield | 1631A - Min Max Swap |
1296B - Food Buying | 133A - HQ9+ |
1650D - Twist the Permutation | 1209A - Paint the Numbers |
1234A - Equalize Prices Again | 1613A - Long Comparison |
1624B - Make AP | 660B - Seating On Bus |
405A - Gravity Flip | 499B - Lecture |
709A - Juicer | 1358C - Celex Update |
1466B - Last minute enhancements | 450B - Jzzhu and Sequences |
1582C - Grandma Capa Knits a Scarf | 492A - Vanya and Cubes |
217A - Ice Skating | 270A - Fancy Fence |
181A - Series of Crimes | 1638A - Reverse |
1654C - Alice and the Cake | 369A - Valera and Plates |
1626A - Equidistant Letters | 977D - Divide by three multiply by two |
1654B - Prefix Removals | 1654A - Maximum Cake Tastiness |