#include #include #include #include #include #include #include #include #include #include #include #include #include int main() { int n; std::cin >> n; std::vector remove_score(n), rest_score(n); for (auto& a : remove_score) std::cin >> a; for (auto& b : rest_score) std::cin >> b; std::vector> nodes(n); for (auto i = 1; i < n; ++i) { int u, v; std::cin >> u >> v; --u; --v; nodes[u].push_back(v); nodes[v].push_back(u); } std::vector parent(n, -1), node_count(n, 0); std::queue queue; queue.push(0); while (!queue.empty()) { const auto front = queue.front(); queue.pop(); node_count[front] = nodes[front].size(); for (const auto to : nodes[front]) { if (node_count[to] == 0) { queue.push(to); parent[to] = front; } } } for (auto i = 0; i < n; ++i) { if (node_count[i] == 1) queue.push(i); } std::vector remove(n), not_remove(n, 0); while (!queue.empty()) { const auto front = queue.front(); queue.pop(); remove[front] = remove_score[front]; for (const auto child : nodes[front]) if (parent[front] != child) { not_remove[front] += std::max(not_remove[child] + rest_score[front] + rest_score[child], remove[child]); remove[front] += std::max(not_remove[child], remove[child]); } if (parent[front] > 0 && --node_count[parent[front]] == 1) { queue.push(parent[front]); } if (parent[front] == 0 && --node_count[0] == 0) { queue.push(0); } } std::cout << std::max(remove[0], not_remove[0]) << '\n'; }