fn main() { let mut nq = String::new(); std::io::stdin().read_line(&mut nq).ok(); let nq: Vec = nq.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let n = nq[0]; let q = nq[1]; let mut items = (0..n).map(|_| { let mut temp = String::new(); std::io::stdin().read_line(&mut temp).ok(); let temp: Vec = temp.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); (temp[0], temp[1]) }) .collect::>(); 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(); items.sort(); let mut summary = (0..n).map(|i| (items[i].0 - x[0]).abs() * items[i].1).sum::(); let mut result = vec![0isize; q]; result[0] = summary; let total_weights = items.iter().map(|&(_, w)| w).sum::(); let mut lidx = 0usize; let mut lweights = 0isize; while lidx < n && items[lidx].0 < x[0] { lweights += items[lidx].1; lidx += 1; } for i in 1..q { let px = x[i-1]; let cx = x[i]; let diff = cx - px; summary += lweights * diff; summary -= (total_weights - lweights) * diff; while lidx < n && items[lidx].0 < cx { let (x, w) = items[lidx]; summary += 2 * (cx - x) * w; lweights += w; lidx += 1; } result[i] = summary; } for &v in result.iter() { println!("{}", v); } }