#include #include template struct Edge { int src, dst; Cost cost; Edge(int src = -1, int dst = -1, Cost cost = 1) : src(src), dst(dst), cost(cost){}; bool operator<(const Edge& e) const { return this->cost < e.cost; } bool operator>(const Edge& e) const { return this->cost > e.cost; } }; template struct Graph { std::vector>> graph; Graph(int n = 0) : graph(n) {} void span(bool direct, int src, int dst, Cost cost = 1) { graph[src].emplace_back(src, dst, cost); if (!direct) graph[dst].emplace_back(dst, src, cost); } int size() const { return graph.size(); } void clear() { graph.clear(); } void resize(int n) { graph.resize(n); } std::vector>& operator[](int v) { return graph[v]; } std::vector> operator[](int v) const { return graph[v]; } }; using lint = long long; void solve() { int n; std::cin >> n; std::vector xs(n), ys(n); for (auto& x : xs) std::cin >> x; for (auto& y : ys) std::cin >> y; Graph<> graph(n); for (int i = 0; i < n - 1; ++i) { int u, v; std::cin >> u >> v; graph.span(false, --u, --v); } auto dfs = [&](auto&& f, int v, int p) -> std::pair { lint s0 = xs[v], s1 = 0; for (auto e : graph[v]) { int u = e.dst; if (u == p) continue; auto [t0, t1] = f(f, u, v); s0 += std::max(t0, t1); s1 += std::max(t0, t1 + ys[v] + ys[u]); } return std::make_pair(s0, s1); }; auto [s0, s1] = dfs(dfs, 0, -1); std::cout << std::max(s0, s1) << "\n"; } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); solve(); return 0; }