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).or_insert(0usize) += 1; } let mut summary = mapping.iter().map(|(&val, &cnt)| val * cnt).sum::(); for i in 0..q { let x = x[i]; let stack = mapping.range(x..).map(|(&val, &cnt)| (val, cnt)).collect::>(); for &(val, cnt) in stack.iter() { let nval = val % x; summary -= val * cnt; summary += nval * cnt; *mapping.entry(nval).or_insert(0usize) += cnt; mapping.remove(&val); } println!("{}", summary); } }