use proconio::input; fn main() { input! { n:usize, a:[i64;n], b:[i64;n], } if n == 2 { let ans = if a[0] - b[0] + a[1] - b[1] == 0 { (a[0] - b[0]).abs() } else { -1 }; println!("{}", ans); return; } let a_sum = a.iter().sum::(); let b_sum = b.iter().sum::(); if a_sum < b_sum || (a_sum - b_sum) % (n - 2) as i64 != 0 { println!("-1"); return; } let cnt = (a_sum - b_sum) / (n - 2) as i64; let ps = (0..n).map(|i| b[i] - (a[i] - cnt)).collect::>(); let f1 = ps.iter().sum::() == 2 * cnt; let f2 = ps.iter().all(|&p| 0 <= p); let f3 = ps.iter().all(|&p| p % 2 == 0); let ans = if f1 && f2 && f3 { cnt } else { -1 }; println!("{}", ans); }