use proconio::{input, marker::Usize1}; fn main() { input! { n:usize, m:usize, x:usize, mut ab:[(usize,Usize1);n], k:usize, c:[usize;k], } ab.sort_by(|x, y| y.0.cmp(&x.0)); let mut seq = vec![]; let mut i = 0; let mut j = 0; let mut used = vec![false; m]; let mut fin = vec![false; n]; let mut scores = vec![0]; while i < n { if j < n && ab[i].0 < ab[j].0 + x { seq.push(j); fin[j] = true; used[ab[j].1] = true; let last_score = *scores.last().unwrap(); scores.push(last_score + ab[j].0 + x); if i == j { i += 1; } while j < n && used[ab[j].1] { j += 1; } } else { seq.push(i); fin[i] = true; let last_score = *scores.last().unwrap(); scores.push(last_score + ab[i].0); while i < n && fin[i] { i += 1; } } } let ans = c.iter().map(|&ci| scores[ci]).sum::(); println!("{}", ans); }