#include #include using namespace std; struct crystal{int x,y,c, otoku;}; bool cospa( const crystal &cry1, const crystal &cry2 ){ if(cry1.otoku != cry2.otoku) return cry1.otoku < cry2.otoku; else return cry1.x+cry1.y < cry2.x+cry2.y; } int main(){ int Gx, Gy, N, F; cin >> Gx >> Gy >> N >> F; crystal crystals[N]; for(int i=0; i> crystals[i].x >> crystals[i].y >> crystals[i].c; if(crystals[i].x>0 && crystals[i].y>0){ crystals[i].otoku = crystals[i].c - F*(crystals[i].x + crystals[i].y); }else{ crystals[i].otoku = 0; } } sort(crystals, crystals+N, cospa); //moveCost[i][j]=(i,j)に行くのにかかる最小料金 int moveCost[Gx+1][Gy+1]; for(int i=0; i<=Gx; i++){ for(int j=0; j<=Gy; j++){ moveCost[i][j] = (i+j)*F; //全部歩いたときのコスト } } for(int k=0; crystals[k].otoku<0; k++){ for(int i=Gx; i>=crystals[k].x; i--){ for(int j=Gy; j>=crystals[k].y; j--){ moveCost[i][j] = moveCost[i-crystals[k].x][j-crystals[k].y] + crystals[k].c; } } } cout << moveCost[Gx][Gy] << endl; return 0; }