#pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef pair P; typedef bitset<16> BS; const ll MOD = 1E+09 + 7; const ll INF = 1E18; const int MAX_N = 1E+05; int N; ll A[MAX_N + 1], B[MAX_N + 1], dp[MAX_N + 1][2]; vector G[MAX_N + 1]; ll tree(int par, int i, int exist); int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> N; for (int i = 1; i <= N; i++) { cin >> A[i]; } for (int i = 1; i <= N; i++) { cin >> B[i]; } for (int i = 1; i <= N - 1; i++) { int from, to; cin >> from >> to; G[from].push_back(to); G[to].push_back(from); } fill(dp[1], dp[N + 1], -INF); /* for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { cout << "i = " << i << ", j = " << j << ", dp = " << dp[i][j] << "\n"; } } */ cout << max(tree(0, 1, 0), tree(0, 1, 1)) << "\n"; return 0; } ll tree(int par, int i, int exist) { if (dp[i][exist] != -INF) return dp[i][exist]; ll res = 0; for (auto to : G[i]) { if (to != par) { if (exist) { res += max(tree(i, to, 0), tree(i, to, 1) + B[i] + B[to]); } else { res += max(tree(i, to, 0), tree(i, to, 1)); } } } if (!exist) res += A[i]; return res; }