fn run() { input! { n: usize, m: usize, e: [(usize1, usize1); m], d: [[i64; n]; 2], } let mut dsu = QuickFind::new(2 * n); for &(a, b) in e.iter() { dsu.unite(a, b + n); dsu.unite(b, a + n); } if (0..n).all(|x| !dsu.same(x, x + n)) { let mut s = 0; for &v in dsu.enumerate(0) { if v >= n { s += d[1][v - n]; } else { s += d[0][v]; } } let sum = d.iter().flatten().sum::(); let ans = s.max(sum - s); println!("{}", ans); return; } let mut g = vec![vec![]; n]; let mut ans = 0; let mut d = d; let all = (0..n).map(|x| d[0][x].max(d[1][x])).sum::(); for _ in 0..2 { for (i, &(a, b)) in e.iter().enumerate() { g.iter_mut().for_each(|g| g.clear()); for (j, &(a, b)) in e.iter().enumerate() { if i != j { g[a].push(b); g[b].push(a); } } let mut dp = vec![vec![std::i64::MAX / 2; n]; 2]; dp[0][a] = d[0][a].max(d[1][a]) - d[0][a]; let mut h = std::collections::BinaryHeap::new(); h.push((-dp[0][a], 0, a)); while let Some((dis, p, v)) = h.pop() { let dis = -dis; if dis > dp[p][v] { continue; } for &u in g[v].iter() { let cost = d[0][u].max(d[1][u]) - d[p ^ 1][u]; assert!(cost >= 0); let dis = dis + cost; if dp[p ^ 1][u].chmin(dis) { h.push((-dis, p ^ 1, u)); } } } ans = ans.max(all - dp[0][b]); } d.swap(0, 1); } println!("{}", ans); } fn main() { run(); } // ---------- begin chmin, chmax ---------- pub trait ChangeMinMax { fn chmin(&mut self, x: Self) -> bool; fn chmax(&mut self, x: Self) -> bool; } impl ChangeMinMax for T { fn chmin(&mut self, x: Self) -> bool { *self > x && { *self = x; true } } fn chmax(&mut self, x: Self) -> bool { *self < x && { *self = x; true } } } // ---------- end chmin, chmax ---------- // ---------- begin quick find ---------- pub struct QuickFind { size: usize, id: Vec, list: Vec>, } impl QuickFind { pub fn new(size: usize) -> Self { let id = (0..size).collect::>(); let list = (0..size).map(|x| vec![x]).collect::>(); QuickFind { size, id, list } } pub fn root(&self, x: usize) -> usize { assert!(x < self.size); self.id[x] } pub fn same(&self, x: usize, y: usize) -> bool { assert!(x < self.size); assert!(y < self.size); self.root(x) == self.root(y) } pub fn unite(&mut self, x: usize, y: usize) -> Option<(usize, usize)> { assert!(x < self.size); assert!(y < self.size); let mut x = self.root(x); let mut y = self.root(y); if x == y { return None; } if (self.list[x].len(), x) < (self.list[y].len(), y) { std::mem::swap(&mut x, &mut y); } let mut z = std::mem::take(&mut self.list[y]); z.iter().for_each(|y| self.id[*y] = x); self.list[x].append(&mut z); Some((x, y)) } pub fn enumerate(&self, x: usize) -> &[usize] { assert!(x < self.size); &self.list[self.root(x)] } pub fn size(&self, x: usize) -> usize { assert!(x < self.size); self.enumerate(x).len() } } // ---------- end quick find ---------- // ---------- begin input macro ---------- // reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 #[macro_export] macro_rules! input { (source = $s:expr, $($r:tt)*) => { let mut iter = $s.split_whitespace(); input_inner!{iter, $($r)*} }; ($($r:tt)*) => { let s = { use std::io::Read; let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); s }; let mut iter = s.split_whitespace(); input_inner!{iter, $($r)*} }; } #[macro_export] macro_rules! input_inner { ($iter:expr) => {}; ($iter:expr, ) => {}; ($iter:expr, $var:ident : $t:tt $($r:tt)*) => { let $var = read_value!($iter, $t); input_inner!{$iter $($r)*} }; } #[macro_export] macro_rules! read_value { ($iter:expr, ( $($t:tt),* )) => { ( $(read_value!($iter, $t)),* ) }; ($iter:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($iter, $t)).collect::>() }; ($iter:expr, chars) => { read_value!($iter, String).chars().collect::>() }; ($iter:expr, bytes) => { read_value!($iter, String).bytes().collect::>() }; ($iter:expr, usize1) => { read_value!($iter, usize) - 1 }; ($iter:expr, $t:ty) => { $iter.next().unwrap().parse::<$t>().expect("Parse error") }; } // ---------- end input macro ----------