using System; using System.Collections.Generic; using System.Text; using System.Linq; class Program { public void Proc() { Reader.IsDebug = false; int[] inpt = Reader.ReadLine().Split(' ').Select(a => int.Parse(a)).ToArray(); this.GoalX = inpt[0]; this.GoalY = inpt[1]; int crystalCount = inpt[2]; this.TohoCost = inpt[3]; for (int i = 0; i < crystalCount; i++) { inpt = Reader.ReadLine().Split(' ').Select(a => int.Parse(a)).ToArray(); clist.Add(new Crystal(inpt[0], inpt[1], inpt[2])); } int ans = this.GetAns(0, 0, 0); Console.WriteLine(ans); } private Dictionary>> dic = new Dictionary>>(); private int GetAns(int idx, int x, int y) { if (x > GoalX || y > GoalY) { return -1; } if (idx >= clist.Count) { int tmp = (GoalX - x) + (GoalY - y); return tmp * TohoCost; } if (!dic.ContainsKey(idx)) { dic.Add(idx, new Dictionary>()); } if (!dic[idx].ContainsKey(x)) { dic[idx].Add(x, new Dictionary()); } if (dic[idx][x].ContainsKey(y)) { return dic[idx][x][y]; } int ans = 0; int ret = this.GetAns(idx + 1, x + clist[idx].MoveX, y + clist[idx].MoveY); if (ret >= 0) { ans = ret + clist[idx].Cost; } ret = this.GetAns(idx + 1, x, y); if (ret >= 0) { ans = Math.Min(ans, ret); } dic[idx][x][y] = ans; return ans; } private int GoalX; private int GoalY; private int TohoCost; private List clist = new List(); private struct Crystal { public int MoveX; public int MoveY; public int Cost; public Crystal(int x, int y, int c) { this.MoveX = x; this.MoveY = y; this.Cost = c; } } public class Reader { public static bool IsDebug = true; private static String PlainInput = @" 3 3 2 1 1 2 2 2 1 4 "; private static System.IO.StringReader Sr = null; public static string ReadLine() { if (IsDebug) { if (Sr == null) { Sr = new System.IO.StringReader(PlainInput.Trim()); } return Sr.ReadLine(); } else { return Console.ReadLine(); } } } static void Main() { Program prg = new Program(); prg.Proc(); } }