#include #define rep(i,n,m) for(int i = (n); i <(m); i++) #define rrep(i,n,m) for(int i = (n) - 1; i >=(m); i--) using namespace std; using ll = long long; struct crystal{int x, y, c;}; int main() { int gx, gy, n, f; cin >> gx >> gy >> n >> f; vector cry; rep(i, 0, n) { crystal c; cin >> c.x >> c.y >> c.c; cry.push_back(c); // cout << "***" << endl; } // cout <<"*LJ:J" << endl; int dp[n+1][2*gx + 1][2*gy + 1]; rep(k, 0, n+1) rep(i, 0, gx + 1) rep(j, 0, gy + 1) dp[k][i][j] = (i + j) * f; // cout << n << ' ' << gx << ' ' << gy << ' ' << f << endl; // cout << dp[n][gx][gy] << endl; // cout << "***********" << endl; rep(k, 0, n) { crystal c = cry[k]; rep(i, 0, gx + 1) rep(j, 0, gy + 1) { dp[k+1][i][j] = min( dp[k+1][i][j], dp[k][i][j] ); if (i+c.x <= gx and j+c.y <= gy) dp[k+1][i+c.x][j+c.y] = min( dp[k][i+c.x][j+c.y], dp[k][i][j] + c.c ); // cout << k << ' ' << i <<' ' << j << ' ' << dp[k+1][i][j] << endl; } } cout << dp[n][gx][gy] << endl; return 0; }