#include #define rep(i,n) for(int i = 0; i < (int)(n); i++) #define rrep(ri,n) for(int ri = (int)(n-1); ri >= 0; ri--) #define rep2(i,x,n) for(int i = (int)(x); i < (int)(n); i++) #define rrep2(ri,x,n) for(int ri = (int)(n-1); ri >= (int)(x); ri--) #define repit(itr,x) for(auto itr = x.begin(); itr != x.end(); itr++) #define rrepit(ritr,x) for(auto ritr = x.rbegin(); ritr != x.rend(); ritr++) #define ALL(x) x.begin(), x.end() using ll = long long; using namespace std; void dfs(vector> &tree, vector &b, vector &po, int now, int bf){ po.at(now) = (ll)tree.at(now).size() * b.at(now); for(auto i : tree.at(now)){ po.at(now) += b.at(i); if(i == bf) continue; dfs(tree, b, po, i, now); } } void fc(vector> &tree, vector &b, vector &po, vector &chk, vector &a, int now){ chk.at(now) = true; for(auto i : tree.at(now)){ if(chk.at(i) == true) continue; po.at(i) -= b.at(i); if(po.at(i) < a.at(i)){ fc(tree, b, po, chk, a, i); } } } void dfs2(vector> &tree, vector &a, vector &b, vector &chk, int now, int bf, ll &ans){ ll tmp = 0; if(chk.at(now) == true){ ans += a.at(now); tmp += a.at(now); } for(auto i : tree.at(now)){ if(chk.at(now) == false && chk.at(i) == false){ ans += b.at(now); tmp += b.at(now); } if(i == bf) continue; dfs2(tree, a, b, chk, i, now, ans); } // cerr << now << " " << tmp << endl; } int main(){ int n; cin >> n; vector a(n); rep(i, n) cin >> a.at(i); vector b(n); rep(i, n) cin >> b.at(i); vector> tree(n, vector()); rep(i, n-1){ int a, b; cin >> a >> b; a--; b--; tree.at(a).push_back(b); tree.at(b).push_back(a); } vector chk(n, false); vector po(n, 0); dfs(tree, b, po, 0, -1); rep(i, n){ if(chk.at(i) == true) continue; if(po.at(i) < a.at(i)){ fc(tree, b, po, chk, a, i); } } ll ans = 0; dfs2(tree, a, b, chk, 0, -1, ans); cout << ans << endl; return 0; }