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 summary = a.iter().sum::(); let mut btree = BTreeMap::new(); for &v in a.iter() { *btree.entry(v).or_insert(0usize) += 1; } for &x in x.iter() { let targets = btree.range(x..).map(|(&k, &v)| (k, v)).collect::>(); for &(val, cnt) in targets.iter() { summary -= val * cnt; let nval = val % x; summary += nval * cnt; btree.remove(&val); *btree.entry(nval).or_insert(0usize) += cnt; } println!("{}", summary); } }