const INF: usize = 1usize << 60; fn main() { 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(); let gx = temp[0]; let gy = temp[1]; let n = temp[2]; let f = temp[3]; let crystals = (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(); let x = temp[0]; let y = temp[1]; let c = temp[2]; (x, y, c) }) .collect::>(); let mut grid = vec![vec![vec![INF; gy+1]; gx+1]; 2]; grid[0][0][0] = 0; for idx in 0..n { let (ax, ay, c) = crystals[idx]; for used in 0..2 { for i in 0..=gx { for j in 0..=gy { if i+1 <= gx { grid[used][i+1][j] = grid[used][i+1][j].min(grid[used][i][j] + f); } if j+1 <= gy { grid[used][i][j+1] = grid[used][i][j+1].min(grid[used][i][j] + f); } if used == 0 && i+ax <= gx && j+ay <= gy { grid[1][i+ax][j+ay] = grid[1][i+ax][j+ay].min(grid[used][i][j] + c); } } } } for i in 0..=gx { for j in 0..=gy { grid[0][i][j] = grid[0][i][j].min(grid[1][i][j]); } } } println!("{}", grid[0][gx][gy].min(grid[1][gx][gy])); }