#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
vector<vector<int>> v;
vector<int> values;
int haku(int index)
{
// If the node has no children
if (v[index].size() == 0) return values[index];
// The node has children
// The number needed to plow the children
int s = 0;
for (int child = 0; child < v[index].size())
{
s += haku(v[index][child]);
}
/*
Return the max of the number needed to
plow the children and the number needed
to plow the node
*/
return max(s, values[index]);
}
int main()
{
int n = 10;
cin >> n;
v.resize(n);
values.resize(n);
for (int i = 0; i < n; i++)
{
int a, b;
cin >> a >> b;
v[a-1].push_back(b-1);
}
for (int i = 0; i < n; i++)
{
cin >> values[i];
}
cout << haku(0);
}