use std::collections::BTreeMap; 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 q = String::new(); std::io::stdin().read_line(&mut q).ok(); let q: usize = q.trim().parse().unwrap(); let mut x = String::new(); std::io::stdin().read_line(&mut x).ok(); let x: Vec = x.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let mut mapping = BTreeMap::new(); for &v in a.iter() { *mapping.entry(v % x[0]).or_insert(0usize) += 1; } let mut summary = mapping.iter().map(|(&val, &cnt)| val * cnt).sum::(); println!("{}", summary); for &v in x.iter().skip(1) { let stack = mapping.range(v..).map(|(&val, &cnt)| (val, cnt)).collect::>(); for &(val, cnt) in stack.iter() { summary -= val * cnt; *mapping.entry(val % v).or_insert(0usize) += cnt; summary += val % v * cnt; mapping.remove(&val); } println!("{}", summary); } }