const INF: isize = 1isize << 60; fn dfs(cur: usize, prev: usize, paths: &Vec>, a: &Vec, b: &Vec, dp: &mut Vec>) { let mut score = 0isize; let mut del_score = 0isize; for &v in paths[cur].iter() { if prev == v { continue; } dfs(v, cur, paths, a, b, dp); let used_score = b[v] + b[cur] + dp[v][1]; let unused_score = dp[v][0]; score += used_score.max(unused_score); del_score += dp[v][0].max(dp[v][1]); } dp[cur][0] = a[cur] + del_score; dp[cur][1] = score; } fn main() { let mut n = String::new(); std::io::stdin().read_line(&mut n).ok(); let n: usize = n.trim().parse().unwrap(); let mut a = String::new(); std::io::stdin().read_line(&mut a).ok(); let a: Vec = a.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let mut b = String::new(); std::io::stdin().read_line(&mut b).ok(); let b: Vec = b.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let mut paths = vec![vec![]; n]; for _ in 0..n-1 { let mut ab = String::new(); std::io::stdin().read_line(&mut ab).ok(); let ab: Vec = ab.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let a = ab[0]-1; let b = ab[1]-1; paths[a].push(b); paths[b].push(a); } let mut dp = vec![vec![-INF; 2]; n]; dfs(0, 0, &paths, &a, &b, &mut dp); println!("{}", dp[0][0].max(dp[0][1])); }