use std::io::Write; fn run() { input! { n: usize, x: [i64; n], a: [i64; n], } let mut edge = vec![]; for i in 0..n { let (p, q) = (x[i], a[i]); for d in [-q, q].iter() { let pos = p + *d; if let Ok(k) = x.binary_search(&pos) { edge.push((i, k)); } } } let graph = CSR::new(n, edge.iter().cloned()); let (len, id) = strongly_connected_components(n, |v| graph.list(v)); let mut dp = vec![0; len]; let mut ord = (0..n).collect::>(); ord.sort_by_key(|v| id[*v]); for &v in ord.iter().rev() { let p = id[v]; dp[p].chmax(x[v] + a[v]); for u in graph.list(v) { dp[p] = dp[p].max(dp[id[u]]); } } let out = std::io::stdout(); let mut out = std::io::BufWriter::new(out.lock()); for i in 0..n { let ans = dp[id[i]] - x[i]; writeln!(out, "{}", ans).ok(); } } fn main() { run(); } // ---------- begin CSR ---------- pub struct CSR { size: usize, pos: Vec, list: Vec, } impl CSR { pub fn new(size: usize, it: I) -> Self where I: Iterator + Clone, { let mut pos = vec![0; size + 1]; for (s, t) in it.clone() { assert!(s < size && t < size); pos[s + 1] += 1; } for i in 1..=size { pos[i] += pos[i - 1]; } let mut x = pos[..size].to_vec(); let mut list = vec![0; pos[size] as usize]; for (s, t) in it { let x = &mut x[s]; list[*x as usize] = t as u32; *x += 1; } CSR { size, pos, list } } pub fn list(&self, v: usize) -> impl Iterator + '_ { assert!(v < self.size); let s = self.pos[v] as usize; let t = self.pos[v + 1] as usize; self.list[s..t].iter().map(|p| *p as usize) } } // ---------- end CSR ---------- // ---------- 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 scc ---------- pub fn strongly_connected_components(size: usize, graph: G) -> (usize, Vec) where G: Fn(usize) -> I, I: Iterator, { let mut ord = vec![0; size]; let mut res = vec![0; size]; let mut vertex = vec![]; let mut dfs = vec![]; let mut id = 0; for s in 0..size { if ord[s] > 0 { continue; } vertex.push(s); ord[s] = vertex.len(); dfs.push((s, graph(s))); while let Some((v, mut it)) = dfs.pop() { (|| { while let Some(u) = it.next() { if ord[u] == 0 { vertex.push(u); ord[u] = vertex.len(); dfs.push((v, it)); dfs.push((u, graph(u))); return; } } let low = graph(v).map(|u| ord[u]).min().unwrap_or(ord[v]); if low < ord[v] { ord[v] = low; return; } while let Some(u) = vertex.pop() { ord[u] = !0; res[u] = id; if u == v { break; } } id += 1; })(); } } res.iter_mut().for_each(|p| *p = id - 1 - *p); (id, res) } // ---------- end scc ---------- // ---------- 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 ----------