import sys from collections import deque def input(): return sys.stdin.buffer.readline()[:-1] INF = 10**15 class Dinic: def __init__(self, n): self.n = n self.g = [[] for i in range(n)] def add_edge(self, fr, to, cap): forward = [to, cap, None] forward[2] = backward = [fr, 0, forward] self.g[fr].append(forward) self.g[to].append(backward) def add_bidirectional_edge(self, v1, v2, cap1, cap2): edge1 = [v2, cap1, None] edge1[2] = edge2 = [v1, cap2, edge1] self.g[v1].append(edge1) self.g[v2].append(edge2) def bfs(self, s, t): self.level = level = [None]*self.n deq = deque([s]) level[s] = 0 g = self.g while deq: v = deq.popleft() lv = level[v] + 1 for w, cap, _ in g[v]: if cap and level[w] is None: level[w] = lv deq.append(w) return level[t] is not None def dfs(self, v, t, f): if v == t: return f level = self.level for e in self.it[v]: w, cap, rev = e if cap and level[v] < level[w]: d = self.dfs(w, t, min(f, cap)) if d: e[1] -= d rev[1] += d return d return 0 def flow(self, s, t): flow = 0 INF = 10**30 g = self.g while self.bfs(s, t): self.it = list(map(iter, self.g)) f = INF while f: f = self.dfs(s, t, INF) flow += f return flow n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) adj = [[] for _ in range(n)] for i in range(n-1): u, v = map(int, input().split()) adj[u-1].append((v-1, n+2+i)) adj[v-1].append((u-1, n+2+i)) S, T = n, n+1 dnc = Dinic(2*n+1) score = 0 def dfs(i, p, sgn): global dnc, score if a[i] > 0: score += a[i] if sgn: dnc.add_edge(S, i, a[i]) else: dnc.add_edge(i, T, a[i]) elif a[i] < 0: if sgn: dnc.add_edge(i, T, -a[i]) else: dnc.add_edge(S, i, -a[i]) for j, e in adj[i]: if j == p: continue if b[i]+b[j] >= 0: score += b[i]+b[j] if sgn: dnc.add_edge(i, e, INF) dnc.add_edge(j, e, INF) dnc.add_edge(e, T, b[i]+b[j]) else: dnc.add_edge(e, i, INF) dnc.add_edge(e, j, INF) dnc.add_edge(S, e, b[i]+b[j]) dfs(j, i, sgn) else: if sgn: dnc.add_edge(j, i, -b[i]-b[j]) else: dnc.add_edge(i, j, -b[i]-b[j]) dfs(j, i, 1^sgn) dfs(0, -1, 0) min_cut = dnc.flow(S, T) print(score - min_cut)