#include <string>
#include <array>
#include <iostream>
#include <sstream>
#include <vector>
#include <ios>
std::array<unsigned int, 100000> requireds;
std::array<std::vector<unsigned short int>, 100000> nodes;
int calculate(int cur_node) {
std::vector<unsigned short int> children = nodes[cur_node];
unsigned int t = 0;
for (int c : children) {
t += calculate(c);
}
if (t > requireds[cur_node - 1]) {
return t;
} else {
return requireds[cur_node - 1];
}
}
int main() {
std::ios_base::sync_with_stdio(false);
int count;
std::cin >> count;
int temp1;
int temp2;
for (int i = 0; i < (count - 1); i++) {
std::cin >> temp1 >> temp2;
nodes[temp1].push_back(temp2);
}
std::string input;
std::getline(std::cin, input);
std::cin.ignore(‘\n’)
std::istringstream ss(input);
for (int i = 0; i < count; i++) {
ss >> requireds[i];
}
/*
for (int i = 0; i < count; i++) {
std::cin >> requireds[i];
}
*/
std::cout << calculate(1);
}
/*
$nodes = Hash.new { Array.new }
while rows.length > 0
a, b = rows.shift
$nodes[a] |= [b]
end
def calculate(current)
children = $nodes[current]
#puts "#{current}, #{children}"
if !children.empty?
t = 0
children.each { |c| t += calculate(c) }
if t >= $requireds[current - 1]
return t
end
end
return $requireds[current - 1] # shift
end
count = gets.chomp.to_i
rows = []
(count - 1).times do
rows << gets.chomp.split(" ").map(&:to_i)
end
$requireds = gets.chomp.split(" ").map(&:to_i)
# hash stores parent
# parent through child until child has no child, then take thing and return with alg
puts calculate(1)
*/