#include <iostream>
#include <vector>
#include <deque>
using namespace std;
struct Node {
long data;
vector<Node> children;
};
vector<Node*> nodes;
Node create_node(long data)
{
Node node;
node.data = data;
node.children = vector<Node>(0);
return node;
}
long eval(Node* root)
{
if (root->children.size() == 0) return root->data;
long sum;
for (Node child : root->children)
{
sum += eval(&child);
}
return max(root->data, sum);
}
int main()
{
int n;
cin >> n;
int path_start[n-1];
int path_end[n-1];
for (int i = 0; i < n - 1; i++)
{
cin >> path_start[i];
cin >> path_end[i];
}
int data[n];
for (int i = 0; i < n; i++)
{
cin >> data[i];
}
for (int i = 0; i < n; i++)
{
nodes.push_back(&create_node(data[i]));
}
for (int i = 0; i < n - 1; i++)
{
nodes[path_start[i]]->children.push_back(*nodes[path_end[i]]);
}
cout << eval(nodes[0]);
}