const INF: usize = 1usize << 60; 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 carries = (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] as isize, 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(); let total_weigth = carries.iter().map(|&(_, w)| w).sum::(); carries.sort(); let mut x = (0..q).map(|i| (x[i], i)).collect::>(); x.sort(); let mut result = vec![INF; q]; let mut summary = (0..n).map(|i| carries[i].1 * (carries[i].0 - x[0].0).abs() as usize).sum::(); result[x[0].1] = summary; let mut lefts = 0usize; let mut lweight = 0usize; while lefts < n && carries[lefts].0 < x[0].0 { lweight += carries[lefts].1; lefts += 1; } for i in 1..q { let (pxpos, _) = x[i-1]; let (xpos, qidx) = x[i]; let mut moves = vec![]; let mut mweight = 0usize; while lefts < n && carries[lefts].0 < xpos { moves.push(carries[lefts]); mweight += carries[lefts].1; lefts += 1; } let diff = (xpos - pxpos) as usize; summary += lweight * diff; summary -= (total_weigth - mweight - lweight) * diff; for &(cx, cw) in moves.iter() { summary += (xpos - cx) as usize * cw; summary -= (cx - pxpos) as usize * cw; } lweight += mweight; result[qidx] = summary; } for &v in result.iter() { println!("{}", v); } }