#include <bits/stdc++.h>
using namespace std;
class Tree {
public:
int n;
vector<vector<int>> adj;
vector<long long> dp, a;
// dp[i] is the maximum coins collected when you start your journey at node
// i.
Tree(int n) {
this->n = n;
adj.resize(n);
a.resize(n);
dp.resize(n);
}
void dfs(int src, int par) {
dp[src] = a[src];
for (auto child : adj[src]) {
if (child == par) {
continue;
}
dfs(child, src);
dp[src] = max(dp[src], dp[src] + dp[child]);
}
}
};
void solve() {
int n;
cin >> n;
Tree t(n);
for (int i = 0; i < n - 1; i++) {
int x, y;
cin >> x >> y;
t.adj[x].push_back(y);
t.adj[y].push_back(x);
}
for (int i = 0; i < n; i++) {
cin >> t.a[i];
}
t.dfs(0, -1);
cout << t.dp[0] << endl;
}
int main() {
solve();
return 0;
}